fish.c revision 1.10 1 1.10 jsm /* $NetBSD: fish.c,v 1.10 1999/09/12 09:02:21 jsm Exp $ */
2 1.3 cgd
3 1.1 cgd /*-
4 1.3 cgd * Copyright (c) 1990, 1993
5 1.3 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.1 cgd * Muffy Barkocy.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.1 cgd * 3. All advertising materials mentioning features or use of this software
19 1.1 cgd * must display the following acknowledgement:
20 1.1 cgd * This product includes software developed by the University of
21 1.1 cgd * California, Berkeley and its contributors.
22 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
23 1.1 cgd * may be used to endorse or promote products derived from this software
24 1.1 cgd * without specific prior written permission.
25 1.1 cgd *
26 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.1 cgd * SUCH DAMAGE.
37 1.1 cgd */
38 1.1 cgd
39 1.4 lukem #include <sys/cdefs.h>
40 1.1 cgd #ifndef lint
41 1.4 lukem __COPYRIGHT("@(#) Copyright (c) 1990, 1993\n\
42 1.4 lukem The Regents of the University of California. All rights reserved.\n");
43 1.1 cgd #endif /* not lint */
44 1.1 cgd
45 1.1 cgd #ifndef lint
46 1.3 cgd #if 0
47 1.3 cgd static char sccsid[] = "@(#)fish.c 8.1 (Berkeley) 5/31/93";
48 1.3 cgd #else
49 1.10 jsm __RCSID("$NetBSD: fish.c,v 1.10 1999/09/12 09:02:21 jsm Exp $");
50 1.3 cgd #endif
51 1.1 cgd #endif /* not lint */
52 1.1 cgd
53 1.1 cgd #include <sys/types.h>
54 1.5 christos #include <sys/wait.h>
55 1.5 christos #include <errno.h>
56 1.1 cgd #include <fcntl.h>
57 1.1 cgd #include <stdio.h>
58 1.1 cgd #include <stdlib.h>
59 1.5 christos #include <unistd.h>
60 1.1 cgd #include <string.h>
61 1.4 lukem #include <time.h>
62 1.5 christos #include <err.h>
63 1.1 cgd #include "pathnames.h"
64 1.1 cgd
65 1.1 cgd #define RANKS 13
66 1.1 cgd #define HANDSIZE 7
67 1.1 cgd #define CARDS 4
68 1.1 cgd
69 1.1 cgd #define USER 1
70 1.1 cgd #define COMPUTER 0
71 1.1 cgd #define OTHER(a) (1 - (a))
72 1.1 cgd
73 1.9 jsm const char *const cards[] = {
74 1.1 cgd "A", "2", "3", "4", "5", "6", "7",
75 1.1 cgd "8", "9", "10", "J", "Q", "K", NULL,
76 1.1 cgd };
77 1.1 cgd #define PRC(card) (void)printf(" %s", cards[card])
78 1.1 cgd
79 1.1 cgd int promode;
80 1.1 cgd int asked[RANKS], comphand[RANKS], deck[RANKS];
81 1.1 cgd int userasked[RANKS], userhand[RANKS];
82 1.1 cgd
83 1.9 jsm void chkwinner __P((int, const int *));
84 1.4 lukem int compmove __P((void));
85 1.9 jsm int countbooks __P((const int *));
86 1.9 jsm int countcards __P((const int *));
87 1.4 lukem int drawcard __P((int, int *));
88 1.4 lukem int gofish __P((int, int, int *));
89 1.4 lukem void goodmove __P((int, int, int *, int *));
90 1.4 lukem void init __P((void));
91 1.4 lukem void instructions __P((void));
92 1.4 lukem int main __P((int, char *[]));
93 1.4 lukem int nrandom __P((int));
94 1.9 jsm void printhand __P((const int *));
95 1.4 lukem void printplayer __P((int));
96 1.4 lukem int promove __P((void));
97 1.6 hubertf void usage __P((void)) __attribute__((__noreturn__));
98 1.4 lukem int usermove __P((void));
99 1.4 lukem
100 1.4 lukem int
101 1.1 cgd main(argc, argv)
102 1.1 cgd int argc;
103 1.1 cgd char **argv;
104 1.1 cgd {
105 1.1 cgd int ch, move;
106 1.1 cgd
107 1.10 jsm /* Revoke setgid privileges */
108 1.10 jsm setregid(getgid(), getgid());
109 1.8 hubertf
110 1.4 lukem while ((ch = getopt(argc, argv, "p")) != -1)
111 1.1 cgd switch(ch) {
112 1.1 cgd case 'p':
113 1.1 cgd promode = 1;
114 1.1 cgd break;
115 1.1 cgd case '?':
116 1.1 cgd default:
117 1.1 cgd (void)fprintf(stderr, "usage: fish [-p]\n");
118 1.1 cgd exit(1);
119 1.1 cgd }
120 1.1 cgd
121 1.1 cgd srandom(time((time_t *)NULL));
122 1.1 cgd instructions();
123 1.1 cgd init();
124 1.1 cgd
125 1.1 cgd if (nrandom(2) == 1) {
126 1.1 cgd printplayer(COMPUTER);
127 1.1 cgd (void)printf("get to start.\n");
128 1.1 cgd goto istart;
129 1.1 cgd }
130 1.1 cgd printplayer(USER);
131 1.1 cgd (void)printf("get to start.\n");
132 1.1 cgd
133 1.1 cgd for (;;) {
134 1.1 cgd move = usermove();
135 1.1 cgd if (!comphand[move]) {
136 1.1 cgd if (gofish(move, USER, userhand))
137 1.1 cgd continue;
138 1.1 cgd } else {
139 1.1 cgd goodmove(USER, move, userhand, comphand);
140 1.1 cgd continue;
141 1.1 cgd }
142 1.1 cgd
143 1.1 cgd istart: for (;;) {
144 1.1 cgd move = compmove();
145 1.1 cgd if (!userhand[move]) {
146 1.1 cgd if (!gofish(move, COMPUTER, comphand))
147 1.1 cgd break;
148 1.1 cgd } else
149 1.1 cgd goodmove(COMPUTER, move, comphand, userhand);
150 1.1 cgd }
151 1.1 cgd }
152 1.1 cgd /* NOTREACHED */
153 1.1 cgd }
154 1.1 cgd
155 1.4 lukem int
156 1.1 cgd usermove()
157 1.1 cgd {
158 1.4 lukem int n;
159 1.9 jsm const char *const *p;
160 1.1 cgd char buf[256];
161 1.1 cgd
162 1.1 cgd (void)printf("\nYour hand is:");
163 1.1 cgd printhand(userhand);
164 1.1 cgd
165 1.1 cgd for (;;) {
166 1.1 cgd (void)printf("You ask me for: ");
167 1.1 cgd (void)fflush(stdout);
168 1.7 kristerw if (fgets(buf, sizeof(buf), stdin) == NULL)
169 1.1 cgd exit(0);
170 1.1 cgd if (buf[0] == '\0')
171 1.1 cgd continue;
172 1.1 cgd if (buf[0] == '\n') {
173 1.1 cgd (void)printf("%d cards in my hand, %d in the pool.\n",
174 1.1 cgd countcards(comphand), countcards(deck));
175 1.1 cgd (void)printf("My books:");
176 1.1 cgd (void)countbooks(comphand);
177 1.1 cgd continue;
178 1.1 cgd }
179 1.1 cgd buf[strlen(buf) - 1] = '\0';
180 1.1 cgd if (!strcasecmp(buf, "p") && !promode) {
181 1.1 cgd promode = 1;
182 1.1 cgd (void)printf("Entering pro mode.\n");
183 1.1 cgd continue;
184 1.1 cgd }
185 1.1 cgd if (!strcasecmp(buf, "quit"))
186 1.1 cgd exit(0);
187 1.1 cgd for (p = cards; *p; ++p)
188 1.1 cgd if (!strcasecmp(*p, buf))
189 1.1 cgd break;
190 1.1 cgd if (!*p) {
191 1.1 cgd (void)printf("I don't understand!\n");
192 1.1 cgd continue;
193 1.1 cgd }
194 1.1 cgd n = p - cards;
195 1.1 cgd if (userhand[n]) {
196 1.1 cgd userasked[n] = 1;
197 1.1 cgd return(n);
198 1.1 cgd }
199 1.1 cgd if (nrandom(3) == 1)
200 1.1 cgd (void)printf("You don't have any of those!\n");
201 1.1 cgd else
202 1.1 cgd (void)printf("You don't have any %s's!\n", cards[n]);
203 1.1 cgd if (nrandom(4) == 1)
204 1.1 cgd (void)printf("No cheating!\n");
205 1.1 cgd (void)printf("Guess again.\n");
206 1.1 cgd }
207 1.1 cgd /* NOTREACHED */
208 1.1 cgd }
209 1.1 cgd
210 1.4 lukem int
211 1.1 cgd compmove()
212 1.1 cgd {
213 1.1 cgd static int lmove;
214 1.1 cgd
215 1.1 cgd if (promode)
216 1.1 cgd lmove = promove();
217 1.1 cgd else {
218 1.1 cgd do {
219 1.1 cgd lmove = (lmove + 1) % RANKS;
220 1.1 cgd } while (!comphand[lmove] || comphand[lmove] == CARDS);
221 1.1 cgd }
222 1.1 cgd asked[lmove] = 1;
223 1.1 cgd
224 1.1 cgd (void)printf("I ask you for: %s.\n", cards[lmove]);
225 1.1 cgd return(lmove);
226 1.1 cgd }
227 1.1 cgd
228 1.4 lukem int
229 1.1 cgd promove()
230 1.1 cgd {
231 1.4 lukem int i, max;
232 1.1 cgd
233 1.1 cgd for (i = 0; i < RANKS; ++i)
234 1.1 cgd if (userasked[i] &&
235 1.1 cgd comphand[i] > 0 && comphand[i] < CARDS) {
236 1.1 cgd userasked[i] = 0;
237 1.1 cgd return(i);
238 1.1 cgd }
239 1.1 cgd if (nrandom(3) == 1) {
240 1.1 cgd for (i = 0;; ++i)
241 1.1 cgd if (comphand[i] && comphand[i] != CARDS) {
242 1.1 cgd max = i;
243 1.1 cgd break;
244 1.1 cgd }
245 1.1 cgd while (++i < RANKS)
246 1.1 cgd if (comphand[i] != CARDS &&
247 1.1 cgd comphand[i] > comphand[max])
248 1.1 cgd max = i;
249 1.1 cgd return(max);
250 1.1 cgd }
251 1.1 cgd if (nrandom(1024) == 0723) {
252 1.1 cgd for (i = 0; i < RANKS; ++i)
253 1.1 cgd if (userhand[i] && comphand[i])
254 1.1 cgd return(i);
255 1.1 cgd }
256 1.1 cgd for (;;) {
257 1.1 cgd for (i = 0; i < RANKS; ++i)
258 1.1 cgd if (comphand[i] && comphand[i] != CARDS &&
259 1.1 cgd !asked[i])
260 1.1 cgd return(i);
261 1.1 cgd for (i = 0; i < RANKS; ++i)
262 1.1 cgd asked[i] = 0;
263 1.1 cgd }
264 1.1 cgd /* NOTREACHED */
265 1.1 cgd }
266 1.1 cgd
267 1.4 lukem int
268 1.1 cgd drawcard(player, hand)
269 1.1 cgd int player;
270 1.1 cgd int *hand;
271 1.1 cgd {
272 1.1 cgd int card;
273 1.1 cgd
274 1.1 cgd while (deck[card = nrandom(RANKS)] == 0);
275 1.1 cgd ++hand[card];
276 1.1 cgd --deck[card];
277 1.1 cgd if (player == USER || hand[card] == CARDS) {
278 1.1 cgd printplayer(player);
279 1.1 cgd (void)printf("drew %s", cards[card]);
280 1.1 cgd if (hand[card] == CARDS) {
281 1.1 cgd (void)printf(" and made a book of %s's!\n",
282 1.1 cgd cards[card]);
283 1.1 cgd chkwinner(player, hand);
284 1.1 cgd } else
285 1.1 cgd (void)printf(".\n");
286 1.1 cgd }
287 1.1 cgd return(card);
288 1.1 cgd }
289 1.1 cgd
290 1.4 lukem int
291 1.1 cgd gofish(askedfor, player, hand)
292 1.1 cgd int askedfor, player;
293 1.1 cgd int *hand;
294 1.1 cgd {
295 1.1 cgd printplayer(OTHER(player));
296 1.1 cgd (void)printf("say \"GO FISH!\"\n");
297 1.1 cgd if (askedfor == drawcard(player, hand)) {
298 1.1 cgd printplayer(player);
299 1.1 cgd (void)printf("drew the guess!\n");
300 1.1 cgd printplayer(player);
301 1.1 cgd (void)printf("get to ask again!\n");
302 1.1 cgd return(1);
303 1.1 cgd }
304 1.1 cgd return(0);
305 1.1 cgd }
306 1.1 cgd
307 1.4 lukem void
308 1.1 cgd goodmove(player, move, hand, opphand)
309 1.1 cgd int player, move;
310 1.1 cgd int *hand, *opphand;
311 1.1 cgd {
312 1.1 cgd printplayer(OTHER(player));
313 1.1 cgd (void)printf("have %d %s%s.\n",
314 1.1 cgd opphand[move], cards[move], opphand[move] == 1 ? "": "'s");
315 1.1 cgd
316 1.1 cgd hand[move] += opphand[move];
317 1.1 cgd opphand[move] = 0;
318 1.1 cgd
319 1.1 cgd if (hand[move] == CARDS) {
320 1.1 cgd printplayer(player);
321 1.1 cgd (void)printf("made a book of %s's!\n", cards[move]);
322 1.1 cgd chkwinner(player, hand);
323 1.1 cgd }
324 1.1 cgd
325 1.1 cgd chkwinner(OTHER(player), opphand);
326 1.1 cgd
327 1.1 cgd printplayer(player);
328 1.1 cgd (void)printf("get another guess!\n");
329 1.1 cgd }
330 1.1 cgd
331 1.4 lukem void
332 1.1 cgd chkwinner(player, hand)
333 1.1 cgd int player;
334 1.9 jsm const int *hand;
335 1.1 cgd {
336 1.4 lukem int cb, i, ub;
337 1.1 cgd
338 1.1 cgd for (i = 0; i < RANKS; ++i)
339 1.1 cgd if (hand[i] > 0 && hand[i] < CARDS)
340 1.1 cgd return;
341 1.1 cgd printplayer(player);
342 1.1 cgd (void)printf("don't have any more cards!\n");
343 1.1 cgd (void)printf("My books:");
344 1.1 cgd cb = countbooks(comphand);
345 1.1 cgd (void)printf("Your books:");
346 1.1 cgd ub = countbooks(userhand);
347 1.1 cgd (void)printf("\nI have %d, you have %d.\n", cb, ub);
348 1.1 cgd if (ub > cb) {
349 1.1 cgd (void)printf("\nYou win!!!\n");
350 1.1 cgd if (nrandom(1024) == 0723)
351 1.1 cgd (void)printf("Cheater, cheater, pumpkin eater!\n");
352 1.1 cgd } else if (cb > ub) {
353 1.1 cgd (void)printf("\nI win!!!\n");
354 1.1 cgd if (nrandom(1024) == 0723)
355 1.1 cgd (void)printf("Hah! Stupid peasant!\n");
356 1.1 cgd } else
357 1.1 cgd (void)printf("\nTie!\n");
358 1.1 cgd exit(0);
359 1.1 cgd }
360 1.1 cgd
361 1.4 lukem void
362 1.1 cgd printplayer(player)
363 1.1 cgd int player;
364 1.1 cgd {
365 1.1 cgd switch (player) {
366 1.1 cgd case COMPUTER:
367 1.1 cgd (void)printf("I ");
368 1.1 cgd break;
369 1.1 cgd case USER:
370 1.1 cgd (void)printf("You ");
371 1.1 cgd break;
372 1.1 cgd }
373 1.1 cgd }
374 1.1 cgd
375 1.4 lukem void
376 1.1 cgd printhand(hand)
377 1.9 jsm const int *hand;
378 1.1 cgd {
379 1.4 lukem int book, i, j;
380 1.1 cgd
381 1.1 cgd for (book = i = 0; i < RANKS; i++)
382 1.1 cgd if (hand[i] < CARDS)
383 1.1 cgd for (j = hand[i]; --j >= 0;)
384 1.1 cgd PRC(i);
385 1.1 cgd else
386 1.1 cgd ++book;
387 1.1 cgd if (book) {
388 1.1 cgd (void)printf(" + Book%s of", book > 1 ? "s" : "");
389 1.1 cgd for (i = 0; i < RANKS; i++)
390 1.1 cgd if (hand[i] == CARDS)
391 1.1 cgd PRC(i);
392 1.1 cgd }
393 1.1 cgd (void)putchar('\n');
394 1.1 cgd }
395 1.1 cgd
396 1.4 lukem int
397 1.1 cgd countcards(hand)
398 1.9 jsm const int *hand;
399 1.1 cgd {
400 1.4 lukem int i, count;
401 1.1 cgd
402 1.1 cgd for (count = i = 0; i < RANKS; i++)
403 1.1 cgd count += *hand++;
404 1.1 cgd return(count);
405 1.1 cgd }
406 1.1 cgd
407 1.4 lukem int
408 1.1 cgd countbooks(hand)
409 1.9 jsm const int *hand;
410 1.1 cgd {
411 1.1 cgd int i, count;
412 1.1 cgd
413 1.1 cgd for (count = i = 0; i < RANKS; i++)
414 1.1 cgd if (hand[i] == CARDS) {
415 1.1 cgd ++count;
416 1.1 cgd PRC(i);
417 1.1 cgd }
418 1.1 cgd if (!count)
419 1.1 cgd (void)printf(" none");
420 1.1 cgd (void)putchar('\n');
421 1.1 cgd return(count);
422 1.1 cgd }
423 1.1 cgd
424 1.4 lukem void
425 1.1 cgd init()
426 1.1 cgd {
427 1.4 lukem int i, rank;
428 1.1 cgd
429 1.1 cgd for (i = 0; i < RANKS; ++i)
430 1.1 cgd deck[i] = CARDS;
431 1.1 cgd for (i = 0; i < HANDSIZE; ++i) {
432 1.1 cgd while (!deck[rank = nrandom(RANKS)]);
433 1.1 cgd ++userhand[rank];
434 1.1 cgd --deck[rank];
435 1.1 cgd }
436 1.1 cgd for (i = 0; i < HANDSIZE; ++i) {
437 1.1 cgd while (!deck[rank = nrandom(RANKS)]);
438 1.1 cgd ++comphand[rank];
439 1.1 cgd --deck[rank];
440 1.1 cgd }
441 1.1 cgd }
442 1.1 cgd
443 1.4 lukem int
444 1.1 cgd nrandom(n)
445 1.1 cgd int n;
446 1.1 cgd {
447 1.1 cgd
448 1.1 cgd return((int)random() % n);
449 1.1 cgd }
450 1.1 cgd
451 1.4 lukem void
452 1.1 cgd instructions()
453 1.1 cgd {
454 1.1 cgd int input;
455 1.5 christos pid_t pid;
456 1.8 hubertf int fd;
457 1.8 hubertf const char *pager;
458 1.5 christos int status;
459 1.1 cgd
460 1.1 cgd (void)printf("Would you like instructions (y or n)? ");
461 1.1 cgd input = getchar();
462 1.1 cgd while (getchar() != '\n');
463 1.1 cgd if (input != 'y')
464 1.1 cgd return;
465 1.1 cgd
466 1.5 christos switch (pid = fork()) {
467 1.5 christos case 0: /* child */
468 1.8 hubertf if (!isatty(1))
469 1.8 hubertf pager = "cat";
470 1.8 hubertf else {
471 1.8 hubertf if (!(pager = getenv("PAGER")) || (*pager == 0))
472 1.8 hubertf pager = _PATH_MORE;
473 1.8 hubertf }
474 1.8 hubertf if ((fd = open(_PATH_INSTR, O_RDONLY)) == -1)
475 1.8 hubertf err(1, "open %s", _PATH_INSTR);
476 1.8 hubertf if (dup2(fd, 0) == -1)
477 1.8 hubertf err(1, "dup2");
478 1.8 hubertf (void)execl("/bin/sh", "sh", "-c", pager, NULL);
479 1.8 hubertf err(1, "exec sh -c %s", pager);
480 1.5 christos /*NOTREACHED*/
481 1.5 christos case -1:
482 1.5 christos err(1, "fork");
483 1.5 christos /*NOTREACHED*/
484 1.5 christos default:
485 1.5 christos (void)waitpid(pid, &status, 0);
486 1.5 christos break;
487 1.5 christos }
488 1.1 cgd (void)printf("Hit return to continue...\n");
489 1.1 cgd while ((input = getchar()) != EOF && input != '\n');
490 1.1 cgd }
491 1.1 cgd
492 1.4 lukem void
493 1.1 cgd usage()
494 1.1 cgd {
495 1.1 cgd (void)fprintf(stderr, "usage: fish [-p]\n");
496 1.1 cgd exit(1);
497 1.1 cgd }
498