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

Python The two-player card game war is played with a standard deck of 52 cards.

ID: 3760308 • Letter: P

Question

Python

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 shuffleDeck(): suits = {'u2660', 'u2661', 'u2662', 'u2663'} ranks = {'2', '3', '4', '5', '6', '7', '8' '9', '10', 'J', 'Q', 'K', 'A'} deck = [] for suit in suits: for rank in ranks: deck.append(rank + ' ' + suit) random.shuffle(deck) return deck def splitDeck(deck): splitdeck = [] for i in range(0, len(deck)//2): splitdeck.append(deck[i]) deck.remove(deck[i]) return splitdeck def dealCard(deck): if deck != []: card = deck[0] deck.remove(card) return card def battle(card1, deck1, card2, deck2): values = {'A':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, '1':10, 'J':11, 'Q':12, 'K':13} if values[card1[0]] > values[card2[0]]: deck1.append(card1) deck1.append(card2) elif values[card1[0]] 2 and len(deck2) > 2: for i in range(0, 3): FDdeck1.append(deck1[i]) FDdeck2.append(deck2[i]) for i in range(-3, 0): deck1.remove(FDdeck1[i]) deck2.remove(FDdeck2[i]) i = random.randrange(len(FDdeck1)-3, len(FDdeck1)) j = random.randrange(len(FDdeck2)-3, len(FDdeck2)) newcard1 = FDdeck1[i] newcard2 = FDdeck2[j] if values[newcard1[0]] > values[newcard2[0]]: for card in FDdeck2: deck1.append(card) # this line break # and this line should be indented further elif values[newcard1[0]] values[newcard2[0]]: for card in FDdeck2: deck1.append(card) elif values[newcard1[0]] values[newcard2[0]]: for card in FDdeck2: deck1.append(card) # this line break # and this line should be indented further elif values[newcard1[0]] values[newcard2[0]]: for card in FDdeck2: deck1.append(card) elif values[newcard1[0]] values[newcard2[0]]: for card in FDdeck2: deck1.append(card) break elif values[newcard1[0]]