* Copyright (c) 1980 Regents of the University of California.
* All rights reserved. The Berkeley software License Agreement
* specifies the terms and conditions for redistribution.
"@(#) Copyright (c) 1980 Regents of the University of California.\n\
static char sccsid
[] = "@(#)fish.c 5.1 (Berkeley) %G%";
/* Through, `my' refers to the program, `your' to the player */
typedef char HAND
[CTSIZ
];
/* utility and output programs */
/* shuffle the deck, and reset nextcd */
/* uses the random number generator `rand' in the C library */
/* assumes that `srand' has already been called */
for( i
=0; i
<DECK
; ++i
) deck
[i
] = (i
%13)+1; /* seed the deck */
for( i
=DECK
; i
>0; --i
){ /* select the next card at random */
deck
[i
-1] = choose( deck
, i
);
choose( a
, n
) char a
[]; {
/* pick and return one at random from the n choices in a */
/* The last one is moved to replace the one chosen */
if( n
<= 0 ) error( "null choice" );
if( nextcd
>= DECK
) return( NOMORE
);
return( deck
[nextcd
++] );
fprintf( stderr
, "error: " );
for( i
=1; i
<=CTYPE
; ++i
){
if( h
[i
] != 0 && h
[i
] != 4 ) return( 0 );
mark( cd
, hand
) HAND hand
; {
error( "mark overflow" );
deal( hand
, n
) HAND hand
; {
if( mark( hand
, draw() ) == NOMORE
) error( "deck exhausted" );
if( proflag
) printf( "Pro level\n" );
for( i
=1; i
<=CTYPE
; ++i
){
if( myhand
[i
] == 4 ) ++b
;
for( i
=1; i
<=CTYPE
; ++i
){
if( myhand
[i
] == 4 ) printf( "%s ", cname
[i
] );
printf( "%d cards in my hand, %d in the pool\n", ct
, DECK
-nextcd
);
printf( "You ask me for: " );
for( i
= 1; i
<= CTYPE
; ++i
){
while( k
-- ) printf( "%s ", cname
[i
] );
for( i
=1; i
<=CTYPE
; ++i
){
if( h
[i
] == 4 ) printf( "%s ", cname
[i
] );
main( argc
, argv
) char * argv
[]; {
/* initialize shuffling, ask for instructions, play game, die */
if( argc
> 1 && argv
[1][0] == '-' ){
while( argv
[1][0] == '-' ) { ++argv
[1]; ++debug
; }
printf( "instructions?\n" );
if( (c
=getchar()) != '\n' ){
if( c
!= 'n' ) instruct();
while( getchar() != '\n' );
"`Go Fish' is a childrens' card game. The Object is to",
"accumulate `books' of 4 cards with the same face value. The",
"players alternate turns; each turn begins with one player",
"selecting a card from his hand, and asking the other player for",
"all cards of that face value. If the other player has one or",
"more cards of that face value in his hand, he gives them to the",
"first player, and the first player makes another request.",
"Eventually, the first player asks for a card which is not in",
"the second player's hand: he replies `GO FISH!' The first",
"player then draws a card from the `pool' of undealt cards. If",
"this is the card he had last requested, he draws again. When a",
"book is made, either through drawing or requesting, the cards",
"are laid down and no further action takes place with that face",
"value. To play the computer, simply make guesses by typing a,",
"2, 3, 4, 5, 6, 7, 8, 9, 10, j, q, or k when asked. Hitting",
"return gives you information about the size of my hand and the",
"pool, and tells you about my books. Saying `p' as a first",
"guess puts you into `pro' level; The default is pretty dumb!",
for( cpp
= inst
; **cpp
!= '\0'; ++cpp
){
/* you make repeated guesses */
printf( "your hand is: " );
printf( "you ask me for: " );
if( !move( yourhand
, myhand
, g
=guess(), 0 ) ) break;
printf( "Guess again\n" );
/* I make repeated guesses */
if( (g
=myguess()) != NOMORE
){
printf( "I ask you for: %s\n", cname
[g
] );
if( !move( myhand
, yourhand
, g
, 1 ) ) break;
printf( "I get another guess\n" );
/* reflect the effect of a move on the hands */
move( hs
, ht
, g
, v
) HAND hs
, ht
; {
/* hand hs has made a guess, g, directed towards ht */
/* v on indicates that the guess was made by the machine */
if( d
== NOMORE
) score();
printf( "Empty Hand\n" );
if( !v
) printf( "You draw %s\n", cname
[d
] );
if( v
) error( "Rotten Guess" );
printf( "You don't have any %s's\n", cname
[g
] );
if( ht
[g
] ){ /* successful guess */
printf( "%s have %d %s%s\n", tp
, ht
[g
], cname
[g
], ht
[g
]>1?"'s":"" );
if( hs
[g
] == 4 ) madebook(g
);
printf( "%s say \"GO FISH!\"\n", tp
);
printf( "No more cards\n" );
if( !v
) printf( "You draw %s\n", cname
[d
] );
if( hs
[d
] == 4 ) madebook(d
);
printf( "%s drew the guess, so draw again\n", sp
);
printf( "Made a book of %s's\n", cname
[x
] );
printf( "The game is over.\nMy books: " );
for( i
=1; i
<=CTYPE
;++i
){
printf( "%s ", cname
[i
] );
printf( "\nYour books: " );
for( i
=1; i
<=CTYPE
;++i
){
printf( "%s ", cname
[i
] );
printf( "\n\nI have %d, you have %d\n", my
, your
);
printf( "\n%s win!!!\n", my
>your
?"I":"You" );
# define G(x) { if(go) goto err; else go = x; }
/* get the guess from the tty and return it... */
if( empty( yourhand
) ) return( NOMORE
);
while( g
!= '\n' ) g
= getchar();
/* the program's strategy appears from here to the end */
if( empty( myhand
) ) return( NOMORE
);
/* make a list of those things which i have */
/* leave off any which are books */
/* if something is found that he has, guess it! */
for( i
=1; i
<=CTYPE
; ++i
){
if( myhand
[i
] == 0 || myhand
[i
] == 4 ) continue;
if( !proflag
) goto random
;
/* get ones he has, if any */
/* is there one that has never been guessed; if so, guess it */
if( haveguessed
[try[i
]] < lg
) lg
= haveguessed
[try[i
]];
/* remove all those not guessed longest ago */
if( haveguessed
[try[i
]] == lg
) try[t
++] = try[i
];
if( t
<= 0 ) error( "bad guessing loop" );
i
= choose( try, ntry
); /* make a random choice */
gotguess
: /* do bookkeeping */
hehas
[i
] = 0; /* he won't anymore! */
for( t
=1; t
<=CTYPE
; ++t
){
if( haveguessed
[t
] ) --haveguessed
[t
];
haveguessed
[i
] = 100; /* will have guessed it */