Problem 5 (500pts)
Implement the play
function, which simulates a full game of Hog.
Players take turns rolling dice until one of the players reaches the goal
score, and the final scores of both players are returned by the function.
To determine how many dice are rolled each turn, each player uses their respective strategy (Player 0 uses strategy0
and Player 1 uses strategy1
).
A strategy is a function that, given a player's score and their opponent's score, returns the number of dice that the current player will roll in the turn.
An example strategy is always_roll_5
which appears below play
.
If any player achieves the goal score by the end of their turn, i.e. after all applicable rules have been applied, the game ends.
Important:
For the user interface to work, a strategy function should be called only once per turn. Only call
strategy0
when it is Player 0's turn and only callstrategy1
when it is Player 1's turn.Hints:
- You should call the functions you have implemented already.
- If
who
is the current player, the next player is1 - who
.
Before writing any code, unlock the tests to verify your understanding of the question.
$ python ok -q 05 -u
Once you are done unlocking, begin implementing your solution. You can check your correctness with:
$ python ok -q 05
Once you are finished, you will be able to play a graphical version of the game. We have provided a file called hog_gui.py
that you can run from the terminal:
$ python hog_gui.py
You can exit the gui by typing Ctrl + C
from terminal.
The GUI relies on your implementation, so if you have any bugs in your code, they will be reflected in the GUI. This means you can also use the GUI as a debugging tool; however, it's better to run the tests first.
Congratulations! You have finished Phase 1 of this project!