-/* $NetBSD: fish.c,v 1.8 1999/07/14 17:30:21 hubertf Exp $ */
+/* $NetBSD: fish.c,v 1.12 2000/03/28 19:37:54 tron Exp $ */
/*-
* Copyright (c) 1990, 1993
#if 0
static char sccsid[] = "@(#)fish.c 8.1 (Berkeley) 5/31/93";
#else
-__RCSID("$NetBSD: fish.c,v 1.8 1999/07/14 17:30:21 hubertf Exp $");
+__RCSID("$NetBSD: fish.c,v 1.12 2000/03/28 19:37:54 tron Exp $");
#endif
#endif /* not lint */
#define RANKS 13
#define HANDSIZE 7
#define CARDS 4
+#define TOTCARDS RANKS * CARDS
#define USER 1
#define COMPUTER 0
#define OTHER(a) (1 - (a))
-char *cards[] = {
+const char *const cards[] = {
"A", "2", "3", "4", "5", "6", "7",
"8", "9", "10", "J", "Q", "K", NULL,
};
#define PRC(card) (void)printf(" %s", cards[card])
int promode;
-int asked[RANKS], comphand[RANKS], deck[RANKS];
+int asked[RANKS], comphand[RANKS], deck[TOTCARDS];
int userasked[RANKS], userhand[RANKS];
+int curcard = TOTCARDS;
-void chkwinner __P((int, int *));
+void chkwinner __P((int, const int *));
int compmove __P((void));
-int countbooks __P((int *));
-int countcards __P((int *));
+int countbooks __P((const int *));
+int countcards __P((const int *));
int drawcard __P((int, int *));
int gofish __P((int, int, int *));
void goodmove __P((int, int, int *, int *));
void instructions __P((void));
int main __P((int, char *[]));
int nrandom __P((int));
-void printhand __P((int *));
+void printhand __P((const int *));
void printplayer __P((int));
int promove __P((void));
void usage __P((void)) __attribute__((__noreturn__));
{
int ch, move;
- setgid(getgid());
+ /* Revoke setgid privileges */
+ setregid(getgid(), getgid());
while ((ch = getopt(argc, argv, "p")) != -1)
switch(ch) {
break;
case '?':
default:
- (void)fprintf(stderr, "usage: fish [-p]\n");
- exit(1);
+ usage();
}
srandom(time((time_t *)NULL));
usermove()
{
int n;
- char **p;
+ const char *const *p;
char buf[256];
(void)printf("\nYour hand is:");
continue;
if (buf[0] == '\n') {
(void)printf("%d cards in my hand, %d in the pool.\n",
- countcards(comphand), countcards(deck));
+ countcards(comphand), curcard);
(void)printf("My books:");
(void)countbooks(comphand);
continue;
{
int card;
- while (deck[card = nrandom(RANKS)] == 0);
- ++hand[card];
- --deck[card];
+ ++hand[card = deck[--curcard]];
if (player == USER || hand[card] == CARDS) {
printplayer(player);
(void)printf("drew %s", cards[card]);
void
chkwinner(player, hand)
int player;
- int *hand;
+ const int *hand;
{
int cb, i, ub;
void
printhand(hand)
- int *hand;
+ const int *hand;
{
int book, i, j;
int
countcards(hand)
- int *hand;
+ const int *hand;
{
int i, count;
int
countbooks(hand)
- int *hand;
+ const int *hand;
{
int i, count;
void
init()
{
- int i, rank;
+ int i, j, temp;
- for (i = 0; i < RANKS; ++i)
- deck[i] = CARDS;
- for (i = 0; i < HANDSIZE; ++i) {
- while (!deck[rank = nrandom(RANKS)]);
- ++userhand[rank];
- --deck[rank];
+ for (i = 0; i < TOTCARDS; ++i)
+ deck[i] = i % RANKS;
+ for (i = 0; i < TOTCARDS - 1; ++i) {
+ j = nrandom(TOTCARDS-i);
+ if (j == 0)
+ continue;
+ temp = deck[i];
+ deck[i] = deck[i+j];
+ deck[i+j] = temp;
}
for (i = 0; i < HANDSIZE; ++i) {
- while (!deck[rank = nrandom(RANKS)]);
- ++comphand[rank];
- --deck[rank];
+ ++userhand[deck[--curcard]];
+ ++comphand[deck[--curcard]];
}
}