在 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.'
是什么导致了这个问题,如何解决?