Python中的简单21游戏问题

时间:2018-03-08 20:12:21

标签: python

我最近编写了一个简单的21游戏(由用户对抗计算机),当用户/计算机赢得游戏时遇到了一些麻烦。这一计划背后的逻辑很简单,做工精细,它'只是那之后有一个赢家(print语句简单地宣布获胜者一旦游戏做)程序会询问用户是否他/她想"辊"他们的骰子又来了。我完全理解为什么会出现这个问题,但我不确定如何使用break语句修复它,因为我的教授不希望我们使用它们。他建议为我的while循环创建一个布尔标志来运行,但我似乎无法弄明白。有什么建议?我的程序的完整代码如下所示。谢谢!

import random
GAME_LIMIT = 21

def main():
    user_points = 0
    computer_points = 0
    print("Welcome to the game of 21! May the odds be ever in your favor...")
    print()
    answer = get_response()
    while answer == "y":
        points, comp_points = roll_dice()
        user_points += points
        computer_points += comp_points
        print("Points:", user_points)
        if user_points == GAME_LIMIT:
            print("User's Points:", user_points)
            print("Computer's Points:", computer_points)
            if computer_points == GAME_LIMIT:
                print("Tie Game!")
            else:
                print("User Wins!")
        if user_points > GAME_LIMIT:
            print("User's Points:", user_points)
            print("Computer's Points:", computer_points)
            if computer_points < GAME_LIMIT:
                print("Computer Wins!")
            elif computer_points == GAME_LIMIT:
                print("Computer Wins!")
            else:
                print("Tie Game!")

        answer = get_response()

    if answer == "n":
        print("User's Points:", user_points)
        print("Computer's Points:", computer_points)
        if computer_points == GAME_LIMIT:
            print("Computer Wins!")
        elif computer_points > GAME_LIMIT:
            print("User Wins!")
        elif computer_points == user_points:
            print("Tie Game!")
        elif computer_points < GAME_LIMIT:
            if user_points < computer_points:
                print("Computer Wins!")
            else:
                print("User Wins!")


def roll_dice():
    user_roll = random.randint(1,6) + random.randint(1,6)
    computer_roll = random.randint(1,6) + random.randint(1,6)
    return user_roll, computer_roll

def get_response():
    response = input("Do you want to roll? (y/n): ")
    return response

main()

1 个答案:

答案 0 :(得分:0)

如果有胜利者或领带结束游戏,你想要的是什么 如果你输入n到角色骰子仍然结束游戏我添加了isEnd并做了一些更改(评论)

import random
GAME_LIMIT = 21

def roll_dice():
    user_roll = random.randint(1,6) + random.randint(1,6)
    computer_roll = random.randint(1,6) + random.randint(1,6)
    return user_roll, computer_roll

def get_response():
    response = raw_input("Do you want to roll? (y/n): ")
    return response


def main():
    user_points = 0
    computer_points = 0
    print("Welcome to the game of 21! May the odds be ever in your favor...")
    print()
    isEnd = False  # added a is game end

    while not isEnd:
        answer = get_response()
        if(answer == "y"): #checkes whther still roll dice
            points, comp_points = roll_dice()
            user_points += points
            computer_points += comp_points
            print("Points:", user_points)
            if user_points == GAME_LIMIT:
                print("User's Points:", user_points)
                print("Computer's Points:", computer_points)
                if computer_points == GAME_LIMIT:
                    print("Tie Game!")
                else:
                    print("User Wins!")
                isEnd = True #When ther is winner
            if user_points > GAME_LIMIT:
                print("User's Points:", user_points)
                print("Computer's Points:", computer_points)
                if computer_points < GAME_LIMIT:
                    print("Computer Wins!")
                elif computer_points == GAME_LIMIT:
                    print("Computer Wins!")
                else:
                    print("Tie Game!")
                isEnd = True #When ther is winner

        if (answer == "n"):
            isEnd = True #or the exit





if __name__ == "__main__":
    main()