如何排除无限循环故障

2024-06-16 09:44:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在写一个棍棒游戏,希望能和我比赛。这个想法在this HTML document中有详细说明,但基本上我有代码,可以创建一个非常基本的人工智能类型,它可以自适应地学习最佳的游戏方式。你知道吗

我有玩家1和人工智能的代码,但问题是我需要人工智能持续运行,直到我告诉它停止。我尝试将所有代码封装在while True:中,但我认为条件语句中的某些内容导致代码停止,而不是不断循环。我希望有人能对我如何实现一个解决方案提出建议。你知道吗

import random
import sys

"Global Variables"
prompt = '>>> ' 
btw_1_3 = 'Please pick a number beteween 1 and 3.'


#Introduction to the game
raw_input("Welcome to the game of sticks! Press <Enter> to continue...")
print("How many sticks are there on the table initially (10-100)?")
num_sticks = int(raw_input(prompt))
print("Ok, there are %s sticks on the board.") % (num_sticks)
print("|")*num_sticks

#Initialize a dictionary that we will change
stick_dict={}
for number in range(1,num_sticks+1):
    stick_dict["hat{0}".format(number)]=[[1,2,3]]



while True:
    while num_sticks!=0:

        #player 1
        while True:
            print "Player 1: How many sticks do you take (1-3)?"
            player_1 =  int(raw_input(prompt))

            if 1 <= player_1 <= 3:
                break
            else:
                print (btw_1_3)

        num_sticks = num_sticks - player_1

        if num_sticks == 1:
            print("Ok, there is %s stick on the board.") % (num_sticks)
            print("|")*num_sticks

        elif num_sticks < 1:
            print("Player 1, you lose.")

            #If Player 1 loses, then the AI wins and we want to execute this 
            for key in stick_dict:
                if len(stick_dict[key]) == 2:
                    stick_dict[key][0].append(stick_dict[key][1])
                    del stick_dict[key][1]
                    sys.exit()

        else:
            print("Ok, there are %s sticks on the board.") % (num_sticks)
            print("|")*num_sticks




        #AI player
        guess = random.choice(stick_dict["hat{0}".format(num_sticks)][0])
        if guess > 1:
            print "The computer chose %s sticks" % (guess)
        elif guess == 1:
            print "The computer chose %s stick" % (guess)


        stick_dict["hat{0}".format(num_sticks)].append(guess)
        print stick_dict

        num_sticks = num_sticks - guess


        if num_sticks == 1:
            print("Ok, there is %s stick on the board.") % (num_sticks)
            print("|")*num_sticks

        elif num_sticks < 1:
            print("Player 2 (AI BOT), you lose.")
            #If the AI loses
            for key in stick_dict:
                if len(stick_dict[key]) == 2:
                    del stick_dict[key][1]
            break
        else:
            print("Ok, there are %s sticks on the board.") % (num_sticks)
            print("|")*num_sticks

作为参考,当我运行上面发布的代码时,它在player2丢失后停止了迭代,我不得不“键盘中断”,而不是让代码循环并继续播放,直到我决定退出。你知道吗

Welcome to the game of sticks! Press <Enter> to continue...
How many sticks are there on the table initially (10-100)?
>>> 10
Ok, there are 10 sticks on the board.
||||||||||
Player 1: How many sticks do you take (1-3)?
>>> 3
Ok, there are 7 sticks on the board.
|||||||
The computer chose 3 sticks
{'hat9': [[1, 2, 3]], 'hat8': [[1, 2, 3]], 'hat1': [[1, 2, 3]], 'hat3': [[1, 2, 3]], 'hat2': [[1, 2, 3]], 'hat5': [[1, 2, 3]], 'hat4': [[1, 2, 3]], 'hat7': [[1, 2, 3], 3], 'hat6': [[1, 2, 3]], 'hat10': [[1, 2, 3]]}
Ok, there are 4 sticks on the board.
||||
Player 1: How many sticks do you take (1-3)?
>>> 3
Ok, there is 1 stick on the board.
|
The computer chose 1 stick
{'hat9': [[1, 2, 3]], 'hat8': [[1, 2, 3]], 'hat1': [[1, 2, 3], 1], 'hat3': [[1, 2, 3]], 'hat2': [[1, 2, 3]], 'hat5': [[1, 2, 3]], 'hat4': [[1, 2, 3]], 'hat7': [[1, 2, 3], 3], 'hat6': [[1, 2, 3]], 'hat10': [[1, 2, 3]]}
Player 2 (AI BOT), you lose.

^C---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
/Users/andrewthappa/Documents/python/scripts/sticks/human_v_ai.py in <module>()
     29 
     30 
---> 31 while True:
     32         while num_sticks!=0:
     33 

Tags: thekey代码boardonoknumare