Question: I need help writing a program to simulate the card game called War in Python Code. You will write a program to simulate the game

I need help writing a program to simulate the card game called War in Python Code.
You will write a program to simulate the game of war and count the number of hands it takes to play a game. You will then run this simulation many times to approximate how long a game lasts.
Rules
There are a few variants to the game of war, so please read these rules carefully even if you are already familiar with the game.
A standard 52-card deck is shuffled and dealt to two players so each receives 26 cards into their personal decks. Then hands are played until the one player runs out of cards. If the other player still has cards, they win, otherwise the game is a draw.
A hand consists of the following steps:
Both players simultaneously reveal the top card from their respective decks placing them into a common pool.
When those cards are revealed the face value, ignoring the suit, is compared. If one is higher than the other, the player with the higher cards takes all of the cards in the common pool and places them on the bottom of their personal decks, ending the turn.
If the face value of the cards is the same, then place the top card from both player decks into the common pool, then reveal another card for comparison. The process of moving the top cards to the common pool followed by another card reveal continues until either there is a winner of the war or one player runs out of cards.
The game ends when one player runs out of cards. If both players run out of cards simultaneously, the game is a draw, otherwise the player that still has cards is the victor.
The order of the faces should be 2,3,4,5,6,7,89,10, J, Q, K, A, where an ace beats every other card.
Your Program
You will write a program to simulate the game and approximate the average number of hands per game by running the game multiple times. There are the following restrictions on your program:
You must create cards as tuples that have both the face value and suit. It is acceptable to use only ints to represent the face values
Tips:
Your program needs to have test functions. So for instance you may have a test_play_hand function that has a piece like:
player1=[(14,"H")]
player2=[(13,"H")]
if play_hand(player1, player2)!=1
print(f"Error: expected player1 to win")
Where you should have more tests with different initial hands and so forth.
You should have tests like this for all of the functionality of your code, where practical. You can test the rules of War by having your test functions set up the various player's decks into small examples with just a couple cards each to test the various scenarios of wins, losses, and draws. It is not practical to test the play_hand on a full random deck as this point, so you can skip that.
Tips:
It is recommended that you represent the various decks of cards as lists. Useful member functions include pop, and append.
To shuffle your cards, use the random.shuffle()Links to an external site. function.
To start, begin by working on building a deck. Then do the initial game setup. Then progress to playing a single game. Then finally the full simulation.
Depending on the exact deck ordering an infinite loop can occur. To fix this, shuffle the common pool of cards before putting them on the bottom of the winner's deck. You can trigger this infinite loop in quite a few scenarios. For example, suppose one player has 2H,3H, and the other 3S,2S. This scenario will result in a game that never ends when the order that cards are added to the common pool.
Some potential pseudocode example tips:
def play_hand(player1_deck, player2_deck):
winner = neither
common_pool
while winner == neither:
# Ensure both players have cards to play
if player1_deck has 0 cards or player2_deck has 0 cards:
return
# Determine who the winner is
if player1_deck[0] beats player2_deck[0]:
winner = player1
elif player1_deck[0] is the same as player2_deck[0]:
winner = neither
else:
winner = player2
# Remove cards from each player and put them into the common pool
# Shuffle cards from the common pool
# Give the common pool to the winner's deck at the back
# return the winner
def count_hands(){
int count =0
while player1_deck has cards and player2_deck has cards:
play_hand()
count +=1
return count
To remove a card from the front of the deck simply do
foo.pop(0)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!