fork download
  1. from random import *
  2.  
  3. def dieRoll():
  4. return randrange(6)+1
  5.  
  6. def playOnePieceGame():
  7. """Find the number of turns to bear off a piece when it starts from the first space"""
  8. space = 0
  9. turns = 0
  10. while space < 24:
  11. space += dieRoll()
  12. # Don't use the negative die if we've already got to the end
  13. if space < 24:
  14. space -= dieRoll()
  15. # Assume we don't move further back than the first space
  16. #(The rules aren't exactly well defined for this case)
  17. if space < 0:
  18. space = 0
  19. turns += 1
  20. return turns
  21.  
  22. numberOfGames = 1000
  23. totalTurns = 0
  24. minTurns = float('inf')
  25. maxTurns = 0
  26. for i in range(numberOfGames):
  27. turns = playOnePieceGame()
  28. totalTurns += turns
  29. minTurns = min(minTurns, turns)
  30. maxTurns = max(maxTurns, turns)
  31.  
  32. print 'The average number of turns in %d games was %d (min = %d, max = %d)'%(numberOfGames, totalTurns / numberOfGames, minTurns, maxTurns)
Success #stdin #stdout 0.26s 10328KB
stdin
Standard input is empty
stdout
The average number of turns in 1000 games was 87 (min = 8, max = 631)