proffreda

hog.py

Sep 8th, 2016
452
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. """The Game of Hog."""
  2.  
  3. from dice import four_sided, six_sided, make_test_dice
  4.  
  5. GOAL_SCORE = 100 # The goal of Hog is to score 100 points.
  6.  
  7. ######################
  8. # Phase 1: Simulator #
  9. ######################
  10.  
  11. def roll_dice(num_rolls, dice=six_sided):
  12. # These assert statements ensure that num_rolls is a positive integer.
  13. assert type(num_rolls) == int, 'num_rolls must be an integer.'
  14. assert num_rolls > 0, 'Must roll at least once.'
  15. # BEGIN Question 1
  16. "*** REPLACE THIS LINE ***"
  17. # END Question 1
  18.  
  19.  
  20. def take_turn(num_rolls, opponent_score, dice=six_sided):
  21. """Simulate a turn rolling NUM_ROLLS dice, which may be 0 (Free bacon).
  22.  
  23. num_rolls: The number of dice rolls that will be made.
  24. opponent_score: The total score of the opponent.
  25. dice: A function of no args that returns an integer outcome.
  26. """
  27. assert type(num_rolls) == int, 'num_rolls must be an integer.'
  28. assert num_rolls >= 0, 'Cannot roll a negative number of dice.'
  29. assert num_rolls <= 10, 'Cannot roll more than 10 dice.'
  30. assert opponent_score < 100, 'The game should be over.'
  31. # BEGIN Question 2
  32. "*** REPLACE THIS LINE ***"
  33. # END Question 2
  34.  
  35. def select_dice(score, opponent_score):
  36. """Select six-sided dice unless the sum of SCORE and OPPONENT_SCORE is a
  37. multiple of 7, in which case select four-sided dice (Hog wild).
  38. """
  39. # BEGIN Question 3
  40. "*** REPLACE THIS LINE ***"
  41. # END Question 3
  42.  
  43. def is_swap(score0, score1):
  44. """Return True if ending a turn with SCORE0 and SCORE1 will result in a
  45. swap.
  46.  
  47. Swaps occur when the last two digits of the first score are the reverse
  48. of the last two digits of the second score.
  49. """
  50. # BEGIN Question 4
  51. "*** REPLACE THIS LINE ***"
  52. # END Question 4
  53.  
  54.  
  55. def other(who):
  56. """Return the other player, for a player WHO numbered 0 or 1.
  57.  
  58. >>> other(0)
  59. 1
  60. >>> other(1)
  61. 0
  62. """
  63. return 1 - who
  64.  
  65. def play(strategy0, strategy1, score0=0, score1=0, goal=GOAL_SCORE):
  66. """Simulate a game and return the final scores of both players, with
  67. Player 0's score first, and Player 1's score second.
  68.  
  69. A strategy is a function that takes two total scores as arguments
  70. (the current player's score, and the opponent's score), and returns a
  71. number of dice that the current player will roll this turn.
  72.  
  73. strategy0: The strategy function for Player 0, who plays first
  74. strategy1: The strategy function for Player 1, who plays second
  75. score0 : The starting score for Player 0
  76. score1 : The starting score for Player 1
  77. """
  78. who = 0 # Which player is about to take a turn, 0 (first) or 1 (second)
  79. # BEGIN Question 5
  80. "*** REPLACE THIS LINE ***"
  81. # END Question 5
  82. return score0, score1
  83.  
  84.  
  85. def always_roll(n):
  86. """Return a strategy that always rolls N dice.
  87.  
  88. A strategy is a function that takes two total scores as arguments
  89. (the current player's score, and the opponent's score), and returns a
  90. number of dice that the current player will roll this turn.
  91.  
  92. >>> strategy = always_roll(5)
  93. >>> strategy(0, 0)
  94. 5
  95. >>> strategy(99, 99)
  96. 5
  97. """
  98. def strategy(score, opponent_score):
  99. return n
  100. return strategy
Advertisement
Add Comment
Please, Sign In to add comment