BSD 4_3_Reno release
[unix-history] / usr / src / games / cribbage / crib.c
index 7d992d8..7c73764 100644 (file)
+/*
+ * Copyright (c) 1980 Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted
+ * provided that: (1) source distributions retain this entire copyright
+ * notice and comment, and (2) distributions including binaries display
+ * the following acknowledgement:  ``This product includes software
+ * developed by the University of California, Berkeley and its contributors''
+ * in the documentation or other materials provided with the distribution
+ * and in all advertising materials mentioning features or use of this
+ * software. Neither the name of the University nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ */
+
+#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.5 (Berkeley) 6/1/90";
+#endif /* not lint */
+
+# include      <sys/signal.h>
 # include      <curses.h>
 # include      <curses.h>
-# include      <signal.h>
 # include      "deck.h"
 # include      "cribbage.h"
 # include      "cribcur.h"
 # include      "deck.h"
 # include      "cribbage.h"
 # include      "cribcur.h"
-
-
-# define       LOGFILE         "/usr/games/lib/criblog"
-# define       INSTRCMD        "ul /usr/games/lib/crib.instr | more -f"
-
+# include      "pathnames.h"
 
 main(argc, argv)
 int    argc;
 char   *argv[];
 {
 
 main(argc, argv)
 int    argc;
 char   *argv[];
 {
+       extern char *optarg;
+       extern int optind;
        register  char          *p;
        register  char          *p;
+       int ch;
        BOOLEAN                 playing;
        char                    *s;             /* for reading arguments */
        BOOLEAN                 playing;
        char                    *s;             /* for reading arguments */
-       char                    bust;           /* flag for arg reader */
        FILE                    *f;
        FILE                    *fopen();
        FILE                    *f;
        FILE                    *fopen();
-       char                    *getline();
-       int                     bye();
+       char                    *getline(), *getlogin();
+       void                    rint();
 
 
-       while (--argc > 0) {
-           if ((*++argv)[0] != '-') {
-               fprintf(stderr, "\n\ncribbage: usage is 'cribbage [-eqr]'\n");
-               exit(1);
-           }
-           bust = FALSE;
-           for (s = argv[0] + 1; *s != NULL; s++) {
-               switch (*s) {
-                   case 'e':
+       while ((ch = getopt(argc, argv, "eqr")) != EOF)
+               switch(ch) {
+               case 'e':
                        explain = TRUE;
                        break;
                        explain = TRUE;
                        break;
-                   case 'q':
+               case 'q':
                        quiet = TRUE;
                        break;
                        quiet = TRUE;
                        break;
-                   case 'r':
+               case 'r':
                        rflag = TRUE;
                        break;
                        rflag = TRUE;
                        break;
-                   default:
-                       fprintf(stderr, "\n\ncribbage: usage is 'cribbage [-eqr]'\n");
-                       exit(2);
-                       break;
+               case '?':
+               default:
+                       (void) fprintf(stderr, "usage: cribbage [-eqr]\n");
+                       exit(1);
                }
                }
-               if (bust)
-                   break;
-           }
-       }
 
        initscr();
 
        initscr();
-       signal(SIGINT, bye);
+       signal(SIGINT, rint);
        crmode();
        noecho();
        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);
        crmode();
        noecho();
        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') {
-               system(INSTRCMD);
+               endwin();
+               clear();
+               mvcur(0, COLS - 1, LINES - 1, 0);
+               fflush(stdout);
+               instructions();
+               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');
        } while (playing);
 
            game();
            msg("Another game? ");
            playing = (getuchar() == 'Y');
        } while (playing);
 
-       if ((f = fopen(LOGFILE, "a")) != NULL) {
-           fprintf(f, "Won %5.5d, Lost %5.5d\n",  cgames, pgames);
-           fclose(f);
+       if (f = fopen(_PATH_LOG, "a")) {
+               (void)fprintf(f, "%s: won %5.5d, lost %5.5d\n",
+                  getlogin(), cgames, pgames);
+               (void)fclose(f);
        }
        }
