骰子游戏的运行计数



我已经进入编码训练营一周了,所以请原谅我过于简单,可能过于草率的编码,但我想知道如何才能为我的双人掷骰子游戏连续获胜。如果球员们想继续比赛,我可以得出最终比分,并循环函数重新开始,但我想保持连胜记录,这样球员们就知道每个球员赢了多少次。这是我的功能:

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!")

由于函数中没有循环,我假设在调用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

我一直把它保持在我认为你正在使用的编程水平首先你要了解它。在接下来的几次训练营中,回来看看你想如何改进你的编码。

最简单的解决方案是让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的循环中,只需检查返回的结果即可。

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

最后一件事。如果玩家选择不掷骰子,则退出程序。这可能会被改变为被没收,并在外循环退出,这样你就可以显示最终的计数。

相关内容

  • 没有找到相关文章

最新更新