0

Here is the code: (from MAIN file)

def BestFirstSearch(startingBoard):
    Q = [startingBoard]
    Visited = []

    while (len(Q) != 0):
        Q.sort()

        currentQ = Q.pop(0)

        Visited.append(currentQ)

        # print(currentQ)

        if (currentQ.Board == currentQ.GOAL):
            return True


        currentQ.createChildrenBoards()

        for items in currentQ.Children:
            if items not in Visited:
                Q.append(items)
        
        print(len(Q))
        print(currentQ)

    return False

(from CLASS file):

    def createChildrenBoards(self):
        """ Creates the set of potential children Boards from the current Board """
        row = self.X
        col = self.Y

        assert( (row >=0 and row < BoardClass.N)
                and
                (col >=0 and col < BoardClass.N) )

        newChildrenBoards = []

        #print(self.Board[row][col])

        # UP(NORTH): slide empty (0) space up
        if ( row != 0 ):
            newChildBoard = self.copyCTOR()
            newChildBoard.Parent = self
            newChildBoard.X = row-1
            newChildBoard.Y = col
            holdCell = newChildBoard.Board[newChildBoard.X][newChildBoard.Y]
            newChildBoard.Board[newChildBoard.X][newChildBoard.Y] = 0
            newChildBoard.Board[row][col] = holdCell
            newChildrenBoards.append(newChildBoard)

        for puzzle in newChildrenBoards:
            puzzle.computeDistanceFromGoal()

        self.Children = newChildrenBoards

Here are portions of the code I'm working with. I initialized the starting board in a class that constructs the puzzle. Then in my main I would call the create children function which creates a list of children based on where you can move the zero (north being an example of how I would move 0). The puzzle looks like this: Goal = [ [0, 1, 2], [3, 4, 5], [6, 7, 8] ] Puzzle = [ [3, 1, 2], [4, 7, 5], [6, 8, 0] ]

I'm not getting why the queue won't add more children from the children created from the starting board. I'm hoping that I can get feedback that will help me understand why my loop isn't registering the "grandchildren". Thank you!

0 Answers0