In this assignment, we will finish our shell game of Go Fish. We will tweak some the existing functions we created last time as well as add new functions that will allow the computer to request a card, determine if the game is over, score the game, and run a full game with the option to play again. There are many versions of Go Fish. Ours works like this:

There are two players: the user and the computer. The goal of the game is to make the most "books" (four of a kind). Aces are the highest points, 2s are the lowest. A book of 2s is worth 2 points, a book of 3s is worth three points, a book of kings (K) is worth 13 points, and a book of aces (A) is worth 14 points. The computer and user both begin with 7 cards. In each turn, one player requests a card from the other player. Players can only request cards they also own a copy of. If the other player has that kind of card, they must transfer all copies to the requesting player. If the other player does not have that kind, the requesting player must Go fish by drawing a card from the deck. Players alternate turns and the user goes first. For example, suppose the user has one jack (J) and requests jacks from the computer. If the computer has at least one jack, then computer must transfer all their copies of jacks to the user. Then it is the computer's turn to request a card. The game ends once every book is owned by a player. Points are then tabulated based on the books owned and a winner is declared. For example, here might be the final hands of a game: user = ['2, '2', '2', '2', '7', '7', '7', '7', '8', '8', '8', '8', '9', '9', '9', '9', 'A', 'A', 'A', 'A', 'J', 'J', 'J', 'J', 'K', 'K', 'K', 'K'] comp = ['10', '10', '10', '10', '3', '3', '3', '3', '4', '4', '4', '4', '5', '5', '5', '5', '6', '6', '6', '6', 'Q', 'Q', 'Q', 'Q'] The users score will be 2 + 7 + 8 + 9 + 11 (J) + 13 (K) + 14 (A) = 64. The computers score will be 3 + 4 + 5 + 6 + 10 + 12 (Q) = 40. The user would be the winner.

For this project, you can start with your own file or the one provided. At the top of the file following user and comp, you will create a dictionary that converts card values to points. Give this variable a sensible name. The following functions will either need to be written or changed. Think carefully about the techniques we've covered in class that allow us to achieve these outcomes.

draw_card() -- Adjust this function to consider the case when deck is empty. If the deck is empty, print "Deck is out of cards" and return None.

display() -- Adjust this function so the user can easily determine what books they have and can strategize better. (I suggest doing this last)

play_again() -- Adjust the case when the user opts to play again. Before returning True, clear user and comp of contents and add cards back into deck. Use only list methods to do this; do not user assignment statement for user, comp or deck. Remember that you can use the dir() function to see the available methods.

welcome_message() -- This function has no parameters. It prints a welcome message for the user announcing they will play Go Fish and giving a basic explanation of the rules.

is_game_over() -- This function takes in no parameters. It returns a boolean value based on whether the conditions for the game to be over are met. It first checks if deck is nonempty. If it has at least one card, the function returns False. The function then loops through all the unique cards held in comp (use a set for this loop!). If the count of any card in comp is less than 4, it returns False. The same approach is repeated with user. At the end of the function, return True (meaning that return False was never called in the previous cases).

ask_comp() -- This function has no parameters. Create an empty list called potential_cards. Loop through all the unique cards in comp. If less than four copies are in comp, add the card to potential_cards. If potential cards is not empty, pick a card from it at random. Otherwise, the computer has only books (4-of-a-kind) so the computer should just pick a random card from comp. Print a statement letting the user know what card the computer is requesting. If the request is in user, print a statement letting the user know that they have this card and call give_cards() with the correct parameters. Otherwise, print a statement letting the user know they do NOT have this card, and call go_fish with the correct parameter.

score() -- This function takes in no parameters. Make two variables representing the computer's total score and the users total score and assign them the value 0. Pick good names for these variables. For each unique card held by the computer, use the dictionary to compute the score of that card/book and add it to the computers total score. For each unique card held by the user, use the dictionary to calculate the score of that card/book and add it to the users total score. Then consider three conditions:

  • if the user's total score is higher, print a statement letting the user know they won the game;
  • if the computer's total score is higher, print a statement letting the user know they lost the game; and
  • otherwise, let the user know they tied with the computer.

game() -- Call welcome_message() and deal_cards(). While is_game_over() is False, perform the following commands: (1) call ask_user(), (2) if is_game_over(), use the break command to end the loop, (3) call ask_comp(), and (4) call display(). After the while loop, print a statement letting the user know the game is over. Call score(). If the user wishes to play again, call game(). All functions should have doc strings and type hints (when necessary). Make this shell game as user-friendly as possible. Slow down the print-ups so as not to overwhelm the user by using the following command: input("Press enter to continue") This will require the user to hit enter in order to read more prints. I leave it to you to determine where these should go.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.