コード例 #1
0
ファイル: main.py プロジェクト: imran443/Pathfinding
def main():
    continueProcessing = 'Y'

    while (continueProcessing == 'y' or continueProcessing == 'Y'):
        # Creates and return the string and object repersentation of the map.
        gameMap, gameMapInternal = createMap()

        # The actual start and end nodes.
        startNode = gameMapInternal[startNodeXY[0]][startNodeXY[1]]
        endNode = gameMapInternal[endNodeXY[0]][endNodeXY[1]]

        algoChoice = input(
            "Choose which algorithm to run? (D - Dijkstra, A - A Star, J - Jump Point Search): "
        )

        # Used for recording time for each algorithm run.
        startTime = datetime.datetime.now()

        nodesExpanded = 0
        displayGame = None

        if (algoChoice == 'D' or algoChoice == 'd'):
            # Each tuple added to the queue will have a second value as a counter to break ties.
            dijkstra = Dijkstra((startNode.f, 0, startNode), endNode)
            dijkstraMap, nodesExpanded = dijkstra.dijkstraAlgo(
                gameMapInternal, gameMap)
            displayPath = Display(dijkstraMap)
            displayPath.createMap()
            printMap(dijkstraMap)

            # Prints results to file.
            # f.write("Dijkstra: \n")
            # f.write("Beginning position: " + str(startNodeXY[0]) + "," + str(startNodeXY[1]) + " Ending position: " + str(endNodeXY[0]) + ", " + str(endNodeXY[1]) + "\n")
            # f.write("Running on file: " + fileName + "\n")

        elif (algoChoice == 'A' or algoChoice == 'a'):
            # This sets up the heuristic values for AStar.
            setHeuristicVals(gameMapInternal, endNode)
            aStar = AStar((startNode.h, 0, startNode), endNode)
            aStarMap, nodesExpanded = aStar.aStarAlgo(gameMapInternal, gameMap)
            displayPath = Display(aStarMap)
            displayPath.createMap()
            printMap(aStarMap)

            # f.write("A-Star: \n")
            # f.write("Beginning position: " + str(startNodeXY[0]) + "," + str(startNodeXY[1]) + " Ending position: " + str(endNodeXY[0]) + ", " + str(endNodeXY[1]) + "\n")
            # f.write("Running on file: " + fileName + "\n")

        elif (algoChoice == 'J' or algoChoice == 'j'):
            jps = JPS(startNode, endNode)
            jpsMap, nodesExpanded = jps.JPSAlgo(gameMapInternal, gameMap)
            displayPath = Display(jpsMap)
            displayPath.createMap()
            printMap(jpsMap)
            # f.write("JPS: \n")
            # f.write("Beginning position: " + str(startNodeXY[0]) + "," + str(startNodeXY[1]) + " Ending position: " + str(endNodeXY[0]) + ", " + str(endNodeXY[1]) + "\n")
            # f.write("Running on file: " + fileName + "\n")

        endTime = datetime.datetime.now()

        timeElapsed = endTime - startTime

        print("Time: ", timeElapsed)
        # f.write("Time: " + str(timeElapsed) + "\n")

        print("Nodes searched: ", nodesExpanded)
        # f.write("Nodes searched: " + str(nodesExpanded) + "\n")
        # f.write("------------------------------------------------------------------ \n")

        continueProcessing = input("Would you like to continue? (Y or N): ")