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
36 static char sccsid[] = "@(#)move.c 8.1 (Berkeley) 5/31/93";
38 static const char rcsid[] =
39 "$FreeBSD: src/games/mille/move.c,v 1.6 1999/12/12 06:17:24 billf Exp $";
49 # define _tty cur_term->Nttyb
53 * @(#)move.c 1.2 (Berkeley) 3/28/83
57 #define CTRL(c) (c - 'A' + 1)
59 const char *Movenames[] = {
60 "M_DISCARD", "M_DRAW", "M_PLAY", "M_ORDER"
63 static void check_go __P((void));
64 static void getmove __P((void));
65 static int haspicked __P((PLAY *));
66 static bool playcard __P((PLAY *));
85 if (pp->hand[Card_no] == C_INIT)
89 error("no card there");
91 if (issafety(pp->hand[Card_no])) {
92 error("discard a safety?");
96 Discard = pp->hand[Card_no];
97 pp->hand[Card_no] = C_INIT;
104 error("must pick first");
107 goodplay = playcard(pp);
112 error("no more cards");
113 else if (haspicked(pp))
114 error("already picked");
116 pp->hand[0] = *--Topcard;
119 fprintf(outf, "DOMOVE: Draw %s\n", C_name[*Topcard]);
124 if (issafety(*Topcard))
125 pp->safety[*Topcard-S_CONV] = S_IN_HAND;
127 if (pp->hand[1] == C_INIT && Topcard > Deck) {
129 pp->hand[1] = *--Topcard;
132 fprintf(outf, "DOMOVE: Draw %s\n", C_name[*Topcard]);
136 pp->new_battle = FALSE;
137 pp->new_speed = FALSE;
145 * move blank card to top by one of two methods. If the
146 * computer's hand was sorted, the randomness for picking
147 * between equally valued cards would be lost
149 if (Order && Movetype != M_DRAW && goodplay && pp == &Player[PLAYER])
152 for (i = 1; i < HAND_SZ; i++)
153 if (pp->hand[i] == C_INIT) {
154 for (j = 0; pp->hand[j] == C_INIT; j++)
159 pp->hand[i] = pp->hand[j];
160 pp->hand[j] = C_INIT;
169 * Check and see if either side can go. If they cannot,
179 for (pp = Player; pp < &Player[2]; pp++) {
180 op = (pp == &Player[COMP] ? &Player[PLAYER] : &Player[COMP]);
181 for (i = 0; i < HAND_SZ; i++) {
183 if (issafety(card) || canplay(pp, op, card)) {
186 fprintf(outf, "CHECK_GO: can play %s (%d), ", C_name[card], card);
187 fprintf(outf, "issafety(card) = %d, ", issafety(card));
188 fprintf(outf, "canplay(pp, op, card) = %d\n", canplay(pp, op, card));
195 fprintf(outf, "CHECK_GO: cannot play %s\n",
211 * check and see if player has picked
213 switch (pp->hand[Card_no]) {
217 return error("must pick first");
218 case C_GAS_SAFE: case C_SPARE_SAFE:
219 case C_DRIVE_SAFE: case C_RIGHT_WAY:
223 card = pp->hand[Card_no];
226 fprintf(outf, "PLAYCARD: Card = %s\n", C_name[card]);
231 if (pp->nummiles[C_200] == 2)
232 return error("only two 200's per hand");
233 case C_100: case C_75:
234 if (pp->speed == C_LIMIT)
235 return error("limit of 50");
237 if (pp->mileage + Value[card] > End)
238 return error("puts you over %d", End);
241 return error("cannot move now");
242 pp->nummiles[card]++;
246 if ((pp->mileage += v) == End)
250 case C_GAS: case C_SPARE: case C_REPAIRS:
251 if (pp->battle != opposite(card))
252 return error("can't play \"%s\"", C_name[card]);
254 if (pp->safety[S_RIGHT_WAY] == S_PLAYED)
259 if (pp->battle != C_INIT && pp->battle != C_STOP
260 && !isrepair(pp->battle))
261 return error("cannot play \"Go\" on a \"%s\"",
268 if (pp->speed != C_LIMIT)
269 return error("not limited");
270 pp->speed = C_END_LIMIT;
273 case C_EMPTY: case C_FLAT: case C_CRASH:
275 pp = &Player[other(Play)];
277 return error("opponent cannot go");
278 else if (pp->safety[safety(card) - S_CONV] == S_PLAYED)
280 return error("opponent is protected");
282 pp->new_battle = TRUE;
288 pp = &Player[other(Play)];
289 if (pp->speed == C_LIMIT)
290 return error("opponent has limit");
291 if (pp->safety[S_RIGHT_WAY] == S_PLAYED)
294 pp->new_speed = TRUE;
298 case C_GAS_SAFE: case C_SPARE_SAFE:
299 case C_DRIVE_SAFE: case C_RIGHT_WAY:
300 if (pp->battle == opposite(card)
301 || (card == C_RIGHT_WAY && pp->speed == C_LIMIT)) {
302 if (!(card == C_RIGHT_WAY && !isrepair(pp->battle))) {
306 if (card == C_RIGHT_WAY && pp->speed == C_LIMIT)
309 || (pp->new_speed && card == C_RIGHT_WAY)) {
310 pp->coups[card - S_CONV] = TRUE;
311 pp->total += SC_COUP;
312 pp->hand_tot += SC_COUP;
313 pp->coupscore += SC_COUP;
317 * if not coup, must pick first
319 else if (pp->hand[0] == C_INIT && Topcard > Deck)
321 pp->safety[card - S_CONV] = S_PLAYED;
322 pp->total += SC_SAFETY;
323 pp->hand_tot += SC_SAFETY;
324 if ((pp->safescore += SC_SAFETY) == NUM_SAFE * SC_SAFETY) {
325 pp->total += SC_ALL_SAFE;
326 pp->hand_tot += SC_ALL_SAFE;
328 if (card == C_RIGHT_WAY) {
329 if (pp->speed == C_LIMIT)
331 if (pp->battle == C_STOP || pp->battle == C_INIT) {
335 if (!pp->can_go && isrepair(pp->battle))
342 error("no card there");
346 if (pp == &Player[PLAYER])
348 pp->hand[Card_no] = C_INIT;
349 Next = (Next == -1 ? FALSE : TRUE);
361 static bool last_ex = FALSE; /* set if last command was E */
371 leaveok(Board, FALSE);
373 while ((c = readch()) == killchar() || c == erasechar())
377 if (isprint(c) && !isspace(c)) {
385 case 'U': /* Use Card */
386 case 'D': /* Discard Card */
387 if ((Card_no = getcard()) < 0)
389 Movetype = (c == 'U' ? M_PLAY : M_DISCARD);
391 case 'O': /* Order */
393 if (Window == W_SMALL) {
395 mvwaddstr(Score, 12, 21,
398 mvwaddstr(Score, 12, 21,
405 rub(0); /* Same as a rubout */
407 case 'W': /* Window toggle */
408 Window = nextwin(Window);
413 case 'R': /* Redraw screen */
417 case 'S': /* Save game */
421 case 'E': /* Extrapolate */
426 if (Window != W_FULL)
433 error("%c: command not implemented", c);
436 case '\r': /* Ignore RETURNs and */
437 case '\n': /* Line Feeds */
438 case ' ': /* Spaces */
439 case '\0': /* and nulls */
442 case 'Z': /* Debug code */
443 if (!Debug && outf == NULL) {
444 char buf[MAXPATHLEN];
447 leaveok(Board, FALSE);
450 while ((*sp = readch()) != '\n') {
451 if (*sp == killchar())
453 else if (*sp == erasechar()) {
464 addstr(unctrl(*sp++));
468 leaveok(Board, TRUE);
469 if ((outf = fopen(buf, "w")) == NULL)
471 setbuf(outf, (char *)NULL);
477 error("unknown command: %s", unctrl(c));
482 leaveok(Board, TRUE);
485 * return whether or not the player has picked
495 switch (pp->hand[Card_no]) {
496 case C_GAS_SAFE: case C_SPARE_SAFE:
497 case C_DRIVE_SAFE: case C_RIGHT_WAY:
504 return (pp->hand[card] != C_INIT);
521 oppos = opposite(card);
522 Numgos += Numcards[oppos] - Numseen[oppos];
537 static char *names[] = {
548 static int last_prompt = -1;
550 if (promptno == last_prompt)
551 move(MOVE_Y, MOVE_X + strlen(names[promptno]) + 1);
553 move(MOVE_Y, MOVE_X);
554 if (promptno == MOVEPROMPT)
556 addstr(names[promptno]);
557 if (promptno == MOVEPROMPT)
560 last_prompt = promptno;
574 for ( ; cp < &hand[-1]; cp++)
575 for (tp = cp + 1; tp < hand; tp++)