2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 static const char copyright[] =
39 "@(#) Copyright (c) 1990, 1993\n\
40 The Regents of the University of California. All rights reserved.\n";
45 static char sccsid[] = "@(#)fish.c 8.1 (Berkeley) 5/31/93";
47 static const char rcsid[] =
51 #include <sys/types.h>
52 #include <sys/errno.h>
58 #include "pathnames.h"
66 #define OTHER(a) (1 - (a))
69 "A", "2", "3", "4", "5", "6", "7",
70 "8", "9", "10", "J", "Q", "K", NULL,
72 #define PRC(card) (void)printf(" %s", cards[card])
75 int asked[RANKS], comphand[RANKS], deck[RANKS];
76 int userasked[RANKS], userhand[RANKS];
78 void chkwinner __P((int player, int *hand));
79 int compmove __P((void));
80 int countbooks __P((int *hand));
81 int countcards __P((int *hand));
82 int drawcard __P((int player, int *hand));
83 int gofish __P((int askedfor, int player, int *hand));
84 void goodmove __P((int player, int move, int *hand, int *opphand));
85 void init __P((void));
86 void instructions __P((void));
87 int nrandom __P((int n));
88 void printhand __P((int *hand));
89 void printplayer __P((int player));
90 int promove __P((void));
91 void usage __P((void));
92 int usermove __P((void));
101 while ((ch = getopt(argc, argv, "p")) != -1)
108 (void)fprintf(stderr, "usage: fish [-p]\n");
116 if (nrandom(2) == 1) {
117 printplayer(COMPUTER);
118 (void)printf("get to start.\n");
122 (void)printf("get to start.\n");
126 if (!comphand[move]) {
127 if (gofish(move, USER, userhand))
130 goodmove(USER, move, userhand, comphand);
136 if (!userhand[move]) {
137 if (!gofish(move, COMPUTER, comphand))
140 goodmove(COMPUTER, move, comphand, userhand);
144 return(EXIT_FAILURE);
154 (void)printf("\nYour hand is:");
158 (void)printf("You ask me for: ");
159 (void)fflush(stdout);
160 if (fgets(buf, sizeof(buf), stdin) == NULL)
164 if (buf[0] == '\n') {
165 (void)printf("%d cards in my hand, %d in the pool.\n",
166 countcards(comphand), countcards(deck));
167 (void)printf("My books:");
168 (void)countbooks(comphand);
171 buf[strlen(buf) - 1] = '\0';
172 if (!strcasecmp(buf, "p") && !promode) {
174 (void)printf("Entering pro mode.\n");
177 if (!strcasecmp(buf, "quit"))
179 for (p = cards; *p; ++p)
180 if (!strcasecmp(*p, buf))
183 (void)printf("I don't understand!\n");
192 (void)printf("You don't have any of those!\n");
194 (void)printf("You don't have any %s's!\n", cards[n]);
196 (void)printf("No cheating!\n");
197 (void)printf("Guess again.\n");
211 lmove = (lmove + 1) % RANKS;
212 } while (!comphand[lmove] || comphand[lmove] == CARDS);
216 (void)printf("I ask you for: %s.\n", cards[lmove]);
225 for (i = 0; i < RANKS; ++i)
227 comphand[i] > 0 && comphand[i] < CARDS) {
231 if (nrandom(3) == 1) {
233 if (comphand[i] && comphand[i] != CARDS) {
238 if (comphand[i] != CARDS &&
239 comphand[i] > comphand[max])
243 if (nrandom(1024) == 0723) {
244 for (i = 0; i < RANKS; ++i)
245 if (userhand[i] && comphand[i])
249 for (i = 0; i < RANKS; ++i)
250 if (comphand[i] && comphand[i] != CARDS &&
253 for (i = 0; i < RANKS; ++i)
260 drawcard(player, hand)
266 while (deck[card = nrandom(RANKS)] == 0);
269 if (player == USER || hand[card] == CARDS) {
271 (void)printf("drew %s", cards[card]);
272 if (hand[card] == CARDS) {
273 (void)printf(" and made a book of %s's!\n",
275 chkwinner(player, hand);
283 gofish(askedfor, player, hand)
284 int askedfor, player;
287 printplayer(OTHER(player));
288 (void)printf("say \"GO FISH!\"\n");
289 if (askedfor == drawcard(player, hand)) {
291 (void)printf("drew the guess!\n");
293 (void)printf("get to ask again!\n");
300 goodmove(player, move, hand, opphand)
304 printplayer(OTHER(player));
305 (void)printf("have %d %s%s.\n",
306 opphand[move], cards[move], opphand[move] == 1 ? "": "'s");
308 hand[move] += opphand[move];
311 if (hand[move] == CARDS) {
313 (void)printf("made a book of %s's!\n", cards[move]);
314 chkwinner(player, hand);
317 chkwinner(OTHER(player), opphand);
320 (void)printf("get another guess!\n");
324 chkwinner(player, hand)
330 for (i = 0; i < RANKS; ++i)
331 if (hand[i] > 0 && hand[i] < CARDS)
334 (void)printf("don't have any more cards!\n");
335 (void)printf("My books:");
336 cb = countbooks(comphand);
337 (void)printf("Your books:");
338 ub = countbooks(userhand);
339 (void)printf("\nI have %d, you have %d.\n", cb, ub);
341 (void)printf("\nYou win!!!\n");
342 if (nrandom(1024) == 0723)
343 (void)printf("Cheater, cheater, pumpkin eater!\n");
344 } else if (cb > ub) {
345 (void)printf("\nI win!!!\n");
346 if (nrandom(1024) == 0723)
347 (void)printf("Hah! Stupid peasant!\n");
349 (void)printf("\nTie!\n");
362 (void)printf("You ");
373 for (book = i = 0; i < RANKS; i++)
375 for (j = hand[i]; --j >= 0;)
380 (void)printf(" + Book%s of", book > 1 ? "s" : "");
381 for (i = 0; i < RANKS; i++)
382 if (hand[i] == CARDS)
394 for (count = i = 0; i < RANKS; i++)
405 for (count = i = 0; i < RANKS; i++)
406 if (hand[i] == CARDS) {
411 (void)printf(" none");
421 for (i = 0; i < RANKS; ++i)
423 for (i = 0; i < HANDSIZE; ++i) {
424 while (!deck[rank = nrandom(RANKS)]);
428 for (i = 0; i < HANDSIZE; ++i) {
429 while (!deck[rank = nrandom(RANKS)]);
440 return((int)random() % n);
449 (void)printf("Would you like instructions (y or n)? ");
451 while (getchar() != '\n');
455 (void)sprintf(buf, "%s %s", _PATH_MORE, _PATH_INSTR);
457 (void)printf("Hit return to continue...\n");
458 while ((input = getchar()) != EOF && input != '\n');
464 (void)fprintf(stderr, "usage: fish [-p]\n");