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

为什么我的TictaToe游戏没有';没有完成运行代码?(Python 2.7.9),python,tic-tac-toe,Python,Tic Tac Toe,当我运行代码时,在我选择先玩还是不玩之后,代码就卡住了。 下面是代码: #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 Pla

当我运行代码时,在我选择先玩还是不玩之后,代码就卡住了。 下面是代码:

#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
回溯(最近一次呼叫最后一次):
文件“C:\Users\HANY\Documents\Nona CS\MyTicTacToe3.py”,第214行,在
主(播放器、计算机、新)
文件“C:\Users\HANY\Documents\Nona CS\MyTicTacToe3.py”,第203行,在Main中
董事会(新)
文件“C:\Users\HANY\Documents\Nona CS\MyTicTacToe3.py”,第50行,在黑板上
打印“[*]”,移动[7],“:”,移动[8],“:”,移动[9],“[*]”
索引器:列表索引超出范围
有人能告诉我我做错了什么以及如何改正吗?
我会很感激的谢谢

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

使用
new[i[1]
defwinn(播放器、计算机、新)
中也应该存在同样的问题

另请参见以了解如何使用列表

编辑: 您在
def Player\u Turn(Player,new)
中也遇到同样的问题。
当你在那里纠正它时,它也会起作用。

你描述的错误似乎与你的代码无关。这有点难看,但在你对
TheBoard
的一次调用中,
move
变量的长度没有9。你应该解决这个问题。