cs61a的hog项目中的question7
def play(strategy0, strategy1, score0=0, score1=0, dice=six_sided,
goal=GOAL_SCORE, say=silence):
"""Simulate a game and return the final scores of both players, with Player
0's score first, and Player 1's score second.
A strategy is a function that takes two total scores as arguments (the
current player's score, and the opponent's score), and returns a number of
dice that the current player will roll this turn.
strategy0: The strategy function for Player 0, who plays first.
strategy1: The strategy function for Player 1, who plays second.
score0: Starting score for Player 0
score1: Starting score for Player 1
dice: A function of zero arguments that simulates a dice roll.
goal: The game ends and someone wins when this score is reached.
say: The commentary function to call every turn.
"""
who = 0 # Who is about to take a turn, 0 (first) or 1 (second)
leader = None # To be used in problem 7
# BEGIN PROBLEM 5
while True:
if who==0:
who=next_player(who)
num0=strategy0(score0,score1)
score0 += take_turn(num0,score0,score1,dice)
if is_prime(score0):
score0+=pigs_on_prime(score0,score1)
say=announce_lead_changes(score0,score1)[1]
if score0>=goal or score1>=goal:
break
say=announce_lead_changes(score0,score1)[1]
if who==1:
who=next_player(who)
num1=strategy1(score1,score0)
score1 += take_turn(num1,score1,score0,dice)
if is_prime(score1):
score1+=pigs_on_prime(score1,score0)
say=announce_lead_changes(score0,score1)[1]
if score1>=goal or score0>=goal:
break
say=announce_lead_changes(score0,score1)[1]
# END PROBLEM 5
结果显示了一个具体的测试例子本应输出的结果,并说我的代码输出没有东西
# Error: expected
# Player 0 takes the lead by 4
# Player 1 takes the lead by 2
# Player 0 takes the lead by 2
# Player 1 takes the lead by 2
# but got
尝试过像上面一样调用announce_lead_changes(score0,score1)[1],也尝试过print(announce_lead_changes(score0,score1)[1])
希望解决question7,达到在每次投掷骰子后输出一句当前比分领先结果,如:
Player 0 takes the lead by 4