Home | History | Annotate | Line # | Download | only in fish
fish.c revision 1.3
      1  1.3  cgd /*	$NetBSD: fish.c,v 1.3 1995/03/23 08:28:18 cgd Exp $	*/
      2  1.3  cgd 
      3  1.1  cgd /*-
      4  1.3  cgd  * Copyright (c) 1990, 1993
      5  1.3  cgd  *	The Regents of the University of California.  All rights reserved.
      6  1.1  cgd  *
      7  1.1  cgd  * This code is derived from software contributed to Berkeley by
      8  1.1  cgd  * Muffy Barkocy.
      9  1.1  cgd  *
     10  1.1  cgd  * Redistribution and use in source and binary forms, with or without
     11  1.1  cgd  * modification, are permitted provided that the following conditions
     12  1.1  cgd  * are met:
     13  1.1  cgd  * 1. Redistributions of source code must retain the above copyright
     14  1.1  cgd  *    notice, this list of conditions and the following disclaimer.
     15  1.1  cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  cgd  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  cgd  *    documentation and/or other materials provided with the distribution.
     18  1.1  cgd  * 3. All advertising materials mentioning features or use of this software
     19  1.1  cgd  *    must display the following acknowledgement:
     20  1.1  cgd  *	This product includes software developed by the University of
     21  1.1  cgd  *	California, Berkeley and its contributors.
     22  1.1  cgd  * 4. Neither the name of the University nor the names of its contributors
     23  1.1  cgd  *    may be used to endorse or promote products derived from this software
     24  1.1  cgd  *    without specific prior written permission.
     25  1.1  cgd  *
     26  1.1  cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  1.1  cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  1.1  cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  1.1  cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  1.1  cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  1.1  cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  1.1  cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  1.1  cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  1.1  cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  1.1  cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  1.1  cgd  * SUCH DAMAGE.
     37  1.1  cgd  */
     38  1.1  cgd 
     39  1.1  cgd #ifndef lint
     40  1.3  cgd static char copyright[] =
     41  1.3  cgd "@(#) Copyright (c) 1990, 1993\n\
     42  1.3  cgd 	The Regents of the University of California.  All rights reserved.\n";
     43  1.1  cgd #endif /* not lint */
     44  1.1  cgd 
     45  1.1  cgd #ifndef lint
     46  1.3  cgd #if 0
     47  1.3  cgd static char sccsid[] = "@(#)fish.c	8.1 (Berkeley) 5/31/93";
     48  1.3  cgd #else
     49  1.3  cgd static char rcsid[] = "$NetBSD: fish.c,v 1.3 1995/03/23 08:28:18 cgd Exp $";
     50  1.3  cgd #endif
     51  1.1  cgd #endif /* not lint */
     52  1.1  cgd 
     53  1.1  cgd #include <sys/types.h>
     54  1.1  cgd #include <sys/errno.h>
     55  1.1  cgd #include <fcntl.h>
     56  1.1  cgd #include <stdio.h>
     57  1.1  cgd #include <stdlib.h>
     58  1.1  cgd #include <string.h>
     59  1.1  cgd #include "pathnames.h"
     60  1.1  cgd 
     61  1.1  cgd #define	RANKS		13
     62  1.1  cgd #define	HANDSIZE	7
     63  1.1  cgd #define	CARDS		4
     64  1.1  cgd 
     65  1.1  cgd #define	USER		1
     66  1.1  cgd #define	COMPUTER	0
     67  1.1  cgd #define	OTHER(a)	(1 - (a))
     68  1.1  cgd 
     69  1.1  cgd char *cards[] = {
     70  1.1  cgd 	"A", "2", "3", "4", "5", "6", "7",
     71  1.1  cgd 	"8", "9", "10", "J", "Q", "K", NULL,
     72  1.1  cgd };
     73  1.1  cgd #define	PRC(card)	(void)printf(" %s", cards[card])
     74  1.1  cgd 
     75  1.1  cgd int promode;
     76  1.1  cgd int asked[RANKS], comphand[RANKS], deck[RANKS];
     77  1.1  cgd int userasked[RANKS], userhand[RANKS];
     78  1.1  cgd 
     79  1.1  cgd main(argc, argv)
     80  1.1  cgd 	int argc;
     81  1.1  cgd 	char **argv;
     82  1.1  cgd {
     83  1.1  cgd 	int ch, move;
     84  1.1  cgd 
     85  1.1  cgd 	while ((ch = getopt(argc, argv, "p")) != EOF)
     86  1.1  cgd 		switch(ch) {
     87  1.1  cgd 		case 'p':
     88  1.1  cgd 			promode = 1;
     89  1.1  cgd 			break;
     90  1.1  cgd 		case '?':
     91  1.1  cgd 		default:
     92  1.1  cgd 			(void)fprintf(stderr, "usage: fish [-p]\n");
     93  1.1  cgd 			exit(1);
     94  1.1  cgd 		}
     95  1.1  cgd 
     96  1.1  cgd 	srandom(time((time_t *)NULL));
     97  1.1  cgd 	instructions();
     98  1.1  cgd 	init();
     99  1.1  cgd 
    100  1.1  cgd 	if (nrandom(2) == 1) {
    101  1.1  cgd 		printplayer(COMPUTER);
    102  1.1  cgd 		(void)printf("get to start.\n");
    103  1.1  cgd 		goto istart;
    104  1.1  cgd 	}
    105  1.1  cgd 	printplayer(USER);
    106  1.1  cgd 	(void)printf("get to start.\n");
    107  1.1  cgd 
    108  1.1  cgd 	for (;;) {
    109  1.1  cgd 		move = usermove();
    110  1.1  cgd 		if (!comphand[move]) {
    111  1.1  cgd 			if (gofish(move, USER, userhand))
    112  1.1  cgd 				continue;
    113  1.1  cgd 		} else {
    114  1.1  cgd 			goodmove(USER, move, userhand, comphand);
    115  1.1  cgd 			continue;
    116  1.1  cgd 		}
    117  1.1  cgd 
    118  1.1  cgd istart:		for (;;) {
    119  1.1  cgd 			move = compmove();
    120  1.1  cgd 			if (!userhand[move]) {
    121  1.1  cgd 				if (!gofish(move, COMPUTER, comphand))
    122  1.1  cgd 					break;
    123  1.1  cgd 			} else
    124  1.1  cgd 				goodmove(COMPUTER, move, comphand, userhand);
    125  1.1  cgd 		}
    126  1.1  cgd 	}
    127  1.1  cgd 	/* NOTREACHED */
    128  1.1  cgd }
    129  1.1  cgd 
    130  1.1  cgd usermove()
    131  1.1  cgd {
    132  1.1  cgd 	register int n;
    133  1.1  cgd 	register char **p;
    134  1.1  cgd 	char buf[256];
    135  1.1  cgd 
    136  1.1  cgd 	(void)printf("\nYour hand is:");
    137  1.1  cgd 	printhand(userhand);
    138  1.1  cgd 
    139  1.1  cgd 	for (;;) {
    140  1.1  cgd 		(void)printf("You ask me for: ");
    141  1.1  cgd 		(void)fflush(stdout);
    142  1.1  cgd 		if (fgets(buf, BUFSIZ, stdin) == NULL)
    143  1.1  cgd 			exit(0);
    144  1.1  cgd 		if (buf[0] == '\0')
    145  1.1  cgd 			continue;
    146  1.1  cgd 		if (buf[0] == '\n') {
    147  1.1  cgd 			(void)printf("%d cards in my hand, %d in the pool.\n",
    148  1.1  cgd 			    countcards(comphand), countcards(deck));
    149  1.1  cgd 			(void)printf("My books:");
    150  1.1  cgd 			(void)countbooks(comphand);
    151  1.1  cgd 			continue;
    152  1.1  cgd 		}
    153  1.1  cgd 		buf[strlen(buf) - 1] = '\0';
    154  1.1  cgd 		if (!strcasecmp(buf, "p") && !promode) {
    155  1.1  cgd 			promode = 1;
    156  1.1  cgd 			(void)printf("Entering pro mode.\n");
    157  1.1  cgd 			continue;
    158  1.1  cgd 		}
    159  1.1  cgd 		if (!strcasecmp(buf, "quit"))
    160  1.1  cgd 			exit(0);
    161  1.1  cgd 		for (p = cards; *p; ++p)
    162  1.1  cgd 			if (!strcasecmp(*p, buf))
    163  1.1  cgd 				break;
    164  1.1  cgd 		if (!*p) {
    165  1.1  cgd 			(void)printf("I don't understand!\n");
    166  1.1  cgd 			continue;
    167  1.1  cgd 		}
    168  1.1  cgd 		n = p - cards;
    169  1.1  cgd 		if (userhand[n]) {
    170  1.1  cgd 			userasked[n] = 1;
    171  1.1  cgd 			return(n);
    172  1.1  cgd 		}
    173  1.1  cgd 		if (nrandom(3) == 1)
    174  1.1  cgd 			(void)printf("You don't have any of those!\n");
    175  1.1  cgd 		else
    176  1.1  cgd 			(void)printf("You don't have any %s's!\n", cards[n]);
    177  1.1  cgd 		if (nrandom(4) == 1)
    178  1.1  cgd 			(void)printf("No cheating!\n");
    179  1.1  cgd 		(void)printf("Guess again.\n");
    180  1.1  cgd 	}
    181  1.1  cgd 	/* NOTREACHED */
    182  1.1  cgd }
    183  1.1  cgd 
    184  1.1  cgd compmove()
    185  1.1  cgd {
    186  1.1  cgd 	static int lmove;
    187  1.1  cgd 
    188  1.1  cgd 	if (promode)
    189  1.1  cgd 		lmove = promove();
    190  1.1  cgd 	else {
    191  1.1  cgd 		do {
    192  1.1  cgd 			lmove = (lmove + 1) % RANKS;
    193  1.1  cgd 		} while (!comphand[lmove] || comphand[lmove] == CARDS);
    194  1.1  cgd 	}
    195  1.1  cgd 	asked[lmove] = 1;
    196  1.1  cgd 
    197  1.1  cgd 	(void)printf("I ask you for: %s.\n", cards[lmove]);
    198  1.1  cgd 	return(lmove);
    199  1.1  cgd }
    200  1.1  cgd 
    201  1.1  cgd promove()
    202  1.1  cgd {
    203  1.1  cgd 	register int i, max;
    204  1.1  cgd 
    205  1.1  cgd 	for (i = 0; i < RANKS; ++i)
    206  1.1  cgd 		if (userasked[i] &&
    207  1.1  cgd 		    comphand[i] > 0 && comphand[i] < CARDS) {
    208  1.1  cgd 			userasked[i] = 0;
    209  1.1  cgd 			return(i);
    210  1.1  cgd 		}
    211  1.1  cgd 	if (nrandom(3) == 1) {
    212  1.1  cgd 		for (i = 0;; ++i)
    213  1.1  cgd 			if (comphand[i] && comphand[i] != CARDS) {
    214  1.1  cgd 				max = i;
    215  1.1  cgd 				break;
    216  1.1  cgd 			}
    217  1.1  cgd 		while (++i < RANKS)
    218  1.1  cgd 			if (comphand[i] != CARDS &&
    219  1.1  cgd 			    comphand[i] > comphand[max])
    220  1.1  cgd 				max = i;
    221  1.1  cgd 		return(max);
    222  1.1  cgd 	}
    223  1.1  cgd 	if (nrandom(1024) == 0723) {
    224  1.1  cgd 		for (i = 0; i < RANKS; ++i)
    225  1.1  cgd 			if (userhand[i] && comphand[i])
    226  1.1  cgd 				return(i);
    227  1.1  cgd 	}
    228  1.1  cgd 	for (;;) {
    229  1.1  cgd 		for (i = 0; i < RANKS; ++i)
    230  1.1  cgd 			if (comphand[i] && comphand[i] != CARDS &&
    231  1.1  cgd 			    !asked[i])
    232  1.1  cgd 				return(i);
    233  1.1  cgd 		for (i = 0; i < RANKS; ++i)
    234  1.1  cgd 			asked[i] = 0;
    235  1.1  cgd 	}
    236  1.1  cgd 	/* NOTREACHED */
    237  1.1  cgd }
    238  1.1  cgd 
    239  1.1  cgd drawcard(player, hand)
    240  1.1  cgd 	int player;
    241  1.1  cgd 	int *hand;
    242  1.1  cgd {
    243  1.1  cgd 	int card;
    244  1.1  cgd 
    245  1.1  cgd 	while (deck[card = nrandom(RANKS)] == 0);
    246  1.1  cgd 	++hand[card];
    247  1.1  cgd 	--deck[card];
    248  1.1  cgd 	if (player == USER || hand[card] == CARDS) {
    249  1.1  cgd 		printplayer(player);
    250  1.1  cgd 		(void)printf("drew %s", cards[card]);
    251  1.1  cgd 		if (hand[card] == CARDS) {
    252  1.1  cgd 			(void)printf(" and made a book of %s's!\n",
    253  1.1  cgd 			     cards[card]);
    254  1.1  cgd 			chkwinner(player, hand);
    255  1.1  cgd 		} else
    256  1.1  cgd 			(void)printf(".\n");
    257  1.1  cgd 	}
    258  1.1  cgd 	return(card);
    259  1.1  cgd }
    260  1.1  cgd 
    261  1.1  cgd gofish(askedfor, player, hand)
    262  1.1  cgd 	int askedfor, player;
    263  1.1  cgd 	int *hand;
    264  1.1  cgd {
    265  1.1  cgd 	printplayer(OTHER(player));
    266  1.1  cgd 	(void)printf("say \"GO FISH!\"\n");
    267  1.1  cgd 	if (askedfor == drawcard(player, hand)) {
    268  1.1  cgd 		printplayer(player);
    269  1.1  cgd 		(void)printf("drew the guess!\n");
    270  1.1  cgd 		printplayer(player);
    271  1.1  cgd 		(void)printf("get to ask again!\n");
    272  1.1  cgd 		return(1);
    273  1.1  cgd 	}
    274  1.1  cgd 	return(0);
    275  1.1  cgd }
    276  1.1  cgd 
    277  1.1  cgd goodmove(player, move, hand, opphand)
    278  1.1  cgd 	int player, move;
    279  1.1  cgd 	int *hand, *opphand;
    280  1.1  cgd {
    281  1.1  cgd 	printplayer(OTHER(player));
    282  1.1  cgd 	(void)printf("have %d %s%s.\n",
    283  1.1  cgd 	    opphand[move], cards[move], opphand[move] == 1 ? "": "'s");
    284  1.1  cgd 
    285  1.1  cgd 	hand[move] += opphand[move];
    286  1.1  cgd 	opphand[move] = 0;
    287  1.1  cgd 
    288  1.1  cgd 	if (hand[move] == CARDS) {
    289  1.1  cgd 		printplayer(player);
    290  1.1  cgd 		(void)printf("made a book of %s's!\n", cards[move]);
    291  1.1  cgd 		chkwinner(player, hand);
    292  1.1  cgd 	}
    293  1.1  cgd 
    294  1.1  cgd 	chkwinner(OTHER(player), opphand);
    295  1.1  cgd 
    296  1.1  cgd 	printplayer(player);
    297  1.1  cgd 	(void)printf("get another guess!\n");
    298  1.1  cgd }
    299  1.1  cgd 
    300  1.1  cgd chkwinner(player, hand)
    301  1.1  cgd 	int player;
    302  1.1  cgd 	register int *hand;
    303  1.1  cgd {
    304  1.1  cgd 	register int cb, i, ub;
    305  1.1  cgd 
    306  1.1  cgd 	for (i = 0; i < RANKS; ++i)
    307  1.1  cgd 		if (hand[i] > 0 && hand[i] < CARDS)
    308  1.1  cgd 			return;
    309  1.1  cgd 	printplayer(player);
    310  1.1  cgd 	(void)printf("don't have any more cards!\n");
    311  1.1  cgd 	(void)printf("My books:");
    312  1.1  cgd 	cb = countbooks(comphand);
    313  1.1  cgd 	(void)printf("Your books:");
    314  1.1  cgd 	ub = countbooks(userhand);
    315  1.1  cgd 	(void)printf("\nI have %d, you have %d.\n", cb, ub);
    316  1.1  cgd 	if (ub > cb) {
    317  1.1  cgd 		(void)printf("\nYou win!!!\n");
    318  1.1  cgd 		if (nrandom(1024) == 0723)
    319  1.1  cgd 			(void)printf("Cheater, cheater, pumpkin eater!\n");
    320  1.1  cgd 	} else if (cb > ub) {
    321  1.1  cgd 		(void)printf("\nI win!!!\n");
    322  1.1  cgd 		if (nrandom(1024) == 0723)
    323  1.1  cgd 			(void)printf("Hah!  Stupid peasant!\n");
    324  1.1  cgd 	} else
    325  1.1  cgd 		(void)printf("\nTie!\n");
    326  1.1  cgd 	exit(0);
    327  1.1  cgd }
    328  1.1  cgd 
    329  1.1  cgd printplayer(player)
    330  1.1  cgd 	int player;
    331  1.1  cgd {
    332  1.1  cgd 	switch (player) {
    333  1.1  cgd 	case COMPUTER:
    334  1.1  cgd 		(void)printf("I ");
    335  1.1  cgd 		break;
    336  1.1  cgd 	case USER:
    337  1.1  cgd 		(void)printf("You ");
    338  1.1  cgd 		break;
    339  1.1  cgd 	}
    340  1.1  cgd }
    341  1.1  cgd 
    342  1.1  cgd printhand(hand)
    343  1.1  cgd 	int *hand;
    344  1.1  cgd {
    345  1.1  cgd 	register int book, i, j;
    346  1.1  cgd 
    347  1.1  cgd 	for (book = i = 0; i < RANKS; i++)
    348  1.1  cgd 		if (hand[i] < CARDS)
    349  1.1  cgd 			for (j = hand[i]; --j >= 0;)
    350  1.1  cgd 				PRC(i);
    351  1.1  cgd 		else
    352  1.1  cgd 			++book;
    353  1.1  cgd 	if (book) {
    354  1.1  cgd 		(void)printf(" + Book%s of", book > 1 ? "s" : "");
    355  1.1  cgd 		for (i = 0; i < RANKS; i++)
    356  1.1  cgd 			if (hand[i] == CARDS)
    357  1.1  cgd 				PRC(i);
    358  1.1  cgd 	}
    359  1.1  cgd 	(void)putchar('\n');
    360  1.1  cgd }
    361  1.1  cgd 
    362  1.1  cgd countcards(hand)
    363  1.1  cgd 	register int *hand;
    364  1.1  cgd {
    365  1.1  cgd 	register int i, count;
    366  1.1  cgd 
    367  1.1  cgd 	for (count = i = 0; i < RANKS; i++)
    368  1.1  cgd 		count += *hand++;
    369  1.1  cgd 	return(count);
    370  1.1  cgd }
    371  1.1  cgd 
    372  1.1  cgd countbooks(hand)
    373  1.1  cgd 	int *hand;
    374  1.1  cgd {
    375  1.1  cgd 	int i, count;
    376  1.1  cgd 
    377  1.1  cgd 	for (count = i = 0; i < RANKS; i++)
    378  1.1  cgd 		if (hand[i] == CARDS) {
    379  1.1  cgd 			++count;
    380  1.1  cgd 			PRC(i);
    381  1.1  cgd 		}
    382  1.1  cgd 	if (!count)
    383  1.1  cgd 		(void)printf(" none");
    384  1.1  cgd 	(void)putchar('\n');
    385  1.1  cgd 	return(count);
    386  1.1  cgd }
    387  1.1  cgd 
    388  1.1  cgd init()
    389  1.1  cgd {
    390  1.1  cgd 	register int i, rank;
    391  1.1  cgd 
    392  1.1  cgd 	for (i = 0; i < RANKS; ++i)
    393  1.1  cgd 		deck[i] = CARDS;
    394  1.1  cgd 	for (i = 0; i < HANDSIZE; ++i) {
    395  1.1  cgd 		while (!deck[rank = nrandom(RANKS)]);
    396  1.1  cgd 		++userhand[rank];
    397  1.1  cgd 		--deck[rank];
    398  1.1  cgd 	}
    399  1.1  cgd 	for (i = 0; i < HANDSIZE; ++i) {
    400  1.1  cgd 		while (!deck[rank = nrandom(RANKS)]);
    401  1.1  cgd 		++comphand[rank];
    402  1.1  cgd 		--deck[rank];
    403  1.1  cgd 	}
    404  1.1  cgd }
    405  1.1  cgd 
    406  1.1  cgd nrandom(n)
    407  1.1  cgd 	int n;
    408  1.1  cgd {
    409  1.1  cgd 	long random();
    410  1.1  cgd 
    411  1.1  cgd 	return((int)random() % n);
    412  1.1  cgd }
    413  1.1  cgd 
    414  1.1  cgd instructions()
    415  1.1  cgd {
    416  1.1  cgd 	int input;
    417  1.1  cgd 	char buf[1024];
    418  1.1  cgd 
    419  1.1  cgd 	(void)printf("Would you like instructions (y or n)? ");
    420  1.1  cgd 	input = getchar();
    421  1.1  cgd 	while (getchar() != '\n');
    422  1.1  cgd 	if (input != 'y')
    423  1.1  cgd 		return;
    424  1.1  cgd 
    425  1.1  cgd 	(void)sprintf(buf, "%s %s", _PATH_MORE, _PATH_INSTR);
    426  1.1  cgd 	(void)system(buf);
    427  1.1  cgd 	(void)printf("Hit return to continue...\n");
    428  1.1  cgd 	while ((input = getchar()) != EOF && input != '\n');
    429  1.1  cgd }
    430  1.1  cgd 
    431  1.1  cgd usage()
    432  1.1  cgd {
    433  1.1  cgd 	(void)fprintf(stderr, "usage: fish [-p]\n");
    434  1.1  cgd 	exit(1);
    435  1.1  cgd }
    436