]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - fish/fish.c
c81da3ff0d00743b3a89a9d381e6ea50220af5b6
1 /* $NetBSD: fish.c,v 1.6 1998/09/13 15:27:27 hubertf 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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include <sys/cdefs.h>
41 __COPYRIGHT("@(#) Copyright (c) 1990, 1993\n\
42 The Regents of the University of California. All rights reserved.\n");
47 static char sccsid
[] = "@(#)fish.c 8.1 (Berkeley) 5/31/93";
49 __RCSID("$NetBSD: fish.c,v 1.6 1998/09/13 15:27:27 hubertf Exp $");
53 #include <sys/types.h>
63 #include "pathnames.h"
71 #define OTHER(a) (1 - (a))
74 "A", "2", "3", "4", "5", "6", "7",
75 "8", "9", "10", "J", "Q", "K", NULL
,
77 #define PRC(card) (void)printf(" %s", cards[card])
80 int asked
[RANKS
], comphand
[RANKS
], deck
[RANKS
];
81 int userasked
[RANKS
], userhand
[RANKS
];
83 void chkwinner
__P((int, int *));
84 int compmove
__P((void));
85 int countbooks
__P((int *));
86 int countcards
__P((int *));
87 int drawcard
__P((int, int *));
88 int gofish
__P((int, int, int *));
89 void goodmove
__P((int, int, int *, int *));
90 void init
__P((void));
91 void instructions
__P((void));
92 int main
__P((int, char *[]));
93 int nrandom
__P((int));
94 void printhand
__P((int *));
95 void printplayer
__P((int));
96 int promove
__P((void));
97 void usage
__P((void)) __attribute__((__noreturn__
));
98 int usermove
__P((void));
107 while ((ch
= getopt(argc
, argv
, "p")) != -1)
114 (void)fprintf(stderr
, "usage: fish [-p]\n");
118 srandom(time((time_t *)NULL
));
122 if (nrandom(2) == 1) {
123 printplayer(COMPUTER
);
124 (void)printf("get to start.\n");
128 (void)printf("get to start.\n");
132 if (!comphand
[move
]) {
133 if (gofish(move
, USER
, userhand
))
136 goodmove(USER
, move
, userhand
, comphand
);
142 if (!userhand
[move
]) {
143 if (!gofish(move
, COMPUTER
, comphand
))
146 goodmove(COMPUTER
, move
, comphand
, userhand
);
159 (void)printf("\nYour hand is:");
163 (void)printf("You ask me for: ");
164 (void)fflush(stdout
);
165 if (fgets(buf
, BUFSIZ
, stdin
) == NULL
)
169 if (buf
[0] == '\n') {
170 (void)printf("%d cards in my hand, %d in the pool.\n",
171 countcards(comphand
), countcards(deck
));
172 (void)printf("My books:");
173 (void)countbooks(comphand
);
176 buf
[strlen(buf
) - 1] = '\0';
177 if (!strcasecmp(buf
, "p") && !promode
) {
179 (void)printf("Entering pro mode.\n");
182 if (!strcasecmp(buf
, "quit"))
184 for (p
= cards
; *p
; ++p
)
185 if (!strcasecmp(*p
, buf
))
188 (void)printf("I don't understand!\n");
197 (void)printf("You don't have any of those!\n");
199 (void)printf("You don't have any %s's!\n", cards
[n
]);
201 (void)printf("No cheating!\n");
202 (void)printf("Guess again.\n");
216 lmove
= (lmove
+ 1) % RANKS
;
217 } while (!comphand
[lmove
] || comphand
[lmove
] == CARDS
);
221 (void)printf("I ask you for: %s.\n", cards
[lmove
]);
230 for (i
= 0; i
< RANKS
; ++i
)
232 comphand
[i
] > 0 && comphand
[i
] < CARDS
) {
236 if (nrandom(3) == 1) {
238 if (comphand
[i
] && comphand
[i
] != CARDS
) {
243 if (comphand
[i
] != CARDS
&&
244 comphand
[i
] > comphand
[max
])
248 if (nrandom(1024) == 0723) {
249 for (i
= 0; i
< RANKS
; ++i
)
250 if (userhand
[i
] && comphand
[i
])
254 for (i
= 0; i
< RANKS
; ++i
)
255 if (comphand
[i
] && comphand
[i
] != CARDS
&&
258 for (i
= 0; i
< RANKS
; ++i
)
265 drawcard(player
, hand
)
271 while (deck
[card
= nrandom(RANKS
)] == 0);
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(askedfor
, player
, hand
)
289 int askedfor
, player
;
292 printplayer(OTHER(player
));
293 (void)printf("say \"GO FISH!\"\n");
294 if (askedfor
== drawcard(player
, hand
)) {
296 (void)printf("drew the guess!\n");
298 (void)printf("get to ask again!\n");
305 goodmove(player
, move
, hand
, opphand
)
309 printplayer(OTHER(player
));
310 (void)printf("have %d %s%s.\n",
311 opphand
[move
], cards
[move
], opphand
[move
] == 1 ? "": "'s");
313 hand
[move
] += opphand
[move
];
316 if (hand
[move
] == CARDS
) {
318 (void)printf("made a book of %s's!\n", cards
[move
]);
319 chkwinner(player
, hand
);
322 chkwinner(OTHER(player
), opphand
);
325 (void)printf("get another guess!\n");
329 chkwinner(player
, hand
)
335 for (i
= 0; i
< RANKS
; ++i
)
336 if (hand
[i
] > 0 && hand
[i
] < CARDS
)
339 (void)printf("don't have any more cards!\n");
340 (void)printf("My books:");
341 cb
= countbooks(comphand
);
342 (void)printf("Your books:");
343 ub
= countbooks(userhand
);
344 (void)printf("\nI have %d, you have %d.\n", cb
, ub
);
346 (void)printf("\nYou win!!!\n");
347 if (nrandom(1024) == 0723)
348 (void)printf("Cheater, cheater, pumpkin eater!\n");
349 } else if (cb
> ub
) {
350 (void)printf("\nI win!!!\n");
351 if (nrandom(1024) == 0723)
352 (void)printf("Hah! Stupid peasant!\n");
354 (void)printf("\nTie!\n");
367 (void)printf("You ");
378 for (book
= i
= 0; i
< RANKS
; i
++)
380 for (j
= hand
[i
]; --j
>= 0;)
385 (void)printf(" + Book%s of", book
> 1 ? "s" : "");
386 for (i
= 0; i
< RANKS
; i
++)
387 if (hand
[i
] == CARDS
)
399 for (count
= i
= 0; i
< RANKS
; i
++)
410 for (count
= i
= 0; i
< RANKS
; i
++)
411 if (hand
[i
] == CARDS
) {
416 (void)printf(" none");
426 for (i
= 0; i
< RANKS
; ++i
)
428 for (i
= 0; i
< HANDSIZE
; ++i
) {
429 while (!deck
[rank
= nrandom(RANKS
)]);
433 for (i
= 0; i
< HANDSIZE
; ++i
) {
434 while (!deck
[rank
= nrandom(RANKS
)]);
445 return((int)random() % n
);
455 (void)printf("Would you like instructions (y or n)? ");
457 while (getchar() != '\n');
461 switch (pid
= fork()) {
463 (void)setuid(getuid());
464 (void)setgid(getgid());
465 (void)execl(_PATH_MORE
, "more", _PATH_INSTR
, NULL
);
466 err(1, "%s %s", _PATH_MORE
, _PATH_INSTR
);
472 (void)waitpid(pid
, &status
, 0);
475 (void)printf("Hit return to continue...\n");
476 while ((input
= getchar()) != EOF
&& input
!= '\n');
482 (void)fprintf(stderr
, "usage: fish [-p]\n");