]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - fish/fish.c
78e2dc528cf13203f1ec478dbce10fc8ce1876a8
1 /* $NetBSD: fish.c,v 1.23 2018/03/05 04:59:54 eadler 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.23 2018/03/05 04:59:54 eadler Exp $");
49 #include <sys/types.h>
59 #include "pathnames.h"
64 #define TOTCARDS RANKS * CARDS
68 #define OTHER(a) (1 - (a))
70 static 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 static int asked
[RANKS
], comphand
[RANKS
], deck
[TOTCARDS
];
78 static int userasked
[RANKS
], userhand
[RANKS
];
79 static int curcard
= TOTCARDS
;
81 static void chkwinner(int, const int *);
82 static int compmove(void);
83 static int countbooks(const int *);
84 static int countcards(const int *);
85 static int drawcard(int, int *);
86 static int gofish(int, int, int *);
87 static void goodmove(int, int, int *, int *);
88 static void init(void);
89 static void instructions(void);
90 static int nrandom(int);
91 static void printhand(const int *);
92 static void printplayer(int);
93 static int promove(void);
94 static void usage(void) __dead
;
95 static int usermove(void);
98 main(int argc
, char **argv
)
102 /* Revoke setgid privileges */
105 while ((ch
= getopt(argc
, argv
, "p")) != -1)
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
);
153 const char *const *p
;
156 (void)printf("\nYour hand is:");
160 (void)printf("You ask me for: ");
161 (void)fflush(stdout
);
162 if (fgets(buf
, sizeof(buf
), stdin
) == NULL
)
166 if (buf
[0] == '\n') {
167 (void)printf("%d cards in my hand, %d in the pool.\n",
168 countcards(comphand
), curcard
);
169 (void)printf("My books:");
170 (void)countbooks(comphand
);
173 buf
[strlen(buf
) - 1] = '\0';
174 if (!strcasecmp(buf
, "p")) {
177 printf("Entering pro mode.\n");
180 printf("Already in pro mode.\n");
184 if (!strcasecmp(buf
, "quit"))
186 for (p
= cards
; *p
; ++p
)
187 if (!strcasecmp(*p
, buf
))
190 (void)printf("I don't understand!\n");
194 if (userhand
[n
] <= 3) {
198 if (userhand
[n
] == 4) {
199 printf("You already have all of those.\n");
204 (void)printf("You don't have any of those!\n");
206 (void)printf("You don't have any %s's!\n", cards
[n
]);
208 (void)printf("No cheating!\n");
209 (void)printf("Guess again.\n");
223 lmove
= (lmove
+ 1) % RANKS
;
224 } while (!comphand
[lmove
] || comphand
[lmove
] == CARDS
);
228 (void)printf("I ask you for: %s.\n", cards
[lmove
]);
237 for (i
= 0; i
< RANKS
; ++i
)
239 comphand
[i
] > 0 && comphand
[i
] < CARDS
) {
243 if (nrandom(3) == 1) {
245 if (comphand
[i
] && comphand
[i
] != CARDS
) {
250 if (comphand
[i
] != CARDS
&&
251 comphand
[i
] > comphand
[max
])
255 if (nrandom(1024) == 0723) {
256 for (i
= 0; i
< RANKS
; ++i
)
257 if (userhand
[i
] && comphand
[i
])
261 for (i
= 0; i
< RANKS
; ++i
)
262 if (comphand
[i
] && comphand
[i
] != CARDS
&&
265 for (i
= 0; i
< RANKS
; ++i
)
272 drawcard(int player
, int *hand
)
276 ++hand
[card
= deck
[--curcard
]];
277 if (player
== USER
|| hand
[card
] == CARDS
) {
279 (void)printf("drew %s", cards
[card
]);
280 if (hand
[card
] == CARDS
) {
281 (void)printf(" and made a book of %s's!\n",
283 chkwinner(player
, hand
);
291 gofish(int askedfor
, int player
, int *hand
)
293 printplayer(OTHER(player
));
294 (void)printf("say \"GO FISH!\"\n");
295 if (askedfor
== drawcard(player
, hand
)) {
297 (void)printf("drew the guess!\n");
299 (void)printf("get to ask again!\n");
306 goodmove(int player
, int move
, int *hand
, int *opphand
)
308 printplayer(OTHER(player
));
309 (void)printf("have %d %s%s.\n",
310 opphand
[move
], cards
[move
], opphand
[move
] == 1 ? "": "'s");
312 hand
[move
] += opphand
[move
];
315 if (hand
[move
] == CARDS
) {
317 (void)printf("made a book of %s's!\n", cards
[move
]);
318 chkwinner(player
, hand
);
321 chkwinner(OTHER(player
), opphand
);
324 (void)printf("get another guess!\n");
328 chkwinner(int player
, const int *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");
356 printplayer(int player
)
363 (void)printf("You ");
369 printhand(const int *hand
)
373 for (book
= i
= 0; i
< RANKS
; i
++)
375 for (j
= hand
[i
]; --j
>= 0;)
380 (void)printf(" + Book%s of", book
> 1 ? "s" : "");
381 for (i
= 0; i
< RANKS
; i
++)
382 if (hand
[i
] == CARDS
)
389 countcards(const int *hand
)
393 for (count
= i
= 0; i
< RANKS
; i
++)
399 countbooks(const int *hand
)
403 for (count
= i
= 0; i
< RANKS
; i
++)
404 if (hand
[i
] == CARDS
) {
409 (void)printf(" none");
419 for (i
= 0; i
< TOTCARDS
; ++i
)
421 for (i
= 0; i
< TOTCARDS
- 1; ++i
) {
422 j
= nrandom(TOTCARDS
-i
);
429 for (i
= 0; i
< HANDSIZE
; ++i
) {
430 ++userhand
[deck
[--curcard
]];
431 ++comphand
[deck
[--curcard
]];
439 return((int)random() % n
);
451 (void)printf("Would you like instructions (y or n)? ");
453 while (getchar() != '\n');
457 switch (pid
= fork()) {
462 if (!(pager
= getenv("PAGER")) || (*pager
== 0))
465 if ((fd
= open(_PATH_INSTR
, O_RDONLY
)) == -1)
466 err(1, "open %s", _PATH_INSTR
);
467 if (dup2(fd
, 0) == -1)
469 (void)execl("/bin/sh", "sh", "-c", pager
, (char *) NULL
);
470 err(1, "exec sh -c %s", pager
);
476 (void)waitpid(pid
, &status
, 0);
479 (void)printf("Hit return to continue...\n");
480 while ((input
= getchar()) != EOF
&& input
!= '\n');
486 (void)fprintf(stderr
, "usage: fish [-p]\n");