首页 > 解决方案 > 骰子游戏 // 到达 GAME_END_POINTS 时循环未正确退出

问题描述

while(user_plcr <= GAME_END_POINTS or computer_plcr <= GAME_END_POINTS):
    print_current_player(is_user_turn)
    user_total = take_turn(is_user_turn,COMPUTER_HOLD)
    user_plcr = user_plcr + user_total
    is_user_turn = get_next_player(is_user_turn)
    print_current_player(is_user_turn)
    computer_total = take_turn(is_user_turn,COMPUTER_HOLD)
    computer_plcr = computer_plcr + computer_total
    report_points(user_plcr,computer_plcr)
    print("\n")
    is_user_turn = get_next_player(is_user_turn)

def take_turn(is_user_turn, COMPUTER_HOLD):
human_score = 0
computer_score = 0
if is_user_turn == True:
    begin = raw_input("roll? [yn]")
    if begin == 'y' or begin == 'Y':
        human_rand = roll_die()
    elif begin == 'n' or begin == 'N':
        is_user_turn = False
        return human_score
    while human_rand != 1:
        human_score = human_score + human_rand
        if human_score != 0:
            human_score = str(human_score)
            human_rand = str(human_rand)
            print("roll: " + human_rand)
            print("Current Score: " + human_score)
            human_rand = int(human_rand)
            human_score = int(human_score)
            again = raw_input("roll again? [yn]")
            print("\n")
            if again == 'y' or again == 'Y':
                human_rand = roll_die()
                continue
            elif again == 'n' or again == 'N':
                is_user_turn = False
                break
        else:
            human_score = 1
            human_rand = str(human_rand)
            print("roll: " + human_rand)
            human_rand = int(human_rand)
            print("You rolled a 1. Turn over.")
            human_score = str(human_score)
            print("Current Score: " + human_score)
            human_score = int(human_score)
            return human_score
    if human_rand == 1:
        human_rand = str(human_rand)
        print("roll: " + human_rand)
        human_rand = int(human_rand)
        print("You rolled a 1. Turn over.")
        human_score = 1
        human_score = str(human_score)
        print("Current Score: " + human_score)
        human_score = int(human_score)
        return human_score
    else:
        return human_score
elif is_user_turn == False:
    computer_rand = roll_die()
    while computer_rand != 1:
        computer_score = computer_score + computer_rand
        if computer_score != 0:
            computer_rand = str(computer_rand)
            print("roll: " + computer_rand)
            computer_rand = int(computer_rand)
            if computer_score <= COMPUTER_HOLD:
                computer_rand = roll_die()
                continue
            else:
                computer_score = str(computer_score)
                print("*computer holds on " + computer_score + "*")
                computer_score = int(computer_score)
                is_user_turn = True
                break
        else:
            computer_score = 1
            computer_rand = str(computer_rand)
            print("roll: " + computer_rand)
            computer_rand = int(computer_rand)
            print("The computer rolled a 1. Turn over.")
            return computer_score
    if computer_rand == 1:
        computer_rand = str(computer_rand)
        print("rollclea " + computer_rand)
        computer_rand = int(computer_rand)
        print("The computer rolled a 1. Turn over.")
        computer_score = 1
        return computer_score
    else:
        return computer_score

函数 take_turn() 存储分数的值。report_points 显示总分。一旦放置者或总论文达到 GAME_END_POINTS,循环应该退出。相反,它让计算机在用户已经赢得游戏后进行掷骰子。我已经从代码块中删除了 if-else 语句,以便为调试提供一个干净的状态

标签: pythondebuggingdice

解决方案


while(user_plcr <= GAME_END_POINTS or computer_plcr <= GAME_END_POINTS):

当两者都成立时,这将继续。所以,即使user_plcr > GAME_END_POINTS,它也会一直持续到computer_plcr > GAME_END_POINTS。当其中任何一个为假时,您都想退出。换句话说,当两者都为真时继续。

while (user_plcr <= GAME_END_POINTS and computer_plcr <= GAME_END_POINTS):

另外,在 中while,如果用户得分超过GAME_END_POINTS,计算机仍然可以运行。所以你需要在允许计算机轮流之前检查用户的分数。

while(user_plcr <= GAME_END_POINTS and computer_plcr <= GAME_END_POINTS):
    print_current_player(is_user_turn)
    user_total = take_turn(is_user_turn,COMPUTER_HOLD)
    user_plcr = user_plcr + user_total
    if user_plcr <= GAME_END_POINTS:
        is_user_turn = get_next_player(is_user_turn)
        print_current_player(is_user_turn)
        computer_total = take_turn(is_user_turn,COMPUTER_HOLD)
        computer_plcr = computer_plcr + computer_total
    report_points(user_plcr,computer_plcr)
    print("\n")
    is_user_turn = get_next_player(is_user_turn)

推荐阅读