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 $
34 * @(#) Copyright (c) 1990, 1993 The Regents of the University of California. All rights reserved.
35 * @(#)fish.c 8.1 (Berkeley) 5/31/93
38 #include <sys/types.h>
46 #include "pathnames.h"
54 #define OTHER(a) (1 - (a))
56 static 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 static int asked
[RANKS
], comphand
[RANKS
], deck
[RANKS
];
64 static int userasked
[RANKS
], userhand
[RANKS
];
66 extern char **environ
;
68 static void chkwinner(int, int *);
69 static int compmove(void);
70 static int countbooks(int *);
71 static int countcards(int *);
72 static int drawcard(int, int *);
73 static int gofish(int, int, int *);
74 static void goodmove(int, int, int *, int *);
75 static void init(void);
76 static void instructions(void);
77 static int nrandom(int);
78 static void printhand(int *);
79 static void printplayer(int);
80 static int promove(void);
81 static void usage(void) __dead2
;
82 static int usermove(void);
85 main(int argc
, char **argv
)
89 while ((ch
= getopt(argc
, argv
, "p")) != -1)
103 if (nrandom(2) == 1) {
104 printplayer(COMPUTER
);
105 printf("get to start.\n");
109 printf("get to start.\n");
113 if (!comphand
[move
]) {
114 if (gofish(move
, USER
, userhand
))
117 goodmove(USER
, move
, userhand
, comphand
);
123 if (!userhand
[move
]) {
124 if (!gofish(move
, COMPUTER
, comphand
))
127 goodmove(COMPUTER
, move
, comphand
, userhand
);
131 return (EXIT_FAILURE
);
141 printf("\nYour hand is:");
145 printf("You ask me for: ");
147 if (fgets(buf
, sizeof(buf
), stdin
) == NULL
)
151 if (buf
[0] == '\n') {
152 printf("%d cards in my hand, %d in the pool.\n",
153 countcards(comphand
), countcards(deck
));
155 countbooks(comphand
);
158 buf
[strlen(buf
) - 1] = '\0';
159 if (!strcasecmp(buf
, "p")) {
161 printf("Already in pro mode.\n");
163 printf("Entering pro mode.\n");
168 if (!strcasecmp(buf
, "quit"))
170 for (p
= cards
; *p
; ++p
)
171 if (!strcasecmp(*p
, buf
))
174 printf("I don't understand!\n");
178 if (userhand
[n
] <= 3) {
182 if (userhand
[n
] == 4) {
183 printf("You already have all of those.\n");
188 printf("You don't have any of those!\n");
190 printf("You don't have any %s's!\n", cards
[n
]);
192 printf("No cheating!\n");
193 printf("Guess again.\n");
207 lmove
= (lmove
+ 1) % RANKS
;
208 } while (!comphand
[lmove
] || comphand
[lmove
] == CARDS
);
212 printf("I ask you for: %s.\n", cards
[lmove
]);
221 for (i
= 0; i
< RANKS
; ++i
)
222 if (userasked
[i
] && comphand
[i
] > 0 && comphand
[i
] < CARDS
) {
226 if (nrandom(3) == 1) {
228 if (comphand
[i
] && comphand
[i
] != CARDS
) {
233 if (comphand
[i
] != CARDS
&& comphand
[i
] > comphand
[max
])
237 if (nrandom(1024) == 0723) {
238 for (i
= 0; i
< RANKS
; ++i
)
239 if (userhand
[i
] && comphand
[i
])
243 for (i
= 0; i
< RANKS
; ++i
)
244 if (comphand
[i
] && comphand
[i
] != CARDS
&& !asked
[i
])
246 for (i
= 0; i
< RANKS
; ++i
)
253 drawcard(int player
, int *hand
)
257 while (deck
[card
= nrandom(RANKS
)] == 0)
261 if (player
== USER
|| hand
[card
] == CARDS
) {
263 printf("drew %s", cards
[card
]);
264 if (hand
[card
] == CARDS
) {
265 printf(" and made a book of %s's!\n", cards
[card
]);
266 chkwinner(player
, hand
);
274 gofish(int askedfor
, int player
, int *hand
)
276 printplayer(OTHER(player
));
277 printf("say \"GO FISH!\"\n");
278 if (askedfor
== drawcard(player
, hand
)) {
280 printf("drew the guess!\n");
282 printf("get to ask again!\n");
289 goodmove(int player
, int move
, int *hand
, int *opphand
)
291 printplayer(OTHER(player
));
292 printf("have %d %s%s.\n",
293 opphand
[move
], cards
[move
], opphand
[move
] == 1 ? "": "'s");
295 hand
[move
] += opphand
[move
];
298 if (hand
[move
] == CARDS
) {
300 printf("made a book of %s's!\n", cards
[move
]);
301 chkwinner(player
, hand
);
304 chkwinner(OTHER(player
), opphand
);
307 printf("get another guess!\n");
311 chkwinner(int player
, int *hand
)
315 for (i
= 0; i
< RANKS
; ++i
)
316 if (hand
[i
] > 0 && hand
[i
] < CARDS
)
319 printf("don't have any more cards!\n");
321 cb
= countbooks(comphand
);
322 printf("Your books:");
323 ub
= countbooks(userhand
);
324 printf("\nI have %d, you have %d.\n", cb
, ub
);
326 printf("\nYou win!!!\n");
327 if (nrandom(1024) == 0723)
328 printf("Cheater, cheater, pumpkin eater!\n");
329 } else if (cb
> ub
) {
330 printf("\nI win!!!\n");
331 if (nrandom(1024) == 0723)
332 printf("Hah! Stupid peasant!\n");
339 printplayer(int player
)
356 for (book
= i
= 0; i
< RANKS
; i
++)
358 for (j
= hand
[i
]; --j
>= 0;)
363 printf(" + Book%s of", book
> 1 ? "s" : "");
364 for (i
= 0; i
< RANKS
; i
++)
365 if (hand
[i
] == CARDS
)
372 countcards(int *hand
)
376 for (count
= i
= 0; i
< RANKS
; i
++)
382 countbooks(int *hand
)
386 for (count
= i
= 0; i
< RANKS
; i
++)
387 if (hand
[i
] == CARDS
) {
402 for (i
= 0; i
< RANKS
; ++i
)
404 for (i
= 0; i
< HANDSIZE
; ++i
) {
405 while (!deck
[rank
= nrandom(RANKS
)])
410 for (i
= 0; i
< HANDSIZE
; ++i
) {
411 while (!deck
[rank
= nrandom(RANKS
)])
422 return((int)random() % n
);
428 int input
, status
, pid
;
429 const char * const argv
[] = {PATH_PAGER
, PATH_INSTR
, NULL
};
431 printf("Would you like instructions (y or n)? ");
433 while (getchar() != '\n');
437 status
= posix_spawnp(&pid
, PATH_PAGER
, NULL
, NULL
,
438 __DECONST(char * const *, argv
), environ
);
440 printf("Unable to spawn %s\n",PATH_PAGER
);
442 printf("Hit return to continue...\n");
443 while ((input
= getchar()) != EOF
&& input
!= '\n');
449 fprintf(stderr
, "usage: fish [-p]\n");