This is UCB CS61A's firt project hog 's problem5
this is wrong code(my code)
while(score0<goal and score1<goal):
if who==0 :
cur_strategy = strategy0
cur_score = score0
cur_op_score=score1
cur_op_strategy = strategy1
else:
cur_strategy = strategy1
cur_score = score1
cur_op_score=score0
cur_op_strategy = strategy0
cur_score=cur_score take_turn(cur_strategy(cur_score,cur_op_score),cur_op_score,dice)
if(extra_turn(cur_score,cur_op_score)==False):
who=other(who)
this is the correct code(I have tested it)
while score0 < goal and score1 < goal:
if who == 0:
num_rolls = strategy0(score0, score1)
score0 = take_turn(num_rolls, score1, dice)
who = other(who) if extra_turn(score0, score1) == False else who
else:
num_rolls = strategy1(score1, score0)
score1 = take_turn(num_rolls, score0, dice)
who = other(who) if extra_turn(score1, score0) == False else who
But actually,I think these two codes are essentially same.
I don't know whether this is the problem (the quote from the project)
Only call a strategy function once per turn (or risk breaking the GUI).
CodePudding user response:
I think that comes from the fact that in order to update the score0 an score1 variables, you define cur_score and update it instead. The problem is that cur_score is only a copy of the score you want to update, it is not the same object.