2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)move.c 8.1 (Berkeley) 5/31/93
34 * $FreeBSD: src/games/mille/move.c,v 1.6 1999/12/12 06:17:24 billf Exp $
35 * $DragonFly: src/games/mille/move.c,v 1.5 2006/08/27 17:17:23 pavalos Exp $
45 # define _tty cur_term->Nttyb
49 * @(#)move.c 1.2 (Berkeley) 3/28/83
53 #define CTRL(c) (c - 'A' + 1)
55 const char *Movenames
[] = {
56 "M_DISCARD", "M_DRAW", "M_PLAY", "M_ORDER"
59 static void check_go (void);
60 static void getmove (void);
61 static int haspicked (PLAY
*);
62 static bool playcard (PLAY
*);
81 if (pp
->hand
[Card_no
] == C_INIT
)
85 error("no card there");
87 if (issafety(pp
->hand
[Card_no
])) {
88 error("discard a safety?");
92 Discard
= pp
->hand
[Card_no
];
93 pp
->hand
[Card_no
] = C_INIT
;
100 error("must pick first");
103 goodplay
= playcard(pp
);
108 error("no more cards");
109 else if (haspicked(pp
))
110 error("already picked");
112 pp
->hand
[0] = *--Topcard
;
115 fprintf(outf
, "DOMOVE: Draw %s\n", C_name
[*Topcard
]);
120 if (issafety(*Topcard
))
121 pp
->safety
[*Topcard
-S_CONV
] = S_IN_HAND
;
123 if (pp
->hand
[1] == C_INIT
&& Topcard
> Deck
) {
125 pp
->hand
[1] = *--Topcard
;
128 fprintf(outf
, "DOMOVE: Draw %s\n", C_name
[*Topcard
]);
132 pp
->new_battle
= FALSE
;
133 pp
->new_speed
= FALSE
;
141 * move blank card to top by one of two methods. If the
142 * computer's hand was sorted, the randomness for picking
143 * between equally valued cards would be lost
145 if (Order
&& Movetype
!= M_DRAW
&& goodplay
&& pp
== &Player
[PLAYER
])
148 for (i
= 1; i
< HAND_SZ
; i
++)
149 if (pp
->hand
[i
] == C_INIT
) {
150 for (j
= 0; pp
->hand
[j
] == C_INIT
; j
++)
155 pp
->hand
[i
] = pp
->hand
[j
];
156 pp
->hand
[j
] = C_INIT
;
165 * Check and see if either side can go. If they cannot,
176 for (pp
= Player
; pp
< &Player
[2]; pp
++) {
177 op
= (pp
== &Player
[COMP
] ? &Player
[PLAYER
] : &Player
[COMP
]);
178 for (i
= 0; i
< HAND_SZ
; i
++) {
180 if (issafety(card
) || canplay(pp
, op
, card
)) {
183 fprintf(outf
, "CHECK_GO: can play %s (%d), ", C_name
[card
], card
);
184 fprintf(outf
, "issafety(card) = %d, ", issafety(card
));
185 fprintf(outf
, "canplay(pp, op, card) = %d\n", canplay(pp
, op
, card
));
192 fprintf(outf
, "CHECK_GO: cannot play %s\n",
207 * check and see if player has picked
209 switch (pp
->hand
[Card_no
]) {
213 return error("must pick first");
214 case C_GAS_SAFE
: case C_SPARE_SAFE
:
215 case C_DRIVE_SAFE
: case C_RIGHT_WAY
:
219 card
= pp
->hand
[Card_no
];
222 fprintf(outf
, "PLAYCARD: Card = %s\n", C_name
[card
]);
227 if (pp
->nummiles
[C_200
] == 2)
228 return error("only two 200's per hand");
229 case C_100
: case C_75
:
230 if (pp
->speed
== C_LIMIT
)
231 return error("limit of 50");
233 if (pp
->mileage
+ Value
[card
] > End
)
234 return error("puts you over %d", End
);
237 return error("cannot move now");
238 pp
->nummiles
[card
]++;
242 if ((pp
->mileage
+= v
) == End
)
246 case C_GAS
: case C_SPARE
: case C_REPAIRS
:
247 if (pp
->battle
!= opposite(card
))
248 return error("can't play \"%s\"", C_name
[card
]);
250 if (pp
->safety
[S_RIGHT_WAY
] == S_PLAYED
)
255 if (pp
->battle
!= C_INIT
&& pp
->battle
!= C_STOP
256 && !isrepair(pp
->battle
))
257 return error("cannot play \"Go\" on a \"%s\"",
264 if (pp
->speed
!= C_LIMIT
)
265 return error("not limited");
266 pp
->speed
= C_END_LIMIT
;
269 case C_EMPTY
: case C_FLAT
: case C_CRASH
:
271 pp
= &Player
[other(Play
)];
273 return error("opponent cannot go");
274 else if (pp
->safety
[safety(card
) - S_CONV
] == S_PLAYED
)
276 return error("opponent is protected");
278 pp
->new_battle
= TRUE
;
284 pp
= &Player
[other(Play
)];
285 if (pp
->speed
== C_LIMIT
)
286 return error("opponent has limit");
287 if (pp
->safety
[S_RIGHT_WAY
] == S_PLAYED
)
290 pp
->new_speed
= TRUE
;
294 case C_GAS_SAFE
: case C_SPARE_SAFE
:
295 case C_DRIVE_SAFE
: case C_RIGHT_WAY
:
296 if (pp
->battle
== opposite(card
)
297 || (card
== C_RIGHT_WAY
&& pp
->speed
== C_LIMIT
)) {
298 if (!(card
== C_RIGHT_WAY
&& !isrepair(pp
->battle
))) {
302 if (card
== C_RIGHT_WAY
&& pp
->speed
== C_LIMIT
)
305 || (pp
->new_speed
&& card
== C_RIGHT_WAY
)) {
306 pp
->coups
[card
- S_CONV
] = TRUE
;
307 pp
->total
+= SC_COUP
;
308 pp
->hand_tot
+= SC_COUP
;
309 pp
->coupscore
+= SC_COUP
;
313 * if not coup, must pick first
315 else if (pp
->hand
[0] == C_INIT
&& Topcard
> Deck
)
317 pp
->safety
[card
- S_CONV
] = S_PLAYED
;
318 pp
->total
+= SC_SAFETY
;
319 pp
->hand_tot
+= SC_SAFETY
;
320 if ((pp
->safescore
+= SC_SAFETY
) == NUM_SAFE
* SC_SAFETY
) {
321 pp
->total
+= SC_ALL_SAFE
;
322 pp
->hand_tot
+= SC_ALL_SAFE
;
324 if (card
== C_RIGHT_WAY
) {
325 if (pp
->speed
== C_LIMIT
)
327 if (pp
->battle
== C_STOP
|| pp
->battle
== C_INIT
) {
331 if (!pp
->can_go
&& isrepair(pp
->battle
))
338 error("no card there");
342 if (pp
== &Player
[PLAYER
])
344 pp
->hand
[Card_no
] = C_INIT
;
345 Next
= (Next
== -1 ? FALSE
: TRUE
);
357 static bool last_ex
= FALSE
; /* set if last command was E */
367 leaveok(Board
, FALSE
);
369 while ((c
= readch()) == killchar() || c
== erasechar())
373 if (isprint(c
) && !isspace(c
)) {
381 case 'U': /* Use Card */
382 case 'D': /* Discard Card */
383 if ((Card_no
= getcard()) < 0)
385 Movetype
= (c
== 'U' ? M_PLAY
: M_DISCARD
);
387 case 'O': /* Order */
389 if (Window
== W_SMALL
) {
391 mvwaddstr(Score
, 12, 21,
394 mvwaddstr(Score
, 12, 21,
401 rub(0); /* Same as a rubout */
403 case 'W': /* Window toggle */
404 Window
= nextwin(Window
);
409 case 'R': /* Redraw screen */
413 case 'S': /* Save game */
417 case 'E': /* Extrapolate */
422 if (Window
!= W_FULL
)
429 error("%c: command not implemented", c
);
432 case '\r': /* Ignore RETURNs and */
433 case '\n': /* Line Feeds */
434 case ' ': /* Spaces */
435 case '\0': /* and nulls */
438 case 'Z': /* Debug code */
439 if (!Debug
&& outf
== NULL
) {
440 char buf
[MAXPATHLEN
];
443 leaveok(Board
, FALSE
);
446 while ((*sp
= readch()) != '\n') {
447 if (*sp
== killchar())
449 else if (*sp
== erasechar()) {
460 addstr(unctrl(*sp
++));
464 leaveok(Board
, TRUE
);
465 if ((outf
= fopen(buf
, "w")) == NULL
)
467 setbuf(outf
, (char *)NULL
);
473 error("unknown command: %s", unctrl(c
));
478 leaveok(Board
, TRUE
);
481 * return whether or not the player has picked
491 switch (pp
->hand
[Card_no
]) {
492 case C_GAS_SAFE
: case C_SPARE_SAFE
:
493 case C_DRIVE_SAFE
: case C_RIGHT_WAY
:
500 return (pp
->hand
[card
] != C_INIT
);
517 oppos
= opposite(card
);
518 Numgos
+= Numcards
[oppos
] - Numseen
[oppos
];
532 static const char *names
[] = {
543 static int last_prompt
= -1;
545 if (promptno
== last_prompt
)
546 move(MOVE_Y
, MOVE_X
+ strlen(names
[promptno
]) + 1);
548 move(MOVE_Y
, MOVE_X
);
549 if (promptno
== MOVEPROMPT
)
551 addstr(names
[promptno
]);
552 if (promptno
== MOVEPROMPT
)
555 last_prompt
= promptno
;
568 for ( ; cp
< &hand
[-1]; cp
++)
569 for (tp
= cp
+ 1; tp
< hand
; tp
++)