3

I have a loop in a method like this:

from random import randint

class Player:
    def __init__(self, position=0):
        self.position = position

    def move(self, move):
        self.position += move

class Game:
    def __init__(self, size=10, p1=1, p2=1):
        self.size = size
        self.p1 = Player()
        self.p2 = Player()

    def finished(self):
        if self.p1.position >= self.size:
            return True
        if self.p2.position >= self.size:
            return True

    def run(self):
        while True:
            roll = randint(1,2)

            self.p2.move(roll)
            if self.finished():
                break

            roll = randint(1,2)

            self.p1.move(roll)
            if self.finished():
                break

        if self.p2.position >= self.p1.position: # if player 2 won return 2, else 1
            return 2
        else:
            return 1

    def jump(self, iterations):
        move1, move2, i = 0,0,0
        while i < iterations:
            print(move1, move2) # move1 is 0 again

            a = Game(self.size, self.p1.position+1, self.p2.position)
            x = a.run()
            print(x) # x is either 1 or 2, for the sake of testing 
                     # it's been set to always 1
            if x == 1:
                move1 += 1
            print(move1) # if move1 is incremented, it is shown here successfully    

            b = Game(self.size, self.p1.position+2, self.p2.position)
            y = b.run()

            if y == 1:
                move2 += 1

            i += 1

        if move2 >= move1:
            return 2
        else:
            return 1

human, comp, i = 0, 0, 0
times = 10
while i < times:
    g = Game()
    while True:
        roll = g.jump(4)
        g.p1.move(roll)
        if g.finished():
            human += 1
            break

        roll = g.jump(4)
        g.p2.move(roll)
        if g.finished():
            comp += 1
            break
    i += 1

However at the beginning of each loop, the variable that was supposed to be updated is being reset. My indentation seems to be right so I'm not sure what the problem is. Could it be variable scope?

Thanks for your help.

EDIT: Here is a pastebin to all of the code: http://pastebin.com/GjKA8yag

EDIT: Added all the code to the post. Here's a run through of what should be happening from the controller code at the bottom:

Initialise a game, g. g has 2 players, each player has a position, they need to reach the end of the board to win, the board is 10 squares long, they can either move 1 or 2 squares at once.

To decide how many squares to move by we use the jump method. The jump method creates two games within it, one where the game is initialised with player1 1 squares ahead of where he was, one where the game is initialised with player1 2 squares ahead of where he was. These games are then completed randomly using the run method, and a value is return to see who won that game (2 for player2, 1 for player1).

This is done iterations amount of times, and then whichever game was more successful, ie initially moving 1 forward or 2 forward, that is the move that will be taken in the actual game, g.

Sample of the print statements:

0 0
1
1
0 0
2
0
0 0
1
1
0 0
1
1

The first line is print(move1, move2), which is at the beginning of the while loop. Then print(x) as the value returned from the run() method, then print(move1) after it has been incremented.

I'm running Python 3.3 x64 on Windows 8.

RESULT: Switched to Python 2.7.3 and it's now working.

14
  • 1
    Nothing in this code resets move1. Is this all of your code? Commented Mar 11, 2013 at 13:57
  • 1
    Which variable is being reset, i? Also, what makes you think that this is what happens? Commented Mar 11, 2013 at 13:59
  • 2
    It would help if you would reduce your program to the smallest complete program that still demonstrates the error. See: SSCCE.org. Commented Mar 11, 2013 at 14:06
  • 3
    Just checked the pastebin, the problem is that you only ever call g.jump(1) so iterations is only ever 1, so it will never actually get back up to print move1 and move2 again Commented Mar 11, 2013 at 14:06
  • 3
    Your code is not actually looping because you only ever call jump with an argument of 1. Commented Mar 11, 2013 at 14:06

1 Answer 1

