结束一个while循环

我目前正在用 Python 3.6 编写骰子游戏的代码我知道我的编码在这方面有点偏差,但是,我真的只是想知道如何开始我的 while 循环。游戏说明如下...

  • 人类玩家与计算机对战。

  • 玩家 1 掷骰子,直到他们获胜、决定保持或掷出 1。玩家 2 也是如此。

  • 他们轮流掷两个骰子,骰子的总数相加,除非掷出 1。

  • 如果掷出一个 1,则不会增加分数,轮到下一个人了。如果掷出两个 1,您将失去所有积分,然后轮到下一个人。

  • 第一个得分为 100 的玩家赢得游戏。

我的游戏运行良好,直到玩家 1 和玩家 2 都击中“y”以背靠背。然后游戏停止在玩家之间切换,直到再次击中“n”以不保持。知道为什么吗?有人告诉我,我需要变量来决定轮到谁,但我不确定如何将它们合并到我的代码中。任何帮助,将不胜感激。


繁花如伊
浏览 224回答 3
3回答

jeck猫

使用 dict 来记录每个玩家名称的分数,切换一个turn持有当前掷骰子的玩家。实现了一些逻辑,即何时turn从一种更改为另一种:import randomdef score(players):&nbsp; &nbsp; for k in players:&nbsp; &nbsp; &nbsp; &nbsp; print("{} has {} points".format(k,players[k]))def hold(player):&nbsp; &nbsp; if input("Does {} want to hold? [y or anything]".format(player)).lower().strip() == "y":&nbsp; &nbsp; &nbsp; &nbsp; return "y"&nbsp; &nbsp; return "n"def main():&nbsp; &nbsp; dice = range(1,7)&nbsp; &nbsp; players = {"p1":0, "p2":0}&nbsp; &nbsp; turn = ""&nbsp; &nbsp; change_player = "y"&nbsp; &nbsp; print("Welcome to the Two Dice Pig Game. You are Player 1!")&nbsp; &nbsp; while all(x < 100 for x in players.values()):&nbsp; &nbsp; &nbsp; &nbsp; # initially changePlayer is&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if change_player == "y":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # print both scores on player changed&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; score(players)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; turn = "p1" if turn != "p1" else "p2"&nbsp; &nbsp; &nbsp; &nbsp; dice1, dice2 = random.choices(dice,k=2)&nbsp; &nbsp; &nbsp; &nbsp; print("{} threw {} and {} for a total of {}".format(turn,d1,d2,d1+d2))&nbsp; &nbsp; &nbsp; &nbsp; if dice1 + dice2 == 2:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; players[turn] = 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("Two 1 - you're done for now. Your points go back to 0.")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; change_player = "y"&nbsp; &nbsp; &nbsp; &nbsp; elif dice1 == 1 or dice2 == 1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("One 1 - you're done for now.")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; change_player&nbsp; = "y"&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # only case where we need to add values and print new score&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; players[turn] += dice1 + dice2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("Your score: {}".format(players[turn]))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if turn == "p1":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; change_player = hold(turn)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; change_player = "n" # computer is greedy, never stops&nbsp; &nbsp; winner, points = max(players.items(),key=lambda x: x[1])&nbsp; &nbsp; print("The winner is {} with {} points.".format(winner,points))main()&nbsp;输出:Welcome to the Two Dice Pig Game. You are Player 1!p1 has 0 pointsp2 has 0 pointsp1 threw 5 and 1 for a total of 6One 1 - you're done for now.p1 has 0 pointsp2 has 0 pointsp2 threw 3 and 6 for a total of 9Your score: 9p2 threw 6 and 2 for a total of 8Your score: 17p2 threw 4 and 1 for a total of 5One 1 - you're done for now.p1 has 0 pointsp2 has 17 pointsp1 threw 4 and 5 for a total of 9Your score: 9Does p1 want to hold? [y or anything]p1 threw 2 and 6 for a total of 8Your score: 17Does p1 want to hold? [y or anything]p1 threw 4 and 6 for a total of 10Your score: 27[snipp]One 1 - you're done for now.p1 has 91 pointsp2 has 51 pointsp1 threw 6 and 4 for a total of 10Your score: 101Does p1 want to hold? [y or anything]The winner is p1 with 101 points.&nbsp;

MM们

您可能希望通过执行以下操作来简化代码:# Keep track of whose turn it isplayer = 1# Keep a dictionary of scores for each player (1 or 2)scores = {1: 0, 2: 0}choice = 'n'# While neither player has > 100while max(d.values()) < 100:&nbsp; &nbsp; # Roll until current player decides to keep roll&nbsp; &nbsp; while choice == 'n':&nbsp; &nbsp; &nbsp; &nbsp; print('Player', player, 'turn')&nbsp; &nbsp; &nbsp; &nbsp; roll = random.randrange(1,7) + random.randrange(1,7)&nbsp; &nbsp; &nbsp; &nbsp; print('You rolled', roll)&nbsp; &nbsp; &nbsp; &nbsp; choice = input('Keep roll? y/n')&nbsp; &nbsp; # Increment that player's score&nbsp; &nbsp; scores[player] += roll&nbsp; &nbsp; choice = 'n'&nbsp; &nbsp; # Change to other player&nbsp; &nbsp; player = (player % 2) + 1
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go