我进入编码训练营一周,所以请原谅我的简单化,可能是草率的编码,但我想知道如何为我的两个玩家骰子滚动游戏输入一系列胜利。如果球员想要继续比赛,我能够产生最终得分并重新启动该功能以重新开始,但是我想保持一个胜利的运行记录,以便玩家知道每个玩家赢了多少次。这是我的功能:
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!")
答案 0 :(得分: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
我把它保持在我认为你正在使用的编程水平上。 首先你可以理解它。在更多的训练营课程中,回过头来看看你希望如何改进你的编码。
答案 1 :(得分: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的循环中,只需检查返回的结果。
现在,您已经在此功能中获得了大量重复代码。你基本上做了两次。这使它成为打破另一个功能的主要候选者。我建议制作一个以玩家为参数的功能,然后骰子为该玩家滚动,然后返回分数。然后你可以为每个玩家调用该函数,比较结果,并从中确定胜利者。
最后一件事。如果玩家选择不滚动,则退出该程序。这可能应该被改为被视为没收,并让它退出外循环,这样你就可以显示最终的结果。