2013-03-11 156 views
3

我有這樣的方法中的循環:While循環變量不更新

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 

然而在每個循環的開始時,這應該被更新的變量處於復位狀態。我的縮進似乎是正確的,所以我不確定問題是什麼。它可能是變量範圍?

感謝您的幫助。

編輯:這裏是一個引擎收錄到的所有代碼:http://pastebin.com/GjKA8yag

編輯:添加的所有代碼的帖子。以下是底部控制器代碼應該發生的情況:

初始化遊戲,gg有2個玩家,每個玩家都有一個位置,他們需要達到董事會的最後贏,董事會是10個方塊長,他們可以一次移動1或2個方格。

決定使用jump方法移動多少個正方形。 jump方法在其中創建兩個遊戲,其中一個遊戲初始化,其中玩家1在他所在的位置之前1個方格,其中一個遊戲初始化,玩家1在他所在的地方之前2個方格。然後使用run方法隨機完成這些遊戲,並且返回值以查看誰贏得了該遊戲(玩家2的2,玩家1的1)。

這是完成iterations次數,然後取其中一個遊戲更成功,即最初移動1個前進或2個前進,這是在實際遊戲中將採取的移動,g

樣品打印語句:

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

第一行是print(move1, move2),這是在while循環的開始。然後將print(x)作爲從run()方法返回的值,然後在print(move1)之後進行遞增。

我在Windows 8

RESULT運行的Python 3.3 x64系統:轉向Python 2.7.3和它現在的工作。

+1

此代碼中的任何內容都不會重置'move1'。這是你的所有代碼嗎? – 2013-03-11 13:57:47

+0

是的,這是整個方法 – siame 2013-03-11 13:59:24

+1

哪個變量正在重置,'我'?另外,是什麼讓你認爲這會發生? – NPE 2013-03-11 13:59:52

回答

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 

這裏是我改變的部分。我只在您的代碼中添加了print語句。
這裏是輸出。
請告訴我哪部分輸出對您沒有意義。

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 
+0

雖然還有些奇怪的事情發生,但是元組中的值應該與它們上面的兩行中的值相同 – 2013-03-11 14:42:24

+0

否。這兩個數字表示a.run()和move1的結果。它與move2無關。 – octref 2013-03-11 14:46:07

+0

我的錯誤,對不起 – 2013-03-11 14:48:36