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