示例#1
0
 def terminalState(self, curDepth: int, maxDepth: int, game: Game) -> bool:
     """
     :param curDepth: current state depth
     :param maxDepth: maximum allowed state depth
     :param game:
     :return: true if the current state is terminal state, false otherwise
     """
     if (curDepth == maxDepth or game.isFinished()):
         self.cnt += 1
         return True
     return False
示例#2
0
    def applyHeuristic(self, initialGame: Game):
        if (initialGame.isFinished()):
            return initialGame
        pq = PriorityQueue()
        visited = {}
        isFirstMove = True
        #           value, g, currentState, firstMoveThatLedToThisState, parent
        curTuple = (0, initialGame, initialGame, None)

        while True:
            value, curGame, firstMoveInPath, parent = curTuple
            if (self.isGoalState(curGame, initialGame)):
                ansGame = firstMoveInPath
                break
            adjacentStates = self.adjacentStates(curGame)
            if (parent != None):
                adjacentStates.append(parent)
            for nextGameState in adjacentStates:
                value: int
                newG: int
                if (nextGameState in visited.keys()):
                    value = visited[nextGameState][0] + 1

                else:
                    newG = 1
                    value = (newG + self.calculateHeuristic(nextGameState))
                if (isFirstMove):
                    pq.put((value, nextGameState, nextGameState, curGame))
                else:
                    pq.put((value, nextGameState, firstMoveInPath, curGame))

            frontTuple = pq.get()
            curState = frontTuple[1]
            visited[curState] = pq.get()
            curTuple = frontTuple
            isFirstMove = False
        return ansGame