3

次のようなメソッドにループがあります。

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

編集:すべてのコードを投稿に追加しました。以下は、一番下のコントローラー コードから何が起きているかを示しています。

ゲームを初期化しgます。gには 2 人のプレイヤーがいて、各プレイヤーにはポジションがあり、勝つにはボードの端に到達する必要があります。ボードの長さは 10 マスで、一度に 1 つまたは 2 つのマスを移動できます。

移動する正方形の数を決定するには、jumpメソッドを使用します。このjumpメソッドは、その中に 2 つのゲームを作成します。1 つは、player1 がいた場所の 1 スクエア先でゲームが初期化され、もう 1 つは、player1 がいた場所の 2 スクエア先でゲームが初期化されます。次に、これらのゲームはrunメソッドを使用してランダムに完了し、値が返されて、そのゲームに誰が勝ったかがわかります ( 2player2 の場合1、player1 の場合)。

これは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 で Python 3.3 x64 を実行しています。

結果: Python 2.7.3 に切り替えて、現在は動作しています。

4

1 に答える 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

変更したパーツはこちら。コードに 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
于 2013-03-11T14:36:55.070 に答える