为什么我的 TicTacToe 游戏没有完成 运行 代码? (Python 2.7.9)

Why my TicTacToe game doesn't complete running the codes? (Python 2.7.9)

当我 运行 我的代码时,在我选择是否先玩后它卡住了。 所以,这里是代码:

#TicTacToe game
import random

null = ''
new = ['','','','','','','','','']
Player = ''
Computer = ''


#X or O signs
def sign(Player, Computer):
    Player = raw_input('Please choose either X or O:')
    while Player not in ('X','x','O','o'):
        print ('Not the appropriate choice!')
        Player = raw_input('Please choose either X or O;')
    if Player == 'X' or Player == 'x':
        print ('You have chosen X!')
        Computer = 'O'
    else:
        print ('You have chosen O!')
        Computer = 'X'
    return Player.upper(), Computer.upper()


#Which player will play first
def WhoPlaysFirst():
    shift = None
    while shift not in ('yes','Yes','YES','no','No','NO'):
        shift = raw_input('Do you want to play first? Yes or No:')
        if shift == 'yes' or shift == 'Yes' or shift == 'YES':
            return 1
        elif shift == 'no' or shift == 'No' or shift == 'NO':
            return 0
        else:
            print ('It is invaild choice!')

def TheBoard(Move):


    print ("=================================")
    print ("[*]        :        :         [*]")
    print "[*]  " , Move[1] ,       "    :   " , Move[2] ,         "   :    " , Move[3] ,            "   [*]"
    print ("[*]        :        :         [*]")
    print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    print ("[*]        :        :         [*]")
    print "[*]  " , Move[4] ,       "    :   " , Move[5] ,         "   :    " , Move[6] ,            "   [*]"
    print ("[*]        :        :         [*]")
    print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    print ("[*]        :        :         [*]")
    print "[*]  " , Move[7] ,       "    :   " , Move[8] ,         "   :    " , Move[9] ,            "   [*]"
    print ("[*]        :        :         [*]")
    print ("=================================")


def winner_Computer():
    print ("Sorry you lost!")

def winner_Player():
    print("You won!")

def FirstMove_Player(Player, Computer, new):
    while winn(Player, Computer, new) is None:
        Turn = Player_Turn(Player, new)
        new[int(Turn)] = Player
        TheBoard(new)
        if winn(Player, Computer, new) != None:
            break
        else:
            pass
        print("The computer will choose..")
        C_Turn = Computer_Turn(Player, Computer, new)
        print C_Turn
        new[int(C_Turn)] = Computer
        TheBoard(new)
    Pr = winn(Player, Computer, new)
    if Pr == 1:
        winner_Player()
    elif Pr == 0:
        winner_Computer()
    else:
        print("It is a tie :/")


def FirstMove_Computer(Player, Computer, new):
    while not winn(Player, Computer, new):
        print("The computer will take..")
        C_Turn = Computer_Turn(Player, Computer, new)
        print C_Turn
        new[C_Turn] = Computer
        TheBoard(new)
        if winn(Player, Computer, new) != None:
            break
        else:
            pass
        Turn = Player_Turn(Player, new)
        new[int(move)] = Player
        TheBoard(new)
    Pr = winn(Player, Computer, new)
    if Pr == 1:
        winner_Player()
    elif Pr == 0:
        winner_Computer()
    else:
        print("It is a tie :/")


def winn(Player, Computer, new):
    Possibilities = ((1,2,3),(4,5,6),(7,8,9),(1,4,7),(2,5,8),(3,6,9),(1,5,9),(3,5,7))
    for i in Possibilities:
        if new[i[1]] == new[i[2]] == new[i[3]] != null:
            Winner = new[i[1]]
            if Winner == Player:
                return 1
            elif Winner == Computer:
                return 0
            if null not in new:
                return 'Tie!'
    if null not in new:
        return 'Tie'
    return None


def Player_Turn(Player, new):
    Move = raw_input("Which box you want to put in?:")
    while True:
        if Move not in ('1','2','3','4','5','6','7','8','9'):
            print("That is invalid move")
            Move = raw_input("Which box you want to put in?:")
        elif new[int(Move)] != null:
            print ("This box is already taken, please choose another one")
            Move = raw_input("Which box you want to put in?:")
        else:
            return int(Move)



