public void TestOn5x5MapWithAlmostWall() { byte[,] map = new byte[5, 5] { { 1, 2, 0, 6, 7 }, { 3, 4, 3, 4, 6 }, { 5, 6, 0, 6, 2 }, { 7, 8, 0, 2, 6 }, { 7, 8, 0, 2, 6 }, }; var start = new Location(0, 0); var finish = new Location(4, 4); var pathFinder = new DijkstraPathFinder(); var path = pathFinder.Find(map, start, finish).ToList(); var result = new List <Location>() { new Location(3, 4), new Location(3, 3), new Location(2, 3), new Location(1, 3), new Location(1, 2), new Location(1, 1), new Location(1, 0), new Location(0, 0), }; Assert.Equal(result, path); }
public void TestOn3x3Map() { byte[,] map = new byte[3, 3] { { 1, 2, 3, }, { 3, 4, 2, }, { 5, 6, 5, }, }; var start = new Location(0, 0); var finish = new Location(2, 2); var pathFinder = new DijkstraPathFinder(); var path = pathFinder.Find(map, start, finish).ToList(); var result = new List <Location>() { new Location(2, 1), new Location(2, 0), new Location(1, 0), new Location(0, 0), }; Assert.Equal(result, path); }
public Location[] FindShortestWay(Location start, Location finish) { // TODO: In real project we could use DI. var pathFinder = new DijkstraPathFinder(); // var pathFinder = new AStarPathFinder(); return(pathFinder.Find(Map, start, finish)); }
public void TestPathfinderDynamicChangeCostOfMove() { DijkstraPathFinder pathFinder = new DijkstraPathFinder(new byte[, ] { { 1, 1, 1, 1 }, { 1, 1, 1, 1 }, { 1, 1, 1, 1 }, { 1, 1, 1, 1 } }); pathFinder.ChangeCostOfMove(1, 1, 0); pathFinder.ChangeCostOfMove(2, 2, 0); var movementInfo = pathFinder.GetHexesInMovementRange(new Point(0, 0), 8); var result = pathFinder.Find(new Point(0, 0), new Point(3, 3), movementInfo); Assert.IsNotNull(result); Assert.IsNotEmpty(result); Assert.AreEqual(5, result.Count); }
public void TestOn5x5MapWithWall() { byte[,] map = new byte[5, 5] { { 1, 2, 0, 6, 7 }, { 3, 4, 0, 4, 6 }, { 5, 6, 0, 6, 2 }, { 7, 8, 0, 2, 6 }, { 7, 8, 0, 2, 6 }, }; var start = new Location(0, 0); var finish = new Location(4, 4); var pathFinder = new DijkstraPathFinder(); var path = pathFinder.Find(map, start, finish)?.ToList(); Assert.Null(path); }
public void TestPathfinder() { DijkstraPathFinder pathFinder = new DijkstraPathFinder(new byte[, ] { { 1, 1, 1, 1, 1, 1, 1, 1 }, { 1, 1, 1, 1, 1, 1, 1, 1 }, { 1, 1, 1, 1, 1, 1, 1, 1 }, { 1, 1, 1, 1, 1, 1, 1, 1 }, { 1, 1, 1, 1, 1, 1, 1, 1 }, { 1, 1, 1, 1, 1, 1, 1, 1 }, { 1, 1, 1, 1, 1, 1, 1, 1 }, { 1, 1, 1, 1, 1, 1, 1, 1 } }); var result = pathFinder.GetHexesInMovementRange(new Point(4, 4), 3); Assert.IsNotNull(result); Assert.IsNotEmpty(result.tileReturnPath); // Assert.AreEqual(4, result.Count); var pathfindingResult = pathFinder.Find(new Point(4, 4), new Point(6, 2), result); }
private void Show(Pos start, Pos goal, int[][] cost) { var objs = GameObject.FindGameObjectsWithTag("Unit"); foreach (var o in objs) { Destroy(o); } // ----- BFS ----- var gridGraph = new GridGraph(16, 16, cost); IPathFinder pathFinder = new BfsPathFinder(); var path = pathFinder.Find(gridGraph, start, goal); Show(gridGraph, new Vector2(0, 0), Color.white, path); var sumCost = 0; foreach (var p in path) { sumCost += cost[p.X][p.Y]; } if (sumCost == 0) { Debug.LogError("BFS 没有找到路径"); } else { Debug.LogError($"BFS 总成本: {sumCost}, 总步数: {path.Count}"); } // ----- Dijkstra ----- pathFinder = new DijkstraPathFinder(); path = pathFinder.Find(gridGraph, start, goal); Show(gridGraph, new Vector2(10, 0), Color.yellow, path); sumCost = 0; foreach (var p in path) { sumCost += cost[p.X][p.Y]; } if (sumCost == 0) { Debug.LogError("Dijkstra 没有找到路径"); } else { Debug.LogError($"Dijkstra 总成本: {sumCost}, 总步数: {path.Count}"); } // ----- GreedyBestFirst ----- pathFinder = new GreedyBestFirstPathFinder(); path = pathFinder.Find(gridGraph, start, goal); Show(gridGraph, new Vector2(0, 10), Color.grey, path); sumCost = 0; foreach (var p in path) { sumCost += cost[p.X][p.Y]; } if (sumCost == 0) { Debug.LogError("GreedyBestFirst 没有找到路径"); } else { Debug.LogError($"GreedyBestFirst 总成本: {sumCost}, 总步数: {path.Count}"); } // ----- AStar ----- pathFinder = new AStarPathFinder(); path = pathFinder.Find(gridGraph, start, goal); Show(gridGraph, new Vector2(10, 10), Color.magenta, path); sumCost = 0; foreach (var p in path) { sumCost += cost[p.X][p.Y]; } if (sumCost == 0) { Debug.LogError("AStar 没有找到路径"); } else { Debug.LogError($"AStar 总成本: {sumCost}, 总步数: {path.Count}"); } }