from random import *
def dieRoll():
return randrange(6)+1
def playOnePieceGame():
"""Find the number of turns to bear off a piece when it starts from the first space"""
space = 0
turns = 0
while space < 24:
space += dieRoll()
# Don't use the negative die if we've already got to the end
if space < 24:
space -= dieRoll()
# Assume we don't move further back than the first space
#(The rules aren't exactly well defined for this case)
if space < 0:
space = 0
turns += 1
return turns
numberOfGames = 1000
totalTurns = 0
minTurns = float('inf')
maxTurns = 0
for i in range(numberOfGames):
turns = playOnePieceGame()
totalTurns += turns
minTurns = min(minTurns, turns)
maxTurns = max(maxTurns, turns)
print 'The average number of turns in %d games was %d (min = %d, max = %d)'%(numberOfGames, totalTurns / numberOfGames, minTurns, maxTurns)