Question
Pedagogical Goal : Refresher of Python and hands-on experience with algorithm coding, input validation, exceptions, file reading, Queues, and data structures with encapsulation. Due date:
Pedagogical Goal: Refresher of Python and hands-on experience with algorithm coding, input validation, exceptions, file reading, Queues, and data structures with encapsulation.
Due date: June 9
The card game War is a card game that is played with a deck of 52 cards.
The goal is to be the first player to win all 52 cards. It is played by two players but can also be played with more.
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.
How do we play the game
Each player turns up a card at the same time and the player with the higher card takes both cards and puts them, face down, on the bottom of their stack.
If the cards are the same rank, it is War. Each player turns up one, two, or three cards face down (so they are not seen) and one card face up. The player with the higher cards face-up at the end takes both piles (six, eight or ten cards). If the turned-up cards are again the same rank, it is War again and each player places another set of cards (one, two or three) face down and turns another card face up. The player with the higher card face-up at the end takes all placed cards, and so on. The game continues until one player has all 52 cards and is declared the winner. There are three versions of this game depending upon the number of cards we place face-down when there is War: one card, two cards, or three cards. It remains consistent throughout the game.
A deck of cards:
A deck of 52 cards has four suits: diamonds, clubs, hearts, and spades). In each suit, we have the King, the Queen, the Jack, the Ace, and cards from 2 to 10.
The Ace is the highest rank, followed respectively by the King, the Queen, the Jack then the cards 10 down to 2. Cards from different suits but with the same rank are equivalent.
To display the cards on the screen, we use two characters to represent a card. The first character is the rank and the second the suit. The rank is either K for king, Q for queen, J for Jack, A for Ace, then 2 to 9 for the numbers and 0 for 10. The suits are D, C, H, and S. Example 8D is eight of Diamond; 0H is 10 of hearts.
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 your program must raise and catch an exception if card validation does not go through. The program should then 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 shuffledDeck.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 may call them player1 and player2. 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. The Circular Queue class should be inside assignment3 file that you submit.
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.
Example of a War with 3 cards down
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. The attributes of the class are: __cards which is a list that will contain the cards, and __faceUp with will have booleans to indicate if the __cards list in the same position in __cards is face-up or face-down. Cards put on the table by player 1 will be added on the left of self._cards. The cards put on the table by player 2 will be added on the right of self._cards. 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 shuffles and then return 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". Moreover, player1 and player 2 cards should be separated by a vertical line.Please see sample output.
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). As indicated in the algorithm, on line 45 and 46, after each round of the game, 60 dashes are displayed and the program will wait for the user to press enter before displaying the next round.
Sample Output
Part of output during game:
Here is a file that contains a complete sample run of the program.
Assignment Deliverables
You are going to submit one Python program that performs the specified tasks. The Circular Queue class should exist inside the file.
You do not need to submit shuffleCards.py of shuffledDeck.txt with your assignment.
Remember, coding style counts.
Submission Instructions
Please follow these instructions to correctly submit your solution.
Name your solution assignment3.py (Please note there should not be a space in the file name)
Submit your program at the end of this page
Note that late submissions will not be accepted. You are able to submit your work as often as you like and only the last submission will be marked. So submit early and submit often.
Marking Guidelines
Following is a tentative marking guideline. There may be changes made to the marking guidelines during the marking process.
Please note a maximum of 3 marks may be given if the program has syntax errors and does not run.
Item | Marks |
Following Submission Instructions | 0.5 |
Good Coding style Descriptive comments Good variable names Leaving blank lines at appropriate places in the program. All identifiers are either in the function definition as parameters or inside the body of the function. This implies that global identifiers outside of function definition should not be used in the program. | 0.5 |
asking for filename and validating filename - program raises and catches exception and terminates program with appropriate custom message | 1 |
validating cards (-0.5 for each failed validation) -lowercase is converted to uppercase 1.exactly 52 cards (raises exception if condition is not satisfied) 2.no duplicates (raises exception if condition is not satisfied) 3.cards are formatted correctly - test for incorrect suit and/or incorrect card number.( (raises exception if condition is not satisfied) program raises and catches exceptions, displays appropriate custom message if any of the above conditions (listed in 1,2,3) are not met. The program terminates after displaying the appropriate message. | 2 |
distributing cards | 1 |
asking for and validating user input for number of war cards | 1 |
Correct Implementation of onTable class as specified in the assignment specificiation | 2 |
Correct Program behavior
| 1 |
Output from program follows the output file given with the assignment | 1 |
Total | 10 |
following code:
########################################################################################################## # Creates a deck of 52 shuffles cards and stores in file shuffledDeck.txt # each line of the file is a card. A card is 2 characters. the first is the rank, the second is the suit # Suits are D, C, H, and S # Ranks are: K, Q, J, A, 2, 3, 4, 5, 6, 7, 8, 9, 0 # # Program by Osmar Zaiane # Version 1.0 2018-02-25 ########################################################################################################## from random import shuffle
suits=["D", "C", "H", "S"] ranks=["K","Q","J","A","2","3","4","5","6","7","8","9","0"]
cards=[] for rank in ranks: for suit in suits: cards.append(rank+suit) shuffle(cards) try: cardFile= open("shuffledDeck.txt", "w") for card in cards: cardFile.write(card+" ") except IOError as e: print ("I/O error({0}: {1}".format(e.errno, e.strerror)) except: print ("Unexpected error") finally: cardFile.close() print ("The following shuffled 52 card deck was saved in shuffledDeck.txt") print (cards)
"shuffledDeck.txt" content
0D 8S 0C 7C 7H 9H 5D JD 5C KH AC 2H KD 4C KS 6S 8C 2D 3H 9S QD 8D 6D KC 9D JS JC QS 7D 4S QC 7S 3D 3C 2S 2C 4H JH 6H 8H 6C 9C AS 5S 3S 5H 0S QH AD 4D AH 0H
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started