-1

Python 2.7.3 で、私は 2 人用ゲームの自動プレイを作成しました。このゲームでは、2 人のプレイヤーが順番になったときに、2、3、4、5、6 のいずれかをプレイします。カードが6の場合、ターンは終了します。それ以外の場合、カードの 2 つの値が合計され、2 つのサイコロが振られ、それらの値が合計されます。プレーヤーがそのターンに受け取るポイントの合計数は、2 つのカードの値の合計に 2 つのサイコロの値の合計を掛けたものです。300ポイントに到達した最初のプレーヤーがゲームに勝ちます。

この「ゲームのプレイ」を 100 回実行し、そのたびに結果を表示し、すべてのゲームの最後に各プレイヤーが勝った合計回数を表示できるようにしたいと考えています。しかし、gamefor ループで繰り返し処理している変数1が 、つまり最初のゲームでスタックしているため、無限ループが発生します。

これが私のコードです:

import random
playerone = 0
playertwo = 0
playeronewins = 0
playertwowins = 0
currentplayer = 1
scoreToWin = 300
games = 100
for game in range(1,games+1):
    while playerone < scoreToWin and playertwo < scoreToWin:
        cardone = random.randint(2,6)
        cardtwo = random.randint(2,6)
        if cardone != 6 and cardtwo != 6:
            cardtotal = cardone + cardtwo
            dieone = random.randint(1,6)
            dietwo = random.randint(1,6)
            dietotal = dieone + dietwo
            points = cardtotal * dietotal
            if currentplayer == 1:
                playerone += points
            else:
                playertwo += points
            if playerone >= scoreToWin:
                print 'Game ' + str(game) + ': Player 1 wins with ' + str(playerone) + ' points. Player 2 loses with ' + str(playertwo) + ' points.'
                playeronewins += 1
                if game != games:
                    playerone = 0
                    playertwo = 0
            if playertwo >= scoreToWin:
                print 'Game ' + str(game) + ': Player 2 wins with ' + str(playertwo) + ' points. Player 1 loses with ' + str(playerone) + ' points.'
                playertwowins += 1
                if game != games:
                    playerone = 0
                    playertwo = 0
        currentplayer = 2 if currentplayer == 1 else 1
print 'Player 1 won ' + str(playeronewins) + ' times.'
print 'Player 2 won ' + str(playertwowins) + ' times.'

この問題の原因は何ですか?どうすれば修正できますか?

4

2 に答える 2

3

while ループ内でプレーヤーのスコアを 0 に設定すると、ループは永遠に続きます。

スコア テストをデデントします。

while playerone < scoreToWin and playertwo < scoreToWin:
    # card game loop
    # this line still belongs in the loop:
    currentplayer = 2 if currentplayer == 1 else 1

# *Now* test for the scores, *outside* the while loop
if playerone >= scoreToWin:
    print 'Game ' + str(game) + ': Player 1 wins with ' + str(playerone) + ' points. Player 2 loses with ' + str(playertwo) + ' points.'
    playeronewins += 1
if playertwo >= scoreToWin:
    print 'Game ' + str(game) + ': Player 2 wins with ' + str(playertwo) + ' points. Player 1 loses with ' + str(playerone) + ' points.'
    playertwowins += 1
playerone = 0
playertwo = 0

そこでテストする必要もありませんgame != games

これらの変更により、テストの実行により次が生成されます。

Game 1: Player 2 wins with 330 points. Player 1 loses with 214 points.
Game 2: Player 2 wins with 301 points. Player 1 loses with 261 points.
Game 3: Player 1 wins with 348 points. Player 2 loses with 207 points.
# .. ~ ..
Game 98: Player 2 wins with 344 points. Player 1 loses with 248 points.
Game 99: Player 1 wins with 323 points. Player 2 loses with 173 points.
Game 100: Player 2 wins with 354 points. Player 1 loses with 105 points.
Player 1 won 45 times.
Player 2 won 55 times.
于 2013-03-27T18:21:04.463 に答える
0

whileループは決して終了しないため、ゲーム 1 から抜け出すことはありません。最善の解決策は、コードをリファクタリングして、理解しやすく推論しやすくすることです。(ヒント: いくつかの関数を使用してください。) ただし、元のコードへの簡単な修正として、次のbreakステートメントを追加するだけです。

        if playerone >= scoreToWin:
            print 'Game ' + str(game) + ': Player 1 wins with ' + str(playerone) + ' points. Player 2 loses with ' + str(playertwo) + ' points.'
            playeronewins += 1
            if game != games:
                playerone = 0
                playertwo = 0
   >>>>     break
        if playertwo >= scoreToWin:
            print 'Game ' + str(game) + ': Player 2 wins with ' + str(playertwo) + ' points. Player 1 loses with ' + str(playerone) + ' points.'
            playertwowins += 1
            if game != games:
                playerone = 0
                playertwo = 0
   >>>>     break
于 2013-03-27T18:22:34.293 に答える