move.c revision 1.13 1 /* $NetBSD: move.c,v 1.13 2003/08/07 09:37:25 agc Exp $ */
2
3 /*
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
18 *
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
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)move.c 8.1 (Berkeley) 5/31/93";
36 #else
37 __RCSID("$NetBSD: move.c,v 1.13 2003/08/07 09:37:25 agc Exp $");
38 #endif
39 #endif /* not lint */
40
41 #include <termios.h>
42
43 #include "mille.h"
44 #ifndef unctrl
45 #include "unctrl.h"
46 #endif
47
48 /*
49 * @(#)move.c 1.2 (Berkeley) 3/28/83
50 */
51
52 #undef CTRL
53 #define CTRL(c) (c - 'A' + 1)
54
55 void
56 domove()
57 {
58 PLAY *pp;
59 int i, j;
60 bool goodplay;
61
62 pp = &Player[Play];
63 if (Play == PLAYER)
64 getmove();
65 else
66 calcmove();
67 Next = FALSE;
68 goodplay = TRUE;
69 switch (Movetype) {
70 case M_DISCARD:
71 if (haspicked(pp)) {
72 if (pp->hand[Card_no] == C_INIT)
73 if (Card_no == 6)
74 Finished = TRUE;
75 else
76 error("no card there");
77 else {
78 if (is_safety(pp->hand[Card_no])) {
79 error("discard a safety?");
80 goodplay = FALSE;
81 break;
82 }
83 Discard = pp->hand[Card_no];
84 pp->hand[Card_no] = C_INIT;
85 Next = TRUE;
86 if (Play == PLAYER)
87 account(Discard);
88 }
89 }
90 else
91 error("must pick first");
92 break;
93 case M_PLAY:
94 goodplay = playcard(pp);
95 break;
96 case M_DRAW:
97 Card_no = 0;
98 if (Topcard <= Deck)
99 error("no more cards");
100 else if (haspicked(pp))
101 error("already picked");
102 else {
103 pp->hand[0] = *--Topcard;
104 #ifdef DEBUG
105 if (Debug)
106 fprintf(outf, "DOMOVE: Draw %s\n", C_name[*Topcard]);
107 #endif
108 acc:
109 if (Play == COMP) {
110 account(*Topcard);
111 if (is_safety(*Topcard))
112 pp->safety[*Topcard-S_CONV] = S_IN_HAND;
113 }
114 if (pp->hand[1] == C_INIT && Topcard > Deck) {
115 Card_no = 1;
116 pp->hand[1] = *--Topcard;
117 #ifdef DEBUG
118 if (Debug)
119 fprintf(outf, "DOMOVE: Draw %s\n", C_name[*Topcard]);
120 #endif
121 goto acc;
122 }
123 pp->new_battle = FALSE;
124 pp->new_speed = FALSE;
125 }
126 break;
127
128 case M_ORDER:
129 break;
130 }
131 /*
132 * move blank card to top by one of two methods. If the
133 * computer's hand was sorted, the randomness for picking
134 * between equally valued cards would be lost
135 */
136 if (Order && Movetype != M_DRAW && goodplay && pp == &Player[PLAYER])
137 sort(pp->hand);
138 else
139 for (i = 1; i < HAND_SZ; i++)
140 if (pp->hand[i] == C_INIT) {
141 for (j = 0; pp->hand[j] == C_INIT; j++)
142 if (j >= HAND_SZ) {
143 j = 0;
144 break;
145 }
146 pp->hand[i] = pp->hand[j];
147 pp->hand[j] = C_INIT;
148 }
149 if (Topcard <= Deck)
150 check_go();
151 if (Next)
152 nextplay();
153 }
154
155 /*
156 * Check and see if either side can go. If they cannot,
157 * the game is over
158 */
159 void
160 check_go()
161 {
162 CARD card;
163 PLAY *pp, *op;
164 int i;
165
166 for (pp = Player; pp < &Player[2]; pp++) {
167 op = (pp == &Player[COMP] ? &Player[PLAYER] : &Player[COMP]);
168 for (i = 0; i < HAND_SZ; i++) {
169 card = pp->hand[i];
170 if (is_safety(card) || canplay(pp, op, card)) {
171 #ifdef DEBUG
172 if (Debug) {
173 fprintf(outf, "CHECK_GO: can play %s (%d), ", C_name[card], card);
174 fprintf(outf, "is_safety(card) = %d, ", is_safety(card));
175 fprintf(outf, "canplay(pp, op, card) = %d\n", canplay(pp, op, card));
176 }
177 #endif
178 return;
179 }
180 #ifdef DEBUG
181 else if (Debug)
182 fprintf(outf, "CHECK_GO: cannot play %s\n",
183 C_name[card]);
184 #endif
185 }
186 }
187 Finished = TRUE;
188 }
189
190 int
191 playcard(pp)
192 PLAY *pp;
193 {
194 int v;
195 CARD card;
196
197 /*
198 * check and see if player has picked
199 */
200 switch (pp->hand[Card_no]) {
201 default:
202 if (!haspicked(pp))
203 mustpick:
204 return error("must pick first");
205 case C_GAS_SAFE: case C_SPARE_SAFE:
206 case C_DRIVE_SAFE: case C_RIGHT_WAY:
207 break;
208 }
209
210 card = pp->hand[Card_no];
211 #ifdef DEBUG
212 if (Debug)
213 fprintf(outf, "PLAYCARD: Card = %s\n", C_name[card]);
214 #endif
215 Next = FALSE;
216 switch (card) {
217 case C_200:
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");
223 case C_50:
224 if (pp->mileage + Value[card] > End)
225 return error("puts you over %d", End);
226 case C_25:
227 if (!pp->can_go)
228 return error("cannot move now");
229 pp->nummiles[card]++;
230 v = Value[card];
231 pp->total += v;
232 pp->hand_tot += v;
233 if ((pp->mileage += v) == End)
234 check_ext(FALSE);
235 break;
236
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]);
240 pp->battle = card;
241 if (pp->safety[S_RIGHT_WAY] == S_PLAYED)
242 pp->can_go = TRUE;
243 break;
244
245 case C_GO:
246 if (pp->battle != C_INIT && pp->battle != C_STOP
247 && !is_repair(pp->battle))
248 return error("cannot play \"Go\" on a \"%s\"",
249 C_name[pp->battle]);
250 pp->battle = C_GO;
251 pp->can_go = TRUE;
252 break;
253
254 case C_END_LIMIT:
255 if (pp->speed != C_LIMIT)
256 return error("not limited");
257 pp->speed = C_END_LIMIT;
258 break;
259
260 case C_EMPTY: case C_FLAT: case C_CRASH:
261 case C_STOP:
262 pp = &Player[other(Play)];
263 if (!pp->can_go)
264 return error("opponent cannot go");
265 else if (pp->safety[safety(card) - S_CONV] == S_PLAYED)
266 protected:
267 return error("opponent is protected");
268 pp->battle = card;
269 pp->new_battle = TRUE;
270 pp->can_go = FALSE;
271 pp = &Player[Play];
272 break;
273
274 case C_LIMIT:
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)
279 goto protected;
280 pp->speed = C_LIMIT;
281 pp->new_speed = TRUE;
282 pp = &Player[Play];
283 break;
284
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 && !is_repair(pp->battle))) {
290 pp->battle = C_GO;
291 pp->can_go = TRUE;
292 }
293 if (card == C_RIGHT_WAY && pp->speed == C_LIMIT)
294 pp->speed = C_INIT;
295 if (pp->new_battle
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;
301 }
302 }
303 /*
304 * if not coup, must pick first
305 */
306 else if (pp->hand[0] == C_INIT && Topcard > Deck)
307 goto mustpick;
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;
314 }
315 if (card == C_RIGHT_WAY) {
316 if (pp->speed == C_LIMIT)
317 pp->speed = C_INIT;
318 if (pp->battle == C_STOP || pp->battle == C_INIT) {
319 pp->can_go = TRUE;
320 pp->battle = C_INIT;
321 }
322 if (!pp->can_go && is_repair(pp->battle))
323 pp->can_go = TRUE;
324 }
325 Next = -1;
326 break;
327
328 case C_INIT:
329 error("no card there");
330 Next = -1;
331 break;
332 }
333 if (pp == &Player[PLAYER])
334 account(card);
335 pp->hand[Card_no] = C_INIT;
336 Next = (Next == (bool)-1 ? FALSE : TRUE);
337 return TRUE;
338 }
339
340 void
341 getmove()
342 {
343 char c;
344 #ifdef EXTRAP
345 static bool last_ex = FALSE; /* set if last command was E */
346
347 if (last_ex) {
348 undoex();
349 prboard();
350 last_ex = FALSE;
351 }
352 #endif
353 for (;;) {
354 prompt(MOVEPROMPT);
355 leaveok(Board, FALSE);
356 refresh();
357 while ((c = readch()) == killchar() || c == erasechar())
358 continue;
359 if (islower(c))
360 c = toupper(c);
361 if (isprint(c) && !isspace(c)) {
362 addch(c);
363 refresh();
364 }
365 switch (c) {
366 case 'P': /* Pick */
367 Movetype = M_DRAW;
368 goto ret;
369 case 'U': /* Use Card */
370 case 'D': /* Discard Card */
371 if ((Card_no = getcard()) < 0)
372 break;
373 Movetype = (c == 'U' ? M_PLAY : M_DISCARD);
374 goto ret;
375 case 'O': /* Order */
376 Order = !Order;
377 if (Window == W_SMALL) {
378 if (!Order)
379 mvwaddstr(Score, 12, 21,
380 "o: order hand");
381 else
382 mvwaddstr(Score, 12, 21,
383 "o: stop ordering");
384 wclrtoeol(Score);
385 }
386 Movetype = M_ORDER;
387 goto ret;
388 case 'Q': /* Quit */
389 rub(0); /* Same as a rubout */
390 break;
391 case 'W': /* Window toggle */
392 Window = nextwin(Window);
393 newscore();
394 prscore(TRUE);
395 wrefresh(Score);
396 break;
397 case 'R': /* Redraw screen */
398 case CTRL('L'):
399 wrefresh(curscr);
400 break;
401 case 'S': /* Save game */
402 On_exit = FALSE;
403 save();
404 break;
405 case 'E': /* Extrapolate */
406 #ifdef EXTRAP
407 if (last_ex)
408 break;
409 Finished = TRUE;
410 if (Window != W_FULL)
411 newscore();
412 prscore(FALSE);
413 wrefresh(Score);
414 last_ex = TRUE;
415 Finished = FALSE;
416 #else
417 error("%c: command not implemented", c);
418 #endif
419 break;
420 case '\r': /* Ignore RETURNs and */
421 case '\n': /* Line Feeds */
422 case ' ': /* Spaces */
423 case '\0': /* and nulls */
424 break;
425 #ifdef DEBUG
426 case 'Z': /* Debug code */
427 if (!Debug && outf == NULL) {
428 char buf[MAXPATHLEN];
429
430 prompt(FILEPROMPT);
431 leaveok(Board, FALSE);
432 refresh();
433 sp = buf;
434 while ((*sp = readch()) != '\n') {
435 if (*sp == killchar())
436 goto over;
437 else if (*sp == erasechar()) {
438 if (--sp < buf)
439 sp = buf;
440 else {
441 addch('\b');
442 if (*sp < ' ')
443 addch('\b');
444 clrtoeol();
445 }
446 }
447 else
448 addstr(unctrl(*sp++));
449 refresh();
450 }
451 *sp = '\0';
452 leaveok(Board, TRUE);
453 if ((outf = fopen(buf, "w")) == NULL)
454 warn("%s", buf);
455 setbuf(outf, (char *)NULL);
456 }
457 Debug = !Debug;
458 break;
459 #endif
460 default:
461 error("unknown command: %s", unctrl(c));
462 break;
463 }
464 }
465 ret:
466 leaveok(Board, TRUE);
467 }
468
469 /*
470 * return whether or not the player has picked
471 */
472 int
473 haspicked(pp)
474 const PLAY *pp;
475 {
476 int card;
477
478 if (Topcard <= Deck)
479 return TRUE;
480 switch (pp->hand[Card_no]) {
481 case C_GAS_SAFE: case C_SPARE_SAFE:
482 case C_DRIVE_SAFE: case C_RIGHT_WAY:
483 card = 1;
484 break;
485 default:
486 card = 0;
487 break;
488 }
489 return (pp->hand[card] != C_INIT);
490 }
491
492 void
493 account(card)
494 CARD card;
495 {
496 CARD oppos;
497
498 if (card == C_INIT)
499 return;
500 ++Numseen[card];
501 if (Play == COMP)
502 switch (card) {
503 case C_GAS_SAFE:
504 case C_SPARE_SAFE:
505 case C_DRIVE_SAFE:
506 oppos = opposite(card);
507 Numgos += Numcards[oppos] - Numseen[oppos];
508 break;
509 case C_CRASH:
510 case C_FLAT:
511 case C_EMPTY:
512 case C_STOP:
513 Numgos++;
514 break;
515 }
516 }
517
518 void
519 prompt(promptno)
520 int promptno;
521 {
522 static const char *const names[] = {
523 ">>:Move:",
524 "Really?",
525 "Another hand?",
526 "Another game?",
527 "Save game?",
528 "Same file?",
529 "file:",
530 "Extension?",
531 "Overwrite file?",
532 };
533 static int last_prompt = -1;
534
535 if (promptno == last_prompt)
536 move(MOVE_Y, MOVE_X + strlen(names[promptno]) + 1);
537 else {
538 move(MOVE_Y, MOVE_X);
539 if (promptno == MOVEPROMPT)
540 standout();
541 addstr(names[promptno]);
542 if (promptno == MOVEPROMPT)
543 standend();
544 addch(' ');
545 last_prompt = promptno;
546 }
547 clrtoeol();
548 }
549
550 void
551 sort(hand)
552 CARD *hand;
553 {
554 CARD *cp, *tp;
555 CARD temp;
556
557 cp = hand;
558 hand += HAND_SZ;
559 for ( ; cp < &hand[-1]; cp++)
560 for (tp = cp + 1; tp < hand; tp++)
561 if (*cp > *tp) {
562 temp = *cp;
563 *cp = *tp;
564 *tp = temp;
565 }
566 }
567