2015-11-03 56 views
0

做Python中的擲骰遊戲,每當我開始我的第二輪我還是最終獲得從上一輪相同的骰子結果。擲骰遊戲不會改變結果

import random 
import time 

#gets the dice side 
def roll_dice(sides): 
    dice_results = list() 
    for side in sides: 
     roll_result = random.randint(1,side+1) 
     dice_results.append(roll_result) 
    return dice_results 

#pulls a dice out from the list 
def dice_fell(roll_result): 
    player1_dice = player1_dice_results 
    player2_dice = player2_dice_results 

    for item in player1_dice: 
     if item % 4 == 0: 
      player1_dice.remove(item) 
     return player1_dice 

    for item in player2_dice: 
     if item % 4 == 0: 
      player2_dice.remove(item) 
      return player2_dice 
# variables 

dice_set1=[4, 6, 8, 10, 12, 20, 100] 
dice_set2=[4, 6, 8, 10, 12, 20, 100] 

player1_dice_results = roll_dice(dice_set1) 
player2_dice_results = roll_dice(dice_set2) 

player1_final_results = dice_fell(player1_dice_results) 
player2_final_results = dice_fell(player2_dice_results) 

player1_total= sum(player1_dice_results) 
player2_total= sum(player2_dice_results) 

player1_score = 0 
player2_score = 0 

while player1_score < 3 or player2_score < 3: 
# This part just announces what happens 

    exit= input(str("Press Enter to start! Press 'q' to leave after each round! \n")) 
    if exit != "q": 
     print("Let's begin! Be careful for the small table!") 
    elif exit == "q": 
     quit() 

    print("You are rolling...") 
    time.sleep(2) 
    print("You have rolled: ",player1_final_results) 

    if len(player1_final_results) < 7: 
     print("Sorry player 1, some of your dice have fallen off the table!") 

    print() 
    print("Your total is: ",player1_total) 
    print() 

    print("Player 2 is rolling...") 
    time.sleep(2) 
    print("Player 2 has rolled:" ,player2_final_results) 
    if len(player2_final_results) < 7: 
     print("Sorry player 2, some of your dice have fallen off the table!") 
    print() 
    print("Player 2's total is: ",player2_total) 
    print() 

    if player1_total > player2_total: 
     print() 
     print("You have won the round with,",player1_total,"!"), 
     player1_score += 1 
     print("Your score is: ",player1_score) 
    elif player2_total > player1_total: 
     print() 
     print("Player 2 has won the round with,",player2_total,"!"), 
     player2_score += 1 
     print("Player 2's score is: ",player2_score) 

    if player1_score == 3: 
     print("Congratulations, you won!") 
    elif player2_score == 3: 
     print("Player 2 wins! Better luck next time champ!") 
+1

_「這部分只是宣佈發生了什麼」_它在哪裏做?我沒有看到它打印有關骰子卷的任何信息。如果我將所有這些代碼粘貼到一個腳本中並運行它,我會看到您描述的問題嗎? – Kevin

+0

加入剩下的部分。必須分解一些,以便我可以提交它。對於那個很抱歉! – Ryan

+6

請閱讀[mcve]並在佈局上花費更多時間;沒有人想要梳理格式不正確的代碼轉儲。 – jonrsharpe

回答

0

我相信我已經修復了縮進問題。 我轉載問題。 正如凱文說,你眼前的問題是,你的霸氣只有一次,你輸入循環之前。以下是循環內的滾子的外觀,但是玩家決定是否繼續。

dice_set1=[4,6,8,10,12,20,100] 
dice_set2=[4,6,8,10,12,20,100] 

player1_score = 0 
player2_score = 0 

while player1_score < 3 or player2_score < 3: 
    # This part just announces what happens 

    exit = raw_input(str("Press Enter to start! Press 'q' to leave after each round! \n")) 
    if exit != "q": 
     print("Let's begin! Be careful for the small table!") 
    elif exit == "q": 
     quit() 

    player1_dice_results = roll_dice(dice_set1) 
    player2_dice_results = roll_dice(dice_set2) 

    player1_final_results = dice_fell(player1_dice_results) 
    player2_final_results = dice_fell(player2_dice_results) 

    player1_total= sum(player1_dice_results) 
    player2_total= sum(player2_dice_results) 

    print("You are rolling...") 
    ... # remainder of code omitted 

也就是說,你注意,你還有其他幾個問題的方案。你將不會終止,直到球員已經贏得了三次 - 在條件使用,而不是

我的大部分其他意見是更適合的代碼審查組;當您完成調試時,您可能會在那裏發帖。

+0

哇,我覺得現在真的很愚蠢,這工作。感謝Prune和Kevin的所有幫助! – Ryan