2015-11-02 97 views
0

我是一个编码训练营一周,所以原谅我简单,可能草率编码,但我想知道如何能为我的两个球员骰子滚动游戏的胜利运行理货。如果球员想要继续比赛,我能够产生最后的比分并且循环回去重新开始,但是我想保持胜利的状态,以便球员知道每个球员赢了多少次。这里是我的功能:为骰子游戏运行的理论

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

这并不真正回答你的问题,但这里有一些代码清理的想法:'dice = [random.randint(1,20)for _in range(6)]'应该简化你的六个变量到一个 – inspectorG4dget

+0

哪里你实际上调用'DiceRoll'? –

回答

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 

我一直保持这种程度,我认为你正在使用。 第一个你明白了。在更多的新兵训练营中,回过头来看看你想如何改进你的编码。

+0

我在哪里定义game_on? –

+0

在while循环之上。这只是一个布尔标志,表示玩家是否决定继续玩。你说你已经知道如何循环以保持游戏进行。由于您没有提供该代码,因此我编写了一个规范的代码。 – Prune

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的循环中,只需检查返回的结果。

现在,你在这个函数中有很多重复的代码。你基本上都做了两次。这使得它成为打破另一个功能的主要候选者。我会建议制作一个以玩家作为参数的函数,然后骰子只为该玩家滚动,然后返回得分。然后你可以调用每个玩家的函数,比较结果,并从中确定赢家。

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