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. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * $FreeBSD: src/games/fish/fish.c,v 1.9 1999/12/10 16:21:50 billf Exp $
33 * $DragonFly: src/games/fish/fish.c,v 1.4 2005/07/31 20:40:26 swildner Exp $
35 * @(#) Copyright (c) 1990, 1993 The Regents of the University of California. All rights reserved.
36 * @(#)fish.c 8.1 (Berkeley) 5/31/93
39 #include <sys/types.h>
46 #include "pathnames.h"
54 #define OTHER(a) (1 - (a))
56 const char *cards[] = {
57 "A", "2", "3", "4", "5", "6", "7",
58 "8", "9", "10", "J", "Q", "K", NULL,
60 #define PRC(card) printf(" %s", cards[card])
63 int asked[RANKS], comphand[RANKS], deck[RANKS];
64 int userasked[RANKS], userhand[RANKS];
66 static void chkwinner(int, int *);
67 static int compmove(void);
68 static int countbooks(int *);
69 static int countcards(int *);
70 static int drawcard(int, int *);
71 static int gofish(int, int, int *);
72 static void goodmove(int, int, int *, int *);
73 static void init(void);
74 static void instructions(void);
75 static int nrandom(int);
76 static void printhand(int *);
77 static void printplayer(int);
78 static int promove(void);
79 static void usage(void);
80 static int usermove(void);
83 main(int argc, char **argv)
87 while ((ch = getopt(argc, argv, "p")) != -1)
101 if (nrandom(2) == 1) {
102 printplayer(COMPUTER);
103 printf("get to start.\n");
107 printf("get to start.\n");
111 if (!comphand[move]) {
112 if (gofish(move, USER, userhand))
115 goodmove(USER, move, userhand, comphand);
121 if (!userhand[move]) {
122 if (!gofish(move, COMPUTER, comphand))
125 goodmove(COMPUTER, move, comphand, userhand);
129 return (EXIT_FAILURE);
139 printf("\nYour hand is:");
143 printf("You ask me for: ");
145 if (fgets(buf, sizeof(buf), stdin) == NULL)
149 if (buf[0] == '\n') {
150 printf("%d cards in my hand, %d in the pool.\n",
151 countcards(comphand), countcards(deck));
153 countbooks(comphand);
156 buf[strlen(buf) - 1] = '\0';
157 if (!strcasecmp(buf, "p") && !promode) {
159 printf("Entering pro mode.\n");
162 if (!strcasecmp(buf, "quit"))
164 for (p = cards; *p; ++p)
165 if (!strcasecmp(*p, buf))
168 printf("I don't understand!\n");
177 printf("You don't have any of those!\n");
179 printf("You don't have any %s's!\n", cards[n]);
181 printf("No cheating!\n");
182 printf("Guess again.\n");
196 lmove = (lmove + 1) % RANKS;
197 } while (!comphand[lmove] || comphand[lmove] == CARDS);
201 printf("I ask you for: %s.\n", cards[lmove]);
210 for (i = 0; i < RANKS; ++i)
211 if (userasked[i] && comphand[i] > 0 && comphand[i] < CARDS) {
215 if (nrandom(3) == 1) {
217 if (comphand[i] && comphand[i] != CARDS) {
222 if (comphand[i] != CARDS && comphand[i] > comphand[max])
226 if (nrandom(1024) == 0723) {
227 for (i = 0; i < RANKS; ++i)
228 if (userhand[i] && comphand[i])
232 for (i = 0; i < RANKS; ++i)
233 if (comphand[i] && comphand[i] != CARDS && !asked[i])
235 for (i = 0; i < RANKS; ++i)
242 drawcard(int player, int *hand)
246 while (deck[card = nrandom(RANKS)] == 0)
250 if (player == USER || hand[card] == CARDS) {
252 printf("drew %s", cards[card]);
253 if (hand[card] == CARDS) {
254 printf(" and made a book of %s's!\n", cards[card]);
255 chkwinner(player, hand);
263 gofish(int askedfor, int player, int *hand)
265 printplayer(OTHER(player));
266 printf("say \"GO FISH!\"\n");
267 if (askedfor == drawcard(player, hand)) {
269 printf("drew the guess!\n");
271 printf("get to ask again!\n");
278 goodmove(int player, int move, int *hand, int *opphand)
280 printplayer(OTHER(player));
281 printf("have %d %s%s.\n",
282 opphand[move], cards[move], opphand[move] == 1 ? "": "'s");
284 hand[move] += opphand[move];
287 if (hand[move] == CARDS) {
289 printf("made a book of %s's!\n", cards[move]);
290 chkwinner(player, hand);
293 chkwinner(OTHER(player), opphand);
296 printf("get another guess!\n");
300 chkwinner(int player, int *hand)
304 for (i = 0; i < RANKS; ++i)
305 if (hand[i] > 0 && hand[i] < CARDS)
308 printf("don't have any more cards!\n");
310 cb = countbooks(comphand);
311 printf("Your books:");
312 ub = countbooks(userhand);
313 printf("\nI have %d, you have %d.\n", cb, ub);
315 printf("\nYou win!!!\n");
316 if (nrandom(1024) == 0723)
317 printf("Cheater, cheater, pumpkin eater!\n");
318 } else if (cb > ub) {
319 printf("\nI win!!!\n");
320 if (nrandom(1024) == 0723)
321 printf("Hah! Stupid peasant!\n");
328 printplayer(int player)
345 for (book = i = 0; i < RANKS; i++)
347 for (j = hand[i]; --j >= 0;)
352 printf(" + Book%s of", book > 1 ? "s" : "");
353 for (i = 0; i < RANKS; i++)
354 if (hand[i] == CARDS)
361 countcards(int *hand)
365 for (count = i = 0; i < RANKS; i++)
371 countbooks(int *hand)
375 for (count = i = 0; i < RANKS; i++)
376 if (hand[i] == CARDS) {
391 for (i = 0; i < RANKS; ++i)
393 for (i = 0; i < HANDSIZE; ++i) {
394 while (!deck[rank = nrandom(RANKS)])
399 for (i = 0; i < HANDSIZE; ++i) {
400 while (!deck[rank = nrandom(RANKS)])
411 return((int)random() % n);
420 printf("Would you like instructions (y or n)? ");
422 while (getchar() != '\n');
426 sprintf(buf, "%s %s", _PATH_MORE, _PATH_INSTR);
428 printf("Hit return to continue...\n");
429 while ((input = getchar()) != EOF && input != '\n');
435 fprintf(stderr, "usage: fish [-p]\n");