我有一个任务,我需要计算机自己玩猪游戏,一旦滚动100便停止。因此,我有两名计算机玩家并肩作战,但问题是,尽管这些玩家本来可以拥有100位玩家,但他们一直坚持到turntotal >= 20
为止,这反过来又给了另一位玩家赢得胜利的机会,因为可以达到100的点。它在我们的任务中特别指出要注意这个问题,但是我不知道如何去解决这个问题。同样,使代码更简洁的任何方法也将有所帮助。
def playerOne(score):
turntotal = 0
scoretotal = 0
while(scoretotal >= 0 and scoretotal <= 100):
while(scoretotal < 100):
roll = random.randint(1,6)
print('Rolled a ', roll)
if(turntotal <= 20 and roll != 1):
turntotal = turntotal + roll
scoretotal = scoretotal + roll
if(turntotal >= 20):
break
if(roll == 1):
scoretotal = scoretotal - turntotal
turntotal = 0
print('Pigged Out!!!')
break
print('Turn score =', turntotal)
turntotal = 0
return scoretotal
def printScore():
print(' ')
print("Player One score is", PlayerOneScore)
print("Player two score is", PlayerTwoScore)
print(' ')
PlayerOneScore = 0
PlayerTwoScore = 0
flipcoin = random.randint(1,2)
if(flipcoin == 1):
while(PlayerOneScore < 100 or PlayerTwoScore < 100):
printScore()
print("Player 1's turn")
PlayerOneScore += playerOne(PlayerOneScore)
if(PlayerOneScore >= 100):
break
printScore()
print("Player 2's turn")
PlayerTwoScore += playerOne(PlayerTwoScore)
if(PlayerTwoScore >= 100):
break
else:
while(PlayerOneScore < 100 or PlayerTwoScore < 100):
printScore()
print("Player 2's turn")
PlayerTwoScore += playerOne(PlayerTwoScore)
if(PlayerTwoScore >= 100):
break
printScore()
print("Player 1's turn")
PlayerOneScore += playerOne(PlayerOneScore)
if(PlayerOneScore >= 100):
break
printScore()
if(PlayerTwoScore > PlayerOneScore):
print("PLAYER 2 WINS")
else:
print("PLAYER 1 WINS")