CS21 Spring 2005
Homework 12
Due: Sunday, May 1, before 11:30pm

You may work with a partner for this project. With the complexity of this assignment, you may want to use Pair Programming instead of emailing a file back and forth.


For this assignment, you will writing a computer program that will let you play the popular (and exciting!) card game "Go Fish!".

Rules

Go Fish! can be played with a normal deck of 52 playing cards. There need to be at least two players, but there may be more.

The goal of the game is to collect pairs of cards that have the same rank (i.e., face value). Whenever a player has a pair of cards in their hand, they are removed from the hand and placed face-up in front of the player. The winner is the player that has the most pairs in front of her at the end of the game.

Play is as follows:

  1. The deck of cards is shuffled.
  2. Each player receives an initial 5 cards. NOTE: Pairs are removed as soon as they encountered, so be sure to remove all pairs now.
  3. Each player takes a turn going in order around the table until the game ends. A turn continues as long as the player keeps making pairs by requesting cards.
    1. If the player has an empty hand, she is dealt 5 cards.
    2. The current player then picks one of the other players and asks them if they have a card matching one in their hand. NOTE: You must have only one card of the same rank in your hand as the card you are requesting.
    3. If the asked player has a card of that rank, they turn that card over to the asking player. The asking player then puts down the pair she just formed and takes another turn.
    4. If the asked player doesn't have that type of card, they tell the askee to "Go Fish!" and the current player draws the top card from the deck. (NOTE: If the card makes a pair, those cards are laid down in front of the player.) The player's turn ends and play proceeds to the next player.
  4. While taking a turn, if a player runs out of cards in her hand, she draws 5 new cards from the deck. If there are not 5 cards in the deck, she draws all that remains.
  5. Play ends when the first player runs out of cards in her hand and there are no mare cards in the deck to draw.
  6. The winner(s) of the game is the player(s) with the most pairs.

Implementation Details and Data Structures

Cards

A card is composed of a rank and a suit. The rank of a card is one of Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King. The suit of a card is one of Club, Diamond, Heart, and Spades. You many want to represent these internally as integers and change them to strings when displaying them to the user. Or, you could #define some strings and use those values and have a card have an integer rank and string of suit.

Cards must be represented as a struct with at least two fields; one for rank and one for suit.

Players

You should allow the human player to select between 1 and 3 computer opponents (total number of players is between 2 and 4). Allocate a dynamic array of player structures. Prompt the user for a name for all players. You'll also need to keep track of the cards in hand for each player and the pairs the player has made. It may simplify some things to maintain a count of the number of cards in hand and pairs on the table as part of the player structure.

You will represent a player internally by a struct that contains the following fields:

You may add additional other fields to the player struct as needed.

Hands and Pairs

Hands and pairs of cards must be implemented as two linked lists of cards; one for the player's hand and one for the player's matched pairs. You'll want to create a node structure that holds a card structure and a pointer to the next card. The lists must be kept in sorted order by the rank of the card (you may ignore suit for sorting purposes). Use insertion sort to maintain this sorted order. This will allow you to detect pairs of cards during insertion (or you could check to see if you already have a card of the same rank beforehand) and you know that the list of pairs will have pairs next to each other in the list.

Computer Play Behavior

You need to be sure that your program has the computer players behave according to the rules of the game. However, such behavior need not be any more complicated than having them randomly pick another player to request, and they randomly pick a card from their hand.

More complicated strategies are possible (e.g., you could have the computer note what other players have asked for, or you might use some strategy based on the number of cards in a players hand) but are not required for this assignment. You are encouraged to think about such things and possibly implement them after you have the main program working.

Displaying of information

You should display all information when the human player is taking a turn. (Number of cards in everyones hands, pairs in front of them, etc.) However during a computer turn, you only need to display the computer player's request and the response.

Bounds checking

You should be sure that the user is always picking from one of the other players, not themselves, and that they are asking for a card of a rank that is represented in their hand.

Winners

The game ends when the deck of cards is empty and someone is trying to draw a card on their turn. The winner is the player with the most pairs of cards. However, it is possible that there is a 2 or 3-way tie, and your program should detect this and handle it appropriately.

Sample Output

A full run of the sample program can be found here.

Here are some of the highlights:

