Add copyright
[unix-history] / usr / src / games / cribbage / crib.c
index 0c13ac7..2029464 100644 (file)
@@ -1,3 +1,19 @@
+/*
+ * Copyright (c) 1980 Regents of the University of California.
+ * All rights reserved.  The Berkeley software License Agreement
+ * specifies the terms and conditions for redistribution.
+ */
+
+#ifndef lint
+char copyright[] =
+"@(#) Copyright (c) 1980 Regents of the University of California.\n\
+ All rights reserved.\n";
+#endif not lint
+
+#ifndef lint
+static char sccsid[] = "@(#)crib.c     5.1 (Berkeley) %G%";
+#endif not lint
+
 # include      <curses.h>
 # include      <signal.h>
 # include      "deck.h"
 # include      <curses.h>
 # include      <signal.h>
 # include      "deck.h"
@@ -56,22 +72,33 @@ char        *argv[];
        Playwin = subwin(stdscr, PLAY_Y, PLAY_X, 0, 0);
        Tablewin = subwin(stdscr, TABLE_Y, TABLE_X, 0, PLAY_X);
        Compwin = subwin(stdscr, COMP_Y, COMP_X, 0, TABLE_X + PLAY_X);
        Playwin = subwin(stdscr, PLAY_Y, PLAY_X, 0, 0);
        Tablewin = subwin(stdscr, TABLE_Y, TABLE_X, 0, PLAY_X);
        Compwin = subwin(stdscr, COMP_Y, COMP_X, 0, TABLE_X + PLAY_X);
+       Msgwin = subwin(stdscr, MSG_Y, MSG_X, Y_MSG_START, SCORE_X + 1);
        leaveok(Playwin, TRUE);
        leaveok(Tablewin, TRUE);
        leaveok(Compwin, TRUE);
        leaveok(Playwin, TRUE);
        leaveok(Tablewin, TRUE);
        leaveok(Compwin, TRUE);
