2015-11-02 59 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的循環中,只需檢查返回的結果。

現在,你在這個函數中有很多重複的代碼。你基本上都做了兩次。這使得它成爲打破另一個功能的主要候選者。我會建議製作一個以玩家作爲參數的函數,然後骰子只爲該玩家滾動,然後返回得分。然後你可以調用每個玩家的函數,比較結果,並從中確定贏家。

最後一件事。如果玩家選擇不滾動,則退出該程序。這應該可能會被改爲被視爲沒收,並讓它在外部循環退出,以便您可以顯示最終的計數。