项目说明: https://inst.eecs.berkeley.edu/~cs61a/sp21/proj/hog/#phase-3-strategies
Phase1: https://www.cnblogs.com/ikventure/p/14815119.html
Phase2: https://www.cnblogs.com/ikventure/p/14885436.html
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
unlock时,注意Sow Sad规则。
def make_averaged(original_function, trials_count=1000):
"""Return a function that returns the average value of ORIGINAL_FUNCTION
when called.
To implement this function, you will have to use *args syntax, a new Python
feature introduced in this project. See the project description.
>>> dice = make_test_dice(4, 2, 5, 1)
>>> averaged_dice = make_averaged(roll_dice, 1000)
>>> averaged_dice(1, dice)
3.0
"""
# BEGIN PROBLEM 8
"*** YOUR CODE HERE ***"
def f(*args):
result = 0
for i in range(trials_count):
result += original_function(*args)
return result / trials_count
return f
# END PROBLEM 8
Problem 9
使用字典avgs存放每轮均值和num_rolls。
def max_scoring_num_rolls(dice=six_sided, trials_count=1000):
"""Return the number of dice (1 to 10) that gives the highest average turn score
by calling roll_dice with the provided DICE a total of TRIALS_COUNT times.
Assume that the dice always return positive outcomes.
>>> dice = make_test_dice(1, 6)
>>> max_scoring_num_rolls(dice)
1
"""
# BEGIN PROBLEM 9
"*** YOUR CODE HERE ***"
avg = 0
avgs = {}
for i in range(1, 11):
avg = make_averaged(roll_dice, trials_count)(i, dice)
if avg not in avgs:
avgs[avg] = i
else:
if i < avgs[avg]:
avgs[avg] = i
return avgs[max(avgs)]
# END PROBLEM 9
Problem 10
用之前定义的piggypoints,大于cutoff就return 0,否则返回num_rolls。
def piggypoints_strategy(score, opponent_score, cutoff=8, num_rolls=6):
"""This strategy rolls 0 dice if that gives at least CUTOFF points, and
rolls NUM_ROLLS otherwise.
"""
# BEGIN PROBLEM 10
return 0 if piggy_points(opponent_score) >= cutoff else num_rolls # Replace this statement
# END PROBLEM 10
Problem 11
如果0 dice后触发再来一轮,0 dice;如果达到cutoff不再来一轮也选择0 dice;否则,num_rolls。注意使用piggypoints_strategy和more_boar比较方便。
def more_boar_strategy(score, opponent_score, cutoff=8, num_rolls=6):
"""This strategy rolls 0 dice when it triggers an extra turn. It also
rolls 0 dice if it gives at least CUTOFF points and does not give an extra turn.
Otherwise, it rolls NUM_ROLLS.
"""
# BEGIN PROBLEM 11
return 0 if more_boar(score + piggy_points(opponent_score), opponent_score) \
else piggypoints_strategy(score, opponent_score, cutoff, num_rolls)
# Replace this statement
# END PROBLEM 11
Optional: Problem 12
设计策略让获胜概率更高(由于连接不上服务器,只能测试代码完成度,不能测试胜率)。
def final_strategy(score, opponent_score):
"""Write a brief description of your final strategy.
*** YOUR DESCRIPTION HERE ***
"""
# BEGIN PROBLEM 12
if score <= 80:
return more_boar_strategy(score, opponent_score, cutoff=8, num_rolls=6)
elif score <= 90 and score > opponent_score:
return piggypoints_strategy(score, opponent_score, cutoff=7, num_rolls=1)
else:
return 2
# Replace this statement
# END PROBLEM 12