+       clearok(stdscr, FALSE);
 
        if (!quiet) {
            msg("Do you need instructions for cribbage? ");
            if (getuchar() == 'Y') {
 
        if (!quiet) {
            msg("Do you need instructions for cribbage? ");
            if (getuchar() == 'Y') {
+               endwin();
+               fflush(stdout);
                system(INSTRCMD);
                system(INSTRCMD);
+               crmode();
+               noecho();
+               clear();
+               refresh();
                msg("For the rules of this program, do \"man cribbage\"");
            }
        }
        playing = TRUE;
        do {
                msg("For the rules of this program, do \"man cribbage\"");
            }
        }
        playing = TRUE;
        do {
-           makeboard();
+           wclrtobot(Msgwin);
            msg(quiet ? "L or S? " : "Long (to 121) or Short (to 61)? ");
            msg(quiet ? "L or S? " : "Long (to 121) or Short (to 61)? ");
-           glimit = (getuchar() == 'S' ? SGAME : LGAME);
+           if (glimit == SGAME)
+               glimit = (getuchar() == 'L' ? LGAME : SGAME);
+           else
+               glimit = (getuchar() == 'S' ? SGAME : LGAME);
            game();
            msg("Another game? ");
            playing = (getuchar() == 'Y');
            game();
            msg("Another game? ");
            playing = (getuchar() == 'Y');
@@ -86,43 +113,43 @@ char       *argv[];
 }
 
 /*
 }
 
 /*
- * bye:
- *     Leave the program, cleaning things up as we go.
+ * makeboard:
+ *     Print out the initial board on the screen
  */
  */
-bye()
+makeboard()
 {
 {
-       signal(SIGINT, SIG_IGN);
-       mvcur(0, COLS - 1, LINES - 1, 0);
-       fflush(stdout);
-       endwin();
-       putchar('\n');
-       exit(1);
+    mvaddstr(SCORE_Y + 0, SCORE_X, "+---------------------------------------+");
+    mvaddstr(SCORE_Y + 1, SCORE_X, "|  Score:   0     YOU                   |");
+    mvaddstr(SCORE_Y + 2, SCORE_X, "| *.....:.....:.....:.....:.....:.....  |");
+    mvaddstr(SCORE_Y + 3, SCORE_X, "| *.....:.....:.....:.....:.....:.....  |");
+    mvaddstr(SCORE_Y + 4, SCORE_X, "|                                       |");
+    mvaddstr(SCORE_Y + 5, SCORE_X, "| *.....:.....:.....:.....:.....:.....  |");
+    mvaddstr(SCORE_Y + 6, SCORE_X, "| *.....:.....:.....:.....:.....:.....  |");
+    mvaddstr(SCORE_Y + 7, SCORE_X, "|  Score:   0      ME                   |");
+    mvaddstr(SCORE_Y + 8, SCORE_X, "+---------------------------------------+");
+    gamescore();
 }
 
 /*
 }
 
 /*
- * makeboard:
- *     Print out the initial board on the screen
+ * gamescore:
+ *     Print out the current game score
  */
  */
-makeboard()
+gamescore()
 {
     extern int Lastscore[];
 
 {
     extern int Lastscore[];
 
-    mvaddstr(SCORE_Y + 0, SCORE_X, "+---------------------------------------+");
-    mvaddstr(SCORE_Y + 1, SCORE_X, "|                                       |");
-    mvaddstr(SCORE_Y + 2, SCORE_X, "|  .....:.....:.....:.....:.....:.....  |");
-    mvaddstr(SCORE_Y + 3, SCORE_X, "|  .....:.....:.....:.....:.....:.....  |");
-    mvaddstr(SCORE_Y + 4, SCORE_X, "|                                       |");
-    mvaddstr(SCORE_Y + 5, SCORE_X, "|  .....:.....:.....:.....:.....:.....  |");
-    mvaddstr(SCORE_Y + 6, SCORE_X, "|  .....:.....:.....:.....:.....:.....  |");
-    mvaddstr(SCORE_Y + 7, SCORE_X, "|                                       |");
-    mvaddstr(SCORE_Y + 8, SCORE_X, "+---------------------------------------+");
-    Lastscore[0] = 0;
-    Lastscore[1] = 0;
+    if (pgames || cgames) {
+           mvprintw(SCORE_Y + 1, SCORE_X + 28, "Games: %3d", pgames);
+           mvprintw(SCORE_Y + 7, SCORE_X + 28, "Games: %3d", cgames);
+    }
+    Lastscore[0] = -1;
+    Lastscore[1] = -1;
 }
 
 /*
  * game:
 }
 
 /*
  * game:
- *     Play one game up to glimit points
+ *     Play one game up to glimit points.  Actually, we only ASK the
+ *     player what card to turn.  We do a random one, anyway.
  */
 game()
 {
  */
 game()
 {
@@ -130,18 +157,19 @@ game()
        BOOLEAN                 flag;
        BOOLEAN                 compcrib;
 
        BOOLEAN                 flag;
        BOOLEAN                 compcrib;
 
+       makeboard();
+       refresh();
        makedeck(deck);
        shuffle(deck);
        if (gamecount == 0) {
            flag = TRUE;
            do {
        makedeck(deck);
        shuffle(deck);
        if (gamecount == 0) {
            flag = TRUE;
            do {
-               if (rflag)                              /* player cuts deck */
-                   i = (rand() >> 4) % CARDS;          /* random cut */
-               else {
+               if (!rflag) {                           /* player cuts deck */
                    msg(quiet ? "Cut for crib? " :
                        "Cut to see whose crib it is -- low card wins? ");
                    msg(quiet ? "Cut for crib? " :
                        "Cut to see whose crib it is -- low card wins? ");
-                   i = number(0, CARDS - 1);
+                   getline();
                }
                }
+               i = (rand() >> 4) % CARDS;              /* random cut */
                do {                                    /* comp cuts deck */
                    j = (rand() >> 4) % CARDS;
                } while (j == i);
                do {                                    /* comp cuts deck */
                    j = (rand() >> 4) % CARDS;
                } while (j == i);
@@ -163,7 +191,11 @@ game()
            } while (flag);
        }
        else {
            } while (flag);
        }
        else {
-           msg("Loser (%s) gets first crib.",  (iwon ? "you" : "me"));
+           werase(Tablewin);
+           wrefresh(Tablewin);
+           werase(Compwin);
+           wrefresh(Compwin);
+           msg("Loser (%s) gets first crib",  (iwon ? "you" : "me"));
            compcrib = !iwon;
        }
 
            compcrib = !iwon;
        }
 
@@ -173,7 +205,6 @@ game()
            shuffle(deck);
            flag = !playhand(compcrib);
            compcrib = !compcrib;
            shuffle(deck);
            flag = !playhand(compcrib);
            compcrib = !compcrib;
-           msg("You have %d points, I have %d.", pscore, cscore);
        } while (flag);
        ++gamecount;
        if (cscore < pscore) {
        } while (flag);
        ++gamecount;
        if (cscore < pscore) {
@@ -206,7 +237,7 @@ game()
            }
            iwon = TRUE;
        }
            }
            iwon = TRUE;
        }
-       msg("I have won %d games, you have won %d", cgames, pgames);
+       gamescore();
 }
 
 /*
 }
 
 /*
@@ -220,17 +251,13 @@ BOOLEAN           mycrib;
        extern char             Msgbuf[];
 
        werase(Compwin);
        extern char             Msgbuf[];
 
        werase(Compwin);
-       wrefresh(Compwin);
-       move(CRIB_Y, 0);
-       clrtobot();
-       mvaddstr(LINES - 1, 0, Msgbuf);
 
        knownum = 0;
        deckpos = deal(mycrib);
        sorthand(chand, FULLHAND);
        sorthand(phand, FULLHAND);
        makeknown(chand, FULLHAND);
 
        knownum = 0;
        deckpos = deal(mycrib);
        sorthand(chand, FULLHAND);
        sorthand(phand, FULLHAND);
        makeknown(chand, FULLHAND);
-       prhand(phand, FULLHAND, Playwin);
+       prhand(phand, FULLHAND, Playwin, FALSE);
        discard(mycrib);
        if (cut(mycrib, deckpos))
            return TRUE;
        discard(mycrib);
        if (cut(mycrib, deckpos))
            return TRUE;
@@ -278,19 +305,17 @@ BOOLEAN           mycrib;
        register char   *prompt;
        CARD            crd;
 
        register char   *prompt;
        CARD            crd;
 
-       msg("It's %s crib...", (mycrib ? "my" : "your"));
+       prcrib(mycrib, TRUE);
        prompt = (quiet ? "Discard --> " : "Discard a card --> ");
        prompt = (quiet ? "Discard --> " : "Discard a card --> ");
-       msg(prompt);
        cdiscard(mycrib);                       /* puts best discard at end */
        crd = phand[infrom(phand, FULLHAND, prompt)];
        remove(crd, phand, FULLHAND);
        cdiscard(mycrib);                       /* puts best discard at end */
        crd = phand[infrom(phand, FULLHAND, prompt)];
        remove(crd, phand, FULLHAND);
-       prhand(phand, FULLHAND, Playwin);
+       prhand(phand, FULLHAND, Playwin, FALSE);
        crib[0] = crd;
 /* next four lines same as last four except for cdiscard() */
        crib[0] = crd;
 /* next four lines same as last four except for cdiscard() */
-       msg(prompt);
        crd = phand[infrom(phand, FULLHAND - 1, prompt)];
        remove(crd, phand, FULLHAND - 1);
        crd = phand[infrom(phand, FULLHAND - 1, prompt)];
        remove(crd, phand, FULLHAND - 1);
-       prhand(phand, FULLHAND, Playwin);
+       prhand(phand, FULLHAND, Playwin, FALSE);
        crib[1] = crd;
        crib[2] = chand[4];
        crib[3] = chand[5];
        crib[1] = crd;
        crib[2] = chand[4];
        crib[3] = chand[5];
@@ -299,7 +324,8 @@ BOOLEAN             mycrib;
 
 /*
  * cut:
 
 /*
  * cut:
- *     Cut the deck and set turnover
+ *     Cut the deck and set turnover.  Actually, we only ASK the
+ *     player what card to turn.  We do a random one, anyway.
  */
 cut(mycrib, pos)
 BOOLEAN                mycrib;
  */
 cut(mycrib, pos)
 BOOLEAN                mycrib;
