X-Git-Url: https://git.cameronkatri.com/bsdgames-darwin.git/blobdiff_plain/e083ae85dc444e6d3aa2f73f144407c00a2acc91..ba1cf76ebd685f2689baa5790f261a8378a56aa3:/fish/fish.c diff --git a/fish/fish.c b/fish/fish.c index 05d73af7..f029e4dc 100644 --- a/fish/fish.c +++ b/fish/fish.c @@ -1,4 +1,4 @@ -/* $NetBSD: fish.c,v 1.11 1999/09/22 18:55:14 jsm Exp $ */ +/* $NetBSD: fish.c,v 1.20 2009/08/12 05:55:53 dholland Exp $ */ /*- * Copyright (c) 1990, 1993 @@ -15,11 +15,7 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. - * 4. Neither the name of the University nor the names of its contributors + * 3. 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. * @@ -38,15 +34,15 @@ #include #ifndef lint -__COPYRIGHT("@(#) Copyright (c) 1990, 1993\n\ - The Regents of the University of California. All rights reserved.\n"); +__COPYRIGHT("@(#) Copyright (c) 1990, 1993\ + The Regents of the University of California. All rights reserved."); #endif /* not lint */ #ifndef lint #if 0 static char sccsid[] = "@(#)fish.c 8.1 (Berkeley) 5/31/93"; #else -__RCSID("$NetBSD: fish.c,v 1.11 1999/09/22 18:55:14 jsm Exp $"); +__RCSID("$NetBSD: fish.c,v 1.20 2009/08/12 05:55:53 dholland Exp $"); #endif #endif /* not lint */ @@ -65,47 +61,46 @@ __RCSID("$NetBSD: fish.c,v 1.11 1999/09/22 18:55:14 jsm Exp $"); #define RANKS 13 #define HANDSIZE 7 #define CARDS 4 +#define TOTCARDS RANKS * CARDS #define USER 1 #define COMPUTER 0 #define OTHER(a) (1 - (a)) -const char *const cards[] = { +static const char *const cards[] = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", NULL, }; #define PRC(card) (void)printf(" %s", cards[card]) -int promode; -int asked[RANKS], comphand[RANKS], deck[RANKS]; -int userasked[RANKS], userhand[RANKS]; - -void chkwinner __P((int, const int *)); -int compmove __P((void)); -int countbooks __P((const int *)); -int countcards __P((const int *)); -int drawcard __P((int, int *)); -int gofish __P((int, int, int *)); -void goodmove __P((int, int, int *, int *)); -void init __P((void)); -void instructions __P((void)); -int main __P((int, char *[])); -int nrandom __P((int)); -void printhand __P((const int *)); -void printplayer __P((int)); -int promove __P((void)); -void usage __P((void)) __attribute__((__noreturn__)); -int usermove __P((void)); +static int promode; +static int asked[RANKS], comphand[RANKS], deck[TOTCARDS]; +static int userasked[RANKS], userhand[RANKS]; +static int curcard = TOTCARDS; + +static void chkwinner(int, const int *); +static int compmove(void); +static int countbooks(const int *); +static int countcards(const int *); +static int drawcard(int, int *); +static int gofish(int, int, int *); +static void goodmove(int, int, int *, int *); +static void init(void); +static void instructions(void); +static int nrandom(int); +static void printhand(const int *); +static void printplayer(int); +static int promove(void); +static void usage(void) __dead; +static int usermove(void); int -main(argc, argv) - int argc; - char **argv; +main(int argc, char **argv) { int ch, move; /* Revoke setgid privileges */ - setregid(getgid(), getgid()); + setgid(getgid()); while ((ch = getopt(argc, argv, "p")) != -1) switch(ch) { @@ -151,8 +146,8 @@ istart: for (;;) { /* NOTREACHED */ } -int -usermove() +static int +usermove(void) { int n; const char *const *p; @@ -170,7 +165,7 @@ usermove() continue; if (buf[0] == '\n') { (void)printf("%d cards in my hand, %d in the pool.\n", - countcards(comphand), countcards(deck)); + countcards(comphand), curcard); (void)printf("My books:"); (void)countbooks(comphand); continue; @@ -206,8 +201,8 @@ usermove() /* NOTREACHED */ } -int -compmove() +static int +compmove(void) { static int lmove; @@ -224,8 +219,8 @@ compmove() return(lmove); } -int -promove() +static int +promove(void) { int i, max; @@ -263,16 +258,12 @@ promove() /* NOTREACHED */ } -int -drawcard(player, hand) - int player; - int *hand; +static int +drawcard(int player, int *hand) { int card; - while (deck[card = nrandom(RANKS)] == 0); - ++hand[card]; - --deck[card]; + ++hand[card = deck[--curcard]]; if (player == USER || hand[card] == CARDS) { printplayer(player); (void)printf("drew %s", cards[card]); @@ -286,10 +277,8 @@ drawcard(player, hand) return(card); } -int -gofish(askedfor, player, hand) - int askedfor, player; - int *hand; +static int +gofish(int askedfor, int player, int *hand) { printplayer(OTHER(player)); (void)printf("say \"GO FISH!\"\n"); @@ -303,10 +292,8 @@ gofish(askedfor, player, hand) return(0); } -void -goodmove(player, move, hand, opphand) - int player, move; - int *hand, *opphand; +static void +goodmove(int player, int move, int *hand, int *opphand) { printplayer(OTHER(player)); (void)printf("have %d %s%s.\n", @@ -327,10 +314,8 @@ goodmove(player, move, hand, opphand) (void)printf("get another guess!\n"); } -void -chkwinner(player, hand) - int player; - const int *hand; +static void +chkwinner(int player, const int *hand) { int cb, i, ub; @@ -357,9 +342,8 @@ chkwinner(player, hand) exit(0); } -void -printplayer(player) - int player; +static void +printplayer(int player) { switch (player) { case COMPUTER: @@ -371,9 +355,8 @@ printplayer(player) } } -void -printhand(hand) - const int *hand; +static void +printhand(const int *hand) { int book, i, j; @@ -392,9 +375,8 @@ printhand(hand) (void)putchar('\n'); } -int -countcards(hand) - const int *hand; +static int +countcards(const int *hand) { int i, count; @@ -403,9 +385,8 @@ countcards(hand) return(count); } -int -countbooks(hand) - const int *hand; +static int +countbooks(const int *hand) { int i, count; @@ -420,35 +401,36 @@ countbooks(hand) return(count); } -void -init() +static void +init(void) { - int i, rank; + int i, j, temp; - for (i = 0; i < RANKS; ++i) - deck[i] = CARDS; - for (i = 0; i < HANDSIZE; ++i) { - while (!deck[rank = nrandom(RANKS)]); - ++userhand[rank]; - --deck[rank]; + for (i = 0; i < TOTCARDS; ++i) + deck[i] = i % RANKS; + for (i = 0; i < TOTCARDS - 1; ++i) { + j = nrandom(TOTCARDS-i); + if (j == 0) + continue; + temp = deck[i]; + deck[i] = deck[i+j]; + deck[i+j] = temp; } for (i = 0; i < HANDSIZE; ++i) { - while (!deck[rank = nrandom(RANKS)]); - ++comphand[rank]; - --deck[rank]; + ++userhand[deck[--curcard]]; + ++comphand[deck[--curcard]]; } } -int -nrandom(n) - int n; +static int +nrandom(int n) { return((int)random() % n); } -void -instructions() +static void +instructions(void) { int input; pid_t pid; @@ -474,7 +456,7 @@ instructions() err(1, "open %s", _PATH_INSTR); if (dup2(fd, 0) == -1) err(1, "dup2"); - (void)execl("/bin/sh", "sh", "-c", pager, NULL); + (void)execl("/bin/sh", "sh", "-c", pager, (char *) NULL); err(1, "exec sh -c %s", pager); /*NOTREACHED*/ case -1: @@ -488,8 +470,8 @@ instructions() while ((input = getchar()) != EOF && input != '\n'); } -void -usage() +static void +usage(void) { (void)fprintf(stderr, "usage: fish [-p]\n"); exit(1);