Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

essentially same code,but different result

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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).

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading