骰子游戏//到达GAME_END_POINTS后,循环无法正确退出

时间:2018-10-12 01:42:44

标签: python debugging dice

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语句,以提供一个干净的调试清单

1 个答案:

答案 0 :(得分:0)

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)