Question
**Code in Python** Your assignment is to write a program that simulates a game of War, the card game, between two players. Basic Rules of
**Code in Python**
Your assignment is to write a program that simulates a game of "War", the card game, between two players.
Basic Rules of War:
War is a card game played with a standard deck of 52 cards. Cards are ranked from high to low in the order Ace, King, Queen, Jack, 10, 9, 8, 7, 6, 5, 4, 3, 2. There are four suits: Spades, Hearts, Diamonds, and Clubs. The suits are of equal value.
To play the two-player game, the dealer shuffles the deck and deals out the entire deck to the players one card at a time. The players keep their "hands" in a stack facedown on the table in front of them. In each round, they simultaneously draws the top card from their hands and places it face up on the table, exposing its value to all players. The player whose card has the greater value wins the round. The played cards are collected, turned face down, and added to the bottom of the winner's deck.
If both cards that are turned face up have the same value, "War" is declared. In this case, each player draws three additional cards, one at a time, and places them in a pile facedown next to their hands. They then draw a fourth card and places it face up on the table. The player whose fourth card has the greater value wins the round. All of the played cards, including the ones dealt facedown, are collected and added to the bottom of the winner's deck.
If both cards that are turned face up during War have the same value, then the players continue dealing three facedown cards and a faceup card until one player wins the War and collects all of the cards played.
If at any time a player runs out of cards, the game is over, and the player holding all of the cards wins.
Note that there is no strategy involved in War as defined above: results are entirely determined by the random order of the cards. This makes it an excellent game for children. . .and for programming homework assignments!
To get started:
- Create a class Card.
- When you create an instance of Card, you pass it a Suit ("C", "D", "H", or "S") and a Rank (2, 3, 4, 5, 6, 7, 8, 9, 10 J, Q, K, A). These values should be stored as instance variables for the new card object.
- Card should have an __str__ method that lets you print an individual card (such as "3S" for the three of spades).
- Create a class Deck.
- When you create an instance of Deck, it should create a list of 52 Card objects and save it to an instance variable cardList.
- It should have a shuffle() method that rearranges the cards in cardList. You can do this easily by importing the random package into Python, and using therandom.shuffle() method. random.shuffle(myList) rearranges the elements of the list myList into a random order. Also, if you insert the statement random.seed(#) (where "#" is an integer) before calling "shuffle", you will always get the same shuffle. This is useful for debugging.
- It should have a dealOne() method that removes the first card from your deck's cardList, and appends it to the hand of a specified player.
- It should have an __str__ method that lets you print out the entire deck in four rows of 13 cards (neatly aligned into columns) for debugging purposes. Note that the __str__method for Deck objects should take advantage of the __str__ method for Card objects.
- Create a class Player.
- When you create an instance of Player, it should have an instance variable hand that's set to an empty list, and an instance variable handTotal that's set to zero. These instance variables will be modified by the Deck class' dealOne() method.
- It should have an __str__ method that lets you print out the hand of a Player.
Your main program might look something like this:
def main(): cardDeck = Deck() # create a deck of 52 cards called "cardDeck" print("Initial deck:") print(cardDeck) # print the deck so we can see that you built it correctly random.seed(15) # leave this in for grading purposes cardDeck.shuffle() # shuffle the deck print("Shuffled deck:") print(cardDeck) # print the deck so we can see that your shuffle worked player1 = Player() # create a player player2 = Player() # create another player for i in range(26): # deal 26 cards to each player, one at cardDeck.dealOne(player1) # a time, alternating between players cardDeck.dealOne(player2) playGame(cardDeck,player1,player2) if player1.handNotEmpty(): print(" Game over. Player 1 wins!") else: print(" Game over. Player 2 wins!") print (" Final hands:") print ("Player 1: ") print (player1) # printing a player object should print that player's hand print (" Player 2:") print (player2) # one of these players will have all of the cards, the other none main()
All of the real work is done in function playGame(). Although no specific output is required from playGame() for grading purposes, it should print out appropriate intermediate results during each round of play for debugging purposes, so you can confirm that your program is behaving correctly.
Input and Output:
There is no input file for this assignment. You will create a deck, shuffle it, and play a game with it.
Your output should consist of everything shown in the main program above.
IMPORTANT: for the purpose of this assignment:
- When you create the deck, create it such that the initial order of the cards are: all of the clubs from 2 to Ace, all of the diamonds from 2 to Ace, all of the hearts from 2 to Ace, then all of the spades from 2 to ace. This will ensure everyone starts with the same new deck.
- When you shuffle the cards, be sure to use random.seed(15) to ensure everyone starts with the same shuffled deck. Using a seed value of 15 shortens the run significantly.
- When you pick up the played cards, add all of the cards from the first player to the bottom of the round winner's hand, in the order that they were played in the round. Then add all of the cards from the second player to the bottom of the round winner's hand, int he order that they were played in the round.
By doing this, everyone should get the exact same results, making it easier for you (and us) to verify the correctness of your program. Your output should look something like this.
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