## CS 313E: Bulko

Programming Assignment 3:
War! (The Card Game)
Due Date: October 6, 11:59 pm

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:

• 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 the random.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("\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:

• 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.

Turning in the Assignment:
• You must submit a file called War.py via Canvas by the deadline shown at the top of this page.
• As always, I will be looking for good documentation, descriptive variable names, clean logical structure, and adherence to all coding conventions expected of an experienced programmer; there will be penalties for failure to meet these standards.
• Your code must compile and run before submission.
• The file MUST include a header with the following format:
```#  File: War.py
#  Description:
#  Student's Name:
#  Student's UT EID:
#  Course Name: CS 313E
#  Unique Number:
#
#  Date Created: