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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)move.c 8.1 (Berkeley) 5/31/93
30 * $FreeBSD: src/games/mille/move.c,v 1.6 1999/12/12 06:17:24 billf Exp $
31 * $DragonFly: src/games/mille/move.c,v 1.5 2006/08/27 17:17:23 pavalos Exp $
41 * @(#)move.c 1.2 (Berkeley) 3/28/83
45 #define CTRL(c) (c - 'A' + 1)
47 const char *Movenames[] = {
48 "M_DISCARD", "M_DRAW", "M_PLAY", "M_ORDER"
51 static void check_go(void);
52 static void getmove(void);
53 static int haspicked(PLAY *);
54 static bool playcard(PLAY *);
73 if (pp->hand[Card_no] == C_INIT)
77 error("no card there");
79 if (issafety(pp->hand[Card_no])) {
80 error("discard a safety?");
84 Discard = pp->hand[Card_no];
85 pp->hand[Card_no] = C_INIT;
92 error("must pick first");
95 goodplay = playcard(pp);
100 error("no more cards");
101 else if (haspicked(pp))
102 error("already picked");
104 pp->hand[0] = *--Topcard;
107 fprintf(outf, "DOMOVE: Draw %s\n", C_name[*Topcard]);
112 if (issafety(*Topcard))
113 pp->safety[*Topcard-S_CONV] = S_IN_HAND;
115 if (pp->hand[1] == C_INIT && Topcard > Deck) {
117 pp->hand[1] = *--Topcard;
120 fprintf(outf, "DOMOVE: Draw %s\n", C_name[*Topcard]);
124 pp->new_battle = FALSE;
125 pp->new_speed = FALSE;
133 * move blank card to top by one of two methods. If the
134 * computer's hand was sorted, the randomness for picking
135 * between equally valued cards would be lost
137 if (Order && Movetype != M_DRAW && goodplay && pp == &Player[PLAYER])
140 for (i = 1; i < HAND_SZ; i++)
141 if (pp->hand[i] == C_INIT) {
142 for (j = 0; pp->hand[j] == C_INIT; j++)
143 if (j == HAND_SZ - 1) {
147 pp->hand[i] = pp->hand[j];
148 pp->hand[j] = C_INIT;
157 * Check and see if either side can go. If they cannot,
167 for (pp = Player; pp < &Player[2]; pp++) {
168 op = (pp == &Player[COMP] ? &Player[PLAYER] : &Player[COMP]);
169 for (i = 0; i < HAND_SZ; i++) {
171 if (issafety(card) || canplay(pp, op, card)) {
174 fprintf(outf, "CHECK_GO: can play %s (%d), ", C_name[card], card);
175 fprintf(outf, "issafety(card) = %d, ", issafety(card));
176 fprintf(outf, "canplay(pp, op, card) = %d\n", canplay(pp, op, card));
183 fprintf(outf, "CHECK_GO: cannot play %s\n",
199 * check and see if player has picked
201 switch (pp->hand[Card_no]) {
205 return error("must pick first");
206 case C_GAS_SAFE: case C_SPARE_SAFE:
207 case C_DRIVE_SAFE: case C_RIGHT_WAY:
211 card = pp->hand[Card_no];
214 fprintf(outf, "PLAYCARD: Card = %s\n", C_name[card]);
219 if (pp->nummiles[C_200] == 2)
220 return error("only two 200's per hand");
221 case C_100: case C_75:
222 if (pp->speed == C_LIMIT)
223 return error("limit of 50");
225 if (pp->mileage + Value[card] > End)
226 return error("puts you over %d", End);
229 return error("cannot move now");
230 pp->nummiles[card]++;
234 if ((pp->mileage += v) == End)
238 case C_GAS: case C_SPARE: case C_REPAIRS:
239 if (pp->battle != opposite(card))
240 return error("can't play \"%s\"", C_name[card]);
242 if (pp->safety[S_RIGHT_WAY] == S_PLAYED)
247 if (pp->battle != C_INIT && pp->battle != C_STOP
248 && !isrepair(pp->battle))
249 return error("cannot play \"Go\" on a \"%s\"",
256 if (pp->speed != C_LIMIT)
257 return error("not limited");
258 pp->speed = C_END_LIMIT;
261 case C_EMPTY: case C_FLAT: case C_CRASH:
263 pp = &Player[other(Play)];
265 return error("opponent cannot go");
266 else if (pp->safety[safety(card) - S_CONV] == S_PLAYED)
268 return error("opponent is protected");
270 pp->new_battle = TRUE;
276 pp = &Player[other(Play)];
277 if (pp->speed == C_LIMIT)
278 return error("opponent has limit");
279 if (pp->safety[S_RIGHT_WAY] == S_PLAYED)
282 pp->new_speed = TRUE;
286 case C_GAS_SAFE: case C_SPARE_SAFE:
287 case C_DRIVE_SAFE: case C_RIGHT_WAY:
288 if (pp->battle == opposite(card)
289 || (card == C_RIGHT_WAY && pp->speed == C_LIMIT)) {
290 if (!(card == C_RIGHT_WAY && !isrepair(pp->battle))) {
294 if (card == C_RIGHT_WAY && pp->speed == C_LIMIT)
297 || (pp->new_speed && card == C_RIGHT_WAY)) {
298 pp->coups[card - S_CONV] = TRUE;
299 pp->total += SC_COUP;
300 pp->hand_tot += SC_COUP;
301 pp->coupscore += SC_COUP;
305 * if not coup, must pick first
307 else if (pp->hand[0] == C_INIT && Topcard > Deck)
309 pp->safety[card - S_CONV] = S_PLAYED;
310 pp->total += SC_SAFETY;
311 pp->hand_tot += SC_SAFETY;
312 if ((pp->safescore += SC_SAFETY) == NUM_SAFE * SC_SAFETY) {
313 pp->total += SC_ALL_SAFE;
314 pp->hand_tot += SC_ALL_SAFE;
316 if (card == C_RIGHT_WAY) {
317 if (pp->speed == C_LIMIT)
319 if (pp->battle == C_STOP || pp->battle == C_INIT) {
323 if (!pp->can_go && isrepair(pp->battle))
330 error("no card there");
334 if (pp == &Player[PLAYER])
336 pp->hand[Card_no] = C_INIT;
349 static bool last_ex = FALSE; /* set if last command was E */
359 leaveok(Board, FALSE);
361 while ((c = readch()) == killchar() || c == erasechar())
365 if (isprint(c) && !isspace(c)) {
373 case 'U': /* Use Card */
374 case 'D': /* Discard Card */
375 if ((Card_no = getcard()) < 0)
377 Movetype = (c == 'U' ? M_PLAY : M_DISCARD);
379 case 'O': /* Order */
381 if (Window == W_SMALL) {
383 mvwaddstr(Score, 12, 21,
386 mvwaddstr(Score, 12, 21,
393 rub(0); /* Same as a rubout */
395 case 'W': /* Window toggle */
396 Window = nextwin(Window);
401 case 'R': /* Redraw screen */
405 case 'S': /* Save game */
409 case 'E': /* Extrapolate */
414 if (Window != W_FULL)
421 error("%c: command not implemented", c);
424 case '\r': /* Ignore RETURNs and */
425 case '\n': /* Line Feeds */
426 case ' ': /* Spaces */
427 case '\0': /* and nulls */
430 case 'Z': /* Debug code */
431 if (!Debug && outf == NULL) {
432 char buf[MAXPATHLEN];
435 leaveok(Board, FALSE);
438 while ((*sp = readch()) != '\n') {
439 if (*sp == killchar())
441 else if (*sp == erasechar()) {
452 addstr(unctrl(*sp++));
456 leaveok(Board, TRUE);
457 if ((outf = fopen(buf, "w")) == NULL)
465 error("unknown command: %s", unctrl(c));
470 leaveok(Board, TRUE);
474 * return whether or not the player has picked
483 switch (pp->hand[Card_no]) {
484 case C_GAS_SAFE: case C_SPARE_SAFE:
485 case C_DRIVE_SAFE: case C_RIGHT_WAY:
492 return (pp->hand[card] != C_INIT);
508 oppos = opposite(card);
509 Numgos += Numcards[oppos] - Numseen[oppos];
523 static const char *names[] = {
534 static int last_prompt = -1;
536 if (promptno == last_prompt)
537 move(MOVE_Y, MOVE_X + strlen(names[promptno]) + 1);
539 move(MOVE_Y, MOVE_X);
540 if (promptno == MOVEPROMPT)
542 addstr(names[promptno]);
543 if (promptno == MOVEPROMPT)
546 last_prompt = promptno;
559 for ( ; cp < &hand[-1]; cp++)
560 for (tp = cp + 1; tp < hand; tp++)