1
    def jump(self, iterations):
        move1, move2, i = 0,0,0
        while i < iterations:
            print(move1, move2) # move1 is 0 again

            a = Game(self.size, self.p1.position+1, self.p2.position)
            x = a.run()
            print("who won game a? : " + str(x)) # x is either 1 or 2, for the sake of testing 
                     # it's been set to always 1
            if x == 1:
                move1 += 1
            print("move 1 is: " + str(move1)) # if move1 is incremented, it is shown here successfully    

            b = Game(self.size, self.p1.position+2, self.p2.position)
            y = b.run()
            print("who won game b? : " + str(y))

            if y == 1:
                move2 += 1
            print("move 2 is: " + str(move2))

            i += 1

        if move2 >= move1:
            return 2
        else:
            return 1

human, comp, i = 0, 0, 0
times = 10
while i < times:
    g = Game()
    print("new game")
    while True:
        print("position of 1 is:" + str(g.p1.position))
        print("position of 2 is:" + str(g.p2.position))
        roll = g.jump(4)
        print("first jump finished")
        g.p1.move(roll)
        if g.finished():
            human += 1
            break

        roll = g.jump(4)
        print("second jump finished")
        g.p2.move(roll)
        if g.finished():
            comp += 1
            break
    i += 1

Here are the parts that I changed. I only added print statement to your code.
Here is the output.
Please tell me which part of the output does not make sense to you.

new game
position of 1 is:0
position of 2 is:0
(0, 0)
who won game a? : 2
move 1 is: 0
who won game b? : 2
move 2 is: 0
(0, 0)
who won game a? : 2
move 1 is: 0
who won game b? : 1
move 2 is: 1
(0, 1)
who won game a? : 1
move 1 is: 1
who won game b? : 1
move 2 is: 2
(1, 2)
who won game a? : 1
move 1 is: 2
who won game b? : 2
move 2 is: 2
first jump finished
(0, 0)
who won game a? : 1
move 1 is: 1
who won game b? : 2
move 2 is: 0
(1, 0)
who won game a? : 1
move 1 is: 2
who won game b? : 1
move 2 is: 1
(2, 1)
who won game a? : 2
move 1 is: 2
who won game b? : 2
move 2 is: 1
(2, 1)
who won game a? : 2
move 1 is: 2
who won game b? : 2
move 2 is: 1
second jump finished
position of 1 is:2
position of 2 is:1
(0, 0)
who won game a? : 1
move 1 is: 1
who won game b? : 2
move 2 is: 0
(1, 0)
who won game a? : 2
move 1 is: 1
who won game b? : 1
move 2 is: 1
(1, 1)
who won game a? : 1
move 1 is: 2
who won game b? : 2
move 2 is: 1
(2, 1)
who won game a? : 2
move 1 is: 2
who won game b? : 2
move 2 is: 1
first jump finished
(0, 0)
who won game a? : 1
move 1 is: 1
who won game b? : 2
move 2 is: 0
(1, 0)
who won game a? : 2
move 1 is: 1
who won game b? : 1
move 2 is: 1
(1, 1)
who won game a? : 1
move 1 is: 2
who won game b? : 2
move 2 is: 1
(2, 1)
who won game a? : 2
move 1 is: 2
who won game b? : 2
move 2 is: 1
second jump finished
position of 1 is:3
position of 2 is:2
Sign up to request clarification or add additional context in comments.

8 Comments

still something strange happening though, the values in the tuple should be the same as those in the two lines above them
No. The two numbers represent the result of a.run(), and move1. It has nothing to do with move2.
move2 is just a counter for how many games have been won by player1 when moving 2 forward in the first step. So the return value I want to check is that player1 won, and then update the counter accordingly.
I'm confused as of what you want to know. "move1, move2, i = 0,0,0" resets your move1, move2 and i of course. What is the variable that you want to keep updated?
Can you tell me specifically, in your question "However at the beginning of each loop, the variable that was supposed to be updated is being reset.", which loop and which variable do you mean.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.