@@ -309,22 +335,20 @@ int               pos;
        BOOLEAN                 win = FALSE;
 
        if (mycrib) {
        BOOLEAN                 win = FALSE;
 
        if (mycrib) {
-           if (rflag)                          /* random cut */
-               i = (rand() >> 4) % (CARDS - pos);
-           else {
+           if (!rflag) {                       /* random cut */
                msg(quiet ? "Cut the deck? " :
                        "How many cards down do you wish to cut the deck? ");
                msg(quiet ? "Cut the deck? " :
                        "How many cards down do you wish to cut the deck? ");
-               i = number(0, CARDS - pos - 1);
+               getline();
            }
            }
+           i = (rand() >> 4) % (CARDS - pos);
            turnover = deck[i + pos];
            addmsg(quiet ? "You cut " : "You cut the ");
            msgcard(turnover, FALSE);
            endmsg();
            if (turnover.rank == JACK) {
            turnover = deck[i + pos];
            addmsg(quiet ? "You cut " : "You cut the ");
            msgcard(turnover, FALSE);
            endmsg();
            if (turnover.rank == JACK) {
-               msg("I get two for his heels.");
+               msg("I get two for his heels");
                win = chkscr(&cscore,2 );
            }
                win = chkscr(&cscore,2 );
            }
-           cardx = CRIB_X;
        }
        else {
            i = (rand() >> 4) % (CARDS - pos) + pos;
        }
        else {
            i = (rand() >> 4) % (CARDS - pos) + pos;
@@ -333,17 +357,41 @@ int               pos;
            msgcard(turnover, FALSE);
            endmsg();
            if (turnover.rank == JACK) {
            msgcard(turnover, FALSE);
            endmsg();
            if (turnover.rank == JACK) {
-               msg("You get two for his heels.");
+               msg("You get two for his heels");
                win = chkscr(&pscore, 2);
            }
                win = chkscr(&pscore, 2);
            }
-           cardx = 0;
        }
        makeknown(&turnover, 1);
        }
        makeknown(&turnover, 1);
-       mvaddstr(CRIB_Y, cardx + 1, "CRIB");
-       prcard(stdscr, CRIB_Y + 1, cardx, turnover);
+       prcrib(mycrib, FALSE);
        return win;
 }
 
        return win;
 }
 
