正在做CS61A 2020Fall 的Hog,卡在problem5b了,看了好久也没看出来哪里有问题,希望有做过此题的朋友能帮我看看!
我的代码如下:
def play(strategy0, strategy1, score0=0, score1=0, dice=six_sided,
goal=GOAL_SCORE, say=silence, feral_hogs=True):
‘’‘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 at the end of the first turn.
feral_hogs: A boolean indicating whether the feral hogs rule should be active.
’‘’
who = 0 # Who is about to take a turn, 0 (first) or 1 (second)
# BEGIN PROBLEM 5
"*** YOUR CODE HERE ***"
score0_pre = score1_pre = 0
while score0 < goal and score1 < goal:
if who:
roll_num = strategy1(score1,score0)
bonus = 3 if abs(roll_num - score1_pre) == 2 else 0
score1_pre += take_turn(roll_num,score0,dice)
score1 += score1_pre + bonus
if is_swap(score1,score0):
score0,score1 = score1,score0
who = other(who)
else:
roll_num = strategy0(score0,score1)
bonus = 3 if abs(roll_num - score0_pre) == 2 else 0
score0_pre = take_turn(roll_num,score1,dice)
score0 += score0_pre + bonus
if is_swap(score0,score1):
score0,score1 = score1,score0
who = other(who)
# END PROBLEM 5
# (note that the indentation for the problem 6 prompt (***YOUR CODE HERE***) might be misleading)
# BEGIN PROBLEM 6
"*** YOUR CODE HERE ***"
# END PROBLEM 6
return score0, score1
结果它报错:
我的思路是:1.扔的骰子数赋值给roll_num与score_pre相减得到绝对值,如果等于2则bonus为3,否则为0
2.玩家本轮摇骰子得到的分数记为score_pre(pre后缀主要是因为下一轮要依次判断是否得到bonus),总得分score += score_pre + bonus
3.判断是否要swap两玩家得分
4.由while的条件判断是否有人的分数达到goal,如果有则跳出循环结束游戏。
根据报错结果我分析是,player0总得分为43(大于goal,本应直接结束游戏)以后,player1又进行了一轮,总得分由15变为19。但我明明用while判断了player0的分数大于goal了,为什么还会继续一轮呀?
哈哈 终于发现了
你好,我是有问必答小助手,非常抱歉,本次您提出的有问必答问题,技术专家团超时未为您做出解答
本次提问扣除的有问必答次数,已经为您补发到账户,我们后续会持续优化,扩大我们的服务范围,为您带来更好地服务。