-1

在 Python 2.7.3 中,我创建了一个 2 人游戏的自动玩游戏,其中 2 名玩家中的每一个,当轮到他们时,从以下开始:2、3、4、5、6。如果 2牌是 6,回合结束。否则,将卡片的 2 个值相加,掷出 2 个骰子并将它们的值相加。玩家在该回合获得的总点数是两张牌值的总和乘以两个骰子值的总和。第一个达到 300 分的玩家赢得比赛。

我希望能够执行 100 次“玩游戏”,每次都显示结果,并在所有游戏结束时显示每个玩家获胜的总次数。但是,我game在 for 循环中迭代的变量卡在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 ,因此它会永远持续下去。

De-dent 分数测试:

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 回答