Description: This program simulates a simple game of War
# in which two players draw cards from from a shuffled deck. # The higher card wins with ties going to the player who # drew first.
from random import shuffle
Generate a shuffled deck of playing cards.
cards = list(range(2, 15)) * 4 shuffle(cards)
Deal the two players the first half of the cards.
player1 = cards[:26] player2 = cards[26:]
Set the score variables to 0
p1_score = 0 p2_score = 0
Play until one player has no cards left.
while player1 and player2:
p1_card = player1.pop(0)
p2_card = player2.pop(0)
if p1_card > p2_card:
p1_score += 1
elif p2_card > p1_card:
p2_score += 1
Output the result of the game.
if p1_score > p2_score: print(“Player 1 wins with a score of %s to %s!” % (p1_score, p2_score)) elif p2_score > p1_score: print(“Player 2 wins with a score of %s to %s!” % (p2_score, p1_score)) else: print(“It was a tie!”)
Loading...