【问题标题】:running tally for dice game骰子游戏的运行计数
【发布时间】:2015-11-02 21:08:03
【问题描述】:

我参加了一周的编程训练营,所以请原谅我的简单且可能草率的编码,但我想知道如何为我的两人掷骰子游戏输入连续的胜利记录。如果玩家想继续玩,我可以产生最终得分并循环该功能以重新开始,但我想保持一个连续的胜利记录,以便玩家知道每个玩家赢了多少次。这是我的功能:

def DiceRoll():
    Dice1 = random.randint(1, 20)
    Dice2 = random.randint(1, 12)
    Dice3 = random.randint(1,6)
    Dice4 = random.randint(1, 20)
    Dice5 = random.randint(1, 12)
    Dice6 = random.randint (1, 6)
    DoubleDice1 = (((Dice1 + Dice2)*2) + Dice3)
    DoubleDice2 = (((Dice1 + Dice3)*2) + Dice2)
    DoubleDice3 = (((Dice2 + Dice3)*2) + Dice1)
    DoubleDice4 = (((Dice4 + Dice5)*2) + Dice6)
    DoubleDice5 = (((Dice4 + Dice6)*2) + Dice5)
    DoubleDice6 = (((Dice5 + Dice6)*2) + Dice4)
    TripleDice1 = ((Dice1 + Dice2 +Dice3) * 3)
    TripleDice2 = ((Dice4 + Dice5 +Dice6) * 3)

    print("Player 1, Roll?")
    Roll = input("Y/N?")
    if Roll =="y":
        print("Ok!")
    if Roll == "n":
        print("Goodbye!")
        time.sleep(2)
        sys.exit(0)
    print("             ")
    print(Dice1, Dice2, Dice3)
    Score1 = Dice1 + Dice2 + Dice3
    if Dice1 == Dice2:
        Score1 = DoubleDice1
        print(Score1)
    elif Dice1 ==Dice3:
        Score1 = DoubleDice2
        print(Score1)
    elif Dice2 == Dice3:
        Score1 = DoubleDice3
        print(Score1)
    elif Dice1 == Dice2 ==Dice3:
        Score1 = TripleDice1
        print(Score1)
    else:
        print(Dice1 + Dice2 + Dice3)
    print("""   
            """)

    print("Player 2, Roll?")
    Roll2 = input("Y/N?")
    if  Roll2 =="y":
        print("Ok!")
    if Roll2 == "n":
        print("Goodbye!")
        time.sleep(2)
        sys.exit(0)
    print("             ")
    print(Dice4, Dice5, Dice6)
    Score2 = (Dice4 + Dice5 + Dice6)
    if Dice4 == Dice5:
        Score2 = DoubleDice4
        print(Score2)
    elif Dice4 == Dice6:
        Score2 = DoubleDice5
        print(Score2)
    elif Dice5 == Dice6:
        Score2 = DoubleDice6
        print(Score2)
    elif Dice4 == Dice5 ==Dice6:
        Score2 = TripleDice2
        print(Score2)
    else:
        print(Dice4 + Dice5 + Dice6)
    print("""   
            """)
    if Score1 > Score2:
        print("Player 1:", Score1, "Player 2:", Score2)
        print("Player 1 Wins!")
    if Score1 < Score2:
        print("Player 1:", Score1, "Player 2:", Score2)
        print("Player 2 Wins!")
    if Score1 == Score2:
        print("Player 1:", Score1, "Player 2:", Score2)
        print("Tie!")

【问题讨论】:

  • 这并不能真正回答您的问题,但这里有一些代码清理思路:dice = [random.randint(1, 20) for _ in range(6)] 应该将您的六个变量简化为一个
  • 你在哪里打电话给DiceRoll

标签: python python-3.x dice cumulative-sum


【解决方案1】:

由于您的函数中没有循环,我假设您在调用 DiceRoll 的程序中有控制循环。要进行干净的计数,您需要将赢/输指定返回给该程序,如下所示:

if Score1 > Score2:
    print("Player 1:", Score1, "Player 2:", Score2)
    print("Player 1 Wins!")
    winner = 1
elif Score1 < Score2:
    print("Player 1:", Score1, "Player 2:", Score2)
    print("Player 2 Wins!")
    winner = 2
else:
    print("Player 1:", Score1, "Player 2:", Score2)
    print("Tie!")
    winner = 0

现在,回到你的主程序,我们将有类似的东西:

p1_wins = 0
p2_wins = 0
ties = 0

while game_on:
    result = DiceRoll()
    if result == 1:
        p1_wins += 1
    elif result = 2
        p2_wins += 1
    else:
        ties += 1

我将其保持在我认为您正在使用的编程级别。 首先你会明白它。在更多的训练营课程中,回来看看您希望如何改进您的编码。

【讨论】:

  • 我应该在哪里定义 game_on?
  • 在 while 循环之上。这只是一个布尔标志,表明玩家是否决定继续玩。你说你已经知道如何循环来保持游戏的进行。由于您没有提供该代码,因此我编写了一个规范的代码。
【解决方案2】:

最简单的解决方案是让 DiceRoll 函数返回哪个玩家获胜。

winner = None
if Score1 > Score2:
    print("Player 1:", Score1, "Player 2:", Score2)
    print("Player 1 Wins!")
    winner = 1
elif Score1 < Score2:
    print("Player 1:", Score1, "Player 2:", Score2)
    print("Player 2 Wins!")
    winner = 2
elif Score1 == Score2:
    print("Player 1:", Score1, "Player 2:", Score2)
    print("Tie!")
    winner = 0
return winner

然后在调用 DiceRoll 的循环中,检查返回的结果即可。

现在,这个函数中有很多重复的代码。你基本上每件事都做两次。这使它成为闯入另一个功能的主要候选者。我建议创建一个将玩家作为参数的函数,然后只为该玩家掷骰子,并返回分数。然后你可以为每个玩家调用该函数,比较结果,并从中确定获胜者。

最后一件事。如果玩家选择不滚动,它就会退出程序。这可能应该被更改为被没收,并让它在外循环退出,这样你就可以显示最终的计数。

【讨论】:

    猜你喜欢
    • 2012-02-29
    • 2021-12-14
    • 2015-08-25
    • 2015-12-24
    • 1970-01-01
    • 2014-02-12
    • 2019-05-09
    相关资源
    最近更新 更多