]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - mille/comp.c
1 /* $NetBSD: comp.c,v 1.14 2019/02/04 03:29:41 mrg 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.14 2019/02/04 03:29:41 mrg Exp $");
44 * @(#)comp.c 1.1 (Berkeley) 4/1/82
55 bool foundend
, canstop
, foundlow
;
57 unsigned 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
)) != 0)
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 (is_safety(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
&& !is_repair(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 (is_safety(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 && (int)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) {
222 case C_75
: case C_100
:
223 *value
= (Value
[card
] >> 3);
224 if (pp
->speed
== C_LIMIT
)
229 if (pp
->mileage
+ Value
[card
] > End
)
230 *value
= (End
== 700 ? card
: 0);
231 else if (pp
->mileage
+ Value
[card
] == End
) {
232 *value
= (foundend
? card
: V_VALUABLE
);
237 if (pp
->safety
[S_RIGHT_WAY
] != S_UNKNOWN
)
238 *value
= (pp
->safety
[S_RIGHT_WAY
] ==
240 else if (pp
->speed
== C_LIMIT
&&
241 End
- pp
->mileage
<= 50)
243 else if (pp
->speed
== C_LIMIT
244 || Numseen
[C_LIMIT
] != Numcards
[C_LIMIT
]) {
251 --count
[C_END_LIMIT
];
254 case C_REPAIRS
: case C_SPARE
: case C_GAS
:
255 safe
= safety(card
) - S_CONV
;
256 oppos
= opposite(card
);
257 if (pp
->safety
[safe
] != S_UNKNOWN
)
258 *value
= (pp
->safety
[safe
] ==
260 else if (pp
->battle
!= oppos
261 && (Numseen
[oppos
] == Numcards
[oppos
] ||
262 Numseen
[oppos
] + count
[card
] >
269 *value
= Numcards
[oppos
] * 6;
270 *value
+= Numseen
[card
] -
273 *value
/= (count
[card
]*count
[card
]);
278 if (pp
->safety
[S_RIGHT_WAY
] != S_UNKNOWN
)
279 *value
= (pp
->safety
[S_RIGHT_WAY
] ==
282 && Numgos
+ count
[C_GO
] == Numneed
[C_GO
]) {
287 *value
= Numneed
[C_GO
] * 3;
288 *value
+= (Numseen
[C_GO
] - Numgos
);
289 *value
/= (count
[C_GO
] * count
[C_GO
]);
294 if (op
->mileage
+ 50 >= End
) {
295 *value
= (End
== 700 && !cango
);
298 if (canstop
|| (cango
&& !op
->can_go
))
301 *value
= (pp
->safety
[S_RIGHT_WAY
] !=
308 case C_CRASH
: case C_EMPTY
: case C_FLAT
:
309 safe
= safety(card
) - S_CONV
;
310 oppos
= opposite(card
);
311 *value
= (pp
->safety
[safe
]!=S_UNKNOWN
? 3 : 4);
313 if (op
->safety
[safe
] == S_PLAYED
)
316 *value
*= Numneed
[oppos
] +
318 if (!pp
->mileage
|| foundend
||
321 if (op
->mileage
== 0 || onecard(op
))
323 if (op
->speed
== C_LIMIT
)
326 pp
->safety
[safe
] != S_UNKNOWN
)
329 *value
/= ++badcount
;
333 if (op
->safety
[S_RIGHT_WAY
] == S_PLAYED
)
336 *value
= (pp
->safety
[S_RIGHT_WAY
] !=
338 *value
*= Numcards
[C_STOP
] +
340 if (!pp
->mileage
|| foundend
||
344 *value
/= ++badcount
;
345 if (op
->mileage
== 0)
347 if (op
->speed
== C_LIMIT
|| !op
->can_go
)
349 if (cango
&& pp
->safety
[S_RIGHT_WAY
] !=
354 case C_GAS_SAFE
: case C_DRIVE_SAFE
:
355 case C_SPARE_SAFE
: case C_RIGHT_WAY
:
356 *value
= cango
? 0 : 101;
364 *value
= cango
? 0 : 101;
365 if (card
!= C_INIT
) {
366 if (*value
>= curmax
) {
370 if (*value
<= curmin
) {
377 mvprintw(i
+ 6, 2, "%3d %-14s", *value
,
378 C_name
[pp
->hand
[i
]]);
382 if (!pp
->can_go
&& !is_repair(pp
->battle
))
383 Numneed
[opposite(pp
->battle
)]++;
386 mvaddstr(MOVE_Y
+ 1, MOVE_X
, "PLAY\n");
391 if (is_safety(pp
->hand
[nummin
])) { /* NEVER discard a safety */
395 mvaddstr(MOVE_Y
+ 1, MOVE_X
, "DISCARD\n");
396 Movetype
= M_DISCARD
;
399 mvprintw(MOVE_Y
+ 2, MOVE_X
, "%16s", C_name
[pp
->hand
[Card_no
]]);
403 * Return true if the given player could conceivably win with his next card.
406 onecard(const PLAY
*pp
)
413 if (pp
->can_go
|| ((is_repair(bat
) || bat
== C_STOP
|| spd
== C_LIMIT
) &&
414 Numseen
[S_RIGHT_WAY
] != 0) ||
415 (bat
>= 0 && Numseen
[safety(bat
)] != 0))
416 switch (End
- pp
->mileage
) {
418 if (pp
->nummiles
[C_200
] == 2)
425 card
= (End
- pp
->mileage
== 75 ? C_75
: C_100
);
427 return Numseen
[S_RIGHT_WAY
] == 0;
432 card
= (End
- pp
->mileage
== 25 ? C_25
: C_50
);
433 return Numseen
[card
] != Numcards
[card
];
439 canplay(const PLAY
*pp
, const PLAY
*op
, CARD card
)
443 if (pp
->nummiles
[C_200
] == 2)
446 case C_75
: case C_100
:
447 if (pp
->speed
== C_LIMIT
)
451 if (pp
->mileage
+ Value
[card
] > End
)
458 case C_EMPTY
: case C_FLAT
: case C_CRASH
:
460 if (op
->can_go
&& op
->safety
[safety(card
) - S_CONV
] != S_PLAYED
)
464 if (op
->speed
!= C_LIMIT
&&
465 op
->safety
[S_RIGHT_WAY
] != S_PLAYED
&&
466 op
->mileage
+ 50 < End
)
469 case C_GAS
: case C_SPARE
: case C_REPAIRS
:
470 if (pp
->battle
== opposite(card
))
475 (is_repair(pp
->battle
) || pp
->battle
== C_STOP
))
479 if (pp
->speed
== C_LIMIT
)