def Computer_Turn(Player, Computer, new):
    choicest = [9, 7, 5, 1, 3]
    blank = []
    for i in range(0,9):
        if new[i] == null:
            blank.append(i)

    for i in blank:
        new[i] = Computer
        if winn(Player, Computer, new) is 0:

            return i
        new[i] = null

    for i in blank:
            new[i] = Player
            if winn(Player, Computer, new) is 1:

                return i
            new[i] = null

    return int(blank[random.randrange(len(blank))])


def Enterance():
    #Intro. to the game
    print ("WELCOME TO...")
    print ("############         ##############                #############")
    print ("############         ##############                #############")
    print ("    ####    WWWWWWWWW     ####         XXXXXXXXX        ####")
    print ("    ####OOO WWW*****W     ####         XXX&&&&&&        ####         WWWWWWWWWWW")
    print ("    ####OOO WWWWWWWWW     ####         XXXXXXXXX        ####         WWW     WWW")
    print ("    ####    WWW           ####GGGGGGG  XXX              ####DDDDDDDD WWWWWWWWWWW")
    print ("    ####O#O WWW           ####G@@@@@G  XXX              ####DDDDDDKD WW%%%%%%%WW")
    print ("    ####O#O WWW           ####G@@@@@G  XXX              ####D    DKD WWWWWWWWWWW")
    print ("    ####O#O WWWWWWWWW     ####G@@@@@@GGGGGGXXXXX        ####D    DKD WW")
    print ("    ####O#O WWW*****W     ####G@@@@@@@@@@@GX&&&&        ####DDDDDDKD WW")
    print ("    ####O#O WWWWWWWWW     ####GGGGGGG GGGGGGXXXXX       ####DDDDDDKD WWWWWWWWWWW")
    print ("All you have to do is to choose the number so that you can make your move")
    print ("And here is how your Game Board will look like:")
    print ("                                    ")
    print ("====================================")
    print ("[*]        :          :          [*]")
    print ("[*]    1   :     2    :     3    [*]")
    print ("[*]        :          :          [*]")
    print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    print ("[*]        :          :          [*]")
    print ("[*]    4   :     5    :     6    [*]")
    print ("[*]        :          :          [*]")
    print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    print ("[*]        :          :          [*]")
    print ("[*]    7   :     8    :     9    [*]")
    print ("[*]        :          :          [*]")
    print ("====================================")
    print ("                LET'S              START                THE GAME!!             ")


def Main(Player, Computer, new):
    Enterance()
    Move = sign(Player, Computer)
    Player = Move[0]
    Computer = Move[1]
    b = WhoPlaysFirst()
    if b == 1:
        print ("You will play first.")
        print ("Lets start. Here is our Game Board!")
        TheBoard(new)
        FirstMove_Player(Player, Computer, new)
    elif b == 0:
        print ("The computer will play first")
        print ("Lets begin the game. That is our Game Board!")
        TheBoard(new)
        FirstMove_Computer(Player, Computer, new)
    else:
        pass


Main(Player, Computer, new)
raw_input("Tab Enter to exit")

这是我在代码卡在某个点时收到的错误消息:

Traceback (most recent call last):
  File "C:\Users\HANY\Documents\Nona CS\MyTicTacToe3.py", line 214, in <module>
    Main(Player, Computer, new)
  File "C:\Users\HANY\Documents\Nona CS\MyTicTacToe3.py", line 203, in Main
    TheBoard(new)
  File "C:\Users\HANY\Documents\Nona CS\MyTicTacToe3.py", line 50, in TheBoard
    print "[*]  " , Move[7] ,       "    :   " , Move[8] ,         "   :    " , Move[9] ,            "   [*]"
IndexError: list index out of range

有人可以告诉我做错了什么以及如何解决吗? 非常感谢!

您的变量new是一个列表,列表的第一个索引是 0。 你应该改变你的def TheBoard(Move)。它应该以 Move[0] 而不是 Move[1] 开头,因为 Move[9] 是列表的第 9 个索引,但最大值为 8。

同样的问题应该出现在 def winn(Player, Computer, new)new[i[1]]

另请参阅 THIS 以了解如何使用列表。

编辑: 您在 def Player_Turn(Player, new) 中遇到了同样的问题。 当您也在那里更正它时,它应该可以工作。