Commit | Line | Data |
---|---|---|
984263bc MD |
1 | /*- |
2 | * Copyright (c) 1990, 1993 | |
3 | * The Regents of the University of California. All rights reserved. | |
4 | * | |
5 | * This code is derived from software contributed to Berkeley by | |
6 | * Muffy Barkocy. | |
7 | * | |
8 | * Redistribution and use in source and binary forms, with or without | |
9 | * modification, are permitted provided that the following conditions | |
10 | * are met: | |
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. | |
6693db17 | 16 | * 3. Neither the name of the University nor the names of its contributors |
984263bc MD |
17 | * may be used to endorse or promote products derived from this software |
18 | * without specific prior written permission. | |
19 | * | |
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 | |
30 | * SUCH DAMAGE. | |
31 | * | |
32 | * $FreeBSD: src/games/fish/fish.c,v 1.9 1999/12/10 16:21:50 billf Exp $ | |
807005d4 | 33 | * $DragonFly: src/games/fish/fish.c,v 1.4 2005/07/31 20:40:26 swildner Exp $ |
1de703da MD |
34 | * |
35 | * @(#) Copyright (c) 1990, 1993 The Regents of the University of California. All rights reserved. | |
36 | * @(#)fish.c 8.1 (Berkeley) 5/31/93 | |
984263bc MD |
37 | */ |
38 | ||
984263bc | 39 | #include <sys/types.h> |
6693db17 | 40 | #include <errno.h> |
984263bc MD |
41 | #include <fcntl.h> |
42 | #include <stdio.h> | |
43 | #include <stdlib.h> | |
44 | #include <string.h> | |
45 | #include <unistd.h> | |
46 | #include "pathnames.h" | |
47 | ||
48 | #define RANKS 13 | |
49 | #define HANDSIZE 7 | |
50 | #define CARDS 4 | |
51 | ||
52 | #define USER 1 | |
53 | #define COMPUTER 0 | |
54 | #define OTHER(a) (1 - (a)) | |
55 | ||
807005d4 | 56 | const char *cards[] = { |
984263bc MD |
57 | "A", "2", "3", "4", "5", "6", "7", |
58 | "8", "9", "10", "J", "Q", "K", NULL, | |
59 | }; | |
807005d4 | 60 | #define PRC(card) printf(" %s", cards[card]) |
984263bc MD |
61 | |
62 | int promode; | |
63 | int asked[RANKS], comphand[RANKS], deck[RANKS]; | |
64 | int userasked[RANKS], userhand[RANKS]; | |
65 | ||
6693db17 SW |
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); | |
984263bc MD |
81 | |
82 | int | |
807005d4 | 83 | main(int argc, char **argv) |
984263bc MD |
84 | { |
85 | int ch, move; | |
86 | ||
87 | while ((ch = getopt(argc, argv, "p")) != -1) | |
88 | switch(ch) { | |
89 | case 'p': | |
90 | promode = 1; | |
91 | break; | |
92 | case '?': | |
93 | default: | |
807005d4 | 94 | usage(); |
984263bc MD |
95 | } |
96 | ||
97 | srandomdev(); | |
98 | instructions(); | |
99 | init(); | |
100 | ||
101 | if (nrandom(2) == 1) { | |
102 | printplayer(COMPUTER); | |
807005d4 | 103 | printf("get to start.\n"); |
984263bc MD |
104 | goto istart; |
105 | } | |
106 | printplayer(USER); | |
807005d4 | 107 | printf("get to start.\n"); |
984263bc MD |
108 | |
109 | for (;;) { | |
110 | move = usermove(); | |
111 | if (!comphand[move]) { | |
112 | if (gofish(move, USER, userhand)) | |
113 | continue; | |
114 | } else { | |
115 | goodmove(USER, move, userhand, comphand); | |
116 | continue; | |
117 | } | |
118 | ||
119 | istart: for (;;) { | |
120 | move = compmove(); | |
121 | if (!userhand[move]) { | |
122 | if (!gofish(move, COMPUTER, comphand)) | |
123 | break; | |
124 | } else | |
125 | goodmove(COMPUTER, move, comphand, userhand); | |
126 | } | |
127 | } | |
128 | /* NOTREACHED */ | |
6693db17 | 129 | return (EXIT_FAILURE); |
984263bc MD |
130 | } |
131 | ||
807005d4 SW |
132 | static int |
133 | usermove(void) | |
984263bc MD |
134 | { |
135 | int n; | |
807005d4 | 136 | const char **p; |
984263bc MD |
137 | char buf[256]; |
138 | ||
807005d4 | 139 | printf("\nYour hand is:"); |
984263bc MD |
140 | printhand(userhand); |
141 | ||
142 | for (;;) { | |
807005d4 SW |
143 | printf("You ask me for: "); |
144 | fflush(stdout); | |
984263bc MD |
145 | if (fgets(buf, sizeof(buf), stdin) == NULL) |
146 | exit(0); | |
147 | if (buf[0] == '\0') | |
148 | continue; | |
149 | if (buf[0] == '\n') { | |
807005d4 | 150 | printf("%d cards in my hand, %d in the pool.\n", |
984263bc | 151 | countcards(comphand), countcards(deck)); |
807005d4 SW |
152 | printf("My books:"); |
153 | countbooks(comphand); | |
984263bc MD |
154 | continue; |
155 | } | |
156 | buf[strlen(buf) - 1] = '\0'; | |
157 | if (!strcasecmp(buf, "p") && !promode) { | |
158 | promode = 1; | |
807005d4 | 159 | printf("Entering pro mode.\n"); |
984263bc MD |
160 | continue; |
161 | } | |
162 | if (!strcasecmp(buf, "quit")) | |
163 | exit(0); | |
164 | for (p = cards; *p; ++p) | |
165 | if (!strcasecmp(*p, buf)) | |
166 | break; | |
167 | if (!*p) { | |
807005d4 | 168 | printf("I don't understand!\n"); |
984263bc MD |
169 | continue; |
170 | } | |
171 | n = p - cards; | |
172 | if (userhand[n]) { | |
173 | userasked[n] = 1; | |
174 | return(n); | |
175 | } | |
176 | if (nrandom(3) == 1) | |
807005d4 | 177 | printf("You don't have any of those!\n"); |
984263bc | 178 | else |
807005d4 | 179 | printf("You don't have any %s's!\n", cards[n]); |
984263bc | 180 | if (nrandom(4) == 1) |
807005d4 SW |
181 | printf("No cheating!\n"); |
182 | printf("Guess again.\n"); | |
984263bc MD |
183 | } |
184 | /* NOTREACHED */ | |
185 | } | |
186 | ||
807005d4 SW |
187 | static int |
188 | compmove(void) | |
984263bc MD |
189 | { |
190 | static int lmove; | |
191 | ||
192 | if (promode) | |
193 | lmove = promove(); | |
194 | else { | |
195 | do { | |
196 | lmove = (lmove + 1) % RANKS; | |
197 | } while (!comphand[lmove] || comphand[lmove] == CARDS); | |
198 | } | |
199 | asked[lmove] = 1; | |
200 | ||
807005d4 | 201 | printf("I ask you for: %s.\n", cards[lmove]); |
984263bc MD |
202 | return(lmove); |
203 | } | |
204 | ||
807005d4 SW |
205 | static int |
206 | promove(void) | |
984263bc MD |
207 | { |
208 | int i, max; | |
209 | ||
210 | for (i = 0; i < RANKS; ++i) | |
807005d4 | 211 | if (userasked[i] && comphand[i] > 0 && comphand[i] < CARDS) { |
984263bc MD |
212 | userasked[i] = 0; |
213 | return(i); | |
214 | } | |
215 | if (nrandom(3) == 1) { | |
216 | for (i = 0;; ++i) | |
217 | if (comphand[i] && comphand[i] != CARDS) { | |
218 | max = i; | |
219 | break; | |
220 | } | |
221 | while (++i < RANKS) | |
807005d4 | 222 | if (comphand[i] != CARDS && comphand[i] > comphand[max]) |
984263bc MD |
223 | max = i; |
224 | return(max); | |
225 | } | |
226 | if (nrandom(1024) == 0723) { | |
227 | for (i = 0; i < RANKS; ++i) | |
228 | if (userhand[i] && comphand[i]) | |
229 | return(i); | |
230 | } | |
231 | for (;;) { | |
232 | for (i = 0; i < RANKS; ++i) | |
807005d4 | 233 | if (comphand[i] && comphand[i] != CARDS && !asked[i]) |
984263bc MD |
234 | return(i); |
235 | for (i = 0; i < RANKS; ++i) | |
236 | asked[i] = 0; | |
237 | } | |
238 | /* NOTREACHED */ | |
239 | } | |
240 | ||
807005d4 SW |
241 | static int |
242 | drawcard(int player, int *hand) | |
984263bc MD |
243 | { |
244 | int card; | |
245 | ||
6693db17 SW |
246 | while (deck[card = nrandom(RANKS)] == 0) |
247 | ; /* nothing */ | |
984263bc MD |
248 | ++hand[card]; |
249 | --deck[card]; | |
250 | if (player == USER || hand[card] == CARDS) { | |
251 | printplayer(player); | |
807005d4 | 252 | printf("drew %s", cards[card]); |
984263bc | 253 | if (hand[card] == CARDS) { |
807005d4 | 254 | printf(" and made a book of %s's!\n", cards[card]); |
984263bc MD |
255 | chkwinner(player, hand); |
256 | } else | |
807005d4 | 257 | printf(".\n"); |
984263bc MD |
258 | } |
259 | return(card); | |
260 | } | |
261 | ||
807005d4 SW |
262 | static int |
263 | gofish(int askedfor, int player, int *hand) | |
984263bc MD |
264 | { |
265 | printplayer(OTHER(player)); | |
807005d4 | 266 | printf("say \"GO FISH!\"\n"); |
984263bc MD |
267 | if (askedfor == drawcard(player, hand)) { |
268 | printplayer(player); | |
807005d4 | 269 | printf("drew the guess!\n"); |
984263bc | 270 | printplayer(player); |
807005d4 | 271 | printf("get to ask again!\n"); |
984263bc MD |
272 | return(1); |
273 | } | |
274 | return(0); | |
275 | } | |
276 | ||
807005d4 SW |
277 | static void |
278 | goodmove(int player, int move, int *hand, int *opphand) | |
984263bc MD |
279 | { |
280 | printplayer(OTHER(player)); | |
807005d4 | 281 | printf("have %d %s%s.\n", |
984263bc MD |
282 | opphand[move], cards[move], opphand[move] == 1 ? "": "'s"); |
283 | ||
284 | hand[move] += opphand[move]; | |
285 | opphand[move] = 0; | |
286 | ||
287 | if (hand[move] == CARDS) { | |
288 | printplayer(player); | |
807005d4 | 289 | printf("made a book of %s's!\n", cards[move]); |
984263bc MD |
290 | chkwinner(player, hand); |
291 | } | |
292 | ||
293 | chkwinner(OTHER(player), opphand); | |
294 | ||
295 | printplayer(player); | |
807005d4 | 296 | printf("get another guess!\n"); |
984263bc MD |
297 | } |
298 | ||
807005d4 SW |
299 | static void |
300 | chkwinner(int player, int *hand) | |
984263bc MD |
301 | { |
302 | int cb, i, ub; | |
303 | ||
304 | for (i = 0; i < RANKS; ++i) | |
305 | if (hand[i] > 0 && hand[i] < CARDS) | |
306 | return; | |
307 | printplayer(player); | |
807005d4 SW |
308 | printf("don't have any more cards!\n"); |
309 | printf("My books:"); | |
984263bc | 310 | cb = countbooks(comphand); |
807005d4 | 311 | printf("Your books:"); |
984263bc | 312 | ub = countbooks(userhand); |
807005d4 | 313 | printf("\nI have %d, you have %d.\n", cb, ub); |
984263bc | 314 | if (ub > cb) { |
807005d4 | 315 | printf("\nYou win!!!\n"); |
984263bc | 316 | if (nrandom(1024) == 0723) |
807005d4 | 317 | printf("Cheater, cheater, pumpkin eater!\n"); |
984263bc | 318 | } else if (cb > ub) { |
807005d4 | 319 | printf("\nI win!!!\n"); |
984263bc | 320 | if (nrandom(1024) == 0723) |
807005d4 | 321 | printf("Hah! Stupid peasant!\n"); |
984263bc | 322 | } else |
807005d4 | 323 | printf("\nTie!\n"); |
984263bc MD |
324 | exit(0); |
325 | } | |
326 | ||
807005d4 SW |
327 | static void |
328 | printplayer(int player) | |
984263bc MD |
329 | { |
330 | switch (player) { | |
331 | case COMPUTER: | |
807005d4 | 332 | printf("I "); |
984263bc MD |
333 | break; |
334 | case USER: | |
807005d4 | 335 | printf("You "); |
984263bc MD |
336 | break; |
337 | } | |
338 | } | |
339 | ||
807005d4 SW |
340 | static void |
341 | printhand(int *hand) | |
984263bc MD |
342 | { |
343 | int book, i, j; | |
344 | ||
345 | for (book = i = 0; i < RANKS; i++) | |
346 | if (hand[i] < CARDS) | |
347 | for (j = hand[i]; --j >= 0;) | |
348 | PRC(i); | |
349 | else | |
350 | ++book; | |
351 | if (book) { | |
807005d4 | 352 | printf(" + Book%s of", book > 1 ? "s" : ""); |
984263bc MD |
353 | for (i = 0; i < RANKS; i++) |
354 | if (hand[i] == CARDS) | |
355 | PRC(i); | |
356 | } | |
807005d4 | 357 | putchar('\n'); |
984263bc MD |
358 | } |
359 | ||
807005d4 SW |
360 | static int |
361 | countcards(int *hand) | |
984263bc MD |
362 | { |
363 | int i, count; | |
364 | ||
365 | for (count = i = 0; i < RANKS; i++) | |
366 | count += *hand++; | |
367 | return(count); | |
368 | } | |
369 | ||
807005d4 SW |
370 | static int |
371 | countbooks(int *hand) | |
984263bc MD |
372 | { |
373 | int i, count; | |
374 | ||
375 | for (count = i = 0; i < RANKS; i++) | |
376 | if (hand[i] == CARDS) { | |
377 | ++count; | |
378 | PRC(i); | |
379 | } | |
380 | if (!count) | |
807005d4 SW |
381 | printf(" none"); |
382 | putchar('\n'); | |
984263bc MD |
383 | return(count); |
384 | } | |
385 | ||
807005d4 SW |
386 | static void |
387 | init(void) | |
984263bc MD |
388 | { |
389 | int i, rank; | |
390 | ||
391 | for (i = 0; i < RANKS; ++i) | |
392 | deck[i] = CARDS; | |
393 | for (i = 0; i < HANDSIZE; ++i) { | |
6693db17 SW |
394 | while (!deck[rank = nrandom(RANKS)]) |
395 | ; /* nothing */ | |
984263bc MD |
396 | ++userhand[rank]; |
397 | --deck[rank]; | |
398 | } | |
399 | for (i = 0; i < HANDSIZE; ++i) { | |
6693db17 SW |
400 | while (!deck[rank = nrandom(RANKS)]) |
401 | ; /* nothing */ | |
984263bc MD |
402 | ++comphand[rank]; |
403 | --deck[rank]; | |
404 | } | |
405 | } | |
406 | ||
807005d4 SW |
407 | static int |
408 | nrandom(int n) | |
984263bc MD |
409 | { |
410 | ||
411 | return((int)random() % n); | |
412 | } | |
413 | ||
807005d4 SW |
414 | static void |
415 | instructions(void) | |
984263bc MD |
416 | { |
417 | int input; | |
418 | char buf[1024]; | |
419 | ||
807005d4 | 420 | printf("Would you like instructions (y or n)? "); |
984263bc MD |
421 | input = getchar(); |
422 | while (getchar() != '\n'); | |
423 | if (input != 'y') | |
424 | return; | |
425 | ||
807005d4 SW |
426 | sprintf(buf, "%s %s", _PATH_MORE, _PATH_INSTR); |
427 | system(buf); | |
428 | printf("Hit return to continue...\n"); | |
984263bc MD |
429 | while ((input = getchar()) != EOF && input != '\n'); |
430 | } | |
431 | ||
807005d4 SW |
432 | static void |
433 | usage(void) | |
984263bc | 434 | { |
807005d4 | 435 | fprintf(stderr, "usage: fish [-p]\n"); |
984263bc MD |
436 | exit(1); |
437 | } |