Transform Image into Map to use with Pathfinder Algorithm

Asked

Viewed 8 times

0

I want to develop a kind of AI that will go a shorter way (Walkable part is just where it is gray) on the map and for that I will use the Pathfinder algorithm.

For this I am using the following algorithm:

https://dotnetcoretutorials.com/2020/07/25/a-search-pathfinding-algorithm-in-c/

static void Main(string[] args)
{
    List<string> map = new List<string>
    {
        "A          ",
        "--| |------",
        "           ",
        "   |-----| ",
        "   |     | ",
        "---|     |B"
    };

    var start = new Tile();
    start.Y = map.FindIndex(x => x.Contains("A"));
    start.X = map[start.Y].IndexOf("A");

    var finish = new Tile();
    finish.Y = map.FindIndex(x => x.Contains("B"));
    finish.X = map[finish.Y].IndexOf("B");
    
    start.SetDistance(finish.X, finish.Y);

    var activeTiles = new List<Tile>();
    activeTiles.Add(start);
    var visitedTiles = new List<Tile>();
}

private static List<Tile> GetWalkableTiles(List<string> map, Tile currentTile, Tile targetTile)
{
    var possibleTiles = new List<Tile>()
    {
        new Tile { X = currentTile.X, Y = currentTile.Y - 1, Parent = currentTile, Cost = currentTile.Cost + 1 },
        new Tile { X = currentTile.X, Y = currentTile.Y + 1, Parent = currentTile, Cost = currentTile.Cost + 1},
        new Tile { X = currentTile.X - 1, Y = currentTile.Y, Parent = currentTile, Cost = currentTile.Cost + 1 },
        new Tile { X = currentTile.X + 1, Y = currentTile.Y, Parent = currentTile, Cost = currentTile.Cost + 1 },
    };

    possibleTiles.ForEach(tile => tile.SetDistance(targetTile.X, targetTile.Y));

    var maxX = map.First().Length - 1;
    var maxY = map.Count - 1;

    return possibleTiles
            .Where(tile => tile.X >= 0 && tile.X <= maxX)
            .Where(tile => tile.Y >= 0 && tile.Y <= maxY)
            .Where(tile => map[tile.Y][tile.X] == ' ' || map[tile.Y][tile.X] == 'B')
            .ToList();
}

My question is, how to turn this image into a string list so that my code can traverse the shortest path?

inserir a descrição da imagem aqui

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.