home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1996 May / PCOnline_05_1996.bin / linux / source / y / bsdgames / bsd-game.000 / bsd-game / games / cribbage / crib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-21  |  14.1 KB  |  615 lines

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