]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - fish/fish.c
2 * Copyright (c) 1990 The Regents of the University of California.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 "@(#) Copyright (c) 1990 The Regents of the University of California.\n\
40 All rights reserved.\n";
44 /*static char sccsid[] = "from: @(#)fish.c 5.4 (Berkeley) 1/18/91";*/
45 static char rcsid
[] = "$Id: fish.c,v 1.2 1993/08/01 18:54:54 mycroft Exp $";
48 #include <sys/types.h>
49 #include <sys/errno.h>
54 #include "pathnames.h"
62 #define OTHER(a) (1 - (a))
65 "A", "2", "3", "4", "5", "6", "7",
66 "8", "9", "10", "J", "Q", "K", NULL
,
68 #define PRC(card) (void)printf(" %s", cards[card])
71 int asked
[RANKS
], comphand
[RANKS
], deck
[RANKS
];
72 int userasked
[RANKS
], userhand
[RANKS
];
80 while ((ch
= getopt(argc
, argv
, "p")) != EOF
)
87 (void)fprintf(stderr
, "usage: fish [-p]\n");
91 srandom(time((time_t *)NULL
));
95 if (nrandom(2) == 1) {
96 printplayer(COMPUTER
);
97 (void)printf("get to start.\n");
101 (void)printf("get to start.\n");
105 if (!comphand
[move
]) {
106 if (gofish(move
, USER
, userhand
))
109 goodmove(USER
, move
, userhand
, comphand
);
115 if (!userhand
[move
]) {
116 if (!gofish(move
, COMPUTER
, comphand
))
119 goodmove(COMPUTER
, move
, comphand
, userhand
);
131 (void)printf("\nYour hand is:");
135 (void)printf("You ask me for: ");
136 (void)fflush(stdout
);
137 if (fgets(buf
, BUFSIZ
, stdin
) == NULL
)
141 if (buf
[0] == '\n') {
142 (void)printf("%d cards in my hand, %d in the pool.\n",
143 countcards(comphand
), countcards(deck
));
144 (void)printf("My books:");
145 (void)countbooks(comphand
);
148 buf
[strlen(buf
) - 1] = '\0';
149 if (!strcasecmp(buf
, "p") && !promode
) {
151 (void)printf("Entering pro mode.\n");
154 if (!strcasecmp(buf
, "quit"))
156 for (p
= cards
; *p
; ++p
)
157 if (!strcasecmp(*p
, buf
))
160 (void)printf("I don't understand!\n");
169 (void)printf("You don't have any of those!\n");
171 (void)printf("You don't have any %s's!\n", cards
[n
]);
173 (void)printf("No cheating!\n");
174 (void)printf("Guess again.\n");
187 lmove
= (lmove
+ 1) % RANKS
;
188 } while (!comphand
[lmove
] || comphand
[lmove
] == CARDS
);
192 (void)printf("I ask you for: %s.\n", cards
[lmove
]);
200 for (i
= 0; i
< RANKS
; ++i
)
202 comphand
[i
] > 0 && comphand
[i
] < CARDS
) {
206 if (nrandom(3) == 1) {
208 if (comphand
[i
] && comphand
[i
] != CARDS
) {
213 if (comphand
[i
] != CARDS
&&
214 comphand
[i
] > comphand
[max
])
218 if (nrandom(1024) == 0723) {
219 for (i
= 0; i
< RANKS
; ++i
)
220 if (userhand
[i
] && comphand
[i
])
224 for (i
= 0; i
< RANKS
; ++i
)
225 if (comphand
[i
] && comphand
[i
] != CARDS
&&
228 for (i
= 0; i
< RANKS
; ++i
)
234 drawcard(player
, hand
)
240 while (deck
[card
= nrandom(RANKS
)] == 0);
243 if (player
== USER
|| hand
[card
] == CARDS
) {
245 (void)printf("drew %s", cards
[card
]);
246 if (hand
[card
] == CARDS
) {
247 (void)printf(" and made a book of %s's!\n",
249 chkwinner(player
, hand
);
256 gofish(askedfor
, player
, hand
)
257 int askedfor
, player
;
260 printplayer(OTHER(player
));
261 (void)printf("say \"GO FISH!\"\n");
262 if (askedfor
== drawcard(player
, hand
)) {
264 (void)printf("drew the guess!\n");
266 (void)printf("get to ask again!\n");
272 goodmove(player
, move
, hand
, opphand
)
276 printplayer(OTHER(player
));
277 (void)printf("have %d %s%s.\n",
278 opphand
[move
], cards
[move
], opphand
[move
] == 1 ? "": "'s");
280 hand
[move
] += opphand
[move
];
283 if (hand
[move
] == CARDS
) {
285 (void)printf("made a book of %s's!\n", cards
[move
]);
286 chkwinner(player
, hand
);
289 chkwinner(OTHER(player
), opphand
);
292 (void)printf("get another guess!\n");
295 chkwinner(player
, hand
)
299 register int cb
, i
, ub
;
301 for (i
= 0; i
< RANKS
; ++i
)
302 if (hand
[i
] > 0 && hand
[i
] < CARDS
)
305 (void)printf("don't have any more cards!\n");
306 (void)printf("My books:");
307 cb
= countbooks(comphand
);
308 (void)printf("Your books:");
309 ub
= countbooks(userhand
);
310 (void)printf("\nI have %d, you have %d.\n", cb
, ub
);
312 (void)printf("\nYou win!!!\n");
313 if (nrandom(1024) == 0723)
314 (void)printf("Cheater, cheater, pumpkin eater!\n");
315 } else if (cb
> ub
) {
316 (void)printf("\nI win!!!\n");
317 if (nrandom(1024) == 0723)
318 (void)printf("Hah! Stupid peasant!\n");
320 (void)printf("\nTie!\n");
332 (void)printf("You ");
340 register int book
, i
, j
;
342 for (book
= i
= 0; i
< RANKS
; i
++)
344 for (j
= hand
[i
]; --j
>= 0;)
349 (void)printf(" + Book%s of", book
> 1 ? "s" : "");
350 for (i
= 0; i
< RANKS
; i
++)
351 if (hand
[i
] == CARDS
)
360 register int i
, count
;
362 for (count
= i
= 0; i
< RANKS
; i
++)
372 for (count
= i
= 0; i
< RANKS
; i
++)
373 if (hand
[i
] == CARDS
) {
378 (void)printf(" none");
385 register int i
, rank
;
387 for (i
= 0; i
< RANKS
; ++i
)
389 for (i
= 0; i
< HANDSIZE
; ++i
) {
390 while (!deck
[rank
= nrandom(RANKS
)]);
394 for (i
= 0; i
< HANDSIZE
; ++i
) {
395 while (!deck
[rank
= nrandom(RANKS
)]);
406 return((int)random() % n
);
414 (void)printf("Would you like instructions (y or n)? ");
416 while (getchar() != '\n');
420 (void)sprintf(buf
, "%s %s", _PATH_MORE
, _PATH_INSTR
);
422 (void)printf("Hit return to continue...\n");
423 while ((input
= getchar()) != EOF
&& input
!= '\n');
428 (void)fprintf(stderr
, "usage: fish [-p]\n");