骰子滚动游戏不会改变结果

时间:2015-11-03 17:00:11

标签: python

做一个骰子滚动游戏在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 个答案:

答案 0 :(得分: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

那就是说,请注意您在程序中还有其他几个问题。你不会终止,直到两个玩家都赢了三次 - 在中使用代替,而强>条件。

我的大多数其他评论更适合codeReview组;当你进行调试时,你可能会在那里发帖。