move.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. /* $NetBSD: move.c,v 1.15 2004/11/05 21:30:32 dsl Exp $ */
  2. /*
  3. * Copyright (c) 1983, 1993
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the University nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. */
  30. #include <sys/cdefs.h>
  31. #ifndef lint
  32. #if 0
  33. static char sccsid[] = "@(#)move.c 8.1 (Berkeley) 5/31/93";
  34. #else
  35. __RCSID("$NetBSD: move.c,v 1.15 2004/11/05 21:30:32 dsl Exp $");
  36. #endif
  37. #endif /* not lint */
  38. #include <termios.h>
  39. #ifdef DEBUG
  40. #include <sys/param.h>
  41. #endif
  42. #include "mille.h"
  43. #ifndef unctrl
  44. #include "unctrl.h"
  45. #endif
  46. /*
  47. * @(#)move.c 1.2 (Berkeley) 3/28/83
  48. */
  49. #undef CTRL
  50. #define CTRL(c) (c - 'A' + 1)
  51. void
  52. domove()
  53. {
  54. PLAY *pp;
  55. int i, j;
  56. bool goodplay;
  57. pp = &Player[Play];
  58. for (i = 0, j = 0; i < HAND_SZ; i++)
  59. if (pp->hand[i] != -1)
  60. j++;
  61. if (!j) {
  62. nextplay();
  63. return;
  64. }
  65. if (Play == PLAYER)
  66. getmove();
  67. else
  68. calcmove();
  69. Next = FALSE;
  70. goodplay = TRUE;
  71. switch (Movetype) {
  72. case M_DISCARD:
  73. if (haspicked(pp)) {
  74. if (pp->hand[Card_no] == C_INIT)
  75. if (Card_no == 6)
  76. Finished = TRUE;
  77. else
  78. error("no card there");
  79. else {
  80. if (is_safety(pp->hand[Card_no])) {
  81. error("discard a safety?");
  82. goodplay = FALSE;
  83. break;
  84. }
  85. Discard = pp->hand[Card_no];
  86. pp->hand[Card_no] = C_INIT;
  87. Next = TRUE;
  88. if (Play == PLAYER)
  89. account(Discard);
  90. }
  91. }
  92. else
  93. error("must pick first");
  94. break;
  95. case M_PLAY:
  96. goodplay = playcard(pp);
  97. break;
  98. case M_DRAW:
  99. Card_no = 0;
  100. if (Topcard <= Deck)
  101. error("no more cards");
  102. else if (haspicked(pp))
  103. error("already picked");
  104. else {
  105. pp->hand[0] = *--Topcard;
  106. #ifdef DEBUG
  107. if (Debug)
  108. fprintf(outf, "DOMOVE: Draw %s\n", C_name[*Topcard]);
  109. #endif
  110. acc:
  111. if (Play == COMP) {
  112. account(*Topcard);
  113. if (is_safety(*Topcard))
  114. pp->safety[*Topcard-S_CONV] = S_IN_HAND;
  115. }
  116. if (pp->hand[1] == C_INIT && Topcard > Deck) {
  117. Card_no = 1;
  118. pp->hand[1] = *--Topcard;
  119. #ifdef DEBUG
  120. if (Debug)
  121. fprintf(outf, "DOMOVE: Draw %s\n", C_name[*Topcard]);
  122. #endif
  123. goto acc;
  124. }
  125. pp->new_battle = FALSE;
  126. pp->new_speed = FALSE;
  127. }
  128. break;
  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. * 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. for (pp = Player; pp < &Player[2]; pp++) {
  166. op = (pp == &Player[COMP] ? &Player[PLAYER] : &Player[COMP]);
  167. for (i = 0; i < HAND_SZ; i++) {
  168. card = pp->hand[i];
  169. if (is_safety(card) || canplay(pp, op, card)) {
  170. #ifdef DEBUG
  171. if (Debug) {
  172. fprintf(outf, "CHECK_GO: can play %s (%d), ", C_name[card], card);
  173. fprintf(outf, "is_safety(card) = %d, ", is_safety(card));
  174. fprintf(outf, "canplay(pp, op, card) = %d\n", canplay(pp, op, card));
  175. }
  176. #endif
  177. return;
  178. }
  179. #ifdef DEBUG
  180. else if (Debug)
  181. fprintf(outf, "CHECK_GO: cannot play %s\n",
  182. C_name[card]);
  183. #endif
  184. }
  185. }
  186. Finished = TRUE;
  187. }
  188. int
  189. playcard(pp)
  190. PLAY *pp;
  191. {
  192. int v;
  193. CARD card;
  194. /*
  195. * check and see if player has picked
  196. */
  197. switch (pp->hand[Card_no]) {
  198. default:
  199. if (!haspicked(pp))
  200. mustpick:
  201. return error("must pick first");
  202. case C_GAS_SAFE: case C_SPARE_SAFE:
  203. case C_DRIVE_SAFE: case C_RIGHT_WAY:
  204. break;
  205. }
  206. card = pp->hand[Card_no];
  207. #ifdef DEBUG
  208. if (Debug)
  209. fprintf(outf, "PLAYCARD: Card = %s\n", C_name[card]);
  210. #endif
  211. Next = FALSE;
  212. switch (card) {
  213. case C_200:
  214. if (pp->nummiles[C_200] == 2)
  215. return error("only two 200's per hand");
  216. case C_100: case C_75:
  217. if (pp->speed == C_LIMIT)
  218. return error("limit of 50");
  219. case C_50:
  220. if (pp->mileage + Value[card] > End)
  221. return error("puts you over %d", End);
  222. case C_25:
  223. if (!pp->can_go)
  224. return error("cannot move now");
  225. pp->nummiles[card]++;
  226. v = Value[card];
  227. pp->total += v;
  228. pp->hand_tot += v;
  229. if ((pp->mileage += v) == End)
  230. check_ext(FALSE);
  231. break;
  232. case C_GAS: case C_SPARE: case C_REPAIRS:
  233. if (pp->battle != opposite(card))
  234. return error("can't play \"%s\"", C_name[card]);
  235. pp->battle = card;
  236. if (pp->safety[S_RIGHT_WAY] == S_PLAYED)
  237. pp->can_go = TRUE;
  238. break;
  239. case C_GO:
  240. if (pp->battle != C_INIT && pp->battle != C_STOP
  241. && !is_repair(pp->battle))
  242. return error("cannot play \"Go\" on a \"%s\"",
  243. C_name[pp->battle]);
  244. pp->battle = C_GO;
  245. pp->can_go = TRUE;
  246. break;
  247. case C_END_LIMIT:
  248. if (pp->speed != C_LIMIT)
  249. return error("not limited");
  250. pp->speed = C_END_LIMIT;
  251. break;
  252. case C_EMPTY: case C_FLAT: case C_CRASH:
  253. case C_STOP:
  254. pp = &Player[other(Play)];
  255. if (!pp->can_go)
  256. return error("opponent cannot go");
  257. else if (pp->safety[safety(card) - S_CONV] == S_PLAYED)
  258. protected:
  259. return error("opponent is protected");
  260. pp->battle = card;
  261. pp->new_battle = TRUE;
  262. pp->can_go = FALSE;
  263. pp = &Player[Play];
  264. break;
  265. case C_LIMIT:
  266. pp = &Player[other(Play)];
  267. if (pp->speed == C_LIMIT)
  268. return error("opponent has limit");
  269. if (pp->safety[S_RIGHT_WAY] == S_PLAYED)
  270. goto protected;
  271. pp->speed = C_LIMIT;
  272. pp->new_speed = TRUE;
  273. pp = &Player[Play];
  274. break;
  275. case C_GAS_SAFE: case C_SPARE_SAFE:
  276. case C_DRIVE_SAFE: case C_RIGHT_WAY:
  277. if (pp->battle == opposite(card)
  278. || (card == C_RIGHT_WAY && pp->speed == C_LIMIT)) {
  279. if (!(card == C_RIGHT_WAY && !is_repair(pp->battle))) {
  280. pp->battle = C_GO;
  281. pp->can_go = TRUE;
  282. }
  283. if (card == C_RIGHT_WAY && pp->speed == C_LIMIT)
  284. pp->speed = C_INIT;
  285. if (pp->new_battle
  286. || (pp->new_speed && card == C_RIGHT_WAY)) {
  287. pp->coups[card - S_CONV] = TRUE;
  288. pp->total += SC_COUP;
  289. pp->hand_tot += SC_COUP;
  290. pp->coupscore += SC_COUP;
  291. }
  292. }
  293. /*
  294. * if not coup, must pick first
  295. */
  296. else if (pp->hand[0] == C_INIT && Topcard > Deck)
  297. goto mustpick;
  298. pp->safety[card - S_CONV] = S_PLAYED;
  299. pp->total += SC_SAFETY;
  300. pp->hand_tot += SC_SAFETY;
  301. if ((pp->safescore += SC_SAFETY) == NUM_SAFE * SC_SAFETY) {
  302. pp->total += SC_ALL_SAFE;
  303. pp->hand_tot += SC_ALL_SAFE;
  304. }
  305. if (card == C_RIGHT_WAY) {
  306. if (pp->speed == C_LIMIT)
  307. pp->speed = C_INIT;
  308. if (pp->battle == C_STOP || pp->battle == C_INIT) {
  309. pp->can_go = TRUE;
  310. pp->battle = C_INIT;
  311. }
  312. if (!pp->can_go && is_repair(pp->battle))
  313. pp->can_go = TRUE;
  314. }
  315. Next = -1;
  316. break;
  317. case C_INIT:
  318. error("no card there");
  319. Next = -1;
  320. break;
  321. }
  322. if (pp == &Player[PLAYER])
  323. account(card);
  324. pp->hand[Card_no] = C_INIT;
  325. Next = (Next == (bool)-1 ? FALSE : TRUE);
  326. return TRUE;
  327. }
  328. void
  329. getmove()
  330. {
  331. char c;
  332. #ifdef EXTRAP
  333. static bool last_ex = FALSE; /* set if last command was E */
  334. if (last_ex) {
  335. undoex();
  336. prboard();
  337. last_ex = FALSE;
  338. }
  339. #endif
  340. for (;;) {
  341. prompt(MOVEPROMPT);
  342. leaveok(Board, FALSE);
  343. refresh();
  344. while ((c = readch()) == killchar() || c == erasechar())
  345. continue;
  346. if (islower((unsigned char)c))
  347. c = toupper((unsigned char)c);
  348. if (isprint((unsigned char)c) && !isspace((unsigned char)c)) {
  349. addch(c);
  350. refresh();
  351. }
  352. switch (c) {
  353. case 'P': /* Pick */
  354. Movetype = M_DRAW;
  355. goto ret;
  356. case 'U': /* Use Card */
  357. case 'D': /* Discard Card */
  358. if ((Card_no = getcard()) < 0)
  359. break;
  360. Movetype = (c == 'U' ? M_PLAY : M_DISCARD);
  361. goto ret;
  362. case 'O': /* Order */
  363. Order = !Order;
  364. if (Window == W_SMALL) {
  365. if (!Order)
  366. mvwaddstr(Score, 12, 21,
  367. "o: order hand");
  368. else
  369. mvwaddstr(Score, 12, 21,
  370. "o: stop ordering");
  371. wclrtoeol(Score);
  372. }
  373. Movetype = M_ORDER;
  374. goto ret;
  375. case 'Q': /* Quit */
  376. rub(0); /* Same as a rubout */
  377. break;
  378. case 'W': /* Window toggle */
  379. Window = nextwin(Window);
  380. newscore();
  381. prscore(TRUE);
  382. wrefresh(Score);
  383. break;
  384. case 'R': /* Redraw screen */
  385. case CTRL('L'):
  386. wrefresh(curscr);
  387. break;
  388. case 'S': /* Save game */
  389. On_exit = FALSE;
  390. save();
  391. break;
  392. case 'E': /* Extrapolate */
  393. #ifdef EXTRAP
  394. if (last_ex)
  395. break;
  396. Finished = TRUE;
  397. if (Window != W_FULL)
  398. newscore();
  399. prscore(FALSE);
  400. wrefresh(Score);
  401. last_ex = TRUE;
  402. Finished = FALSE;
  403. #else
  404. error("%c: command not implemented", c);
  405. #endif
  406. break;
  407. case '\r': /* Ignore RETURNs and */
  408. case '\n': /* Line Feeds */
  409. case ' ': /* Spaces */
  410. case '\0': /* and nulls */
  411. break;
  412. #ifdef DEBUG
  413. case 'Z': /* Debug code */
  414. if (!Debug && outf == NULL) {
  415. char buf[MAXPATHLEN];
  416. char *sp;
  417. prompt(FILEPROMPT);
  418. leaveok(Board, FALSE);
  419. refresh();
  420. over:
  421. sp = buf;
  422. while ((*sp = readch()) != '\n') {
  423. if (*sp == killchar())
  424. goto over;
  425. else if (*sp == erasechar()) {
  426. if (--sp < buf)
  427. sp = buf;
  428. else {
  429. addch('\b');
  430. if (*sp < ' ')
  431. addch('\b');
  432. clrtoeol();
  433. }
  434. }
  435. else
  436. addstr(unctrl(*sp++));
  437. refresh();
  438. }
  439. *sp = '\0';
  440. leaveok(Board, TRUE);
  441. if ((outf = fopen(buf, "w")) == NULL)
  442. warn("%s", buf);
  443. setbuf(outf, (char *)NULL);
  444. }
  445. Debug = !Debug;
  446. break;
  447. #endif
  448. default:
  449. error("unknown command: %s", unctrl(c));
  450. break;
  451. }
  452. }
  453. ret:
  454. leaveok(Board, TRUE);
  455. }
  456. /*
  457. * return whether or not the player has picked
  458. */
  459. int
  460. haspicked(pp)
  461. const PLAY *pp;
  462. {
  463. int card;
  464. if (Topcard <= Deck)
  465. return TRUE;
  466. switch (pp->hand[Card_no]) {
  467. case C_GAS_SAFE: case C_SPARE_SAFE:
  468. case C_DRIVE_SAFE: case C_RIGHT_WAY:
  469. card = 1;
  470. break;
  471. default:
  472. card = 0;
  473. break;
  474. }
  475. return (pp->hand[card] != C_INIT);
  476. }
  477. void
  478. account(card)
  479. CARD card;
  480. {
  481. CARD oppos;
  482. if (card == C_INIT)
  483. return;
  484. ++Numseen[card];
  485. if (Play == COMP)
  486. switch (card) {
  487. case C_GAS_SAFE:
  488. case C_SPARE_SAFE:
  489. case C_DRIVE_SAFE:
  490. oppos = opposite(card);
  491. Numgos += Numcards[oppos] - Numseen[oppos];
  492. break;
  493. case C_CRASH:
  494. case C_FLAT:
  495. case C_EMPTY:
  496. case C_STOP:
  497. Numgos++;
  498. break;
  499. }
  500. }
  501. void
  502. prompt(promptno)
  503. int promptno;
  504. {
  505. static const char *const names[] = {
  506. ">>:Move:",
  507. "Really?",
  508. "Another hand?",
  509. "Another game?",
  510. "Save game?",
  511. "Same file?",
  512. "file:",
  513. "Extension?",
  514. "Overwrite file?",
  515. };
  516. static int last_prompt = -1;
  517. if (promptno == last_prompt)
  518. move(MOVE_Y, MOVE_X + strlen(names[promptno]) + 1);
  519. else {
  520. move(MOVE_Y, MOVE_X);
  521. if (promptno == MOVEPROMPT)
  522. standout();
  523. addstr(names[promptno]);
  524. if (promptno == MOVEPROMPT)
  525. standend();
  526. addch(' ');
  527. last_prompt = promptno;
  528. }
  529. clrtoeol();
  530. }
  531. void
  532. sort(hand)
  533. CARD *hand;
  534. {
  535. CARD *cp, *tp;
  536. CARD temp;
  537. cp = hand;
  538. hand += HAND_SZ;
  539. for ( ; cp < &hand[-1]; cp++)
  540. for (tp = cp + 1; tp < hand; tp++)
  541. if (*cp > *tp) {
  542. temp = *cp;
  543. *cp = *tp;
  544. *tp = temp;
  545. }
  546. }