crib.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. /* $NetBSD: crib.c,v 1.19 2004/01/27 20:30:29 jsm Exp $ */
  2. /*-
  3. * Copyright (c) 1980, 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. __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
  33. The Regents of the University of California. All rights reserved.\n");
  34. #endif /* not lint */
  35. #ifndef lint
  36. #if 0
  37. static char sccsid[] = "@(#)crib.c 8.1 (Berkeley) 5/31/93";
  38. #else
  39. __RCSID("$NetBSD: crib.c,v 1.19 2004/01/27 20:30:29 jsm Exp $");
  40. #endif
  41. #endif /* not lint */
  42. #include <curses.h>
  43. #include <err.h>
  44. #include <fcntl.h>
  45. #include <signal.h>
  46. #include <stdlib.h>
  47. #include <string.h>
  48. #include <unistd.h>
  49. #include "deck.h"
  50. #include "cribbage.h"
  51. #include "cribcur.h"
  52. #include "pathnames.h"
  53. int main(int, char *[]);
  54. int
  55. main(argc, argv)
  56. int argc;
  57. char *argv[];
  58. {
  59. BOOLEAN playing;
  60. FILE *f;
  61. int ch;
  62. int fd;
  63. int flags;
  64. f = fopen(_PATH_LOG, "a");
  65. if (f == NULL)
  66. warn("fopen %s", _PATH_LOG);
  67. if (f != NULL && fileno(f) < 3)
  68. exit(1);
  69. /* Revoke setgid privileges */
  70. setregid(getgid(), getgid());
  71. /* Set close-on-exec flag on log file */
  72. if (f != NULL) {
  73. fd = fileno(f);
  74. flags = fcntl(fd, F_GETFD);
  75. if (flags < 0)
  76. err(1, "fcntl F_GETFD");
  77. flags |= FD_CLOEXEC;
  78. if (fcntl(fd, F_SETFD, flags) == -1)
  79. err(1, "fcntl F_SETFD");
  80. }
  81. while ((ch = getopt(argc, argv, "eqr")) != -1)
  82. switch (ch) {
  83. case 'e':
  84. explain = TRUE;
  85. break;
  86. case 'q':
  87. quiet = TRUE;
  88. break;
  89. case 'r':
  90. rflag = TRUE;
  91. break;
  92. case '?':
  93. default:
  94. (void) fprintf(stderr, "usage: cribbage [-eqr]\n");
  95. exit(1);
  96. }
  97. initscr();
  98. (void)signal(SIGINT, receive_intr);
  99. cbreak();
  100. noecho();
  101. Playwin = subwin(stdscr, PLAY_Y, PLAY_X, 0, 0);
  102. Tablewin = subwin(stdscr, TABLE_Y, TABLE_X, 0, PLAY_X);
  103. Compwin = subwin(stdscr, COMP_Y, COMP_X, 0, TABLE_X + PLAY_X);
  104. Msgwin = subwin(stdscr, MSG_Y, MSG_X, Y_MSG_START, SCORE_X + 1);
  105. leaveok(Playwin, TRUE);
  106. leaveok(Tablewin, TRUE);
  107. leaveok(Compwin, TRUE);
  108. clearok(stdscr, FALSE);
  109. if (!quiet) {
  110. msg("Do you need instructions for cribbage? ");
  111. if (getuchar() == 'Y') {
  112. endwin();
  113. clear();
  114. mvcur(0, COLS - 1, LINES - 1, 0);
  115. fflush(stdout);
  116. instructions();
  117. cbreak();
  118. noecho();
  119. clear();
  120. refresh();
  121. msg("For cribbage rules, use \"man cribbage\"");
  122. }
  123. }
  124. playing = TRUE;
  125. do {
  126. wclrtobot(Msgwin);
  127. msg(quiet ? "L or S? " : "Long (to 121) or Short (to 61)? ");
  128. if (glimit == SGAME)
  129. glimit = (getuchar() == 'L' ? LGAME : SGAME);
  130. else
  131. glimit = (getuchar() == 'S' ? SGAME : LGAME);
  132. game();
  133. msg("Another game? ");
  134. playing = (getuchar() == 'Y');
  135. } while (playing);
  136. if (f != NULL) {
  137. (void)fprintf(f, "%s: won %5.5d, lost %5.5d\n",
  138. getlogin(), cgames, pgames);
  139. (void) fclose(f);
  140. }
  141. bye();
  142. exit(0);
  143. }
  144. /*
  145. * makeboard:
  146. * Print out the initial board on the screen
  147. */
  148. void
  149. makeboard()
  150. {
  151. mvaddstr(SCORE_Y + 0, SCORE_X,
  152. "+---------------------------------------+");
  153. mvaddstr(SCORE_Y + 1, SCORE_X,
  154. "| Score: 0 YOU |");
  155. mvaddstr(SCORE_Y + 2, SCORE_X,
  156. "| *.....:.....:.....:.....:.....:..... |");
  157. mvaddstr(SCORE_Y + 3, SCORE_X,
  158. "| *.....:.....:.....:.....:.....:..... |");
  159. mvaddstr(SCORE_Y + 4, SCORE_X,
  160. "| |");
  161. mvaddstr(SCORE_Y + 5, SCORE_X,
  162. "| *.....:.....:.....:.....:.....:..... |");
  163. mvaddstr(SCORE_Y + 6, SCORE_X,
  164. "| *.....:.....:.....:.....:.....:..... |");
  165. mvaddstr(SCORE_Y + 7, SCORE_X,
  166. "| Score: 0 ME |");
  167. mvaddstr(SCORE_Y + 8, SCORE_X,
  168. "+---------------------------------------+");
  169. gamescore();
  170. }
  171. /*
  172. * gamescore:
  173. * Print out the current game score
  174. */
  175. void
  176. gamescore()
  177. {
  178. if (pgames || cgames) {
  179. mvprintw(SCORE_Y + 1, SCORE_X + 28, "Games: %3d", pgames);
  180. mvprintw(SCORE_Y + 7, SCORE_X + 28, "Games: %3d", cgames);
  181. }
  182. Lastscore[0] = -1;
  183. Lastscore[1] = -1;
  184. }
  185. /*
  186. * game:
  187. * Play one game up to glimit points. Actually, we only ASK the
  188. * player what card to turn. We do a random one, anyway.
  189. */
  190. void
  191. game()
  192. {
  193. int i, j;
  194. BOOLEAN flag;
  195. BOOLEAN compcrib;
  196. compcrib = FALSE;
  197. makedeck(deck);
  198. shuffle(deck);
  199. if (gamecount == 0) {
  200. flag = TRUE;
  201. do {
  202. if (!rflag) { /* player cuts deck */
  203. msg(quiet ? "Cut for crib? " :
  204. "Cut to see whose crib it is -- low card wins? ");
  205. getline();
  206. }
  207. i = (rand() >> 4) % CARDS; /* random cut */
  208. do { /* comp cuts deck */
  209. j = (rand() >> 4) % CARDS;
  210. } while (j == i);
  211. addmsg(quiet ? "You cut " : "You cut the ");
  212. msgcard(deck[i], FALSE);
  213. endmsg();
  214. addmsg(quiet ? "I cut " : "I cut the ");
  215. msgcard(deck[j], FALSE);
  216. endmsg();
  217. flag = (deck[i].rank == deck[j].rank);
  218. if (flag) {
  219. msg(quiet ? "We tied..." :
  220. "We tied and have to try again...");
  221. shuffle(deck);
  222. continue;
  223. } else
  224. compcrib = (deck[i].rank > deck[j].rank);
  225. } while (flag);
  226. do_wait();
  227. clear();
  228. makeboard();
  229. refresh();
  230. } else {
  231. makeboard();
  232. refresh();
  233. werase(Tablewin);
  234. wrefresh(Tablewin);
  235. werase(Compwin);
  236. wrefresh(Compwin);
  237. msg("Loser (%s) gets first crib", (iwon ? "you" : "me"));
  238. compcrib = !iwon;
  239. }
  240. pscore = cscore = 0;
  241. flag = TRUE;
  242. do {
  243. shuffle(deck);
  244. flag = !playhand(compcrib);
  245. compcrib = !compcrib;
  246. } while (flag);
  247. ++gamecount;
  248. if (cscore < pscore) {
  249. if (glimit - cscore > 60) {
  250. msg("YOU DOUBLE SKUNKED ME!");
  251. pgames += 4;
  252. } else
  253. if (glimit - cscore > 30) {
  254. msg("YOU SKUNKED ME!");
  255. pgames += 2;
  256. } else {
  257. msg("YOU WON!");
  258. ++pgames;
  259. }
  260. iwon = FALSE;
  261. } else {
  262. if (glimit - pscore > 60) {
  263. msg("I DOUBLE SKUNKED YOU!");
  264. cgames += 4;
  265. } else
  266. if (glimit - pscore > 30) {
  267. msg("I SKUNKED YOU!");
  268. cgames += 2;
  269. } else {
  270. msg("I WON!");
  271. ++cgames;
  272. }
  273. iwon = TRUE;
  274. }
  275. gamescore();
  276. }
  277. /*
  278. * playhand:
  279. * Do up one hand of the game
  280. */
  281. int
  282. playhand(mycrib)
  283. BOOLEAN mycrib;
  284. {
  285. int deckpos;
  286. werase(Compwin);
  287. wrefresh(Compwin);
  288. werase(Tablewin);
  289. wrefresh(Tablewin);
  290. knownum = 0;
  291. deckpos = deal(mycrib);
  292. sorthand(chand, FULLHAND);
  293. sorthand(phand, FULLHAND);
  294. makeknown(chand, FULLHAND);
  295. prhand(phand, FULLHAND, Playwin, FALSE);
  296. discard(mycrib);
  297. if (cut(mycrib, deckpos))
  298. return TRUE;
  299. if (peg(mycrib))
  300. return TRUE;
  301. werase(Tablewin);
  302. wrefresh(Tablewin);
  303. if (score(mycrib))
  304. return TRUE;
  305. return FALSE;
  306. }
  307. /*
  308. * deal cards to both players from deck
  309. */
  310. int
  311. deal(mycrib)
  312. BOOLEAN mycrib;
  313. {
  314. int i, j;
  315. for (i = j = 0; i < FULLHAND; i++) {
  316. if (mycrib) {
  317. phand[i] = deck[j++];
  318. chand[i] = deck[j++];
  319. } else {
  320. chand[i] = deck[j++];
  321. phand[i] = deck[j++];
  322. }
  323. }
  324. return (j);
  325. }
  326. /*
  327. * discard:
  328. * Handle players discarding into the crib...
  329. * Note: we call cdiscard() after prining first message so player doesn't wait
  330. */
  331. void
  332. discard(mycrib)
  333. BOOLEAN mycrib;
  334. {
  335. const char *prompt;
  336. CARD crd;
  337. prcrib(mycrib, TRUE);
  338. prompt = (quiet ? "Discard --> " : "Discard a card --> ");
  339. cdiscard(mycrib); /* puts best discard at end */
  340. crd = phand[infrom(phand, FULLHAND, prompt)];
  341. cremove(crd, phand, FULLHAND);
  342. prhand(phand, FULLHAND, Playwin, FALSE);
  343. crib[0] = crd;
  344. /* Next four lines same as last four except for cdiscard(). */
  345. crd = phand[infrom(phand, FULLHAND - 1, prompt)];
  346. cremove(crd, phand, FULLHAND - 1);
  347. prhand(phand, FULLHAND, Playwin, FALSE);
  348. crib[1] = crd;
  349. crib[2] = chand[4];
  350. crib[3] = chand[5];
  351. chand[4].rank = chand[4].suit = chand[5].rank = chand[5].suit = EMPTY;
  352. }
  353. /*
  354. * cut:
  355. * Cut the deck and set turnover. Actually, we only ASK the
  356. * player what card to turn. We do a random one, anyway.
  357. */
  358. int
  359. cut(mycrib, pos)
  360. BOOLEAN mycrib;
  361. int pos;
  362. {
  363. int i;
  364. BOOLEAN win;
  365. win = FALSE;
  366. if (mycrib) {
  367. if (!rflag) { /* random cut */
  368. msg(quiet ? "Cut the deck? " :
  369. "How many cards down do you wish to cut the deck? ");
  370. getline();
  371. }
  372. i = (rand() >> 4) % (CARDS - pos);
  373. turnover = deck[i + pos];
  374. addmsg(quiet ? "You cut " : "You cut the ");
  375. msgcard(turnover, FALSE);
  376. endmsg();
  377. if (turnover.rank == JACK) {
  378. msg("I get two for his heels");
  379. win = chkscr(&cscore, 2);
  380. }
  381. } else {
  382. i = (rand() >> 4) % (CARDS - pos) + pos;
  383. turnover = deck[i];
  384. addmsg(quiet ? "I cut " : "I cut the ");
  385. msgcard(turnover, FALSE);
  386. endmsg();
  387. if (turnover.rank == JACK) {
  388. msg("You get two for his heels");
  389. win = chkscr(&pscore, 2);
  390. }
  391. }
  392. makeknown(&turnover, 1);
  393. prcrib(mycrib, FALSE);
  394. return (win);
  395. }
  396. /*
  397. * prcrib:
  398. * Print out the turnover card with crib indicator
  399. */
  400. void
  401. prcrib(mycrib, blank)
  402. BOOLEAN mycrib, blank;
  403. {
  404. int y, cardx;
  405. if (mycrib)
  406. cardx = CRIB_X;
  407. else
  408. cardx = 0;
  409. mvaddstr(CRIB_Y, cardx + 1, "CRIB");
  410. prcard(stdscr, CRIB_Y + 1, cardx, turnover, blank);
  411. if (mycrib)
  412. cardx = 0;
  413. else
  414. cardx = CRIB_X;
  415. for (y = CRIB_Y; y <= CRIB_Y + 5; y++)
  416. mvaddstr(y, cardx, " ");
  417. refresh();
  418. }
  419. /*
  420. * peg:
  421. * Handle all the pegging...
  422. */
  423. static CARD Table[14];
  424. static int Tcnt;
  425. int
  426. peg(mycrib)
  427. BOOLEAN mycrib;
  428. {
  429. static CARD ch[CINHAND], ph[CINHAND];
  430. int i, j, k;
  431. int l;
  432. int cnum, pnum, sum;
  433. BOOLEAN myturn, mego, ugo, last, played;
  434. CARD crd;
  435. played = FALSE;
  436. cnum = pnum = CINHAND;
  437. for (i = 0; i < CINHAND; i++) { /* make copies of hands */
  438. ch[i] = chand[i];
  439. ph[i] = phand[i];
  440. }
  441. Tcnt = 0; /* index to table of cards played */
  442. sum = 0; /* sum of cards played */
  443. mego = ugo = FALSE;
  444. myturn = !mycrib;
  445. for (;;) {
  446. last = TRUE; /* enable last flag */
  447. prhand(ph, pnum, Playwin, FALSE);
  448. prhand(ch, cnum, Compwin, TRUE);
  449. prtable(sum);
  450. if (myturn) { /* my tyrn to play */
  451. if (!anymove(ch, cnum, sum)) { /* if no card to play */
  452. if (!mego && cnum) { /* go for comp? */
  453. msg("GO");
  454. mego = TRUE;
  455. }
  456. /* can player move? */
  457. if (anymove(ph, pnum, sum))
  458. myturn = !myturn;
  459. else { /* give him his point */
  460. msg(quiet ? "You get one" :
  461. "You get one point");
  462. do_wait();
  463. if (chkscr(&pscore, 1))
  464. return TRUE;
  465. sum = 0;
  466. mego = ugo = FALSE;
  467. Tcnt = 0;
  468. }
  469. } else {
  470. played = TRUE;
  471. j = -1;
  472. k = 0;
  473. /* maximize score */
  474. for (i = 0; i < cnum; i++) {
  475. l = pegscore(ch[i], Table, Tcnt, sum);
  476. if (l > k) {
  477. k = l;
  478. j = i;
  479. }
  480. }
  481. if (j < 0) /* if nothing scores */
  482. j = cchose(ch, cnum, sum);
  483. crd = ch[j];
  484. cremove(crd, ch, cnum--);
  485. sum += VAL(crd.rank);
  486. Table[Tcnt++] = crd;
  487. if (k > 0) {
  488. addmsg(quiet ? "I get %d playing " :
  489. "I get %d points playing ", k);
  490. msgcard(crd, FALSE);
  491. endmsg();
  492. if (chkscr(&cscore, k))
  493. return TRUE;
  494. }
  495. myturn = !myturn;
  496. }
  497. } else {
  498. if (!anymove(ph, pnum, sum)) { /* can player move? */
  499. if (!ugo && pnum) { /* go for player */
  500. msg("You have a GO");
  501. ugo = TRUE;
  502. }
  503. /* can computer play? */
  504. if (anymove(ch, cnum, sum))
  505. myturn = !myturn;
  506. else {
  507. msg(quiet ? "I get one" :
  508. "I get one point");
  509. do_wait();
  510. if (chkscr(&cscore, 1))
  511. return TRUE;
  512. sum = 0;
  513. mego = ugo = FALSE;
  514. Tcnt = 0;
  515. }
  516. } else { /* player plays */
  517. played = FALSE;
  518. if (pnum == 1) {
  519. crd = ph[0];
  520. msg("You play your last card");
  521. } else
  522. for (;;) {
  523. prhand(ph,
  524. pnum, Playwin, FALSE);
  525. crd = ph[infrom(ph,
  526. pnum, "Your play: ")];
  527. if (sum + VAL(crd.rank) <= 31)
  528. break;
  529. else
  530. msg("Total > 31 -- try again");
  531. }
  532. makeknown(&crd, 1);
  533. cremove(crd, ph, pnum--);
  534. i = pegscore(crd, Table, Tcnt, sum);
  535. sum += VAL(crd.rank);
  536. Table[Tcnt++] = crd;
  537. if (i > 0) {
  538. msg(quiet ? "You got %d" :
  539. "You got %d points", i);
  540. if (pnum == 0)
  541. do_wait();
  542. if (chkscr(&pscore, i))
  543. return TRUE;
  544. }
  545. myturn = !myturn;
  546. }
  547. }
  548. if (sum >= 31) {
  549. if (!myturn)
  550. do_wait();
  551. sum = 0;
  552. mego = ugo = FALSE;
  553. Tcnt = 0;
  554. last = FALSE; /* disable last flag */
  555. }
  556. if (!pnum && !cnum)
  557. break; /* both done */
  558. }
  559. prhand(ph, pnum, Playwin, FALSE);
  560. prhand(ch, cnum, Compwin, TRUE);
  561. prtable(sum);
  562. if (last) {
  563. if (played) {
  564. msg(quiet ? "I get one for last" :
  565. "I get one point for last");
  566. do_wait();
  567. if (chkscr(&cscore, 1))
  568. return TRUE;
  569. } else {
  570. msg(quiet ? "You get one for last" :
  571. "You get one point for last");
  572. do_wait();
  573. if (chkscr(&pscore, 1))
  574. return TRUE;
  575. }
  576. }
  577. return (FALSE);
  578. }
  579. /*
  580. * prtable:
  581. * Print out the table with the current score
  582. */
  583. void
  584. prtable(score)
  585. int score;
  586. {
  587. prhand(Table, Tcnt, Tablewin, FALSE);
  588. mvwprintw(Tablewin, (Tcnt + 2) * 2, Tcnt + 1, "%2d", score);
  589. wrefresh(Tablewin);
  590. }
  591. /*
  592. * score:
  593. * Handle the scoring of the hands
  594. */
  595. int
  596. score(mycrib)
  597. BOOLEAN mycrib;
  598. {
  599. sorthand(crib, CINHAND);
  600. if (mycrib) {
  601. if (plyrhand(phand, "hand"))
  602. return (TRUE);
  603. if (comphand(chand, "hand"))
  604. return (TRUE);
  605. do_wait();
  606. if (comphand(crib, "crib"))
  607. return (TRUE);
  608. do_wait();
  609. } else {
  610. if (comphand(chand, "hand"))
  611. return (TRUE);
  612. if (plyrhand(phand, "hand"))
  613. return (TRUE);
  614. if (plyrhand(crib, "crib"))
  615. return (TRUE);
  616. }
  617. return (FALSE);
  618. }