2 * Copyright (c) 1982, 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[] = "@(#)comp.c 8.1 (Berkeley) 5/31/93";
38 static const char rcsid[] =
45 * @(#)comp.c 1.1 (Berkeley) 4/1/82
48 # define V_VALUABLE 40
56 bool foundend, cango, canstop, foundlow;
57 unsgn int i, count200, badcount, nummin, nummax, diff;
60 int valbuf[HAND_SZ], count[NUM_CARDS];
63 wmove(Score, ERR_Y, ERR_X); /* get rid of error messages */
72 /* Try for a Coup Forre, and see what we have. */
73 for (i = 0; i < NUM_CARDS; i++)
75 for (i = 0; i < HAND_SZ; i++) {
78 case C_STOP: case C_CRASH:
79 case C_FLAT: case C_EMPTY:
80 if ((playit[i] = canplay(pp, op, card)) != NULL)
84 if ((playit[i] = canplay(pp, op, card))
85 && Numseen[C_25] == Numcards[C_25]
86 && Numseen[C_50] == Numcards[C_50])
89 case C_25: case C_50: case C_75:
90 case C_100: case C_200:
91 if ((playit[i] = canplay(pp, op, card))
92 && pp->mileage + Value[card] == End)
96 playit[i] = canplay(pp, op, card);
101 case C_GAS_SAFE: case C_DRIVE_SAFE:
102 case C_SPARE_SAFE: case C_RIGHT_WAY:
103 if (pp->battle == opposite(card) ||
104 (pp->speed == C_LIMIT && card == C_RIGHT_WAY)) {
117 /* No Coup Forre. Draw to fill hand, then restart, as needed. */
118 if (pp->hand[0] == C_INIT && Topcard > Deck) {
125 fprintf(outf, "CALCMOVE: cango = %d, canstop = %d, safe = %d\n",
126 cango, canstop, safe);
129 foundend = !check_ext(TRUE);
130 for (i = 0; safe && i < HAND_SZ; i++) {
131 if (issafety(pp->hand[i])) {
132 if (onecard(op) || (foundend && cango && !canstop)) {
136 "CALCMOVE: onecard(op) = %d, foundend = %d\n",
137 onecard(op), foundend);
144 oppos = opposite(pp->hand[i]);
145 if (Numseen[oppos] == Numcards[oppos] &&
146 !(pp->hand[i] == C_RIGHT_WAY &&
147 Numseen[C_LIMIT] != Numcards[C_LIMIT]))
150 && (op->can_go || !pp->can_go || Topcard < Deck)) {
151 card = (Topcard - Deck) - roll(1, 10);
152 if ((!pp->mileage) != (!op->mileage))
157 "CALCMOVE: card = %d, DECK_SZ / 4 = %d\n",
160 if (card < DECK_SZ / 4)
167 if (!pp->can_go && !isrepair(pp->battle))
168 Numneed[opposite(pp->battle)]++;
170 foundlow = (cango || count[C_END_LIMIT] != 0
171 || Numseen[C_LIMIT] == Numcards[C_LIMIT]
172 || pp->safety[S_RIGHT_WAY] != S_UNKNOWN);
174 count200 = pp->nummiles[C_200];
181 for (i = 0; i < HAND_SZ; i++) {
183 if (issafety(card) || playit[i] == (cango != 0)) {
186 fprintf(outf, "CALCMOVE: switch(\"%s\")\n",
190 case C_25: case C_50:
191 diff = End - pp->mileage;
192 /* avoid getting too close */
193 if (Topcard > Deck && cango && diff <= 100
194 && diff / Value[card] > count[card]
195 && (card == C_25 || diff % 50 == 0)) {
196 if (card == C_50 && diff - 50 == 25
205 *value = (Value[card] >> 3);
206 if (pp->speed == C_LIMIT)
211 && (card == C_50 || count[C_50] == 0)) {
212 *value = (pp->mileage ? 10 : 20);
217 if (++count200 > 2) {
221 case C_75: case C_100:
222 *value = (Value[card] >> 3);
223 if (pp->speed == C_LIMIT)
228 if (pp->mileage + Value[card] > End)
229 *value = (End == 700 ? card : 0);
230 else if (pp->mileage + Value[card] == End) {
231 *value = (foundend ? card : V_VALUABLE);
236 if (pp->safety[S_RIGHT_WAY] != S_UNKNOWN)
237 *value = (pp->safety[S_RIGHT_WAY] ==
239 else if (pp->speed == C_LIMIT &&
240 End - pp->mileage <= 50)
242 else if (pp->speed == C_LIMIT
243 || Numseen[C_LIMIT] != Numcards[C_LIMIT]) {
250 --count[C_END_LIMIT];
253 case C_REPAIRS: case C_SPARE: case C_GAS:
254 safe = safety(card) - S_CONV;
255 oppos = opposite(card);
256 if (pp->safety[safe] != S_UNKNOWN)
257 *value = (pp->safety[safe] ==
259 else if (pp->battle != oppos
260 && (Numseen[oppos] == Numcards[oppos] ||
261 Numseen[oppos] + count[card] >
268 *value = Numcards[oppos] * 6;
269 *value += Numseen[card] -
272 *value /= (count[card]*count[card]);
277 if (pp->safety[S_RIGHT_WAY] != S_UNKNOWN)
278 *value = (pp->safety[S_RIGHT_WAY] ==
281 && Numgos + count[C_GO] == Numneed[C_GO]) {
286 *value = Numneed[C_GO] * 3;
287 *value += (Numseen[C_GO] - Numgos);
288 *value /= (count[C_GO] * count[C_GO]);
293 if (op->mileage + 50 >= End) {
294 *value = (End == 700 && !cango);
297 if (canstop || (cango && !op->can_go))
300 *value = (pp->safety[S_RIGHT_WAY] !=
307 case C_CRASH: case C_EMPTY: case C_FLAT:
308 safe = safety(card) - S_CONV;
309 oppos = opposite(card);
310 *value = (pp->safety[safe]!=S_UNKNOWN ? 3 : 4);
312 if (op->safety[safe] == S_PLAYED)
315 *value *= Numneed[oppos] +
317 if (!pp->mileage || foundend ||
320 if (op->mileage == 0 || onecard(op))
322 if (op->speed == C_LIMIT)
325 pp->safety[safe] != S_UNKNOWN)
328 *value /= ++badcount;
332 if (op->safety[S_RIGHT_WAY] == S_PLAYED)
335 *value = (pp->safety[S_RIGHT_WAY] !=
337 *value *= Numcards[C_STOP] +
339 if (!pp->mileage || foundend ||
343 *value /= ++badcount;
344 if (op->mileage == 0)
346 if ((card == C_LIMIT &&
347 op->speed == C_LIMIT) ||
350 if (cango && pp->safety[S_RIGHT_WAY] !=
355 case C_GAS_SAFE: case C_DRIVE_SAFE:
356 case C_SPARE_SAFE: case C_RIGHT_WAY:
357 *value = cango ? 0 : 101;
365 *value = cango ? 0 : 101;
366 if (card != C_INIT) {
367 if (*value >= curmax) {
371 if (*value <= curmin) {
378 mvprintw(i + 6, 2, "%3d %-14s", *value,
379 C_name[pp->hand[i]]);
383 if (!pp->can_go && !isrepair(pp->battle))
384 Numneed[opposite(pp->battle)]++;
387 mvaddstr(MOVE_Y + 1, MOVE_X, "PLAY\n");
392 if (issafety(pp->hand[nummin])) { /* NEVER discard a safety */
396 mvaddstr(MOVE_Y + 1, MOVE_X, "DISCARD\n");
397 Movetype = M_DISCARD;
400 mvprintw(MOVE_Y + 2, MOVE_X, "%16s", C_name[pp->hand[Card_no]]);
404 * Return true if the given player could conceivably win with his next card.
415 if (pp->can_go || ((isrepair(bat) || bat == C_STOP || spd == C_LIMIT) &&
416 Numseen[S_RIGHT_WAY] != 0) ||
417 (bat >= 0 && Numseen[safety(bat)] != 0))
418 switch (End - pp->mileage) {
420 if (pp->nummiles[C_200] == 2)
427 card = (End - pp->mileage == 75 ? C_75 : C_100);
429 return Numseen[S_RIGHT_WAY] == 0;
433 card = (End - pp->mileage == 25 ? C_25 : C_50);
434 return Numseen[card] != Numcards[card];
440 canplay(pp, op, card)
446 if (pp->nummiles[C_200] == 2)
449 case C_75: case C_100:
450 if (pp->speed == C_LIMIT)
454 if (pp->mileage + Value[card] > End)
461 case C_EMPTY: case C_FLAT: case C_CRASH:
463 if (op->can_go && op->safety[safety(card) - S_CONV] != S_PLAYED)
467 if (op->speed != C_LIMIT &&
468 op->safety[S_RIGHT_WAY] != S_PLAYED &&
469 op->mileage + 50 < End)
472 case C_GAS: case C_SPARE: case C_REPAIRS:
473 if (pp->battle == opposite(card))
478 (isrepair(pp->battle) || pp->battle == C_STOP))
482 if (pp->speed == C_LIMIT)