1*6a76678eSAlexandre Beletti /* $NetBSD: fish.c,v 1.22 2011/09/01 07:18:50 plunky Exp $ */
2*6a76678eSAlexandre Beletti
3*6a76678eSAlexandre Beletti /*-
4*6a76678eSAlexandre Beletti * Copyright (c) 1990, 1993
5*6a76678eSAlexandre Beletti * The Regents of the University of California. All rights reserved.
6*6a76678eSAlexandre Beletti *
7*6a76678eSAlexandre Beletti * This code is derived from software contributed to Berkeley by
8*6a76678eSAlexandre Beletti * Muffy Barkocy.
9*6a76678eSAlexandre Beletti *
10*6a76678eSAlexandre Beletti * Redistribution and use in source and binary forms, with or without
11*6a76678eSAlexandre Beletti * modification, are permitted provided that the following conditions
12*6a76678eSAlexandre Beletti * are met:
13*6a76678eSAlexandre Beletti * 1. Redistributions of source code must retain the above copyright
14*6a76678eSAlexandre Beletti * notice, this list of conditions and the following disclaimer.
15*6a76678eSAlexandre Beletti * 2. Redistributions in binary form must reproduce the above copyright
16*6a76678eSAlexandre Beletti * notice, this list of conditions and the following disclaimer in the
17*6a76678eSAlexandre Beletti * documentation and/or other materials provided with the distribution.
18*6a76678eSAlexandre Beletti * 3. Neither the name of the University nor the names of its contributors
19*6a76678eSAlexandre Beletti * may be used to endorse or promote products derived from this software
20*6a76678eSAlexandre Beletti * without specific prior written permission.
21*6a76678eSAlexandre Beletti *
22*6a76678eSAlexandre Beletti * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23*6a76678eSAlexandre Beletti * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24*6a76678eSAlexandre Beletti * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25*6a76678eSAlexandre Beletti * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26*6a76678eSAlexandre Beletti * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27*6a76678eSAlexandre Beletti * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28*6a76678eSAlexandre Beletti * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29*6a76678eSAlexandre Beletti * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30*6a76678eSAlexandre Beletti * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31*6a76678eSAlexandre Beletti * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32*6a76678eSAlexandre Beletti * SUCH DAMAGE.
33*6a76678eSAlexandre Beletti */
34*6a76678eSAlexandre Beletti
35*6a76678eSAlexandre Beletti #include <sys/cdefs.h>
36*6a76678eSAlexandre Beletti #ifndef lint
37*6a76678eSAlexandre Beletti __COPYRIGHT("@(#) Copyright (c) 1990, 1993\
38*6a76678eSAlexandre Beletti The Regents of the University of California. All rights reserved.");
39*6a76678eSAlexandre Beletti #endif /* not lint */
40*6a76678eSAlexandre Beletti
41*6a76678eSAlexandre Beletti #ifndef lint
42*6a76678eSAlexandre Beletti #if 0
43*6a76678eSAlexandre Beletti static char sccsid[] = "@(#)fish.c 8.1 (Berkeley) 5/31/93";
44*6a76678eSAlexandre Beletti #else
45*6a76678eSAlexandre Beletti __RCSID("$NetBSD: fish.c,v 1.22 2011/09/01 07:18:50 plunky Exp $");
46*6a76678eSAlexandre Beletti #endif
47*6a76678eSAlexandre Beletti #endif /* not lint */
48*6a76678eSAlexandre Beletti
49*6a76678eSAlexandre Beletti #include <sys/types.h>
50*6a76678eSAlexandre Beletti #include <sys/wait.h>
51*6a76678eSAlexandre Beletti #include <errno.h>
52*6a76678eSAlexandre Beletti #include <fcntl.h>
53*6a76678eSAlexandre Beletti #include <stdio.h>
54*6a76678eSAlexandre Beletti #include <stdlib.h>
55*6a76678eSAlexandre Beletti #include <unistd.h>
56*6a76678eSAlexandre Beletti #include <string.h>
57*6a76678eSAlexandre Beletti #include <time.h>
58*6a76678eSAlexandre Beletti #include <err.h>
59*6a76678eSAlexandre Beletti #include "pathnames.h"
60*6a76678eSAlexandre Beletti
61*6a76678eSAlexandre Beletti #define RANKS 13
62*6a76678eSAlexandre Beletti #define HANDSIZE 7
63*6a76678eSAlexandre Beletti #define CARDS 4
64*6a76678eSAlexandre Beletti #define TOTCARDS RANKS * CARDS
65*6a76678eSAlexandre Beletti
66*6a76678eSAlexandre Beletti #define USER 1
67*6a76678eSAlexandre Beletti #define COMPUTER 0
68*6a76678eSAlexandre Beletti #define OTHER(a) (1 - (a))
69*6a76678eSAlexandre Beletti
70*6a76678eSAlexandre Beletti static const char *const cards[] = {
71*6a76678eSAlexandre Beletti "A", "2", "3", "4", "5", "6", "7",
72*6a76678eSAlexandre Beletti "8", "9", "10", "J", "Q", "K", NULL,
73*6a76678eSAlexandre Beletti };
74*6a76678eSAlexandre Beletti #define PRC(card) (void)printf(" %s", cards[card])
75*6a76678eSAlexandre Beletti
76*6a76678eSAlexandre Beletti static int promode;
77*6a76678eSAlexandre Beletti static int asked[RANKS], comphand[RANKS], deck[TOTCARDS];
78*6a76678eSAlexandre Beletti static int userasked[RANKS], userhand[RANKS];
79*6a76678eSAlexandre Beletti static int curcard = TOTCARDS;
80*6a76678eSAlexandre Beletti
81*6a76678eSAlexandre Beletti static void chkwinner(int, const int *);
82*6a76678eSAlexandre Beletti static int compmove(void);
83*6a76678eSAlexandre Beletti static int countbooks(const int *);
84*6a76678eSAlexandre Beletti static int countcards(const int *);
85*6a76678eSAlexandre Beletti static int drawcard(int, int *);
86*6a76678eSAlexandre Beletti static int gofish(int, int, int *);
87*6a76678eSAlexandre Beletti static void goodmove(int, int, int *, int *);
88*6a76678eSAlexandre Beletti static void init(void);
89*6a76678eSAlexandre Beletti static void instructions(void);
90*6a76678eSAlexandre Beletti static int nrandom(int);
91*6a76678eSAlexandre Beletti static void printhand(const int *);
92*6a76678eSAlexandre Beletti static void printplayer(int);
93*6a76678eSAlexandre Beletti static int promove(void);
94*6a76678eSAlexandre Beletti static void usage(void) __dead;
95*6a76678eSAlexandre Beletti static int usermove(void);
96*6a76678eSAlexandre Beletti
97*6a76678eSAlexandre Beletti int
main(int argc,char ** argv)98*6a76678eSAlexandre Beletti main(int argc, char **argv)
99*6a76678eSAlexandre Beletti {
100*6a76678eSAlexandre Beletti int ch, move;
101*6a76678eSAlexandre Beletti
102*6a76678eSAlexandre Beletti /* Revoke setgid privileges */
103*6a76678eSAlexandre Beletti setgid(getgid());
104*6a76678eSAlexandre Beletti
105*6a76678eSAlexandre Beletti while ((ch = getopt(argc, argv, "p")) != -1)
106*6a76678eSAlexandre Beletti switch(ch) {
107*6a76678eSAlexandre Beletti case 'p':
108*6a76678eSAlexandre Beletti promode = 1;
109*6a76678eSAlexandre Beletti break;
110*6a76678eSAlexandre Beletti case '?':
111*6a76678eSAlexandre Beletti default:
112*6a76678eSAlexandre Beletti usage();
113*6a76678eSAlexandre Beletti }
114*6a76678eSAlexandre Beletti
115*6a76678eSAlexandre Beletti srandom(time(NULL));
116*6a76678eSAlexandre Beletti instructions();
117*6a76678eSAlexandre Beletti init();
118*6a76678eSAlexandre Beletti
119*6a76678eSAlexandre Beletti if (nrandom(2) == 1) {
120*6a76678eSAlexandre Beletti printplayer(COMPUTER);
121*6a76678eSAlexandre Beletti (void)printf("get to start.\n");
122*6a76678eSAlexandre Beletti goto istart;
123*6a76678eSAlexandre Beletti }
124*6a76678eSAlexandre Beletti printplayer(USER);
125*6a76678eSAlexandre Beletti (void)printf("get to start.\n");
126*6a76678eSAlexandre Beletti
127*6a76678eSAlexandre Beletti for (;;) {
128*6a76678eSAlexandre Beletti move = usermove();
129*6a76678eSAlexandre Beletti if (!comphand[move]) {
130*6a76678eSAlexandre Beletti if (gofish(move, USER, userhand))
131*6a76678eSAlexandre Beletti continue;
132*6a76678eSAlexandre Beletti } else {
133*6a76678eSAlexandre Beletti goodmove(USER, move, userhand, comphand);
134*6a76678eSAlexandre Beletti continue;
135*6a76678eSAlexandre Beletti }
136*6a76678eSAlexandre Beletti
137*6a76678eSAlexandre Beletti istart: for (;;) {
138*6a76678eSAlexandre Beletti move = compmove();
139*6a76678eSAlexandre Beletti if (!userhand[move]) {
140*6a76678eSAlexandre Beletti if (!gofish(move, COMPUTER, comphand))
141*6a76678eSAlexandre Beletti break;
142*6a76678eSAlexandre Beletti } else
143*6a76678eSAlexandre Beletti goodmove(COMPUTER, move, comphand, userhand);
144*6a76678eSAlexandre Beletti }
145*6a76678eSAlexandre Beletti }
146*6a76678eSAlexandre Beletti /* NOTREACHED */
147*6a76678eSAlexandre Beletti }
148*6a76678eSAlexandre Beletti
149*6a76678eSAlexandre Beletti static int
usermove(void)150*6a76678eSAlexandre Beletti usermove(void)
151*6a76678eSAlexandre Beletti {
152*6a76678eSAlexandre Beletti int n;
153*6a76678eSAlexandre Beletti const char *const *p;
154*6a76678eSAlexandre Beletti char buf[256];
155*6a76678eSAlexandre Beletti
156*6a76678eSAlexandre Beletti (void)printf("\nYour hand is:");
157*6a76678eSAlexandre Beletti printhand(userhand);
158*6a76678eSAlexandre Beletti
159*6a76678eSAlexandre Beletti for (;;) {
160*6a76678eSAlexandre Beletti (void)printf("You ask me for: ");
161*6a76678eSAlexandre Beletti (void)fflush(stdout);
162*6a76678eSAlexandre Beletti if (fgets(buf, sizeof(buf), stdin) == NULL)
163*6a76678eSAlexandre Beletti exit(0);
164*6a76678eSAlexandre Beletti if (buf[0] == '\0')
165*6a76678eSAlexandre Beletti continue;
166*6a76678eSAlexandre Beletti if (buf[0] == '\n') {
167*6a76678eSAlexandre Beletti (void)printf("%d cards in my hand, %d in the pool.\n",
168*6a76678eSAlexandre Beletti countcards(comphand), curcard);
169*6a76678eSAlexandre Beletti (void)printf("My books:");
170*6a76678eSAlexandre Beletti (void)countbooks(comphand);
171*6a76678eSAlexandre Beletti continue;
172*6a76678eSAlexandre Beletti }
173*6a76678eSAlexandre Beletti buf[strlen(buf) - 1] = '\0';
174*6a76678eSAlexandre Beletti if (!strcasecmp(buf, "p") && !promode) {
175*6a76678eSAlexandre Beletti promode = 1;
176*6a76678eSAlexandre Beletti (void)printf("Entering pro mode.\n");
177*6a76678eSAlexandre Beletti continue;
178*6a76678eSAlexandre Beletti }
179*6a76678eSAlexandre Beletti if (!strcasecmp(buf, "quit"))
180*6a76678eSAlexandre Beletti exit(0);
181*6a76678eSAlexandre Beletti for (p = cards; *p; ++p)
182*6a76678eSAlexandre Beletti if (!strcasecmp(*p, buf))
183*6a76678eSAlexandre Beletti break;
184*6a76678eSAlexandre Beletti if (!*p) {
185*6a76678eSAlexandre Beletti (void)printf("I don't understand!\n");
186*6a76678eSAlexandre Beletti continue;
187*6a76678eSAlexandre Beletti }
188*6a76678eSAlexandre Beletti n = p - cards;
189*6a76678eSAlexandre Beletti if (userhand[n]) {
190*6a76678eSAlexandre Beletti userasked[n] = 1;
191*6a76678eSAlexandre Beletti return(n);
192*6a76678eSAlexandre Beletti }
193*6a76678eSAlexandre Beletti if (nrandom(3) == 1)
194*6a76678eSAlexandre Beletti (void)printf("You don't have any of those!\n");
195*6a76678eSAlexandre Beletti else
196*6a76678eSAlexandre Beletti (void)printf("You don't have any %s's!\n", cards[n]);
197*6a76678eSAlexandre Beletti if (nrandom(4) == 1)
198*6a76678eSAlexandre Beletti (void)printf("No cheating!\n");
199*6a76678eSAlexandre Beletti (void)printf("Guess again.\n");
200*6a76678eSAlexandre Beletti }
201*6a76678eSAlexandre Beletti /* NOTREACHED */
202*6a76678eSAlexandre Beletti }
203*6a76678eSAlexandre Beletti
204*6a76678eSAlexandre Beletti static int
compmove(void)205*6a76678eSAlexandre Beletti compmove(void)
206*6a76678eSAlexandre Beletti {
207*6a76678eSAlexandre Beletti static int lmove;
208*6a76678eSAlexandre Beletti
209*6a76678eSAlexandre Beletti if (promode)
210*6a76678eSAlexandre Beletti lmove = promove();
211*6a76678eSAlexandre Beletti else {
212*6a76678eSAlexandre Beletti do {
213*6a76678eSAlexandre Beletti lmove = (lmove + 1) % RANKS;
214*6a76678eSAlexandre Beletti } while (!comphand[lmove] || comphand[lmove] == CARDS);
215*6a76678eSAlexandre Beletti }
216*6a76678eSAlexandre Beletti asked[lmove] = 1;
217*6a76678eSAlexandre Beletti
218*6a76678eSAlexandre Beletti (void)printf("I ask you for: %s.\n", cards[lmove]);
219*6a76678eSAlexandre Beletti return(lmove);
220*6a76678eSAlexandre Beletti }
221*6a76678eSAlexandre Beletti
222*6a76678eSAlexandre Beletti static int
promove(void)223*6a76678eSAlexandre Beletti promove(void)
224*6a76678eSAlexandre Beletti {
225*6a76678eSAlexandre Beletti int i, max;
226*6a76678eSAlexandre Beletti
227*6a76678eSAlexandre Beletti for (i = 0; i < RANKS; ++i)
228*6a76678eSAlexandre Beletti if (userasked[i] &&
229*6a76678eSAlexandre Beletti comphand[i] > 0 && comphand[i] < CARDS) {
230*6a76678eSAlexandre Beletti userasked[i] = 0;
231*6a76678eSAlexandre Beletti return(i);
232*6a76678eSAlexandre Beletti }
233*6a76678eSAlexandre Beletti if (nrandom(3) == 1) {
234*6a76678eSAlexandre Beletti for (i = 0;; ++i)
235*6a76678eSAlexandre Beletti if (comphand[i] && comphand[i] != CARDS) {
236*6a76678eSAlexandre Beletti max = i;
237*6a76678eSAlexandre Beletti break;
238*6a76678eSAlexandre Beletti }
239*6a76678eSAlexandre Beletti while (++i < RANKS)
240*6a76678eSAlexandre Beletti if (comphand[i] != CARDS &&
241*6a76678eSAlexandre Beletti comphand[i] > comphand[max])
242*6a76678eSAlexandre Beletti max = i;
243*6a76678eSAlexandre Beletti return(max);
244*6a76678eSAlexandre Beletti }
245*6a76678eSAlexandre Beletti if (nrandom(1024) == 0723) {
246*6a76678eSAlexandre Beletti for (i = 0; i < RANKS; ++i)
247*6a76678eSAlexandre Beletti if (userhand[i] && comphand[i])
248*6a76678eSAlexandre Beletti return(i);
249*6a76678eSAlexandre Beletti }
250*6a76678eSAlexandre Beletti for (;;) {
251*6a76678eSAlexandre Beletti for (i = 0; i < RANKS; ++i)
252*6a76678eSAlexandre Beletti if (comphand[i] && comphand[i] != CARDS &&
253*6a76678eSAlexandre Beletti !asked[i])
254*6a76678eSAlexandre Beletti return(i);
255*6a76678eSAlexandre Beletti for (i = 0; i < RANKS; ++i)
256*6a76678eSAlexandre Beletti asked[i] = 0;
257*6a76678eSAlexandre Beletti }
258*6a76678eSAlexandre Beletti /* NOTREACHED */
259*6a76678eSAlexandre Beletti }
260*6a76678eSAlexandre Beletti
261*6a76678eSAlexandre Beletti static int
drawcard(int player,int * hand)262*6a76678eSAlexandre Beletti drawcard(int player, int *hand)
263*6a76678eSAlexandre Beletti {
264*6a76678eSAlexandre Beletti int card;
265*6a76678eSAlexandre Beletti
266*6a76678eSAlexandre Beletti ++hand[card = deck[--curcard]];
267*6a76678eSAlexandre Beletti if (player == USER || hand[card] == CARDS) {
268*6a76678eSAlexandre Beletti printplayer(player);
269*6a76678eSAlexandre Beletti (void)printf("drew %s", cards[card]);
270*6a76678eSAlexandre Beletti if (hand[card] == CARDS) {
271*6a76678eSAlexandre Beletti (void)printf(" and made a book of %s's!\n",
272*6a76678eSAlexandre Beletti cards[card]);
273*6a76678eSAlexandre Beletti chkwinner(player, hand);
274*6a76678eSAlexandre Beletti } else
275*6a76678eSAlexandre Beletti (void)printf(".\n");
276*6a76678eSAlexandre Beletti }
277*6a76678eSAlexandre Beletti return(card);
278*6a76678eSAlexandre Beletti }
279*6a76678eSAlexandre Beletti
280*6a76678eSAlexandre Beletti static int
gofish(int askedfor,int player,int * hand)281*6a76678eSAlexandre Beletti gofish(int askedfor, int player, int *hand)
282*6a76678eSAlexandre Beletti {
283*6a76678eSAlexandre Beletti printplayer(OTHER(player));
284*6a76678eSAlexandre Beletti (void)printf("say \"GO FISH!\"\n");
285*6a76678eSAlexandre Beletti if (askedfor == drawcard(player, hand)) {
286*6a76678eSAlexandre Beletti printplayer(player);
287*6a76678eSAlexandre Beletti (void)printf("drew the guess!\n");
288*6a76678eSAlexandre Beletti printplayer(player);
289*6a76678eSAlexandre Beletti (void)printf("get to ask again!\n");
290*6a76678eSAlexandre Beletti return(1);
291*6a76678eSAlexandre Beletti }
292*6a76678eSAlexandre Beletti return(0);
293*6a76678eSAlexandre Beletti }
294*6a76678eSAlexandre Beletti
295*6a76678eSAlexandre Beletti static void
goodmove(int player,int move,int * hand,int * opphand)296*6a76678eSAlexandre Beletti goodmove(int player, int move, int *hand, int *opphand)
297*6a76678eSAlexandre Beletti {
298*6a76678eSAlexandre Beletti printplayer(OTHER(player));
299*6a76678eSAlexandre Beletti (void)printf("have %d %s%s.\n",
300*6a76678eSAlexandre Beletti opphand[move], cards[move], opphand[move] == 1 ? "": "'s");
301*6a76678eSAlexandre Beletti
302*6a76678eSAlexandre Beletti hand[move] += opphand[move];
303*6a76678eSAlexandre Beletti opphand[move] = 0;
304*6a76678eSAlexandre Beletti
305*6a76678eSAlexandre Beletti if (hand[move] == CARDS) {
306*6a76678eSAlexandre Beletti printplayer(player);
307*6a76678eSAlexandre Beletti (void)printf("made a book of %s's!\n", cards[move]);
308*6a76678eSAlexandre Beletti chkwinner(player, hand);
309*6a76678eSAlexandre Beletti }
310*6a76678eSAlexandre Beletti
311*6a76678eSAlexandre Beletti chkwinner(OTHER(player), opphand);
312*6a76678eSAlexandre Beletti
313*6a76678eSAlexandre Beletti printplayer(player);
314*6a76678eSAlexandre Beletti (void)printf("get another guess!\n");
315*6a76678eSAlexandre Beletti }
316*6a76678eSAlexandre Beletti
317*6a76678eSAlexandre Beletti static void
chkwinner(int player,const int * hand)318*6a76678eSAlexandre Beletti chkwinner(int player, const int *hand)
319*6a76678eSAlexandre Beletti {
320*6a76678eSAlexandre Beletti int cb, i, ub;
321*6a76678eSAlexandre Beletti
322*6a76678eSAlexandre Beletti for (i = 0; i < RANKS; ++i)
323*6a76678eSAlexandre Beletti if (hand[i] > 0 && hand[i] < CARDS)
324*6a76678eSAlexandre Beletti return;
325*6a76678eSAlexandre Beletti printplayer(player);
326*6a76678eSAlexandre Beletti (void)printf("don't have any more cards!\n");
327*6a76678eSAlexandre Beletti (void)printf("My books:");
328*6a76678eSAlexandre Beletti cb = countbooks(comphand);
329*6a76678eSAlexandre Beletti (void)printf("Your books:");
330*6a76678eSAlexandre Beletti ub = countbooks(userhand);
331*6a76678eSAlexandre Beletti (void)printf("\nI have %d, you have %d.\n", cb, ub);
332*6a76678eSAlexandre Beletti if (ub > cb) {
333*6a76678eSAlexandre Beletti (void)printf("\nYou win!!!\n");
334*6a76678eSAlexandre Beletti if (nrandom(1024) == 0723)
335*6a76678eSAlexandre Beletti (void)printf("Cheater, cheater, pumpkin eater!\n");
336*6a76678eSAlexandre Beletti } else if (cb > ub) {
337*6a76678eSAlexandre Beletti (void)printf("\nI win!!!\n");
338*6a76678eSAlexandre Beletti if (nrandom(1024) == 0723)
339*6a76678eSAlexandre Beletti (void)printf("Hah! Stupid peasant!\n");
340*6a76678eSAlexandre Beletti } else
341*6a76678eSAlexandre Beletti (void)printf("\nTie!\n");
342*6a76678eSAlexandre Beletti exit(0);
343*6a76678eSAlexandre Beletti }
344*6a76678eSAlexandre Beletti
345*6a76678eSAlexandre Beletti static void
printplayer(int player)346*6a76678eSAlexandre Beletti printplayer(int player)
347*6a76678eSAlexandre Beletti {
348*6a76678eSAlexandre Beletti switch (player) {
349*6a76678eSAlexandre Beletti case COMPUTER:
350*6a76678eSAlexandre Beletti (void)printf("I ");
351*6a76678eSAlexandre Beletti break;
352*6a76678eSAlexandre Beletti case USER:
353*6a76678eSAlexandre Beletti (void)printf("You ");
354*6a76678eSAlexandre Beletti break;
355*6a76678eSAlexandre Beletti }
356*6a76678eSAlexandre Beletti }
357*6a76678eSAlexandre Beletti
358*6a76678eSAlexandre Beletti static void
printhand(const int * hand)359*6a76678eSAlexandre Beletti printhand(const int *hand)
360*6a76678eSAlexandre Beletti {
361*6a76678eSAlexandre Beletti int book, i, j;
362*6a76678eSAlexandre Beletti
363*6a76678eSAlexandre Beletti for (book = i = 0; i < RANKS; i++)
364*6a76678eSAlexandre Beletti if (hand[i] < CARDS)
365*6a76678eSAlexandre Beletti for (j = hand[i]; --j >= 0;)
366*6a76678eSAlexandre Beletti PRC(i);
367*6a76678eSAlexandre Beletti else
368*6a76678eSAlexandre Beletti ++book;
369*6a76678eSAlexandre Beletti if (book) {
370*6a76678eSAlexandre Beletti (void)printf(" + Book%s of", book > 1 ? "s" : "");
371*6a76678eSAlexandre Beletti for (i = 0; i < RANKS; i++)
372*6a76678eSAlexandre Beletti if (hand[i] == CARDS)
373*6a76678eSAlexandre Beletti PRC(i);
374*6a76678eSAlexandre Beletti }
375*6a76678eSAlexandre Beletti (void)putchar('\n');
376*6a76678eSAlexandre Beletti }
377*6a76678eSAlexandre Beletti
378*6a76678eSAlexandre Beletti static int
countcards(const int * hand)379*6a76678eSAlexandre Beletti countcards(const int *hand)
380*6a76678eSAlexandre Beletti {
381*6a76678eSAlexandre Beletti int i, count;
382*6a76678eSAlexandre Beletti
383*6a76678eSAlexandre Beletti for (count = i = 0; i < RANKS; i++)
384*6a76678eSAlexandre Beletti count += *hand++;
385*6a76678eSAlexandre Beletti return(count);
386*6a76678eSAlexandre Beletti }
387*6a76678eSAlexandre Beletti
388*6a76678eSAlexandre Beletti static int
countbooks(const int * hand)389*6a76678eSAlexandre Beletti countbooks(const int *hand)
390*6a76678eSAlexandre Beletti {
391*6a76678eSAlexandre Beletti int i, count;
392*6a76678eSAlexandre Beletti
393*6a76678eSAlexandre Beletti for (count = i = 0; i < RANKS; i++)
394*6a76678eSAlexandre Beletti if (hand[i] == CARDS) {
395*6a76678eSAlexandre Beletti ++count;
396*6a76678eSAlexandre Beletti PRC(i);
397*6a76678eSAlexandre Beletti }
398*6a76678eSAlexandre Beletti if (!count)
399*6a76678eSAlexandre Beletti (void)printf(" none");
400*6a76678eSAlexandre Beletti (void)putchar('\n');
401*6a76678eSAlexandre Beletti return(count);
402*6a76678eSAlexandre Beletti }
403*6a76678eSAlexandre Beletti
404*6a76678eSAlexandre Beletti static void
init(void)405*6a76678eSAlexandre Beletti init(void)
406*6a76678eSAlexandre Beletti {
407*6a76678eSAlexandre Beletti int i, j, temp;
408*6a76678eSAlexandre Beletti
409*6a76678eSAlexandre Beletti for (i = 0; i < TOTCARDS; ++i)
410*6a76678eSAlexandre Beletti deck[i] = i % RANKS;
411*6a76678eSAlexandre Beletti for (i = 0; i < TOTCARDS - 1; ++i) {
412*6a76678eSAlexandre Beletti j = nrandom(TOTCARDS-i);
413*6a76678eSAlexandre Beletti if (j == 0)
414*6a76678eSAlexandre Beletti continue;
415*6a76678eSAlexandre Beletti temp = deck[i];
416*6a76678eSAlexandre Beletti deck[i] = deck[i+j];
417*6a76678eSAlexandre Beletti deck[i+j] = temp;
418*6a76678eSAlexandre Beletti }
419*6a76678eSAlexandre Beletti for (i = 0; i < HANDSIZE; ++i) {
420*6a76678eSAlexandre Beletti ++userhand[deck[--curcard]];
421*6a76678eSAlexandre Beletti ++comphand[deck[--curcard]];
422*6a76678eSAlexandre Beletti }
423*6a76678eSAlexandre Beletti }
424*6a76678eSAlexandre Beletti
425*6a76678eSAlexandre Beletti static int
nrandom(int n)426*6a76678eSAlexandre Beletti nrandom(int n)
427*6a76678eSAlexandre Beletti {
428*6a76678eSAlexandre Beletti
429*6a76678eSAlexandre Beletti return((int)random() % n);
430*6a76678eSAlexandre Beletti }
431*6a76678eSAlexandre Beletti
432*6a76678eSAlexandre Beletti static void
instructions(void)433*6a76678eSAlexandre Beletti instructions(void)
434*6a76678eSAlexandre Beletti {
435*6a76678eSAlexandre Beletti int input;
436*6a76678eSAlexandre Beletti pid_t pid;
437*6a76678eSAlexandre Beletti int fd;
438*6a76678eSAlexandre Beletti const char *pager;
439*6a76678eSAlexandre Beletti int status;
440*6a76678eSAlexandre Beletti
441*6a76678eSAlexandre Beletti (void)printf("Would you like instructions (y or n)? ");
442*6a76678eSAlexandre Beletti input = getchar();
443*6a76678eSAlexandre Beletti while (getchar() != '\n');
444*6a76678eSAlexandre Beletti if (input != 'y')
445*6a76678eSAlexandre Beletti return;
446*6a76678eSAlexandre Beletti
447*6a76678eSAlexandre Beletti switch (pid = fork()) {
448*6a76678eSAlexandre Beletti case 0: /* child */
449*6a76678eSAlexandre Beletti if (!isatty(1))
450*6a76678eSAlexandre Beletti pager = "cat";
451*6a76678eSAlexandre Beletti else {
452*6a76678eSAlexandre Beletti if (!(pager = getenv("PAGER")) || (*pager == 0))
453*6a76678eSAlexandre Beletti pager = _PATH_MORE;
454*6a76678eSAlexandre Beletti }
455*6a76678eSAlexandre Beletti if ((fd = open(_PATH_INSTR, O_RDONLY)) == -1)
456*6a76678eSAlexandre Beletti err(1, "open %s", _PATH_INSTR);
457*6a76678eSAlexandre Beletti if (dup2(fd, 0) == -1)
458*6a76678eSAlexandre Beletti err(1, "dup2");
459*6a76678eSAlexandre Beletti (void)execl("/bin/sh", "sh", "-c", pager, (char *) NULL);
460*6a76678eSAlexandre Beletti err(1, "exec sh -c %s", pager);
461*6a76678eSAlexandre Beletti /*NOTREACHED*/
462*6a76678eSAlexandre Beletti case -1:
463*6a76678eSAlexandre Beletti err(1, "fork");
464*6a76678eSAlexandre Beletti /*NOTREACHED*/
465*6a76678eSAlexandre Beletti default:
466*6a76678eSAlexandre Beletti (void)waitpid(pid, &status, 0);
467*6a76678eSAlexandre Beletti break;
468*6a76678eSAlexandre Beletti }
469*6a76678eSAlexandre Beletti (void)printf("Hit return to continue...\n");
470*6a76678eSAlexandre Beletti while ((input = getchar()) != EOF && input != '\n');
471*6a76678eSAlexandre Beletti }
472*6a76678eSAlexandre Beletti
473*6a76678eSAlexandre Beletti static void
usage(void)474*6a76678eSAlexandre Beletti usage(void)
475*6a76678eSAlexandre Beletti {
476*6a76678eSAlexandre Beletti (void)fprintf(stderr, "usage: fish [-p]\n");
477*6a76678eSAlexandre Beletti exit(1);
478*6a76678eSAlexandre Beletti }
479