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++)
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",
198 * check and see if player has picked
200 switch (pp->hand[Card_no]) {
204 return error("must pick first");
205 case C_GAS_SAFE: case C_SPARE_SAFE:
206 case C_DRIVE_SAFE: case C_RIGHT_WAY:
210 card = pp->hand[Card_no];
213 fprintf(outf, "PLAYCARD: Card = %s\n", C_name[card]);
218 if (pp->nummiles[C_200] == 2)
219 return error("only two 200's per hand");
220 case C_100: case C_75:
221 if (pp->speed == C_LIMIT)
222 return error("limit of 50");
224 if (pp->mileage + Value[card] > End)
225 return error("puts you over %d", End);
228 return error("cannot move now");
229 pp->nummiles[card]++;
233 if ((pp->mileage += v) == End)
237 case C_GAS: case C_SPARE: case C_REPAIRS:
238 if (pp->battle != opposite(card))
239 return error("can't play \"%s\"", C_name[card]);
241 if (pp->safety[S_RIGHT_WAY] == S_PLAYED)
246 if (pp->battle != C_INIT && pp->battle != C_STOP
247 && !isrepair(pp->battle))
248 return error("cannot play \"Go\" on a \"%s\"",
255 if (pp->speed != C_LIMIT)
256 return error("not limited");
257 pp->speed = C_END_LIMIT;
260 case C_EMPTY: case C_FLAT: case C_CRASH:
262 pp = &Player[other(Play)];
264 return error("opponent cannot go");
265 else if (pp->safety[safety(card) - S_CONV] == S_PLAYED)
267 return error("opponent is protected");
269 pp->new_battle = TRUE;
275 pp = &Player[other(Play)];
276 if (pp->speed == C_LIMIT)
277 return error("opponent has limit");
278 if (pp->safety[S_RIGHT_WAY] == S_PLAYED)
281 pp->new_speed = TRUE;
285 case C_GAS_SAFE: case C_SPARE_SAFE:
286 case C_DRIVE_SAFE: case C_RIGHT_WAY:
287 if (pp->battle == opposite(card)
288 || (card == C_RIGHT_WAY && pp->speed == C_LIMIT)) {
289 if (!(card == C_RIGHT_WAY && !isrepair(pp->battle))) {
293 if (card == C_RIGHT_WAY && pp->speed == C_LIMIT)
296 || (pp->new_speed && card == C_RIGHT_WAY)) {
297 pp->coups[card - S_CONV] = TRUE;
298 pp->total += SC_COUP;
299 pp->hand_tot += SC_COUP;
300 pp->coupscore += SC_COUP;
304 * if not coup, must pick first
306 else if (pp->hand[0] == C_INIT && Topcard > Deck)
308 pp->safety[card - S_CONV] = S_PLAYED;
309 pp->total += SC_SAFETY;
310 pp->hand_tot += SC_SAFETY;
311 if ((pp->safescore += SC_SAFETY) == NUM_SAFE * SC_SAFETY) {
312 pp->total += SC_ALL_SAFE;
313 pp->hand_tot += SC_ALL_SAFE;
315 if (card == C_RIGHT_WAY) {
316 if (pp->speed == C_LIMIT)
318 if (pp->battle == C_STOP || pp->battle == C_INIT) {
322 if (!pp->can_go && isrepair(pp->battle))
329 error("no card there");
333 if (pp == &Player[PLAYER])
335 pp->hand[Card_no] = C_INIT;
336 Next = (Next == -1 ? FALSE : TRUE);
348 static bool last_ex = FALSE; /* set if last command was E */
358 leaveok(Board, FALSE);
360 while ((c = readch()) == killchar() || c == erasechar())
364 if (isprint(c) && !isspace(c)) {
372 case 'U': /* Use Card */
373 case 'D': /* Discard Card */
374 if ((Card_no = getcard()) < 0)
376 Movetype = (c == 'U' ? M_PLAY : M_DISCARD);
378 case 'O': /* Order */
380 if (Window == W_SMALL) {
382 mvwaddstr(Score, 12, 21,
385 mvwaddstr(Score, 12, 21,
392 rub(0); /* Same as a rubout */
394 case 'W': /* Window toggle */
395 Window = nextwin(Window);
400 case 'R': /* Redraw screen */
404 case 'S': /* Save game */
408 case 'E': /* Extrapolate */
413 if (Window != W_FULL)
420 error("%c: command not implemented", c);
423 case '\r': /* Ignore RETURNs and */
424 case '\n': /* Line Feeds */
425 case ' ': /* Spaces */
426 case '\0': /* and nulls */
429 case 'Z': /* Debug code */
430 if (!Debug && outf == NULL) {
431 char buf[MAXPATHLEN];
434 leaveok(Board, FALSE);
437 while ((*sp = readch()) != '\n') {
438 if (*sp == killchar())
440 else if (*sp == erasechar()) {
451 addstr(unctrl(*sp++));
455 leaveok(Board, TRUE);
456 if ((outf = fopen(buf, "w")) == NULL)
464 error("unknown command: %s", unctrl(c));
469 leaveok(Board, TRUE);
473 * return whether or not the player has picked
482 switch (pp->hand[Card_no]) {
483 case C_GAS_SAFE: case C_SPARE_SAFE:
484 case C_DRIVE_SAFE: case C_RIGHT_WAY:
491 return (pp->hand[card] != C_INIT);
507 oppos = opposite(card);
508 Numgos += Numcards[oppos] - Numseen[oppos];
522 static const char *names[] = {
533 static int last_prompt = -1;
535 if (promptno == last_prompt)
536 move(MOVE_Y, MOVE_X + strlen(names[promptno]) + 1);
538 move(MOVE_Y, MOVE_X);
539 if (promptno == MOVEPROMPT)
541 addstr(names[promptno]);
542 if (promptno == MOVEPROMPT)
545 last_prompt = promptno;
558 for ( ; cp < &hand[-1]; cp++)
559 for (tp = cp + 1; tp < hand; tp++)