]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - fish/fish.c
1 /* $NetBSD: fish.c,v 1.16 2005/02/15 12:56:20 jsm 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\n\
38 The Regents of the University of California. All rights reserved.\n");
43 static char sccsid
[] = "@(#)fish.c 8.1 (Berkeley) 5/31/93";
45 __RCSID("$NetBSD: fish.c,v 1.16 2005/02/15 12:56:20 jsm Exp $");
49 #include <sys/types.h>
59 #include "pathnames.h"
64 #define TOTCARDS RANKS * CARDS
68 #define OTHER(a) (1 - (a))
70 const char *const cards
[] = {
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
[TOTCARDS
];
78 int userasked
[RANKS
], userhand
[RANKS
];
79 int curcard
= TOTCARDS
;
81 void chkwinner(int, const int *);
83 int countbooks(const int *);
84 int countcards(const int *);
85 int drawcard(int, int *);
86 int gofish(int, int, int *);
87 void goodmove(int, int, int *, int *);
89 void instructions(void);
90 int main(int, char *[]);
92 void printhand(const int *);
93 void printplayer(int);
95 void usage(void) __attribute__((__noreturn__
));
105 /* Revoke setgid privileges */
108 while ((ch
= getopt(argc
, argv
, "p")) != -1)
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
);
156 const char *const *p
;
159 (void)printf("\nYour hand is:");
163 (void)printf("You ask me for: ");
164 (void)fflush(stdout
);
165 if (fgets(buf
, sizeof(buf
), stdin
) == NULL
)
169 if (buf
[0] == '\n') {
170 (void)printf("%d cards in my hand, %d in the pool.\n",
171 countcards(comphand
), curcard
);
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 ++hand
[card
= deck
[--curcard
]];
272 if (player
== USER
|| hand
[card
] == CARDS
) {
274 (void)printf("drew %s", cards
[card
]);
275 if (hand
[card
] == CARDS
) {
276 (void)printf(" and made a book of %s's!\n",
278 chkwinner(player
, hand
);
286 gofish(askedfor
, player
, hand
)
287 int askedfor
, player
;
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(player
, move
, hand
, opphand
)
307 printplayer(OTHER(player
));
308 (void)printf("have %d %s%s.\n",
309 opphand
[move
], cards
[move
], opphand
[move
] == 1 ? "": "'s");
311 hand
[move
] += opphand
[move
];
314 if (hand
[move
] == CARDS
) {
316 (void)printf("made a book of %s's!\n", cards
[move
]);
317 chkwinner(player
, hand
);
320 chkwinner(OTHER(player
), opphand
);
323 (void)printf("get another guess!\n");
327 chkwinner(player
, hand
)
333 for (i
= 0; i
< RANKS
; ++i
)
334 if (hand
[i
] > 0 && hand
[i
] < CARDS
)
337 (void)printf("don't have any more cards!\n");
338 (void)printf("My books:");
339 cb
= countbooks(comphand
);
340 (void)printf("Your books:");
341 ub
= countbooks(userhand
);
342 (void)printf("\nI have %d, you have %d.\n", cb
, ub
);
344 (void)printf("\nYou win!!!\n");
345 if (nrandom(1024) == 0723)
346 (void)printf("Cheater, cheater, pumpkin eater!\n");
347 } else if (cb
> ub
) {
348 (void)printf("\nI win!!!\n");
349 if (nrandom(1024) == 0723)
350 (void)printf("Hah! Stupid peasant!\n");
352 (void)printf("\nTie!\n");
365 (void)printf("You ");
376 for (book
= i
= 0; i
< RANKS
; i
++)
378 for (j
= hand
[i
]; --j
>= 0;)
383 (void)printf(" + Book%s of", book
> 1 ? "s" : "");
384 for (i
= 0; i
< RANKS
; i
++)
385 if (hand
[i
] == CARDS
)
397 for (count
= i
= 0; i
< RANKS
; i
++)
408 for (count
= i
= 0; i
< RANKS
; i
++)
409 if (hand
[i
] == CARDS
) {
414 (void)printf(" none");
424 for (i
= 0; i
< TOTCARDS
; ++i
)
426 for (i
= 0; i
< TOTCARDS
- 1; ++i
) {
427 j
= nrandom(TOTCARDS
-i
);
434 for (i
= 0; i
< HANDSIZE
; ++i
) {
435 ++userhand
[deck
[--curcard
]];
436 ++comphand
[deck
[--curcard
]];
445 return((int)random() % n
);
457 (void)printf("Would you like instructions (y or n)? ");
459 while (getchar() != '\n');
463 switch (pid
= fork()) {
468 if (!(pager
= getenv("PAGER")) || (*pager
== 0))
471 if ((fd
= open(_PATH_INSTR
, O_RDONLY
)) == -1)
472 err(1, "open %s", _PATH_INSTR
);
473 if (dup2(fd
, 0) == -1)
475 (void)execl("/bin/sh", "sh", "-c", pager
, (char *) NULL
);
476 err(1, "exec sh -c %s", pager
);
482 (void)waitpid(pid
, &status
, 0);
485 (void)printf("Hit return to continue...\n");
486 while ((input
= getchar()) != EOF
&& input
!= '\n');
492 (void)fprintf(stderr
, "usage: fish [-p]\n");