Skip to content

On names and parameter orders of coodinates #50

@XinyiLiu577086410

Description

@XinyiLiu577086410

Hi,
It is my first time to use python-pathfinding, it's nice but sth make me confused...
To be brief, I am writing a bot to play a game, this is what I try to do tonight:

   def ParseMap(map:List[Map]) -> (List[List[Map]], List[List[List[tuple]]], tuple):
    parsedMap = [[Map() for i in range(MapEdgeLength)] for j in range(MapEdgeLength)]
    paths = [[[] for i in range(MapEdgeLength)] for j in range(MapEdgeLength)]
    accessableNow = [[1 for i in range(MapEdgeLength)] for j in range(MapEdgeLength)]
    playerPosition = None
    for grid in map:
        parsedMap[grid.x][grid.y] = grid
        for obj in grid.objs:
            if obj.type == ObjType.Player and obj.property.player_id != gContext["playerID"] and playerPosition is None:
                playerPosition = (grid.x, grid.y)
            if obj.type == ObjType.Block or obj.type == ObjType.Bomb:
                accessableNow[grid.x][grid.y] = 0 # (a)
    # pfGrid: an abbreviation of pathfinding grid
    pfGrid = Grid(matrix=accessableNow)
    for grid in map:
        pfGrid.cleanup()
        finder = AStarFinder(diagonal_movement=DiagonalMovement.never)
        newPath, _ = finder.find_path(pfGrid.node(playerPosition[0], playerPosition[1]),
                                        pfGrid.node(grid.x, grid.y, pfGrid) #(b)
        myNewPath = [(newPath[i].x, newPath[i].y) for i in range(len(newPath))]
        paths[grid.x][grid.y] = myNewPath
    return parsedMap, paths, playerPosition

Some names of variables in the code above are provided by the organizer of the contest I am taking part in.
My code above failed to get correct answer until I rewite (a) as:

 accessableNow[grid.y][grid.x] = 0

or rewrite (b) as:

 newPath, _ = finder.find_path(pfGrid.node(playerPosition[1], playerPosition[0]),
                                        pfGrid.node(grid.y, grid.x, pfGrid) #(b)

Later, I refer to the docs but find nothing about parameter order.
As I look into the grid.py i found:

       def node(self, x, y) -> GridNode:
        """
            get node at position
            :param x: x pos
            :param y: y pos
            :return:
        """
        return self.nodes[y][x]

Below is what I suggest: Rearrangeing parameters x and y as the order they appear in the List indexes:

       def node(self, y, x) -> GridNode:
           return self.nodes[y][x]

or rename them to the form xi where i is the order they appear in the indexes:

       def node(self, x1, x2) -> GridNode:
           return self.nodes[x1][x2]

so that People who use the pathfinding can access a node not according to a specific name convention, but the order they pass parameters to these functions before.

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issueGood for newcomershelp wantedExtra attention is neededquestionFurther information is requested

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions