如何解决无限循环问题

时间:2015-01-23 21:41:27

标签: python python-2.7 loops while-loop 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 

1 个答案:

答案 0 :(得分:0)

while True:
    num_sticks = 10
    while num_sticks!=0:
        ...

您需要重置计数器