Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
170908_riddler-nation-goes-to-war
# https://fivethirtyeight.com/features/riddler-nation-goes-to-war/
# http://www.bicyclecards.com/how-to-play/war/
import random
# https://docs.python.org/3/library/random.html
random.seed(0)
# Ace is 14
def deck_of_aces():
return [(14,'club'),(14,'diamond'),(14,'heart'),(14,'spade')]
# King is 13, Queen is 12, Jack is 11
def deck_without_aces():
# The suits don't matter, but it helped with debugging.
unshuffled_deck = [
(13,'club'), (12,'club'), (11,'club'), (10,'club'), (9,'club'), (8,'club'), (7,'club'), (6,'club'), (5,'club'), (4,'club'), (3,'club'), (2,'club'),\
(13,'diamond'), (12,'diamond'), (11,'diamond'), (10,'diamond'), (9,'diamond'), (8,'diamond'), (7,'diamond'), (6,'diamond'), (5,'diamond'), (4,'diamond'), (3,'diamond'), (2,'diamond'),\
(13,'heart'), (12,'heart'), (11,'heart'), (10,'heart'), (9,'heart'), (8,'heart'), (7,'heart'), (6,'heart'), (5,'heart'), (4,'heart'), (3,'heart'), (2,'heart'),\
(13,'spade'), (12,'spade'), (11,'spade'), (10,'spade'), (9,'spade'), (8,'spade'), (7,'spade'), (6,'spade'), (5,'spade'), (4,'spade'), (3,'spade'), (2,'spade')
]
shuffled_deck = random.sample(unshuffled_deck, len(unshuffled_deck))
return shuffled_deck
player_aces = deck_of_aces()
player_two = deck_without_aces()
# for debugging, print each player's decks.
def get_current_state():
print('player aces',len(player_aces), '\n', player_aces, '\n')
print('player two',len(player_two), '\n', player_two, '\n')
# All the game logic for a single iteration
def play_game(player_aces, player_two):
def take_cards(winner, loser, i):
# If loser doesn't have enough cards, take them all.
if len(loser) < i:
i = len(loser)
# Puts winner card on bottom
for cards in range(i):
winner.insert(len(winner),winner.pop(0))
# Puts their card on bottom
winner.append(loser.pop(0))
def determine_hand_winner(player_aces, player_two, i, number_of_cards):
if player_aces[i][0] > player_two[i][0]:
# print('ACES deck wins hand')
# print(player_aces[i], '<=--', player_two[i])
take_cards(player_aces, player_two, number_of_cards)
elif player_aces[i][0] < player_two[i][0]:
# print('player TWO deck wins hand')
# print(player_aces[i], '--=>', player_two[i])
take_cards(player_two, player_aces, number_of_cards)
elif player_aces[i][0] == player_two[i][0]:
# print(player_aces[i], '==', player_two[i])
# print('WARRR!!', len(player_aces), len(player_two))
war(player_aces, player_two, i)
else:
'something broke'
def war(player_aces, player_two, i):
# print('beginning war script')
i = i + 2
# print(i, 'i')
cards = i
# Check if each player has enough cards
if len(player_aces) <= i or len(player_two) <= i:
# print('oh no!, not enough cards. Instant lose.')
cards = min(len(player_aces), len(player_two))
if len(player_aces) < len(player_two):
take_cards(player_two, player_aces, cards)
else:
take_cards(player_two, player_aces, cards)
elif player_aces[i][0] > player_two[i][0]:
# print('ACES deck wins WAR')
take_cards(player_aces, player_two, cards)
elif player_aces[i][0] < player_two[i][0]:
# print('TWO deck wins WAR')
take_cards(player_two, player_aces, cards)
elif player_aces[i][0] == player_two[i][0]:
war(player_aces, player_two, i)
else:
# print('something is wrong.')
get_current_state()
def winner_check():
if len(player_aces) == 52:
# print('Aces deck wins!')
return 'player_aces'
elif len(player_two) == 52:
# print('Player Two wins!')
return 'player_two'
else:
# print('game not over')
pass
winner = None
while winner_check() == None:
determine_hand_winner(player_aces, player_two, 0, 1)
return winner_check()
print('GAME OVER \n\n\n\n\n')
# END play game function
record_winner = []
for i in range(100000):
# Print how far along the iterations are.
if i % 1000 == 0 : print(i)
# get_current_state()
player_aces = deck_of_aces()
player_two = deck_without_aces()
record_winner.append(play_game(player_aces, player_two))
play_game(player_aces, player_two)
# get_current_state()
print('total games')
print(len(record_winner))
print('aces deck won # times')
print(record_winner.count('player_aces'))
print((record_winner.count('player_aces')) / len(record_winner))
'''
total games
100000
aces deck won # times
84571
0.84571
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.