BSD 4_3_Reno release
[unix-history] / usr / src / games / cribbage / crib.c
index 4a6834f..7c73764 100644 (file)
@@ -1,62 +1,79 @@
+/*
+ * 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();
        crmode();
        noecho();
-       Playwin = subwin(stdscr, PLAY_Y, PLAY_X, 0, X_SCORE_SZ);
-       Tablewin = subwin(stdscr, TABLE_Y, TABLE_X, 0, PLAY_X + X_SCORE_SZ);
-       Compwin = subwin(stdscr, COMP_Y, COMP_X, 0, TABLE_X + PLAY_X + X_SCORE_SZ);
-       Msgwin = subwin(stdscr, MSG_Y, MSG_X, Y_SCORE_SZ + 1, 0);
+       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);
@@ -66,8 +83,10 @@ char *argv[];
            msg("Do you need instructions for cribbage? ");
            if (getuchar() == 'Y') {
                endwin();
            msg("Do you need instructions for cribbage? ");
            if (getuchar() == 'Y') {
                endwin();
+               clear();
+               mvcur(0, COLS - 1, LINES - 1, 0);
                fflush(stdout);
                fflush(stdout);
-               system(INSTRCMD);
+               instructions();
                crmode();
                noecho();
                clear();
                crmode();
                noecho();
                clear();
@@ -77,6 +96,7 @@ char  *argv[];
        }
        playing = TRUE;
        do {
        }
        playing = TRUE;
        do {
+           wclrtobot(Msgwin);
            msg(quiet ? "L or S? " : "Long (to 121) or Short (to 61)? ");
            if (glimit == SGAME)
                glimit = (getuchar() == 'L' ? LGAME : SGAME);
            msg(quiet ? "L or S? " : "Long (to 121) or Short (to 61)? ");
            if (glimit == SGAME)
                glimit = (getuchar() == 'L' ? LGAME : SGAME);
@@ -87,26 +107,18 @@ char       *argv[];
            playing = (getuchar() == 'Y');
        } while (playing);
 
            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);
 }
 
 /*
 }
 
 /*
@@ -192,7 +204,7 @@ game()
            wrefresh(Tablewin);
            werase(Compwin);
            wrefresh(Compwin);
            wrefresh(Tablewin);
            werase(Compwin);
            wrefresh(Compwin);
-           msg("Loser (%s) gets first crib.",  (iwon ? "you" : "me"));
+           msg("Loser (%s) gets first crib",  (iwon ? "you" : "me"));
            compcrib = !iwon;
        }
 
            compcrib = !iwon;
        }
 
@@ -343,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 );
            }
        }
@@ -354,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);
            }
        }
@@ -375,13 +387,13 @@ BOOLEAN           mycrib, blank;
        if (mycrib)
            cardx = CRIB_X;
        else
        if (mycrib)
            cardx = CRIB_X;
        else
-           cardx = X_SCORE_SZ;
+           cardx = 0;
 
        mvaddstr(CRIB_Y, cardx + 1, "CRIB");
        prcard(stdscr, CRIB_Y + 1, cardx, turnover, blank);
 
        if (mycrib)
 
        mvaddstr(CRIB_Y, cardx + 1, "CRIB");
        prcard(stdscr, CRIB_Y + 1, cardx, turnover, blank);
 
        if (mycrib)
-           cardx = X_SCORE_SZ;
+           cardx = 0;
        else
            cardx = CRIB_X;
 
        else
            cardx = CRIB_X;
 
@@ -425,13 +437,13 @@ BOOLEAN           mycrib;
            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;
                        if (chkscr(&pscore, 1))
                            return TRUE;
                        sum = 0;
@@ -470,13 +482,14 @@ 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;
                        if (chkscr(&cscore, 1))
                            return TRUE;
                        sum = 0;
@@ -497,7 +510,7 @@ BOOLEAN             mycrib;
                            if (sum + VAL(crd.rank) <= 31)
                                break;
                            else
                            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--);
@@ -513,6 +526,8 @@ BOOLEAN             mycrib;
                }
            }
            if (sum >= 31) {
                }
            }
            if (sum >= 31) {
+               if (!myturn)
+                   do_wait();
                sum = 0;
                mego = ugo = FALSE;
                Tcnt = 0;
                sum = 0;
                mego = ugo = FALSE;
                Tcnt = 0;
@@ -527,6 +542,7 @@ BOOLEAN             mycrib;
        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;
            }
@@ -564,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;
        }