Printing out instructions
% ./a.out
This program plays the game of Go Fish
Would you like to see instructions?
please enter yes or no: yes
****************************************************************
 The game of Go Fish can be played with 2 or more players
 After entering the number of players and each players
 information, the game proceeds as follows:
  (1) Each player is dealt 5 cards
      (note: any time a player is dealt cards if there are any pairs
      added to his/her hand, they are laid down on the table.  As a
      result, a player dealt 5 new cards may only have 3 or 1 in
      his/her hand due to being dealt some matching pairs).
  (2) Each player takes a turns (in order), until the game ends
      The current player keeps his/her turn as long as he/she 
      finds pairs.  Otherwise, play moves to the next player.
      (a) if the player is out of cards, 5 more are dealt to him/her
      (b) the current player asks some other player if 
          he/she has a card matching one in the player's hand
      (c) if the asked player has the card, he/she gives
          it to the current player, the current player lays the
          pair down and gets to keep his/her turn (goes to step 2a)
      (d) if the asked player doesn't have a card, then he/she
          tells the current player to 'GO FISH' and the current
          player draws one card from the deck (if the drawn card
          matches a card in the player's hand, the pair is laid down
          on the table).  The current player's turn is over, and
          play moves to the next player who starts at step 2a
  (3) If at any time a player runs out of cards in his her hand, he/
      she draws up to 5 new cards from the deck and continues playing
  (4) Play ends when the first player runs out of cards in his/her
      hand, are no more cards in the deck to draw
  (5) The winner of the game is the player with the most pairs
****************************************************************
Getting player information
How many computer players would you like to play with?
  enter an value between 1 and 3: 3
Enter your name (you are player number 0): Tia
Enter a name for player 1): Mookie
Enter a name for player 2): Cookie
Enter a name for player 3): Rookie
User's turn
###############
Player 0's turn
###############
******   PAIRS ON THE  TABLE   *****
Player 0, Tia, has 3 cards in his/her hand & 1 pairs:
	9 of Diamonds 	9 of Spades 

Player 1, Mookie, has 3 cards in his/her hand & 1 pairs:
	2 of Hearts 	2 of Spades 

Player 2, Cookie, has 5 cards in his/her hand & 0 pairs:

Player 3, Rookie, has 3 cards in his/her hand & 1 pairs:
	10 of Spades 	10 of Diamonds 

**************************************
 Tia, your hand is:
	3 of Diamonds 	4 of Diamonds 
	5 of Diamonds 
 From which player do you want a card?
  enter an value between 1 and 3: 6
   hey, 6 isn't between 1 and 3
   ...try again
  enter an value between 1 and 3: 1
 Which rank card of card do want to ask for
 (1-Ace, 2-10, 11-Jack, 12-Queen, 13-King)?
  enter an value between 1 and 13: 13
  13 is not a valid choice
  you must pick a card that is in your hand
	3 of Diamonds 	4 of Diamonds 
	5 of Diamonds 

  try again....
 Which rank card of card do want to ask for
 (1-Ace, 2-10, 11-Jack, 12-Queen, 13-King)?
  enter an value between 1 and 13: 3
 Tia chooses player 1, Mookie, to ask for a 3:

 Player 1 had the card asked for!
 Tia gets another turn!!!
 Tia, your hand is:
	4 of Diamonds 	5 of Diamonds 

 From which player do you want a card?
  enter an value between 1 and 3: 1
 Which rank card of card do want to ask for
 (1-Ace, 2-10, 11-Jack, 12-Queen, 13-King)?
  enter an value between 1 and 13: 4
 Tia chooses player 1, Mookie, to ask for a 4:

 Player 1 doesn't have that card, GO FISH!!!
Computer Turn
###############
Player 2's turn
###############
 Cookie chooses player 3, Rookie, to ask for a 13:

 Player 3 had the card asked for!
 Cookie gets another turn!!!
 Cookie chooses player 3, Rookie, to ask for a 8:

 Player 3 had the card asked for!
 Cookie gets another turn!!!
 Cookie chooses player 1, Mookie, to ask for a 12:

 Player 1 doesn't have that card, GO FISH!!!
Winning
Player 0, Tia, and Player 3, Rookie, won with 8 pairs

Last Modified: April 18, 2005 - Benjamin A. KupermanVI Powered