Phase 3 - Strategies
https://inst.eecs.berkeley.edu/~cs61a/su19/proj/hog/#phase-3-strategies
In the third phase, you will experiment with ways to improve upon the basic strategy of always rolling a fixed number of dice. First, you need to develop some tools to evaluate strategies.
Problem 8
Implement the make_averaged
function, which is a higher-order function that takes a function fn
as an argument. It returns another function that takes the same number of arguments as fn
(the function originally passed into make_averaged
). This returned function differs from the input function in that it returns the average value of repeatedly calling fn
on the same arguments. This function should call fn
a total of num_samples
times and return the average of the results.
To implement this function, you need a new piece of Python syntax! You must write a function that accepts an arbitrary number of arguments, then calls another function using exactly those arguments.
Solution:
Problem 9
Implement the max_scoring_num_rolls
function, which runs an experiment to determine the number of rolls (from 1 to 10) that gives the maximum average score for a turn. Your implementation should use make_averaged
and roll_dice
.
If two numbers of rolls are tied for the maximum average score, return the lower number. For example, if both 3 and 6 achieve a maximum average score, return 3.
Solution:
Problem 10
A strategy can take advantage of the Free Bacon rule by rolling 0 when it is most beneficial to do so. Implement bacon_strategy
, which returns 0 whenever rolling 0 would give at least margin
points and returns num_rolls
otherwise.
Solution:
Problem 11
A strategy can also take advantage of the Swine Swap rule. The swap_strategy
rolls 0 if it would cause a beneficial swap. It also returns 0 if rolling 0 would give at least margin
points, unless this would cause a non-beneficial swap. Otherwise, the strategy rolls num_rolls
.
Last updated