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.4 2005/07/31 20:40:26 swildner 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
43 #include <sys/types.h>
44 #include <sys/errno.h>
50 #include "pathnames.h"
58 #define OTHER(a) (1 - (a))
60 const char *cards
[] = {
61 "A", "2", "3", "4", "5", "6", "7",
62 "8", "9", "10", "J", "Q", "K", NULL
,
64 #define PRC(card) printf(" %s", cards[card])
67 int asked
[RANKS
], comphand
[RANKS
], deck
[RANKS
];
68 int userasked
[RANKS
], userhand
[RANKS
];
70 static void chkwinner(int, int *);
71 static int compmove(void);
72 static int countbooks(int *);
73 static int countcards(int *);
74 static int drawcard(int, int *);
75 static int gofish(int, int, int *);
76 static void goodmove(int, int, int *, int *);
77 static void init(void);
78 static void instructions(void);
79 static int nrandom(int);
80 static void printhand(int *);
81 static void printplayer(int);
82 static int promove(void);
83 static void usage(void);
84 static int usermove(void);
87 main(int argc
, char **argv
)
91 while ((ch
= getopt(argc
, argv
, "p")) != -1)
105 if (nrandom(2) == 1) {
106 printplayer(COMPUTER
);
107 printf("get to start.\n");
111 printf("get to start.\n");
115 if (!comphand
[move
]) {
116 if (gofish(move
, USER
, userhand
))
119 goodmove(USER
, move
, userhand
, comphand
);
125 if (!userhand
[move
]) {
126 if (!gofish(move
, COMPUTER
, comphand
))
129 goodmove(COMPUTER
, move
, comphand
, userhand
);
133 return(EXIT_FAILURE
);
143 printf("\nYour hand is:");
147 printf("You ask me for: ");
149 if (fgets(buf
, sizeof(buf
), stdin
) == NULL
)
153 if (buf
[0] == '\n') {
154 printf("%d cards in my hand, %d in the pool.\n",
155 countcards(comphand
), countcards(deck
));
157 countbooks(comphand
);
160 buf
[strlen(buf
) - 1] = '\0';
161 if (!strcasecmp(buf
, "p") && !promode
) {
163 printf("Entering pro mode.\n");
166 if (!strcasecmp(buf
, "quit"))
168 for (p
= cards
; *p
; ++p
)
169 if (!strcasecmp(*p
, buf
))
172 printf("I don't understand!\n");
181 printf("You don't have any of those!\n");
183 printf("You don't have any %s's!\n", cards
[n
]);
185 printf("No cheating!\n");
186 printf("Guess again.\n");
200 lmove
= (lmove
+ 1) % RANKS
;
201 } while (!comphand
[lmove
] || comphand
[lmove
] == CARDS
);
205 printf("I ask you for: %s.\n", cards
[lmove
]);
214 for (i
= 0; i
< RANKS
; ++i
)
215 if (userasked
[i
] && comphand
[i
] > 0 && comphand
[i
] < CARDS
) {
219 if (nrandom(3) == 1) {
221 if (comphand
[i
] && comphand
[i
] != CARDS
) {
226 if (comphand
[i
] != CARDS
&& comphand
[i
] > comphand
[max
])
230 if (nrandom(1024) == 0723) {
231 for (i
= 0; i
< RANKS
; ++i
)
232 if (userhand
[i
] && comphand
[i
])
236 for (i
= 0; i
< RANKS
; ++i
)
237 if (comphand
[i
] && comphand
[i
] != CARDS
&& !asked
[i
])
239 for (i
= 0; i
< RANKS
; ++i
)
246 drawcard(int player
, int *hand
)
250 while (deck
[card
= nrandom(RANKS
)] == 0);
253 if (player
== USER
|| hand
[card
] == CARDS
) {
255 printf("drew %s", cards
[card
]);
256 if (hand
[card
] == CARDS
) {
257 printf(" and made a book of %s's!\n", cards
[card
]);
258 chkwinner(player
, hand
);
266 gofish(int askedfor
, int player
, int *hand
)
268 printplayer(OTHER(player
));
269 printf("say \"GO FISH!\"\n");
270 if (askedfor
== drawcard(player
, hand
)) {
272 printf("drew the guess!\n");
274 printf("get to ask again!\n");
281 goodmove(int player
, int move
, int *hand
, int *opphand
)
283 printplayer(OTHER(player
));
284 printf("have %d %s%s.\n",
285 opphand
[move
], cards
[move
], opphand
[move
] == 1 ? "": "'s");
287 hand
[move
] += opphand
[move
];
290 if (hand
[move
] == CARDS
) {
292 printf("made a book of %s's!\n", cards
[move
]);
293 chkwinner(player
, hand
);
296 chkwinner(OTHER(player
), opphand
);
299 printf("get another guess!\n");
303 chkwinner(int player
, int *hand
)
307 for (i
= 0; i
< RANKS
; ++i
)
308 if (hand
[i
] > 0 && hand
[i
] < CARDS
)
311 printf("don't have any more cards!\n");
313 cb
= countbooks(comphand
);
314 printf("Your books:");
315 ub
= countbooks(userhand
);
316 printf("\nI have %d, you have %d.\n", cb
, ub
);
318 printf("\nYou win!!!\n");
319 if (nrandom(1024) == 0723)
320 printf("Cheater, cheater, pumpkin eater!\n");
321 } else if (cb
> ub
) {
322 printf("\nI win!!!\n");
323 if (nrandom(1024) == 0723)
324 printf("Hah! Stupid peasant!\n");
331 printplayer(int player
)
348 for (book
= i
= 0; i
< RANKS
; i
++)
350 for (j
= hand
[i
]; --j
>= 0;)
355 printf(" + Book%s of", book
> 1 ? "s" : "");
356 for (i
= 0; i
< RANKS
; i
++)
357 if (hand
[i
] == CARDS
)
364 countcards(int *hand
)
368 for (count
= i
= 0; i
< RANKS
; i
++)
374 countbooks(int *hand
)
378 for (count
= i
= 0; i
< RANKS
; i
++)
379 if (hand
[i
] == CARDS
) {
394 for (i
= 0; i
< RANKS
; ++i
)
396 for (i
= 0; i
< HANDSIZE
; ++i
) {
397 while (!deck
[rank
= nrandom(RANKS
)]);
401 for (i
= 0; i
< HANDSIZE
; ++i
) {
402 while (!deck
[rank
= nrandom(RANKS
)]);
412 return((int)random() % n
);
421 printf("Would you like instructions (y or n)? ");
423 while (getchar() != '\n');
427 sprintf(buf
, "%s %s", _PATH_MORE
, _PATH_INSTR
);
429 printf("Hit return to continue...\n");
430 while ((input
= getchar()) != EOF
&& input
!= '\n');
436 fprintf(stderr
, "usage: fish [-p]\n");