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
36 * $FreeBSD: src/games/fish/fish.c,v 1.9 1999/12/10 16:21:50 billf Exp $
37 * $DragonFly: src/games/fish/fish.c,v 1.3 2003/11/12 14:53:53 eirikn Exp $
39 * @(#) Copyright (c) 1990, 1993 The Regents of the University of California. All rights reserved.
40 * @(#)fish.c 8.1 (Berkeley) 5/31/93
41 * $FreeBSD: src/games/fish/fish.c,v 1.9 1999/12/10 16:21:50 billf Exp $
44 #include <sys/types.h>
45 #include <sys/errno.h>
51 #include "pathnames.h"
59 #define OTHER(a) (1 - (a))
62 "A", "2", "3", "4", "5", "6", "7",
63 "8", "9", "10", "J", "Q", "K", NULL,
65 #define PRC(card) (void)printf(" %s", cards[card])
68 int asked[RANKS], comphand[RANKS], deck[RANKS];
69 int userasked[RANKS], userhand[RANKS];
71 void chkwinner (int player, int *hand);
73 int countbooks (int *hand);
74 int countcards (int *hand);
75 int drawcard (int player, int *hand);
76 int gofish (int askedfor, int player, int *hand);
77 void goodmove (int player, int move, int *hand, int *opphand);
79 void instructions (void);
81 void printhand (int *hand);
82 void printplayer (int player);
94 while ((ch = getopt(argc, argv, "p")) != -1)
101 (void)fprintf(stderr, "usage: fish [-p]\n");
109 if (nrandom(2) == 1) {
110 printplayer(COMPUTER);
111 (void)printf("get to start.\n");
115 (void)printf("get to start.\n");
119 if (!comphand[move]) {
120 if (gofish(move, USER, userhand))
123 goodmove(USER, move, userhand, comphand);
129 if (!userhand[move]) {
130 if (!gofish(move, COMPUTER, comphand))
133 goodmove(COMPUTER, move, comphand, userhand);
137 return(EXIT_FAILURE);
147 (void)printf("\nYour hand is:");
151 (void)printf("You ask me for: ");
152 (void)fflush(stdout);
153 if (fgets(buf, sizeof(buf), stdin) == NULL)
157 if (buf[0] == '\n') {
158 (void)printf("%d cards in my hand, %d in the pool.\n",
159 countcards(comphand), countcards(deck));
160 (void)printf("My books:");
161 (void)countbooks(comphand);
164 buf[strlen(buf) - 1] = '\0';
165 if (!strcasecmp(buf, "p") && !promode) {
167 (void)printf("Entering pro mode.\n");
170 if (!strcasecmp(buf, "quit"))
172 for (p = cards; *p; ++p)
173 if (!strcasecmp(*p, buf))
176 (void)printf("I don't understand!\n");
185 (void)printf("You don't have any of those!\n");
187 (void)printf("You don't have any %s's!\n", cards[n]);
189 (void)printf("No cheating!\n");
190 (void)printf("Guess again.\n");
204 lmove = (lmove + 1) % RANKS;
205 } while (!comphand[lmove] || comphand[lmove] == CARDS);
209 (void)printf("I ask you for: %s.\n", cards[lmove]);
218 for (i = 0; i < RANKS; ++i)
220 comphand[i] > 0 && comphand[i] < CARDS) {
224 if (nrandom(3) == 1) {
226 if (comphand[i] && comphand[i] != CARDS) {
231 if (comphand[i] != CARDS &&
232 comphand[i] > comphand[max])
236 if (nrandom(1024) == 0723) {
237 for (i = 0; i < RANKS; ++i)
238 if (userhand[i] && comphand[i])
242 for (i = 0; i < RANKS; ++i)
243 if (comphand[i] && comphand[i] != CARDS &&
246 for (i = 0; i < RANKS; ++i)
253 drawcard(player, hand)
259 while (deck[card = nrandom(RANKS)] == 0);
262 if (player == USER || hand[card] == CARDS) {
264 (void)printf("drew %s", cards[card]);
265 if (hand[card] == CARDS) {
266 (void)printf(" and made a book of %s's!\n",
268 chkwinner(player, hand);
276 gofish(askedfor, player, hand)
277 int askedfor, player;
280 printplayer(OTHER(player));
281 (void)printf("say \"GO FISH!\"\n");
282 if (askedfor == drawcard(player, hand)) {
284 (void)printf("drew the guess!\n");
286 (void)printf("get to ask again!\n");
293 goodmove(player, move, hand, opphand)
297 printplayer(OTHER(player));
298 (void)printf("have %d %s%s.\n",
299 opphand[move], cards[move], opphand[move] == 1 ? "": "'s");
301 hand[move] += opphand[move];
304 if (hand[move] == CARDS) {
306 (void)printf("made a book of %s's!\n", cards[move]);
307 chkwinner(player, hand);
310 chkwinner(OTHER(player), opphand);
313 (void)printf("get another guess!\n");
317 chkwinner(player, hand)
323 for (i = 0; i < RANKS; ++i)
324 if (hand[i] > 0 && hand[i] < CARDS)
327 (void)printf("don't have any more cards!\n");
328 (void)printf("My books:");
329 cb = countbooks(comphand);
330 (void)printf("Your books:");
331 ub = countbooks(userhand);
332 (void)printf("\nI have %d, you have %d.\n", cb, ub);
334 (void)printf("\nYou win!!!\n");
335 if (nrandom(1024) == 0723)
336 (void)printf("Cheater, cheater, pumpkin eater!\n");
337 } else if (cb > ub) {
338 (void)printf("\nI win!!!\n");
339 if (nrandom(1024) == 0723)
340 (void)printf("Hah! Stupid peasant!\n");
342 (void)printf("\nTie!\n");
355 (void)printf("You ");
366 for (book = i = 0; i < RANKS; i++)
368 for (j = hand[i]; --j >= 0;)
373 (void)printf(" + Book%s of", book > 1 ? "s" : "");
374 for (i = 0; i < RANKS; i++)
375 if (hand[i] == CARDS)
387 for (count = i = 0; i < RANKS; i++)
398 for (count = i = 0; i < RANKS; i++)
399 if (hand[i] == CARDS) {
404 (void)printf(" none");
414 for (i = 0; i < RANKS; ++i)
416 for (i = 0; i < HANDSIZE; ++i) {
417 while (!deck[rank = nrandom(RANKS)]);
421 for (i = 0; i < HANDSIZE; ++i) {
422 while (!deck[rank = nrandom(RANKS)]);
433 return((int)random() % n);
442 (void)printf("Would you like instructions (y or n)? ");
444 while (getchar() != '\n');
448 (void)sprintf(buf, "%s %s", _PATH_MORE, _PATH_INSTR);
450 (void)printf("Hit return to continue...\n");
451 while ((input = getchar()) != EOF && input != '\n');
457 (void)fprintf(stderr, "usage: fish [-p]\n");