CS 21: Algorithmic Problem Solving

 

HW #10: Go Farm (or Fish)

Due 11:59pm Tuesday, April 10

The program handin21 will only submit files in the cs21/homework/10 directory.

Your programs are graded on both correctness and style. Please review the comments regarding programming style on the main page

Some of the problems will have optional components that allow you to further practice your skills in Python. Optional portions will not be graded, but may be interesting for those wanting some extra challenges.

The game of Go Farm
The game of Go Farm is a vegetarian alternative to the more well known card game of Go Fish. The game is played with a standard deck of 52 cards. The goal of the game is to collect more books than the other players. A book is a set of four cards with the same rank. There are 13 total books in the entire deck. The suits of the cards do not matter in this game. We will describe a two-player version of the game. Rules for three or more players can be found at Wikipedia or some other online site.

To start the game, the deck is shuffled and seven cards are dealt to each player. The rest of the deck is set aside as a draw pile. Players then take turns playing until the draw pile is empty and one player has no cards in his/her hand. A turn consists of the following steps. For simplicity, we refer to the players as Alice and Bob and describe the turn for Alice.

  1. If Alice's hand contains any books, these books are removed from her hand and set aside.
  2. If Alice does not have any cards in her hand, she draws one card from the draw pile and adds it to her hand.
  3. Alice then picks a card rank from one of the cards in her hand and asks Bob if he has any cards of that rank.
  4. If Bob has any cards of the requested rank, he hands all of his cards matching that rank to Alice, who then adds the cards to her hand. As long as Alice receives one or more cards from Bob, she proceeds to take another turn.
  5. If Bob does not have any cards of the requested rank, Alice must "Go Farm" and draw a single card from the draw pile and add it to her hand. If the card she draws matches the card she requested, Alice proceeds to take another turn. Otherwise, Alice's turn is over and it is now Bob's turn.

At the end of the game, both players total their books and the player with the most books wins. Note that with two players and thirteen books, there cannot be ties.

Your assignment is to write a few classes that will play the game Go Farm. The outline of your program will be similar to the Black Jack game we wrote in class. You should have Card, Deck, GoFarmPlayer, and GoFarmGame classes. You should reuse or extend the Card and Deck classes that we wrote in class.

Your program must have the following components:

  1. Support for at least two players. The program should prompt for the names of all players.
  2. Support for interactive players that type responses to prompts.
  3. Support for one automated player that plays the game automatically according to the rules. Implementing a strategy is optional.
  4. The game class must play at least one full game of Go Farm.
  5. The program must show the player's hand at the start of his/her turn, for all interactive players. You should not show the hands for automated players, though you may want to enable this feature temporarily while you are testing your code.
  6. The player class must be able to display the total number of books of each player.
  7. The player class must have methods to detect and remove books from a player's hand and count the total number of books each player has.
  8. The game class must print who won the game.
  9. When a user select a rank to search for, your program must check that the rank is valid, and that the rank matches the rank of at least one of the cards in the player's hand. If a user selects an invalid rank, inform them of the mistake and ask again.
  10. Document your class methods.
  11. Test that your program works.

Some sample output is shown below:

Welcome to the game Go Farm.

Enter name of player 1: Alice
Enter name of player 2: Bob

Alice, your hand is:
[one 6, one 7, one 10, two Jacks, one Queen, one King]
You currently have 0 books and 7 cards
Which rank do you want to ask for? 2
You do not have a card of this rank. Choose again
Which rank do you want to ask for? acorn
Acorn is not a valid rank. Choose again
Which rank do you want to ask for? queen
Got 1 Queen from Bob
You get another turn

Alice, your hand is:
[one 6, one 7, one 10, two Jacks, two Queens, one King]
You currently have 0 books and 8 cards
Which rank do you want to ask for? queen
Bob doesn't have that rank
Looks like it is time to Go Farm!
You drew the 6 of Clubs.


...
SKIPPING a lot of turns
...


Bob, your hand is:
[one 2, two 3s, one 4, two 5s, two 8s, three 10s]
You currently have 0 books and 11 cards
Which rank do you want to ask for?  5
Alice doesn't have that rank
Looks like it is time to Go Farm!
You drew the 5 of Diamonds.
You got want you wanted and get another turn.


...SKIP...


Alice, your hand is:
[two 6s, two 7s, two 9s, one 10, three Jacks, two Queens, three Kings, one Ace]
You currently have 0 books and 16 cards
Which rank do you want to ask for?  10
Got 3 10s from Bob
You get another turn
You formed a new book!
Alice, your hand is:
[two 6s, two 7s, two 9s, three Jacks, two Queens, three Kings, one Ace]
You currently have 1 books and 15 cards
Which rank do you want to ask for?


...SKIP...


Alice, your hand is:
[three 7s]
You currently have 8 books and 3 cards
Which rank do you want to ask for?  7
Got 1 7 from Bob
You get another turn
You formed a new book!
You currently have 9 books and 0 cards

Alice, your hand is:
No cards in Hand
You currently have 9 books and 0 cards
You have no cards and must draw a card.
You drew the 8 of Diamonds.
Alice, your hand is:
[one 8]
Which rank do you want to ask for?  8
Got 3 8s from Bob
You get another turn
You formed a new book!
You currently have 10 books and 0 cards

No cards left. Game is over.
Alice beat Bob 10 books to 3
Hints, Tips, or Clarifications

Simplify the hand description

Because the card suit does not matter in this game, you do not need to display the suits when you print a hand. Thus a description such as
[two 6s, two 7s, two 9s, three Jacks, two Queens, three Kings, one Ace]
is much more compact than writing out all 15 cards with their suits. It is not mandatory that you follow this format, but you might find it to be a useful representation of a hand.

Use python in the terminal

Now that you are editing and running code from multiple files, IDLE will sometimes get confused regarding import statements. For this assignment, a better testing method is to run your code from the terminal, aka, the command line, the shell, or the black screen. You can open a new terminal by clicking on the black rectangular icon at the bottom of your XFCE desktop. You may also want to look at Homework 1 for a Unix skills refresher.

You can change into your homework 10 directory using the cd command

cd cs21/homework/10/

To run your game or other test code, type python game.py where game.py is the name of your game program. If you would like to stay in the python environment after running you game, you can type python -i game.py which will return you to a python prompt >>>. Here you can type additional python commands and test your code some more. To quit the python shell, press Ctrl-D.

You can still use IDLE to edit and save your work, but using python from the terminal will save you from a number of strange IDLE problems.

Optional Extensions
As noted above, these questions are NOT required to receive full credit. There are several extensions you could add to this program. Here are a few we thought might be interesting.

Support more than 2 players

Implement support for more than two players. You can find rules for more players online on Wikipedia or some other site.

Variations

Some people play that you only need two cards with the same rank to form a book. Perhaps you get an extra turn if you draw a card and complete a book, even if you did not draw the card you asked for. Given the number of books each player has, you can determine if a player is guaranteed to win and stop the game early. In the example above Alice wins when she collects her seventh book. You could display a message saying Alice has clinched a win and quit early. Implement one or more of these variations or come up with your own.