Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Card game: War(python) The card game War is a card game that is played with a deck of 52 cards. The deck, after being shuffled,

Card game: War(python)

The card game War is a card game that is played with a deck of 52 cards.

The deck, after being shuffled, is divided evenly between the players. When there are two players each one receives 26 cards, dealt one at a time, face down. So the cards and their order are unknown. Each player places their stack of cards face down. It may seem as a STACK as we know it, since the player takes and plays one card at a time from the top of this stack. However, it is more like a QUEUE, since as we shall see it, when a player wins cards, these are placed at the bottom of this stack of cards.

Task 1 : Reading and Validating cards

You are given a text file of 52 shuffled cards, each card is in a line coded on two characters as described above. You need to prompt the user for the name of the file, open the file and read it. Make sure the file exists and make sure the cards in the file are correctly formatted as described above. If the cards are in lower case, it is not an error as you can simply transform them in upper case. You can assume the cards are shuffled but don't assume they are formatted as specified or that there are exactly 52, or even that they are not repeated. Make sure all the standard 52 cards are there in the shuffled deck. You are not asked to correct the cards, but display an appropriate error message and terminate the program in case there is an issue with the cards.

You are given a python program shuffleCards.py that generates the file suffledDeck.txt as we described above with 52 lines constituting the shuffled deck. However, while this program generates a correct input to your program don't assume the input file is always correct. The assessment of your assignment may be made with a file that is incorrect.

Task 2: Distributing cards

Now that you have read the 52 cards from the file and you know you shuffled deck is complete and correct, distribute the cards to both players: the user and the computer. You should give one card to each player repeatedly until all cards are distributed. You should start randomly with either player.

The players keep their cards in such a way that the first card served is the first that will be played. In other words, the containers of these cards should receive the cards on one end and use them from the other end. Use the circular queue we saw and implemented in class to represent the player's hand. The capacity should be set to 52 since you will never have more than 52 cards.

Task 3: Asking user for data

Ask the user whether they would like to play a war with one, two, or three cards face-down. validate the input from the user and don't proceed until a valid input is given.

Task 4: Comparing cards

You need a way to compare two cards. Write a function that given two correct cards returns 0 if the cards are of equal rank; 1 if the first card is of higher rank; and -1 if the second card is of higher rank. The ranks are explained above. Also, remember that the rank is the first character of the card and the cards are strings of two characters.

Task 5: class OnTable

We need to represent the cards on the table, the cards that are currently placed on the table by both players either face-up or face-down. Create an encapsulated class OnTable with the behaviour described below. We will use two simple lists: one list for the cards and one list to indicate whether they are face-up or face-down. Cards put on the table by player 1 will be added from the front of the list (position 0). The cards put on the table by player 2 will be added from the end of the list. write a class to represent this double list data structure. The attributes are: __cards which is a list that will contain the cards, and __faceUpwith will have booleans to indicate if the __cards list in the same position in __cards is face-up or face-down. The interface of this class should include place(player, card, hidden), where player is either 1 or 2 for player 1 or player 2, card is the card being placed on the table, and hidden is False when the card is face-up and True when the card is face-down. This method should update the attributes of the class appropriately. The interface should also have the method cleanTable(). This method returns a list of all cards on the table as stored in the attribute __cards and resets __cards and __faceUp. All cards returned this way are visible. Finally, also write the method __str__() that should allow the conversion of the cards on the table into a string to be displayed. We would like to display the cards as a list. However, cards that are face-down should be displayed as "XX".

Task 6: The Game

Given the following algorithm and based on the previous tasks, implement the card game War. Obviously, there are details in the algorithm specific to the implementation in Python that are missing and it is up to you as an exercise to do the conversion from this pseudo-code to Python.

There are other practical details to add to the algorithm. At the end of the game, we need to display who was the winner (player1, the user, or player2 the computer). Also in line 12, a pause is required. For this pause, display the number of cards in each player's hand and wait for a keyboard return key. In other words, get an input from the user.

image text in transcribed

Examples of outputs during game:

[8D, KH] Player1: 20 cards Player2: 30 cards Press return key to continue. 
[8C, XX, XX, XX, 4H,4D, XX, XX, XX, 6H] Player1: 16 cards Player2: 26 cards Press return key to continue. 
01. Read file with shuffled deck 02. Validate cards 03. Distribute cards to userl and user2 04. nbWarCards ? input from user 05. endGame False 06. cardsOnTable f- new OnTable0 07. While not endGame do 08 09 10 faceUpl faceUp2 then 12 13 14 15 16 17 18 19 20 21 userl gets all cards in cardsOnTable else if faceUpl

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Focus On Geodatabases In ArcGIS Pro

Authors: David W. Allen

1st Edition

1589484452, 978-1589484450

More Books

Students also viewed these Databases questions