如何解决无限循环

How to troubleshoot an infinite loop

我正在编写一个棍子游戏,希望能与我对战。这个想法在 this HTML document 中有详细说明,但基本上我有代码可以创建一种非常基本的 AI 类型,它将自适应地学习最佳游戏方式。

我有玩家 1 和 AI 的代码,但问题是我需要 AI 持续 运行 直到我告诉它停止。我尝试将我的所有代码都包含在 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

作为参考,当我 运行 上面发布的代码在玩家 2 失败后停止迭代,我不得不 "KeyboardInterrupt" 而不是让代码循环并继续播放直到我决定退出。

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 
while True:
    num_sticks = 10
    while num_sticks!=0:
        ...

您需要重置计数器