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 $
40 * @(#)move.c 1.2 (Berkeley) 3/28/83
44 #define CTRL(c) (c - 'A' + 1)
46 static void check_go(void);
47 static void getmove(void);
48 static int haspicked(PLAY
*);
49 static bool playcard(PLAY
*);
68 if (pp
->hand
[Card_no
] == C_INIT
) {
72 error("no card there");
74 if (issafety(pp
->hand
[Card_no
])) {
75 error("discard a safety?");
79 Discard
= pp
->hand
[Card_no
];
80 pp
->hand
[Card_no
] = C_INIT
;
87 error("must pick first");
90 goodplay
= playcard(pp
);
95 error("no more cards");
96 else if (haspicked(pp
))
97 error("already picked");
99 pp
->hand
[0] = *--Topcard
;
102 fprintf(outf
, "DOMOVE: Draw %s\n", C_name
[*Topcard
]);
107 if (issafety(*Topcard
))
108 pp
->safety
[*Topcard
-S_CONV
] = S_IN_HAND
;
110 if (pp
->hand
[1] == C_INIT
&& Topcard
> Deck
) {
112 pp
->hand
[1] = *--Topcard
;
115 fprintf(outf
, "DOMOVE: Draw %s\n", C_name
[*Topcard
]);
119 pp
->new_battle
= FALSE
;
120 pp
->new_speed
= FALSE
;
128 * move blank card to top by one of two methods. If the
129 * computer's hand was sorted, the randomness for picking
130 * between equally valued cards would be lost
132 if (Order
&& Movetype
!= M_DRAW
&& goodplay
&& pp
== &Player
[PLAYER
])
135 for (i
= 1; i
< HAND_SZ
; i
++)
136 if (pp
->hand
[i
] == C_INIT
) {
137 for (j
= 0; pp
->hand
[j
] == C_INIT
; j
++)
138 if (j
== HAND_SZ
- 1) {
142 pp
->hand
[i
] = pp
->hand
[j
];
143 pp
->hand
[j
] = C_INIT
;
152 * Check and see if either side can go. If they cannot,
162 for (pp
= Player
; pp
< &Player
[2]; pp
++) {
163 op
= (pp
== &Player
[COMP
] ? &Player
[PLAYER
] : &Player
[COMP
]);
164 for (i
= 0; i
< HAND_SZ
; i
++) {
166 if (issafety(card
) || canplay(pp
, op
, card
)) {
169 fprintf(outf
, "CHECK_GO: can play %s (%d), ", C_name
[card
], card
);
170 fprintf(outf
, "issafety(card) = %d, ", issafety(card
));
171 fprintf(outf
, "canplay(pp, op, card) = %d\n", canplay(pp
, op
, card
));
178 fprintf(outf
, "CHECK_GO: cannot play %s\n",
194 * check and see if player has picked
196 switch (pp
->hand
[Card_no
]) {
200 return error("must pick first");
201 case C_GAS_SAFE
: case C_SPARE_SAFE
:
202 case C_DRIVE_SAFE
: case C_RIGHT_WAY
:
206 card
= pp
->hand
[Card_no
];
209 fprintf(outf
, "PLAYCARD: Card = %s\n", C_name
[card
]);
214 if (pp
->nummiles
[C_200
] == 2)
215 return error("only two 200's per hand");
217 case C_100
: case C_75
:
218 if (pp
->speed
== C_LIMIT
)
219 return error("limit of 50");
222 if (pp
->mileage
+ Value
[card
] > End
)
223 return error("puts you over %d", End
);
227 return error("cannot move now");
228 pp
->nummiles
[card
]++;
232 if ((pp
->mileage
+= v
) == End
)
236 case C_GAS
: case C_SPARE
: case C_REPAIRS
:
237 if (pp
->battle
!= opposite(card
))
238 return error("can't play \"%s\"", C_name
[card
]);
240 if (pp
->safety
[S_RIGHT_WAY
] == S_PLAYED
)
245 if (pp
->battle
!= C_INIT
&& pp
->battle
!= C_STOP
246 && !isrepair(pp
->battle
))
247 return error("cannot play \"Go\" on a \"%s\"",
254 if (pp
->speed
!= C_LIMIT
)
255 return error("not limited");
256 pp
->speed
= C_END_LIMIT
;
259 case C_EMPTY
: case C_FLAT
: case C_CRASH
:
261 pp
= &Player
[other(Play
)];
263 return error("opponent cannot go");
264 else if (pp
->safety
[safety(card
) - S_CONV
] == S_PLAYED
)
266 return error("opponent is protected");
268 pp
->new_battle
= TRUE
;
274 pp
= &Player
[other(Play
)];
275 if (pp
->speed
== C_LIMIT
)
276 return error("opponent has limit");
277 if (pp
->safety
[S_RIGHT_WAY
] == S_PLAYED
)
280 pp
->new_speed
= TRUE
;
284 case C_GAS_SAFE
: case C_SPARE_SAFE
:
285 case C_DRIVE_SAFE
: case C_RIGHT_WAY
:
286 if (pp
->battle
== opposite(card
)
287 || (card
== C_RIGHT_WAY
&& pp
->speed
== C_LIMIT
)) {
288 if (!(card
== C_RIGHT_WAY
&& !isrepair(pp
->battle
))) {
292 if (card
== C_RIGHT_WAY
&& pp
->speed
== C_LIMIT
)
295 || (pp
->new_speed
&& card
== C_RIGHT_WAY
)) {
296 pp
->coups
[card
- S_CONV
] = TRUE
;
297 pp
->total
+= SC_COUP
;
298 pp
->hand_tot
+= SC_COUP
;
299 pp
->coupscore
+= SC_COUP
;
303 * if not coup, must pick first
305 else if (pp
->hand
[0] == C_INIT
&& Topcard
> Deck
)
307 pp
->safety
[card
- S_CONV
] = S_PLAYED
;
308 pp
->total
+= SC_SAFETY
;
309 pp
->hand_tot
+= SC_SAFETY
;
310 if ((pp
->safescore
+= SC_SAFETY
) == NUM_SAFE
* SC_SAFETY
) {
311 pp
->total
+= SC_ALL_SAFE
;
312 pp
->hand_tot
+= SC_ALL_SAFE
;
314 if (card
== C_RIGHT_WAY
) {
315 if (pp
->speed
== C_LIMIT
)
317 if (pp
->battle
== C_STOP
|| pp
->battle
== C_INIT
) {
321 if (!pp
->can_go
&& isrepair(pp
->battle
))
328 error("no card there");
332 if (pp
== &Player
[PLAYER
])
334 pp
->hand
[Card_no
] = C_INIT
;
347 static bool last_ex
= FALSE
; /* set if last command was E */
357 leaveok(Board
, FALSE
);
359 while ((c
= readch()) == killchar() || c
== erasechar())
363 if (isprint(c
) && !isspace(c
)) {
371 case 'U': /* Use Card */
372 case 'D': /* Discard Card */
373 if ((Card_no
= getcard()) < 0)
375 Movetype
= (c
== 'U' ? M_PLAY
: M_DISCARD
);
377 case 'O': /* Order */
379 if (Window
== W_SMALL
) {
381 mvwaddstr(Score
, 12, 21,
384 mvwaddstr(Score
, 12, 21,
391 rub(0); /* Same as a rubout */
393 case 'W': /* Window toggle */
394 Window
= nextwin(Window
);
399 case 'R': /* Redraw screen */
403 case 'S': /* Save game */
407 case 'E': /* Extrapolate */
412 if (Window
!= W_FULL
)
419 error("%c: command not implemented", c
);
422 case '\r': /* Ignore RETURNs and */
423 case '\n': /* Line Feeds */
424 case ' ': /* Spaces */
425 case '\0': /* and nulls */
428 case 'Z': /* Debug code */
429 if (!Debug
&& outf
== NULL
) {
430 char buf
[MAXPATHLEN
];
433 leaveok(Board
, FALSE
);
436 while ((*sp
= readch()) != '\n') {
437 if (*sp
== killchar())
439 else if (*sp
== erasechar()) {
450 addstr(unctrl(*sp
++));
454 leaveok(Board
, TRUE
);
455 if ((outf
= fopen(buf
, "w")) == NULL
)
463 error("unknown command: %s", unctrl(c
));
468 leaveok(Board
, TRUE
);
472 * return whether or not the player has picked
481 switch (pp
->hand
[Card_no
]) {
482 case C_GAS_SAFE
: case C_SPARE_SAFE
:
483 case C_DRIVE_SAFE
: case C_RIGHT_WAY
:
490 return (pp
->hand
[card
] != C_INIT
);
506 oppos
= opposite(card
);
507 Numgos
+= Numcards
[oppos
] - Numseen
[oppos
];
521 static const char *const names
[] = {
532 static int last_prompt
= -1;
534 if (promptno
== last_prompt
)
535 move(MOVE_Y
, MOVE_X
+ strlen(names
[promptno
]) + 1);
537 move(MOVE_Y
, MOVE_X
);
538 if (promptno
== MOVEPROMPT
)
540 addstr(names
[promptno
]);
541 if (promptno
== MOVEPROMPT
)
544 last_prompt
= promptno
;
557 for ( ; cp
< &hand
[-1]; cp
++)
558 for (tp
= cp
+ 1; tp
< hand
; tp
++)