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. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#)move.c 8.1 (Berkeley) 5/31/93 30 * $FreeBSD: src/games/mille/move.c,v 1.6 1999/12/12 06:17:24 billf Exp $ 31 * $DragonFly: src/games/mille/move.c,v 1.5 2006/08/27 17:17:23 pavalos Exp $ 32 */ 33 34 #include <termios.h> 35 36 #include "mille.h" 37 #include <unctrl.h> 38 #include <term.h> 39 40 /* 41 * @(#)move.c 1.2 (Berkeley) 3/28/83 42 */ 43 44 #undef CTRL 45 #define CTRL(c) (c - 'A' + 1) 46 47 const char *Movenames[] = { 48 "M_DISCARD", "M_DRAW", "M_PLAY", "M_ORDER" 49 }; 50 51 static void check_go(void); 52 static void getmove(void); 53 static int haspicked(PLAY *); 54 static bool playcard(PLAY *); 55 56 void 57 domove(void) 58 { 59 PLAY *pp; 60 int i, j; 61 bool goodplay; 62 63 pp = &Player[Play]; 64 if (Play == PLAYER) 65 getmove(); 66 else 67 calcmove(); 68 Next = FALSE; 69 goodplay = TRUE; 70 switch (Movetype) { 71 case M_DISCARD: 72 if (haspicked(pp)) { 73 if (pp->hand[Card_no] == C_INIT) 74 if (Card_no == 6) 75 Finished = TRUE; 76 else 77 error("no card there"); 78 else { 79 if (issafety(pp->hand[Card_no])) { 80 error("discard a safety?"); 81 goodplay = FALSE; 82 break; 83 } 84 Discard = pp->hand[Card_no]; 85 pp->hand[Card_no] = C_INIT; 86 Next = TRUE; 87 if (Play == PLAYER) 88 account(Discard); 89 } 90 } 91 else 92 error("must pick first"); 93 break; 94 case M_PLAY: 95 goodplay = playcard(pp); 96 break; 97 case M_DRAW: 98 Card_no = 0; 99 if (Topcard <= Deck) 100 error("no more cards"); 101 else if (haspicked(pp)) 102 error("already picked"); 103 else { 104 pp->hand[0] = *--Topcard; 105 #ifdef DEBUG 106 if (Debug) 107 fprintf(outf, "DOMOVE: Draw %s\n", C_name[*Topcard]); 108 #endif 109 acc: 110 if (Play == COMP) { 111 account(*Topcard); 112 if (issafety(*Topcard)) 113 pp->safety[*Topcard-S_CONV] = S_IN_HAND; 114 } 115 if (pp->hand[1] == C_INIT && Topcard > Deck) { 116 Card_no = 1; 117 pp->hand[1] = *--Topcard; 118 #ifdef DEBUG 119 if (Debug) 120 fprintf(outf, "DOMOVE: Draw %s\n", C_name[*Topcard]); 121 #endif 122 goto acc; 123 } 124 pp->new_battle = FALSE; 125 pp->new_speed = FALSE; 126 } 127 break; 128 129 case M_ORDER: 130 break; 131 } 132 /* 133 * move blank card to top by one of two methods. If the 134 * computer's hand was sorted, the randomness for picking 135 * between equally valued cards would be lost 136 */ 137 if (Order && Movetype != M_DRAW && goodplay && pp == &Player[PLAYER]) 138 sort(pp->hand); 139 else 140 for (i = 1; i < HAND_SZ; i++) 141 if (pp->hand[i] == C_INIT) { 142 for (j = 0; pp->hand[j] == C_INIT; j++) 143 if (j >= HAND_SZ) { 144 j = 0; 145 break; 146 } 147 pp->hand[i] = pp->hand[j]; 148 pp->hand[j] = C_INIT; 149 } 150 if (Topcard <= Deck) 151 check_go(); 152 if (Next) 153 nextplay(); 154 } 155 156 /* 157 * Check and see if either side can go. If they cannot, 158 * the game is over 159 */ 160 static void 161 check_go(void) 162 { 163 CARD card; 164 PLAY *pp, *op; 165 int i; 166 167 for (pp = Player; pp < &Player[2]; pp++) { 168 op = (pp == &Player[COMP] ? &Player[PLAYER] : &Player[COMP]); 169 for (i = 0; i < HAND_SZ; i++) { 170 card = pp->hand[i]; 171 if (issafety(card) || canplay(pp, op, card)) { 172 #ifdef DEBUG 173 if (Debug) { 174 fprintf(outf, "CHECK_GO: can play %s (%d), ", C_name[card], card); 175 fprintf(outf, "issafety(card) = %d, ", issafety(card)); 176 fprintf(outf, "canplay(pp, op, card) = %d\n", canplay(pp, op, card)); 177 } 178 #endif 179 return; 180 } 181 #ifdef DEBUG 182 else if (Debug) 183 fprintf(outf, "CHECK_GO: cannot play %s\n", 184 C_name[card]); 185 #endif 186 } 187 } 188 Finished = TRUE; 189 } 190 191 static bool 192 playcard(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 && !isrepair(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 && !isrepair(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 && isrepair(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 == -1 ? FALSE : TRUE); 337 return TRUE; 338 } 339 340 static void 341 getmove(void) 342 { 343 char c; 344 #ifdef DEBUG 345 char *sp; 346 #endif 347 #ifdef EXTRAP 348 static bool last_ex = FALSE; /* set if last command was E */ 349 350 if (last_ex) { 351 undoex(); 352 prboard(); 353 last_ex = FALSE; 354 } 355 #endif 356 for (;;) { 357 prompt(MOVEPROMPT); 358 leaveok(Board, FALSE); 359 refresh(); 360 while ((c = readch()) == killchar() || c == erasechar()) 361 continue; 362 if (islower(c)) 363 c = toupper(c); 364 if (isprint(c) && !isspace(c)) { 365 addch(c); 366 refresh(); 367 } 368 switch (c) { 369 case 'P': /* Pick */ 370 Movetype = M_DRAW; 371 goto ret; 372 case 'U': /* Use Card */ 373 case 'D': /* Discard Card */ 374 if ((Card_no = getcard()) < 0) 375 break; 376 Movetype = (c == 'U' ? M_PLAY : M_DISCARD); 377 goto ret; 378 case 'O': /* Order */ 379 Order = !Order; 380 if (Window == W_SMALL) { 381 if (!Order) 382 mvwaddstr(Score, 12, 21, 383 "o: order hand"); 384 else 385 mvwaddstr(Score, 12, 21, 386 "o: stop ordering"); 387 wclrtoeol(Score); 388 } 389 Movetype = M_ORDER; 390 goto ret; 391 case 'Q': /* Quit */ 392 rub(0); /* Same as a rubout */ 393 break; 394 case 'W': /* Window toggle */ 395 Window = nextwin(Window); 396 newscore(); 397 prscore(TRUE); 398 wrefresh(Score); 399 break; 400 case 'R': /* Redraw screen */ 401 case CTRL('L'): 402 wrefresh(curscr); 403 break; 404 case 'S': /* Save game */ 405 On_exit = FALSE; 406 save(); 407 break; 408 case 'E': /* Extrapolate */ 409 #ifdef EXTRAP 410 if (last_ex) 411 break; 412 Finished = TRUE; 413 if (Window != W_FULL) 414 newscore(); 415 prscore(FALSE); 416 wrefresh(Score); 417 last_ex = TRUE; 418 Finished = FALSE; 419 #else 420 error("%c: command not implemented", c); 421 #endif 422 break; 423 case '\r': /* Ignore RETURNs and */ 424 case '\n': /* Line Feeds */ 425 case ' ': /* Spaces */ 426 case '\0': /* and nulls */ 427 break; 428 #ifdef DEBUG 429 case 'Z': /* Debug code */ 430 if (!Debug && outf == NULL) { 431 char buf[MAXPATHLEN]; 432 433 prompt(FILEPROMPT); 434 leaveok(Board, FALSE); 435 refresh(); 436 sp = buf; 437 while ((*sp = readch()) != '\n') { 438 if (*sp == killchar()) 439 goto over; 440 else if (*sp == erasechar()) { 441 if (--sp < buf) 442 sp = buf; 443 else { 444 addch('\b'); 445 if (*sp < ' ') 446 addch('\b'); 447 clrtoeol(); 448 } 449 } 450 else 451 addstr(unctrl(*sp++)); 452 refresh(); 453 } 454 *sp = '\0'; 455 leaveok(Board, TRUE); 456 if ((outf = fopen(buf, "w")) == NULL) 457 perror(buf); 458 setbuf(outf, NULL); 459 } 460 Debug = !Debug; 461 break; 462 #endif 463 default: 464 error("unknown command: %s", unctrl(c)); 465 break; 466 } 467 } 468 ret: 469 leaveok(Board, TRUE); 470 } 471 472 /* 473 * return whether or not the player has picked 474 */ 475 static int 476 haspicked(PLAY *pp) 477 { 478 int card; 479 480 if (Topcard <= Deck) 481 return TRUE; 482 switch (pp->hand[Card_no]) { 483 case C_GAS_SAFE: case C_SPARE_SAFE: 484 case C_DRIVE_SAFE: case C_RIGHT_WAY: 485 card = 1; 486 break; 487 default: 488 card = 0; 489 break; 490 } 491 return (pp->hand[card] != C_INIT); 492 } 493 494 void 495 account(CARD card) 496 { 497 CARD oppos; 498 499 if (card == C_INIT) 500 return; 501 ++Numseen[card]; 502 if (Play == COMP) 503 switch (card) { 504 case C_GAS_SAFE: 505 case C_SPARE_SAFE: 506 case C_DRIVE_SAFE: 507 oppos = opposite(card); 508 Numgos += Numcards[oppos] - Numseen[oppos]; 509 break; 510 case C_CRASH: 511 case C_FLAT: 512 case C_EMPTY: 513 case C_STOP: 514 Numgos++; 515 break; 516 } 517 } 518 519 void 520 prompt(int promptno) 521 { 522 static const char *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(CARD *hand) 552 { 553 CARD *cp, *tp; 554 CARD temp; 555 556 cp = hand; 557 hand += HAND_SZ; 558 for ( ; cp < &hand[-1]; cp++) 559 for (tp = cp + 1; tp < hand; tp++) 560 if (*cp > *tp) { 561 temp = *cp; 562 *cp = *tp; 563 *tp = temp; 564 } 565 } 566