+/*
+ * prcrib:
+ *     Print out the turnover card with crib indicator
+ */
+prcrib(mycrib, blank)
+BOOLEAN                mycrib, blank;
+{
+       register int    y, cardx;
+
+       if (mycrib)
+           cardx = CRIB_X;
+       else
+           cardx = 0;
+
+       mvaddstr(CRIB_Y, cardx + 1, "CRIB");
+       prcard(stdscr, CRIB_Y + 1, cardx, turnover, blank);
+
+       if (mycrib)
+           cardx = 0;
+       else
+           cardx = CRIB_X;
+
+       for (y = CRIB_Y; y <= CRIB_Y + 5; y++)
+           mvaddstr(y, cardx, "       ");
+}
+
 /*
  * peg:
  *     Handle all the pegging...
 /*
  * peg:
  *     Handle all the pegging...
@@ -359,9 +407,9 @@ BOOLEAN             mycrib;
        static CARD             ch[CINHAND], ph[CINHAND];
        CARD                    crd;
        register int            i, j, k;
        static CARD             ch[CINHAND], ph[CINHAND];
        CARD                    crd;
        register int            i, j, k;
-       int                     l;
-       int                     cnum, pnum, sum;
-       BOOLEAN                 myturn, mego, ugo, last, played;
+       register int            l;
+       register int            cnum, pnum, sum;
+       register BOOLEAN        myturn, mego, ugo, last, played;
 
        cnum = pnum = CINHAND;
        for (i = 0; i < CINHAND; i++) {         /* make copies of hands */
 
        cnum = pnum = CINHAND;
        for (i = 0; i < CINHAND; i++) {         /* make copies of hands */
@@ -374,24 +422,24 @@ BOOLEAN           mycrib;
        myturn = !mycrib;
        for (;;) {
            last = TRUE;                                /* enable last flag */
        myturn = !mycrib;
        for (;;) {
            last = TRUE;                                /* enable last flag */
+           prhand(ph, pnum, Playwin, FALSE);
+           prhand(ch, cnum, Compwin, TRUE);
            prtable(sum);
            prtable(sum);
-           prhand(ph, pnum, Playwin);
            if (myturn) {                               /* my tyrn to play */
                if (!anymove(ch, cnum, sum)) {          /* if no card to play */
                    if (!mego && cnum) {                /* go for comp? */
            if (myturn) {                               /* my tyrn to play */
                if (!anymove(ch, cnum, sum)) {          /* if no card to play */
                    if (!mego && cnum) {                /* go for comp? */
-                       msg("GO.");
+                       msg("GO");
                        mego = TRUE;
                    }
                    if (anymove(ph, pnum, sum))         /* can player move? */
                        myturn = !myturn;
                    else {                              /* give him his point */
                        mego = TRUE;
                    }
                    if (anymove(ph, pnum, sum))         /* can player move? */
                        myturn = !myturn;
                    else {                              /* give him his point */
-                       msg(quiet ? "You get one." : "You get one point.");
+                       msg(quiet ? "You get one" : "You get one point");
                        if (chkscr(&pscore, 1))
                            return TRUE;
                        sum = 0;
                        mego = ugo = FALSE;
                        Tcnt = 0;
                        if (chkscr(&pscore, 1))
                            return TRUE;
                        sum = 0;
                        mego = ugo = FALSE;
                        Tcnt = 0;
-                       Hasread = FALSE;
                    }
                }
                else {
                    }
                }
                else {
@@ -412,7 +460,8 @@ BOOLEAN             mycrib;
                    sum += VAL(crd.rank);
                    Table[Tcnt++] = crd;
                    if (k > 0) {
                    sum += VAL(crd.rank);
                    Table[Tcnt++] = crd;
                    if (k > 0) {
-                       addmsg(quiet ? "I get %d playing " : "I get %d points playing ", k);
+                       addmsg(quiet ? "I get %d playing " :
+                           "I get %d points playing ", k);
                        msgcard(crd, FALSE);
                        endmsg();
                        if (chkscr(&cscore, k))
                        msgcard(crd, FALSE);
                        endmsg();
                        if (chkscr(&cscore, k))
@@ -424,19 +473,19 @@ BOOLEAN           mycrib;
            else {
                if (!anymove(ph, pnum, sum)) {          /* can player move? */
                    if (!ugo && pnum) {                 /* go for player */
            else {
                if (!anymove(ph, pnum, sum)) {          /* can player move? */
                    if (!ugo && pnum) {                 /* go for player */
-                       msg("You have a GO.");
+                       msg("You have a GO");
                        ugo = TRUE;
                    }
                    if (anymove(ch, cnum, sum))         /* can computer play? */
                        myturn = !myturn;
                    else {
                        ugo = TRUE;
                    }
                    if (anymove(ch, cnum, sum))         /* can computer play? */
                        myturn = !myturn;
                    else {
-                       msg(quiet ? "I get one." : "I get one point.");
+                       msg(quiet ? "I get one" : "I get one point");
+                       do_wait();
                        if (chkscr(&cscore, 1))
                            return TRUE;
                        sum = 0;
                        mego = ugo = FALSE;
                        Tcnt = 0;
                        if (chkscr(&cscore, 1))
                            return TRUE;
                        sum = 0;
                        mego = ugo = FALSE;
                        Tcnt = 0;
-                       Hasread = FALSE;
                    }
                }
                else {                                  /* player plays */
                    }
                }
                else {                                  /* player plays */
@@ -447,13 +496,12 @@ BOOLEAN           mycrib;
                    }
                    else
                        for (;;) {
                    }
                    else
                        for (;;) {
-                           msg("Your play: ");
-                           prhand(ph, pnum, Playwin);
+                           prhand(ph, pnum, Playwin, FALSE);
                            crd = ph[infrom(ph, pnum, "Your play: ")];
                            if (sum + VAL(crd.rank) <= 31)
                                break;
                            else
                            crd = ph[infrom(ph, pnum, "Your play: ")];
                            if (sum + VAL(crd.rank) <= 31)
                                break;
                            else
-                               msg("Total > 31 -- try again.");
+                               msg("Total > 31 -- try again");
                        }
                    makeknown(&crd, 1);
                    remove(crd, ph, pnum--);
                        }
                    makeknown(&crd, 1);
                    remove(crd, ph, pnum--);
@@ -469,18 +517,23 @@ BOOLEAN           mycrib;
                }
            }
            if (sum >= 31) {
                }
            }
            if (sum >= 31) {
+               if (!myturn)
+                   do_wait();
                sum = 0;
                mego = ugo = FALSE;
                Tcnt = 0;
                last = FALSE;                           /* disable last flag */
                sum = 0;
                mego = ugo = FALSE;
                Tcnt = 0;
                last = FALSE;                           /* disable last flag */
-               Hasread = FALSE;
            }
            if (!pnum && !cnum)
                break;                                  /* both done */
        }
            }
            if (!pnum && !cnum)
                break;                                  /* both done */
        }
+       prhand(ph, pnum, Playwin, FALSE);
+       prhand(ch, cnum, Compwin, TRUE);
+       prtable(sum);
        if (last)
            if (played) {
                msg(quiet ? "I get one for last" : "I get one point for last");
        if (last)
            if (played) {
                msg(quiet ? "I get one for last" : "I get one point for last");
+               do_wait();
                if (chkscr(&cscore, 1))
                    return TRUE;
            }
                if (chkscr(&cscore, 1))
                    return TRUE;
            }
@@ -500,7 +553,7 @@ BOOLEAN             mycrib;
 prtable(score)
 int    score;
 {
 prtable(score)
 int    score;
 {
-       prhand(Table, Tcnt, Tablewin);
+       prhand(Table, Tcnt, Tablewin, FALSE);
        mvwprintw(Tablewin, (Tcnt + 2) * 2, Tcnt + 1, "%2d", score);
        wrefresh(Tablewin);
 }
        mvwprintw(Tablewin, (Tcnt + 2) * 2, Tcnt + 1, "%2d", score);
        wrefresh(Tablewin);
 }
@@ -518,6 +571,7 @@ BOOLEAN             mycrib;
                return TRUE;
            if (comphand(chand, "hand"))
                return TRUE;
                return TRUE;
            if (comphand(chand, "hand"))
                return TRUE;
+           do_wait();
            if (comphand(crib, "crib"))
                return TRUE;
        }
            if (comphand(crib, "crib"))
                return TRUE;
        }