"""The Game of Hog."""from dice import six_sided, make_test_dice
from ucb import main, trace, interact
from math import log2
GOAL =100# The goal of Hog is to score 100 points.####################### Phase 1: Simulator #######################defroll_dice(num_rolls, dice=six_sided):"""Simulate rolling the DICE exactly NUM_ROLLS > 0 times. Return the sum of
the outcomes unless any of the outcomes is 1. In that case, return 1.
num_rolls: The number of dice rolls that will be made.
dice: A function that simulates a single dice roll outcome.
"""# These assert statements ensure that num_rolls is a positive integer.asserttype(num_rolls)==int,'num_rolls must be an integer.'assert num_rolls >0,'Must roll at least once.'# BEGIN PROBLEM 1
total=0
rollone=False#这里必须先定义rollone, 否则后面直接引用会出错误:#Error: local variable 'rollone' referenced before assignmentwhile num_rolls>0:
result=dice()
num_rolls-=1if result==1:
rollone=Trueelse:
total=total+result
if rollone==True:return1else:return total
# END PROBLEM 1deftail_points(opponent_score):"""Return the points scored by rolling 0 dice according to Pig Tail.
opponent_score: The total score of the other player.
"""# BEGIN PROBLEM 2
ones=opponent_score%10
opponent_score=opponent_score//10
tens=opponent_score%10return2*abs(tens-ones)+1# END PROBLEM 2deftake_turn(num_rolls, opponent_score, dice=six_sided):"""Return the points scored on a turn rolling NUM_ROLLS dice when the
opponent has OPPONENT_SCORE points.
num_rolls: The number of dice rolls that will be made.
opponent_score: The total score of the other player.
dice: A function that simulates a single dice roll outcome.
"""# Leave these assert statements here; they help check for errors.asserttype(num_rolls)==int,'num_rolls must be an integer.'assert num_rolls >=0,'Cannot roll a negative number of dice in take_turn.'assert num_rolls <=10,'Cannot roll more than 10 dice.'# BEGIN PROBLEM 3if num_rolls==0:return tail_points(opponent_score)else:return roll_dice(num_rolls,dice)#注意这里并不能省略dice function#若省略,则默认dice=six_sided=make_fair_dice(6),生成随机数#在test中我们可以看