1 /* $NetBSD: comp.c,v 1.12 2009/05/25 23:34:50 dholland Exp $ */
4 * Copyright (c) 1982, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
35 static char sccsid
[] = "@(#)comp.c 8.1 (Berkeley) 5/31/93";
37 __RCSID("$NetBSD: comp.c,v 1.12 2009/05/25 23:34:50 dholland Exp $");
44 * @(#)comp.c 1.1 (Berkeley) 4/1/82
55 bool foundend
, cango
, canstop
, foundlow
;
56 unsigned int i
, count200
, badcount
, nummin
, nummax
, diff
;
59 int valbuf
[HAND_SZ
], count
[NUM_CARDS
];
62 wmove(Score
, ERR_Y
, ERR_X
); /* get rid of error messages */
71 /* Try for a Coup Forre, and see what we have. */
72 for (i
= 0; i
< NUM_CARDS
; i
++)
74 for (i
= 0; i
< HAND_SZ
; i
++) {
77 case C_STOP
: case C_CRASH
:
78 case C_FLAT
: case C_EMPTY
:
79 if ((playit
[i
] = canplay(pp
, op
, card
)) != 0)
83 if ((playit
[i
] = canplay(pp
, op
, card
))
84 && Numseen
[C_25
] == Numcards
[C_25
]
85 && Numseen
[C_50
] == Numcards
[C_50
])
88 case C_25
: case C_50
: case C_75
:
89 case C_100
: case C_200
:
90 if ((playit
[i
] = canplay(pp
, op
, card
))
91 && pp
->mileage
+ Value
[card
] == End
)
95 playit
[i
] = canplay(pp
, op
, card
);
100 case C_GAS_SAFE
: case C_DRIVE_SAFE
:
101 case C_SPARE_SAFE
: case C_RIGHT_WAY
:
102 if (pp
->battle
== opposite(card
) ||
103 (pp
->speed
== C_LIMIT
&& card
== C_RIGHT_WAY
)) {
116 /* No Coup Forre. Draw to fill hand, then restart, as needed. */
117 if (pp
->hand
[0] == C_INIT
&& Topcard
> Deck
) {
124 fprintf(outf
, "CALCMOVE: cango = %d, canstop = %d, safe = %d\n",
125 cango
, canstop
, safe
);
128 foundend
= !check_ext(TRUE
);
129 for (i
= 0; safe
&& i
< HAND_SZ
; i
++) {
130 if (is_safety(pp
->hand
[i
])) {
131 if (onecard(op
) || (foundend
&& cango
&& !canstop
)) {
135 "CALCMOVE: onecard(op) = %d, foundend = %d\n",
136 onecard(op
), foundend
);
143 oppos
= opposite(pp
->hand
[i
]);
144 if (Numseen
[oppos
] == Numcards
[oppos
] &&
145 !(pp
->hand
[i
] == C_RIGHT_WAY
&&
146 Numseen
[C_LIMIT
] != Numcards
[C_LIMIT
]))
149 && (op
->can_go
|| !pp
->can_go
|| Topcard
< Deck
)) {
150 card
= (Topcard
- Deck
) - roll(1, 10);
151 if ((!pp
->mileage
) != (!op
->mileage
))
156 "CALCMOVE: card = %d, DECK_SZ / 4 = %d\n",
159 if (card
< DECK_SZ
/ 4)
166 if (!pp
->can_go
&& !is_repair(pp
->battle
))
167 Numneed
[opposite(pp
->battle
)]++;
169 foundlow
= (cango
|| count
[C_END_LIMIT
] != 0
170 || Numseen
[C_LIMIT
] == Numcards
[C_LIMIT
]
171 || pp
->safety
[S_RIGHT_WAY
] != S_UNKNOWN
);
173 count200
= pp
->nummiles
[C_200
];
180 for (i
= 0; i
< HAND_SZ
; i
++) {
182 if (is_safety(card
) || playit
[i
] == (cango
!= 0)) {
185 fprintf(outf
, "CALCMOVE: switch(\"%s\")\n",
189 case C_25
: case C_50
:
190 diff
= End
- pp
->mileage
;
191 /* avoid getting too close */
192 if (Topcard
> Deck
&& cango
&& diff
<= 100
193 && (int)diff
/ Value
[card
] > count
[card
]
194 && (card
== C_25
|| diff
% 50 == 0)) {
195 if (card
== C_50
&& diff
- 50 == 25
204 *value
= (Value
[card
] >> 3);
205 if (pp
->speed
== C_LIMIT
)
210 && (card
== C_50
|| count
[C_50
] == 0)) {
211 *value
= (pp
->mileage
? 10 : 20);
216 if (++count200
> 2) {
220 case C_75
: case C_100
:
221 *value
= (Value
[card
] >> 3);
222 if (pp
->speed
== C_LIMIT
)
227 if (pp
->mileage
+ Value
[card
] > End
)
228 *value
= (End
== 700 ? card
: 0);
229 else if (pp
->mileage
+ Value
[card
] == End
) {
230 *value
= (foundend
? card
: V_VALUABLE
);
235 if (pp
->safety
[S_RIGHT_WAY
] != S_UNKNOWN
)
236 *value
= (pp
->safety
[S_RIGHT_WAY
] ==
238 else if (pp
->speed
== C_LIMIT
&&
239 End
- pp
->mileage
<= 50)
241 else if (pp
->speed
== C_LIMIT
242 || Numseen
[C_LIMIT
] != Numcards
[C_LIMIT
]) {
249 --count
[C_END_LIMIT
];
252 case C_REPAIRS
: case C_SPARE
: case C_GAS
:
253 safe
= safety(card
) - S_CONV
;
254 oppos
= opposite(card
);
255 if (pp
->safety
[safe
] != S_UNKNOWN
)
256 *value
= (pp
->safety
[safe
] ==
258 else if (pp
->battle
!= oppos
259 && (Numseen
[oppos
] == Numcards
[oppos
] ||
260 Numseen
[oppos
] + count
[card
] >
267 *value
= Numcards
[oppos
] * 6;
268 *value
+= Numseen
[card
] -
271 *value
/= (count
[card
]*count
[card
]);
276 if (pp
->safety
[S_RIGHT_WAY
] != S_UNKNOWN
)
277 *value
= (pp
->safety
[S_RIGHT_WAY
] ==
280 && Numgos
+ count
[C_GO
] == Numneed
[C_GO
]) {
285 *value
= Numneed
[C_GO
] * 3;
286 *value
+= (Numseen
[C_GO
] - Numgos
);
287 *value
/= (count
[C_GO
] * count
[C_GO
]);
292 if (op
->mileage
+ 50 >= End
) {
293 *value
= (End
== 700 && !cango
);
296 if (canstop
|| (cango
&& !op
->can_go
))
299 *value
= (pp
->safety
[S_RIGHT_WAY
] !=
306 case C_CRASH
: case C_EMPTY
: case C_FLAT
:
307 safe
= safety(card
) - S_CONV
;
308 oppos
= opposite(card
);
309 *value
= (pp
->safety
[safe
]!=S_UNKNOWN
? 3 : 4);
311 if (op
->safety
[safe
] == S_PLAYED
)
314 *value
*= Numneed
[oppos
] +
316 if (!pp
->mileage
|| foundend
||
319 if (op
->mileage
== 0 || onecard(op
))
321 if (op
->speed
== C_LIMIT
)
324 pp
->safety
[safe
] != S_UNKNOWN
)
327 *value
/= ++badcount
;
331 if (op
->safety
[S_RIGHT_WAY
] == S_PLAYED
)
334 *value
= (pp
->safety
[S_RIGHT_WAY
] !=
336 *value
*= Numcards
[C_STOP
] +
338 if (!pp
->mileage
|| foundend
||
342 *value
/= ++badcount
;
343 if (op
->mileage
== 0)
345 if (op
->speed
== C_LIMIT
|| !op
->can_go
)
347 if (cango
&& pp
->safety
[S_RIGHT_WAY
] !=
352 case C_GAS_SAFE
: case C_DRIVE_SAFE
:
353 case C_SPARE_SAFE
: case C_RIGHT_WAY
:
354 *value
= cango
? 0 : 101;
362 *value
= cango
? 0 : 101;
363 if (card
!= C_INIT
) {
364 if (*value
>= curmax
) {
368 if (*value
<= curmin
) {
375 mvprintw(i
+ 6, 2, "%3d %-14s", *value
,
376 C_name
[pp
->hand
[i
]]);
380 if (!pp
->can_go
&& !is_repair(pp
->battle
))
381 Numneed
[opposite(pp
->battle
)]++;
384 mvaddstr(MOVE_Y
+ 1, MOVE_X
, "PLAY\n");
389 if (is_safety(pp
->hand
[nummin
])) { /* NEVER discard a safety */
393 mvaddstr(MOVE_Y
+ 1, MOVE_X
, "DISCARD\n");
394 Movetype
= M_DISCARD
;
397 mvprintw(MOVE_Y
+ 2, MOVE_X
, "%16s", C_name
[pp
->hand
[Card_no
]]);
401 * Return true if the given player could conceivably win with his next card.
404 onecard(const PLAY
*pp
)
411 if (pp
->can_go
|| ((is_repair(bat
) || bat
== C_STOP
|| spd
== C_LIMIT
) &&
412 Numseen
[S_RIGHT_WAY
] != 0) ||
413 (bat
>= 0 && Numseen
[safety(bat
)] != 0))
414 switch (End
- pp
->mileage
) {
416 if (pp
->nummiles
[C_200
] == 2)
423 card
= (End
- pp
->mileage
== 75 ? C_75
: C_100
);
425 return Numseen
[S_RIGHT_WAY
] == 0;
429 card
= (End
- pp
->mileage
== 25 ? C_25
: C_50
);
430 return Numseen
[card
] != Numcards
[card
];
436 canplay(const PLAY
*pp
, const PLAY
*op
, CARD card
)
440 if (pp
->nummiles
[C_200
] == 2)
443 case C_75
: case C_100
:
444 if (pp
->speed
== C_LIMIT
)
448 if (pp
->mileage
+ Value
[card
] > End
)
455 case C_EMPTY
: case C_FLAT
: case C_CRASH
:
457 if (op
->can_go
&& op
->safety
[safety(card
) - S_CONV
] != S_PLAYED
)
461 if (op
->speed
!= C_LIMIT
&&
462 op
->safety
[S_RIGHT_WAY
] != S_PLAYED
&&
463 op
->mileage
+ 50 < End
)
466 case C_GAS
: case C_SPARE
: case C_REPAIRS
:
467 if (pp
->battle
== opposite(card
))
472 (is_repair(pp
->battle
) || pp
->battle
== C_STOP
))
476 if (pp
->speed
== C_LIMIT
)