Python 树没有被仔细检查的功能

Python 树没有被仔细检查的功能,python,tree,tic-tac-toe,Python,Tree,Tic Tac Toe,我试着让我的程序加载一个包含所有可能的tic-tac趾板的树,然后当给定一个随机板输入时,它可以根据树打印出所有可能的下一步动作。但是,当我尝试执行此操作时,除了根节点之外,我的所有节点似乎都返回None 我现在的代码: import random def main(): root = createNode(board, None, "X") setBoard = (" ", " ", " ", " ", "X", "X",

我试着让我的程序加载一个包含所有可能的tic-tac趾板的树,然后当给定一个随机板输入时,它可以根据树打印出所有可能的下一步动作。但是,当我尝试执行此操作时,除了根节点之外,我的所有节点似乎都返回
None

我现在的代码:

import random

def main():        
    root = createNode(board, None, "X") 
    setBoard = (" ", " ", " ",
                " ", "X", "X",
                " ", " ", "O")

    nextMoves = find(root, setBoard)
    print(nextMoves)
    for child in nextMoves.children:
        printBoard(child.boardState)

boardCases = set()

board = [" ", " ", " ", " ", " ", " ", " ", " ", " "]

def printBoard(board):
    for i in range(3):
        print(board[i*3], end="")
        print("|", end="")
        print(board[i*3+1], end="")
        print("|", end="")
        print(board[i*3+2], end="")
        print("")
        if i < 2:
            print("-+-+-")
    print("~~~~~~~~~~~~")


def checkForWin(board, c):

    # all column cases
    for i in range(3):
        if board[i] == c and board[i+3] == c and board[i+6] == c:
            #print(c + " wins!")
            return True

    #all row cases     
    for i in range(3):
        if board[i*3] == c and board[i*3+1] == c and board[i*3+2] == c:
            #print(c + " wins!")
            return True

    #all across cases
    if board[0] == c and board[4] == c and board[8] == c:
        #print(c + " wins!")
        return True
    if board[2] == c and board[4] == c and board[6] == c:
        #print(c + " wins!")
        return True

    #no wins game tie
    counter = 0
    for i in range(9):
        if board[i] == " ":
            # no tie found ---> game countinues
            return False
        else:
            counter += 1

    if counter == 9:
        #print("Tie Game!")
        return True

class boardNode:
    def __init__(self):
        self.boardState = ()
        self.children = []
        self.winner = ""

    def numChildren(self):
        return len(self.availableMoves())

    def availableMoves(self):
        moves = set()
        for i in range(9):
            if self.boardState[i] == " ":
                moves.add(i)
        return moves

counter = 0 
def createNode(currentBoard, posToFill, c):
    global counter
    newNode = boardNode()
    board = list(currentBoard)
    counter +=1
    if posToFill != None:
        board[int(str(posToFill))] = c
    newNode.boardState = tuple(board)
    if checkForWin(tuple(board), c) == False:
        newNode.winner = None
        if c == "X":
            for pos in newNode.availableMoves():
                newNode.children.append(createNode(newNode.boardState, pos, "O"))
        else:
            for pos in newNode.availableMoves():
                newNode.children.append(createNode(newNode.boardState, pos, "X"))
    else: 
        newNode.winner = c
    if newNode.boardState not in boardCases:
        boardCases.add(newNode.boardState)
    return newNode


def find(node, boardToSearch):
    if list(node.boardState) == list(boardToSearch):
        return node
    else:
        for child in node.children:
            return find(child, boardToSearch)


if __name__ == "__main__":
    main()
随机导入
def main():
根=创建节点(板,无,“X”)
立板条=(“”,“”,“”,“”,
“,”X“,”X“,
“,”和“O”)
nextMoves=查找(根,立根板)
打印(下一个移动)
对于nextMoves.children中的子项:
印刷电路板(child.boardState)
boardCases=set()
线路板=[“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”]
def打印板(板):
对于范围(3)中的i:
打印(电路板[i*3],end=”“)
打印(“|”,end=”“)
打印(板[i*3+1],end=”“)
打印(“|”,end=”“)
打印(板[i*3+2],end=”“)
打印(“”)
如果i<2:
打印(“-+-+-”)
打印(“~~~~~~~~~~~~”)
def checkForWin(电路板,c):
#所有列案例
对于范围(3)中的i:
如果板[i]==c和板[i+3]==c和板[i+6]==c:
#打印(c+“赢!”)
返回真值
#所有行案例
对于范围(3)中的i:
如果板[i*3]==c和板[i*3+1]==c和板[i*3+2]==c:
#打印(c+“赢!”)
返回真值
#所有案例
如果板[0]==c和板[4]==c和板[8]==c:
#打印(c+“赢!”)
返回真值
如果板[2]==c和板[4]==c和板[6]==c:
#打印(c+“赢!”)
返回真值
#不赢比赛平局
计数器=0
对于范围(9)内的i:
如果板[i]==“”:
#未找到平局--->游戏计数
返回错误
其他:
计数器+=1
如果计数器==9:
#打印(“平局游戏!”)
返回真值
类boardNode:
定义初始化(自):
self.boardState=()
self.children=[]
self.winner=“”
def儿童(自我):
返回len(self.availableMoves())
def可用移动(自):
移动=设置()
对于范围(9)内的i:
如果self.boardState[i]=“”:
增加(i)
回击动作
计数器=0
def createNode(电流板、posToFill、c):
全局计数器
newNode=boardNode()
线路板=列表(当前线路板)
计数器+=1
如果posToFill!=无:
板[int(str(posToFill))]=c
newNode.boardState=元组(板)
如果checkForWin(元组(板),c)=False:
newNode.winner=无
如果c==“X”:
对于newNode.availableMoves()中的pos:
newNode.children.append(createNode(newNode.boardState,pos,“O”))
其他:
对于newNode.availableMoves()中的pos:
newNode.children.append(createNode(newNode.boardState,pos,“X”))
其他:
newNode.winner=c
如果newNode.boardState不在boardCases中:
boardCases.add(newNode.boardState)
返回新节点
def查找(节点、boardToSearch):
如果列表(node.boardState)=列表(boardToSearch):
返回节点
其他:
对于node.children中的子节点:
返回查找(子项、boardToSearch)
如果名称=“\uuuuu main\uuuuuuuu”:
main()
以下是我当前案例的输出:

$ python ticTacToe.py

None
Traceback (most recent call last):
  File "test.py", line 115, in <module>
    main()
  File "test.py", line 11, in main
    for child in nextMoves.children:
AttributeError: 'NoneType' object has no attribute 'children'
$python ticTacToe.py
没有一个
回溯(最近一次呼叫最后一次):
文件“test.py”,第115行,在
main()
文件“test.py”,第11行,在main中
对于nextMoves.children中的子项:
AttributeError:“非类型”对象没有属性“子对象”

find(child,boardToSearch)
应该是
return find(child,boardToSearch)
@Blorgbeard我补充了这一点,但我仍然得到同样的错误嗯,实际上比我想的要复杂。仅当值不是None时才返回该值,否则,将继续通过子项进行循环。但是你真的需要一步一步地调试你的代码来看看发生了什么。
find(child,boardToSearch)
应该是
return find(child,boardToSearch)
@Blorgbeard我补充了这一点,但我仍然得到了同样的错误嗯,实际上比我想象的要复杂。仅当值不是None时才返回该值,否则,将继续通过子项进行循环。但是,您确实需要逐步调试代码,以查看发生了什么。