Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is a python question Question 6.34 The two-player card game war is played w

ID: 3681476 • Letter: T

Question

This is a python question

Question 6.34

The two-player card game war is played with a standard deck of 52 cards. A shuffled deck is evenly split among the two players who keep their decks face-down. The game consists of battles until one of the players runs out of cards. In a battle, each player reveals the card on top of their deck; the player with the higher card takes both cards and adds them face-down to the bottom of her stack. If both cards have the same value, a war occurs.

In a war, each player lays, face-down,their top three cards and picks one of them. The player who picks the higher valued card adds all eight to the bottom of her deck. In case of another tie, wars are repeated until a player wins and collects all cards on the table. If a player runs out of cards before laying down three cards in a war, he is allowed to complete the war, using his last card as his pick.

In War, the value of a number card is its rank, and the values of cards with rank A,K,Q, and J are 14,13,12, and 11, respectively.

(a) Write a function war() that simulates one game of war and returns a tuple containing the number of battles,wars, and two-round wars in the game. Note: When adding cards to the bottom of a player's deck, make sure to shuffle the cards first to add additional randomness to the simulation.

(b)Write a function warStats() that takes a positive integer n as input, simulates n games of war, and computes the average number of battles, wars, and two round wars.

Explanation / Answer

import random

def buildDeck():   
ranks = [str(x) for x in range(2, 11)] + ['J', 'Q', 'K', 'A']
suits = ['D', 'H', 'S', 'C']
deck = [rank + ' of ' + suit for rank in ranks for suit in suits]
random.shuffle(deck)

return deck

def cardValue(card):
faceCards = ['J', 'Q', 'K', 'A']

if card[0] in faceCards:
return faceCards.index(card[0]) + 11

else:
return int(card[0])

def cleanUp(pileOne, pileTwo):
if not pileOne[-1]:
pileOne.pop()
if not pileTwo[-1]:
pileTwo.pop()

def cardChoice(pile):
if pile[-1]:
return random.choice(pile[-3:])
else:
return pile[-2]

def replaceCards(deck, pileOne, pileTwo):
pileOne.extend(pileTwo)
random.shuffle(pileOne)
deck.extend(pileOne)

def playWar():

deck = buildDeck()
battleStat, warStat, doubleWarStat = 0, 0, 0
pileOne, pileTwo, deckOne, deckTwo = [], [], [], []

for i in xrange(26):
deckOne.append(deck.pop())
deckTwo.append(deck.pop())

while deckOne and deckTwo:

del pileOne[:], pileTwo[:]
pileOne.append(deckOne.pop(0))
pileTwo.append(deckTwo.pop(0))

if cardValue(pileOne[0]) > cardValue(pileTwo[0]):
battleStat += 1
replaceCards(deckOne, pileOne, pileTwo)

elif cardValue(pileTwo[0]) > cardValue(pileOne[0]):
battleStat += 1
replaceCards(deckTwo, pileOne, pileTwo)

else:

warCount = 1

while True:
battleStat += 1
warStat += 1

for i in xrange(3):
if deckOne:
pileOne.append(deckOne.pop(0))
elif pileOne[-1]:
pileOne.append('')

if deckTwo:
pileTwo.append(deckTwo.pop(0))
elif pileTwo[-1]:
pileTwo.append('')

if cardValue(cardChoice(pileOne)) > cardValue(cardChoice(pileTwo)):
cleanUp(pileOne, pileTwo)
replaceCards(deckOne, pileOne, pileTwo)

if warCount == 2:
doubleWarStat += 1
break

elif cardValue(cardChoice(pileTwo)) > cardValue(cardChoice(pileOne)):
cleanUp(pileOne, pileTwo)
replaceCards(deckTwo, pileOne, pileTwo)

if warCount == 2:
doubleWarStat += 1
break

warCount += 1

return battleStat, warStat, doubleWarStat

def warStats(2):
'''Records statistics from 2 number of War games.
Prints out calculated averages for battles, wars, and double wars.'''

battleStat, warStat, doubleWarStat = 0.0, 0.0, 0.0

for i in range(2):
result = playWar()
battleStat += result[0]
warStat += result[1]
doubleWarStat += result[2]

print ' Avg # of battles: ' + str(battleStat/2)
print 'Avg # of wars: ' + str(warStat/2)
print 'Avg # of double wars: ' + str(doubleWarStat/2)

while True:
selection = 2;

try:
if selection.lower()[0] == 'q':
break

elif 0 < int(selection):
warStats(int(selection))

else:
print ' Please enter an integer between 1 and 2'

except:
print ' Please enter an integer between 1 and 2'

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote