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.
move1. Is this all of your code?i? Also, what makes you think that this is what happens?jumpwith an argument of1.