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 card deck is shuffled and dealt to two players so each receives 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 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 testplayhand function that has a piece like:
playerH
playerH
if playhandplayer player
printfError: expected player 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 playhand 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.shuffleLinks 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 HH and the other SS 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 playhandplayerdeck, playerdeck:
winner neither
commonpool
while winner neither:
# Ensure both players have cards to play
if playerdeck has cards or playerdeck has cards:
return
# Determine who the winner is
if playerdeck beats playerdeck:
winner player
elif playerdeck is the same as playerdeck:
winner neither
else:
winner player
# 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 counthands
int count
while playerdeck has cards and playerdeck has cards:
playhand
count
return count
To remove a card from the front of the deck simply do
foo.pop
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
