]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - fish/fish.c
1a9be58a5a603bbd5cd5f0383f253eb97b2a18b6
1 /* $NetBSD: fish.c,v 1.25 2021/05/02 12:24:59 rillig Exp $ */
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
37 __COPYRIGHT("@(#) Copyright (c) 1990, 1993\
38 The Regents of the University of California. All rights reserved.");
43 static char sccsid
[] = "@(#)fish.c 8.1 (Berkeley) 5/31/93";
45 __RCSID("$NetBSD: fish.c,v 1.25 2021/05/02 12:24:59 rillig Exp $");
49 #include <sys/types.h>
58 #include "pathnames.h"
63 #define TOTCARDS RANKS * CARDS
67 #define OTHER(a) (1 - (a))
69 static const char *const cards
[] = {
70 "A", "2", "3", "4", "5", "6", "7",
71 "8", "9", "10", "J", "Q", "K", NULL
,
73 #define PRC(card) (void)printf(" %s", cards[card])
76 static int asked
[RANKS
], comphand
[RANKS
], deck
[TOTCARDS
];
77 static int userasked
[RANKS
], userhand
[RANKS
];
78 static int curcard
= TOTCARDS
;
80 static void chkwinner(int, const int *);
81 static int compmove(void);
82 static int countbooks(const int *);
83 static int countcards(const int *);
84 static int drawcard(int, int *);
85 static int gofish(int, int, int *);
86 static void goodmove(int, int, int *, int *);
87 static void init(void);
88 static void instructions(void);
89 static void printhand(const int *);
90 static void printplayer(int);
91 static int promove(void);
92 static void usage(void) __dead
;
93 static int usermove(void);
96 main(int argc
, char **argv
)
100 /* Revoke setgid privileges */
103 while ((ch
= getopt(argc
, argv
, "p")) != -1)
116 if (arc4random_uniform(2) == 1) {
117 printplayer(COMPUTER
);
118 (void)printf("get to start.\n");
122 (void)printf("get to start.\n");
126 if (!comphand
[move
]) {
127 if (gofish(move
, USER
, userhand
))
130 goodmove(USER
, move
, userhand
, comphand
);
136 if (!userhand
[move
]) {
137 if (!gofish(move
, COMPUTER
, comphand
))
140 goodmove(COMPUTER
, move
, comphand
, userhand
);
150 const char *const *p
;
153 (void)printf("\nYour hand is:");
157 (void)printf("You ask me for: ");
158 (void)fflush(stdout
);
159 if (fgets(buf
, sizeof(buf
), stdin
) == NULL
)
163 if (buf
[0] == '\n') {
164 (void)printf("%d cards in my hand, %d in the pool.\n",
165 countcards(comphand
), curcard
);
166 (void)printf("My books:");
167 (void)countbooks(comphand
);
170 buf
[strlen(buf
) - 1] = '\0';
171 if (!strcasecmp(buf
, "p")) {
174 printf("Entering pro mode.\n");
177 printf("Already in pro mode.\n");
181 if (!strcasecmp(buf
, "quit"))
183 for (p
= cards
; *p
; ++p
)
184 if (!strcasecmp(*p
, buf
))
187 (void)printf("I don't understand!\n");
191 if (userhand
[n
] <= 3) {
195 if (userhand
[n
] == 4) {
196 printf("You already have all of those.\n");
200 if (arc4random_uniform(3) == 1)
201 (void)printf("You don't have any of those!\n");
203 (void)printf("You don't have any %s's!\n", cards
[n
]);
204 if (arc4random_uniform(4) == 1)
205 (void)printf("No cheating!\n");
206 (void)printf("Guess again.\n");
220 lmove
= (lmove
+ 1) % RANKS
;
221 } while (!comphand
[lmove
] || comphand
[lmove
] == CARDS
);
225 (void)printf("I ask you for: %s.\n", cards
[lmove
]);
234 for (i
= 0; i
< RANKS
; ++i
)
236 comphand
[i
] > 0 && comphand
[i
] < CARDS
) {
240 if (arc4random_uniform(3) == 1) {
242 if (comphand
[i
] && comphand
[i
] != CARDS
) {
247 if (comphand
[i
] != CARDS
&&
248 comphand
[i
] > comphand
[max
])
252 if (arc4random_uniform(1024) == 0723) {
253 for (i
= 0; i
< RANKS
; ++i
)
254 if (userhand
[i
] && comphand
[i
])
258 for (i
= 0; i
< RANKS
; ++i
)
259 if (comphand
[i
] && comphand
[i
] != CARDS
&&
262 for (i
= 0; i
< RANKS
; ++i
)
269 drawcard(int player
, int *hand
)
273 ++hand
[card
= deck
[--curcard
]];
274 if (player
== USER
|| hand
[card
] == CARDS
) {
276 (void)printf("drew %s", cards
[card
]);
277 if (hand
[card
] == CARDS
) {
278 (void)printf(" and made a book of %s's!\n",
280 chkwinner(player
, hand
);
288 gofish(int askedfor
, int player
, int *hand
)
290 printplayer(OTHER(player
));
291 (void)printf("say \"GO FISH!\"\n");
292 if (askedfor
== drawcard(player
, hand
)) {
294 (void)printf("drew the guess!\n");
296 (void)printf("get to ask again!\n");
303 goodmove(int player
, int move
, int *hand
, int *opphand
)
305 printplayer(OTHER(player
));
306 (void)printf("have %d %s%s.\n",
307 opphand
[move
], cards
[move
], opphand
[move
] == 1 ? "": "'s");
309 hand
[move
] += opphand
[move
];
312 if (hand
[move
] == CARDS
) {
314 (void)printf("made a book of %s's!\n", cards
[move
]);
315 chkwinner(player
, hand
);
318 chkwinner(OTHER(player
), opphand
);
321 (void)printf("get another guess!\n");
325 chkwinner(int player
, const int *hand
)
329 for (i
= 0; i
< RANKS
; ++i
)
330 if (hand
[i
] > 0 && hand
[i
] < CARDS
)
333 (void)printf("don't have any more cards!\n");
334 (void)printf("My books:");
335 cb
= countbooks(comphand
);
336 (void)printf("Your books:");
337 ub
= countbooks(userhand
);
338 (void)printf("\nI have %d, you have %d.\n", cb
, ub
);
340 (void)printf("\nYou win!!!\n");
341 if (arc4random_uniform(1024) == 0723)
342 (void)printf("Cheater, cheater, pumpkin eater!\n");
343 } else if (cb
> ub
) {
344 (void)printf("\nI win!!!\n");
345 if (arc4random_uniform(1024) == 0723)
346 (void)printf("Hah! Stupid peasant!\n");
348 (void)printf("\nTie!\n");
353 printplayer(int player
)
360 (void)printf("You ");
366 printhand(const int *hand
)
370 for (book
= i
= 0; i
< RANKS
; i
++)
372 for (j
= hand
[i
]; --j
>= 0;)
377 (void)printf(" + Book%s of", book
> 1 ? "s" : "");
378 for (i
= 0; i
< RANKS
; i
++)
379 if (hand
[i
] == CARDS
)
386 countcards(const int *hand
)
390 for (count
= i
= 0; i
< RANKS
; i
++)
396 countbooks(const int *hand
)
400 for (count
= i
= 0; i
< RANKS
; i
++)
401 if (hand
[i
] == CARDS
) {
406 (void)printf(" none");
416 for (i
= 0; i
< TOTCARDS
; ++i
)
418 for (i
= 0; i
< TOTCARDS
- 1; ++i
) {
419 j
= arc4random_uniform(TOTCARDS
-i
);
426 for (i
= 0; i
< HANDSIZE
; ++i
) {
427 ++userhand
[deck
[--curcard
]];
428 ++comphand
[deck
[--curcard
]];
441 (void)printf("Would you like instructions (y or n)? ");
443 while (getchar() != '\n');
447 switch (pid
= fork()) {
452 if (!(pager
= getenv("PAGER")) || (*pager
== 0))
455 if ((fd
= open(_PATH_INSTR
, O_RDONLY
)) == -1)
456 err(1, "open %s", _PATH_INSTR
);
457 if (dup2(fd
, 0) == -1)
459 (void)execl("/bin/sh", "sh", "-c", pager
, (char *) NULL
);
460 err(1, "exec sh -c %s", pager
);
466 (void)waitpid(pid
, &status
, 0);
469 (void)printf("Hit return to continue...\n");
470 while ((input
= getchar()) != EOF
&& input
!= '\n');
476 (void)fprintf(stderr
, "usage: fish [-p]\n");