我需要帮助制作一个Tic Tac Toe游戏。蟒蛇

时间:2018-01-12 14:48:56

标签: python

我是否可以获得一些帮助,使计算机的选择和完成这个程序,我仍然对我必须做的事情感到困惑...最后可能有一些循环功能?我对如何制作电脑的选择感到非常困惑。到目前为止,玩家的选择,董事会和wincheck可能都有效,我只需要其他所有工作,然后才能测试出来。出于某种原因,当我尝试在板上使用1时,它给了我一个值错误。

import random
board = [1, 2, 3, 4, 5, 6, 7, 8, 9]

Computer = [1, 2, 3, 4, 5, 6, 7, 8, 9]
player = []
pc = []
wincheck = []
def WinCheck():
    for i in range(0,9):
        o = player.count[i]
        if o == 1:
            wincheck.append[i]
        else:
            continue

    if wincheck == [1,2,3]:
        print("You Win! The board will reset in 5 seconds!")
        reset()    
    elif wincheck == [4,5,6]:
        print("You Win! The board will reset in 5 seconds!")
        reset()
    elif wincheck == [7,8,9]:
        print("You Win! The board will reset in 5 seconds!")
        reset()
    elif wincheck == [1,4,7]:
        print("You Win! The board will reset in 5 seconds!")
        reset()
    elif wincheck == [2,5,8]:
        print("You Win! The board will reset in 5 seconds!")
        reset()
    elif wincheck == [3,6,9]:
        print("You Win! The board will reset in 5 seconds!")
        reset()
    elif wincheck == [1,5,9]:
        print("You Win! The board will reset in 5 seconds!")
        reset()
    elif wincheck == [3,5,7]:
        print("You Win! The board will reset in 5 seconds!")
        reset()


def draw():

    print ("\n" , "|",board[0],"|",board[1],"|",board[2], "|")
    print ("\n", "--------")
    print ("\n", "|",board[3],"|",board[4],"|",board[5], "|")
    print ("\n", "--------")
    print ("\n", "|", board[6],"|", board[7],"|", board[8], "|" , " \n")


#Resets the board
def reset():
    while True:
        board = [1, 2, 3, 4, 5, 6, 7, 8, 9]
        time.sleep(5)
        break

#Choosing a number from the board
def ChooseNum():
    while 1 == 1:
        while True:
            print('Where do you want to place you X')
            choice = input('')
            try:
                choice = int(choice)
            except ValueError:
                print("You didn't enter a number... Try again")
                break

            x = board.index(choice)
            Computer.remove(x)
            board.remove(choice)
            board.insert(x,'X')
            print(Computer)
            draw()




def CompChoice():
    while 1 == 1:
        while True:
            compchoice = random.choice(board)
            o = board.index(compchoice)
            Computer.remove(o)
            board.remove(compchoice)
            board.insert(o,'O')
            print(Computer)
            draw()

draw()
ChooseNum()

1 个答案:

答案 0 :(得分:1)

现在你有办法让计算机移动,从我的评论中,注意这段代码:

Computer.remove(x)
board.remove(choice)

首先,您使用您找到的索引x,而另一个使用该值。

remove删除" 列表中值为x的第一项。如果没有这样的项目,则会出错。"如果找不到您要求其查找的值,则会为您提供ValueError

这会出错。 您需要从列表中删除选项:

Computer.remove(choice)
board.remove(choice)

您在计算机选择功能中遇到类似问题。 为了让你与你玩tic-tac-toe,你需要做一些改变。 指出你的方法:从函数中删除while循环,并使用类似的东西来玩:

draw()
while any(i not in ['X', 'O'] for i in board):
  ChooseNum()
  CompChoice()

如果您只有一个board,您可能会觉得更容易 - 您试图在两个列表中保留相同的信息。 您还需要检查移动是否合法 - 您无法使用'X''O'进入某个地点。 Fianlly,你可能想检查某人是否赢了。