]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - fish/fish.c
1 /* $NetBSD: fish.c,v 1.4 1997/10/10 12:58:32 lukem 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.4 1997/10/10 12:58:32 lukem Exp $");
53 #include <sys/types.h>
54 #include <sys/errno.h>
60 #include "pathnames.h"
68 #define OTHER(a) (1 - (a))
71 "A", "2", "3", "4", "5", "6", "7",
72 "8", "9", "10", "J", "Q", "K", NULL
,
74 #define PRC(card) (void)printf(" %s", cards[card])
77 int asked
[RANKS
], comphand
[RANKS
], deck
[RANKS
];
78 int userasked
[RANKS
], userhand
[RANKS
];
80 void chkwinner
__P((int, int *));
81 int compmove
__P((void));
82 int countbooks
__P((int *));
83 int countcards
__P((int *));
84 int drawcard
__P((int, int *));
85 int gofish
__P((int, int, int *));
86 void goodmove
__P((int, int, int *, int *));
87 void init
__P((void));
88 void instructions
__P((void));
89 int main
__P((int, char *[]));
90 int nrandom
__P((int));
91 void printhand
__P((int *));
92 void printplayer
__P((int));
93 int promove
__P((void));
94 void usage
__P((void));
95 int usermove
__P((void));
104 while ((ch
= getopt(argc
, argv
, "p")) != -1)
111 (void)fprintf(stderr
, "usage: fish [-p]\n");
115 srandom(time((time_t *)NULL
));
119 if (nrandom(2) == 1) {
120 printplayer(COMPUTER
);
121 (void)printf("get to start.\n");
125 (void)printf("get to start.\n");
129 if (!comphand
[move
]) {
130 if (gofish(move
, USER
, userhand
))
133 goodmove(USER
, move
, userhand
, comphand
);
139 if (!userhand
[move
]) {
140 if (!gofish(move
, COMPUTER
, comphand
))
143 goodmove(COMPUTER
, move
, comphand
, userhand
);
156 (void)printf("\nYour hand is:");
160 (void)printf("You ask me for: ");
161 (void)fflush(stdout
);
162 if (fgets(buf
, BUFSIZ
, stdin
) == NULL
)
166 if (buf
[0] == '\n') {
167 (void)printf("%d cards in my hand, %d in the pool.\n",
168 countcards(comphand
), countcards(deck
));
169 (void)printf("My books:");
170 (void)countbooks(comphand
);
173 buf
[strlen(buf
) - 1] = '\0';
174 if (!strcasecmp(buf
, "p") && !promode
) {
176 (void)printf("Entering pro mode.\n");
179 if (!strcasecmp(buf
, "quit"))
181 for (p
= cards
; *p
; ++p
)
182 if (!strcasecmp(*p
, buf
))
185 (void)printf("I don't understand!\n");
194 (void)printf("You don't have any of those!\n");
196 (void)printf("You don't have any %s's!\n", cards
[n
]);
198 (void)printf("No cheating!\n");
199 (void)printf("Guess again.\n");
213 lmove
= (lmove
+ 1) % RANKS
;
214 } while (!comphand
[lmove
] || comphand
[lmove
] == CARDS
);
218 (void)printf("I ask you for: %s.\n", cards
[lmove
]);
227 for (i
= 0; i
< RANKS
; ++i
)
229 comphand
[i
] > 0 && comphand
[i
] < CARDS
) {
233 if (nrandom(3) == 1) {
235 if (comphand
[i
] && comphand
[i
] != CARDS
) {
240 if (comphand
[i
] != CARDS
&&
241 comphand
[i
] > comphand
[max
])
245 if (nrandom(1024) == 0723) {
246 for (i
= 0; i
< RANKS
; ++i
)
247 if (userhand
[i
] && comphand
[i
])
251 for (i
= 0; i
< RANKS
; ++i
)
252 if (comphand
[i
] && comphand
[i
] != CARDS
&&
255 for (i
= 0; i
< RANKS
; ++i
)
262 drawcard(player
, hand
)
268 while (deck
[card
= nrandom(RANKS
)] == 0);
271 if (player
== USER
|| hand
[card
] == CARDS
) {
273 (void)printf("drew %s", cards
[card
]);
274 if (hand
[card
] == CARDS
) {
275 (void)printf(" and made a book of %s's!\n",
277 chkwinner(player
, hand
);
285 gofish(askedfor
, player
, hand
)
286 int askedfor
, player
;
289 printplayer(OTHER(player
));
290 (void)printf("say \"GO FISH!\"\n");
291 if (askedfor
== drawcard(player
, hand
)) {
293 (void)printf("drew the guess!\n");
295 (void)printf("get to ask again!\n");
302 goodmove(player
, move
, hand
, opphand
)
306 printplayer(OTHER(player
));
307 (void)printf("have %d %s%s.\n",
308 opphand
[move
], cards
[move
], opphand
[move
] == 1 ? "": "'s");
310 hand
[move
] += opphand
[move
];
313 if (hand
[move
] == CARDS
) {
315 (void)printf("made a book of %s's!\n", cards
[move
]);
316 chkwinner(player
, hand
);
319 chkwinner(OTHER(player
), opphand
);
322 (void)printf("get another guess!\n");
326 chkwinner(player
, hand
)
332 for (i
= 0; i
< RANKS
; ++i
)
333 if (hand
[i
] > 0 && hand
[i
] < CARDS
)
336 (void)printf("don't have any more cards!\n");
337 (void)printf("My books:");
338 cb
= countbooks(comphand
);
339 (void)printf("Your books:");
340 ub
= countbooks(userhand
);
341 (void)printf("\nI have %d, you have %d.\n", cb
, ub
);
343 (void)printf("\nYou win!!!\n");
344 if (nrandom(1024) == 0723)
345 (void)printf("Cheater, cheater, pumpkin eater!\n");
346 } else if (cb
> ub
) {
347 (void)printf("\nI win!!!\n");
348 if (nrandom(1024) == 0723)
349 (void)printf("Hah! Stupid peasant!\n");
351 (void)printf("\nTie!\n");
364 (void)printf("You ");
375 for (book
= i
= 0; i
< RANKS
; i
++)
377 for (j
= hand
[i
]; --j
>= 0;)
382 (void)printf(" + Book%s of", book
> 1 ? "s" : "");
383 for (i
= 0; i
< RANKS
; i
++)
384 if (hand
[i
] == CARDS
)
396 for (count
= i
= 0; i
< RANKS
; i
++)
407 for (count
= i
= 0; i
< RANKS
; i
++)
408 if (hand
[i
] == CARDS
) {
413 (void)printf(" none");
423 for (i
= 0; i
< RANKS
; ++i
)
425 for (i
= 0; i
< HANDSIZE
; ++i
) {
426 while (!deck
[rank
= nrandom(RANKS
)]);
430 for (i
= 0; i
< HANDSIZE
; ++i
) {
431 while (!deck
[rank
= nrandom(RANKS
)]);
442 return((int)random() % n
);
451 (void)printf("Would you like instructions (y or n)? ");
453 while (getchar() != '\n');
457 (void)sprintf(buf
, "%s %s", _PATH_MORE
, _PATH_INSTR
);
459 (void)printf("Hit return to continue...\n");
460 while ((input
= getchar()) != EOF
&& input
!= '\n');
466 (void)fprintf(stderr
, "usage: fish [-p]\n");