Your assignment is to write a program that simulates a game of "War", the card game, between two players.Basic Rules of War:
More on the card game can be found at this Wikipedia page.
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:
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("\n\nGame over. Player 1 wins!") else: print("\n\nGame over. Player 2 wins!") print ("\n\nFinal hands:") print ("Player 1: ") print (player1) # printing a player object should print that player's hand print ("\nPlayer 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:
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.Turning in the Assignment:
# File: War.py # Description: # Student's Name: # Student's UT EID: # Course Name: CS 313E # Unique Number: # # Date Created: # Date Last Modified: