Problem 1 (300pts)
Implement the roll_dice
function in hog.py
.
It takes three arguments: the current player's score cur_score
, a positive integer called num_rolls
giving the number of dice to roll and a dice
function.
It returns the number of points scored by rolling the dice that number of times in a turn: either the sum of the outcomes or 1 (Pig Out), plus the number of dices that have a same parity of player's score (Feral Hogs).
- Pig Out. If any of the dice outcomes is a 1, the current player's score for the turn is 1.
- Feral Hogs. A player gets 1 extra point for each dice whose outcome has the same parity (i.e. both odd or both even) of the player's score.
To obtain a single outcome of a dice roll, call dice()
.
You should call dice()
exactly num_rolls
times in the body of roll_dice
.
Remember to call dice()
exactly num_rolls
times even if Pig Out happens in the middle of rolling.
In this way, you correctly simulate rolling all the dice together.
Understand the problem:
Before writing any code, unlock the tests to verify your understanding of the question. Note: you will not be able to test your code using ok
until you unlock the test cases for the corresponding question.
$ python ok -q 01 -u
Write code and check your work:
Once you are done unlocking, begin implementing your solution. You can check your correctness with:
$ python ok -q 01
Debugging Tips
If the tests don't pass, it's time to debug.
You can observe the behavior of your function using Python directly.
First, start the Python interpreter and load the hog.py
file.
$ python -i hog.py
Then, you can call your roll_dice
function on any number of dice you want.
The roll_dice
function has a default argument value for dice
that is a random six-sided dice function.
Therefore, the following call to roll_dice
simulates rolling four fair six-sided dice.
>>> roll_dice(4)
You will find that the previous expression may have a different result each time you call it, since it is simulating random dice rolls. You can also use test dice that fix the outcomes of the dice in advance. For example, rolling twice when you know that the dice will come up 3 and 4 should give a total outcome of 7.
>>> fixed_dice = make_test_dice(3, 4)
>>> roll_dice(2, fixed_dice)
7
On most systems, you can evaluate the same expression again by pressing the up arrow, then pressing enter or return. To evaluate earlier commands, press the up arrow repeatedly.
If you find a problem, you need to change your
hog.py
file, save it, quit Python, start Python again, and then start evaluating expressions. Pressing the up arrow should give you access to your previous expressions, even after restarting Python.
Continue debugging your code and running the ok
tests until they all pass.
You should follow this same procedure of understanding the problem, implementing a solution, testing, and debugging for all the problems in this project.
One more debugging tip: to start the interactive interpreter automatically upon failing an
ok
test, use-i
. For example,python ok -q 01 -i
will run the tests for question 1, then start an interactive interpreter withhog.py
loaded if a test fails.