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