# simulate playing table racing game with two different dice
# An ordinary dice with values 1-6 and a dice with values 2, 4,
# 8, 16, 32
# Players start at position 1. The board has 52 positions. Position
# 52 is the goal, but if the player does not land on the exact position,
# has to go another round.
# If player lands on positions 3, 11, 15, 25, 27, 38, 43 and 48, has to
# go forward or back a given amount as follows:
# 3 --> +7
# 11 --> +8
# 15 --> +1
# 25 --> +5
# 27 --> +5
# 38 --> -6
# 43 --> +2
# 48 --> +1
# imports
import random
# create a function to advance player from current position to next
# position taking into account extra forward or backward movements
def advance(current_position, dice_count):
landing_place = (current_position + dice_count) % 52
if landing_place == 3:
next_position = 10
elif landing_place == 11:
next_position = 19
elif landing_place == 15:
next_position = 16
elif landing_place == 25:
next_position = 30
elif landing_place == 27:
next_position = 21
elif landing_place == 38:
next_position = 34
elif landing_place == 43:
next_position = 45
elif landing_place == 48:
next_position = 49
else:
next_position = landing_place
return next_position
# define functions to trow dice
def throw_ord_dice ():
return random.choice([1, 2, 3, 4, 5, 6])
def throw_exp_dice ():
return random.choice([2, 4, 8, 16, 32, 64])
# define function to simulate game between two players. Returns
# 'ordinary' or 'exponential' according to which player wins
# returns 'undecided' if the game does not end after 1000 turns
def play_game():
position_ord = 0
position_exp = 0
turns = 0
while (turns < 1000 and position_ord != 0 and position_exp !=0) or turns ==0:
turns +=1
position_ord = advance( position_ord, throw_ord_dice())
position_exp = advance( position_exp, throw_exp_dice())
if position_ord != 0 and position_exp !=0:
return 'undecided'
elif position_ord == 0:
return 'ordinary'
else:
return 'exponential'
# run the simulation 100000 times and print the results
if __name__ == '__main__':
# store results for ordinary dice and exponential dice
wins = { 'ordinary': 0, 'exponential': 0, 'undecided': 0}
# play game
for _ in range(100000):
wins[play_game()] += 1
# print results
print (wins)