-
        bye();
        bye();
-}
-
-/*
- * bye:
- *     Leave the program, cleaning things up as we go.
- */
-bye()
-{
-       signal(SIGINT, SIG_IGN);
-       mvcur(0, COLS - 1, LINES - 1, 0);
-       fflush(stdout);
-       endwin();
-       putchar('\n');
-       exit(1);
+       if (!f) {
+               (void)fprintf(stderr, "\ncribbage: can't open %s.\n",
+                   _PATH_LOG);
+               exit(1);
+       }
+       exit(0);
 }
 
 /*
 }
 
 /*
@@ -105,17 +127,30 @@ bye()
  */
 makeboard()
 {
  */
 makeboard()
 {
-    extern int Lastscore[];
-
     mvaddstr(SCORE_Y + 0, SCORE_X, "+---------------------------------------+");
     mvaddstr(SCORE_Y + 0, SCORE_X, "+---------------------------------------+");
-    mvaddstr(SCORE_Y + 1, 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 + 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 + 7, SCORE_X, "|  Score:   0      ME                   |");
     mvaddstr(SCORE_Y + 8, SCORE_X, "+---------------------------------------+");
     mvaddstr(SCORE_Y + 8, SCORE_X, "+---------------------------------------+");
+    gamescore();
+}
+
+/*
+ * gamescore:
+ *     Print out the current game score
+ */
+gamescore()
+{
+    extern int Lastscore[];
+
+    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;
 }
     Lastscore[0] = -1;
     Lastscore[1] = -1;
 }
@@ -131,6 +166,8 @@ game()
        BOOLEAN                 flag;
        BOOLEAN                 compcrib;
 
        BOOLEAN                 flag;
        BOOLEAN                 compcrib;
 
+       makeboard();
+       refresh();
        makedeck(deck);
        shuffle(deck);
        if (gamecount == 0) {
        makedeck(deck);
        shuffle(deck);
        if (gamecount == 0) {
@@ -163,7 +200,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 +214,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 +246,7 @@ game()
            }
            iwon = TRUE;
        }
            }
            iwon = TRUE;
        }
-       msg("I have won %d games, you have won %d", cgames, pgames);
+       gamescore();
 }
 
 /*
 }
 
 /*
@@ -220,17 +260,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;
@@ -280,17 +316,15 @@ BOOLEAN           mycrib;
 
        prcrib(mycrib, TRUE);
        prompt = (quiet ? "Discard --> " : "Discard a card --> ");
 
        prcrib(mycrib, TRUE);
        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];
@@ -321,7 +355,7 @@ int         pos;
            msgcard(turnover, FALSE);
            endmsg();
            if (turnover.rank == JACK) {
            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 );
            }
        }
@@ -332,7 +366,7 @@ 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);
            }
        }
@@ -348,7 +382,7 @@ int         pos;
 prcrib(mycrib, blank)
 BOOLEAN                mycrib, blank;
 {
 prcrib(mycrib, blank)
 BOOLEAN                mycrib, blank;
 {
-       register int    cardx;
+       register int    y, cardx;
 
        if (mycrib)
            cardx = CRIB_X;
 
        if (mycrib)
            cardx = CRIB_X;
@@ -357,6 +391,14 @@ BOOLEAN            mycrib, blank;
 
        mvaddstr(CRIB_Y, cardx + 1, "CRIB");
        prcard(stdscr, CRIB_Y + 1, cardx, turnover, blank);
 
        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, "       ");
 }
 
 /*
 }
 
 /*
@@ -374,9 +416,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 */
@@ -389,24 +431,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 {
@@ -427,7 +469,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))
@@ -439,19 +482,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 */
@@ -462,13 +505,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--);
@@ -484,18 +526,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;
            }
@@ -515,7 +562,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);
 }
@@ -533,6 +580,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;
        }