|
@@ -5,12 +5,14 @@ public class Map
|
|
|
private readonly Tile[,] _tiles;
|
|
private readonly Tile[,] _tiles;
|
|
|
private readonly int _width;
|
|
private readonly int _width;
|
|
|
private readonly int _height;
|
|
private readonly int _height;
|
|
|
|
|
+ private Func<Path?, Tile, Vec, Path> _pathFactory;
|
|
|
|
|
|
|
|
public Map(Tile[,] tiles, int width, int height)
|
|
public Map(Tile[,] tiles, int width, int height)
|
|
|
{
|
|
{
|
|
|
_tiles = tiles;
|
|
_tiles = tiles;
|
|
|
_width = width;
|
|
_width = width;
|
|
|
_height = height;
|
|
_height = height;
|
|
|
|
|
+ _pathFactory = (path, tile, vRun) => new Path(path, tile, vRun);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public void Print()
|
|
public void Print()
|
|
@@ -20,6 +22,7 @@ public class Map
|
|
|
for (var x = 0; x < _width; x++)
|
|
for (var x = 0; x < _width; x++)
|
|
|
{
|
|
{
|
|
|
Console.Write(_tiles[x, y].Display);
|
|
Console.Write(_tiles[x, y].Display);
|
|
|
|
|
+ _tiles[x, y].ClearDisplay();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
Console.WriteLine();
|
|
Console.WriteLine();
|
|
@@ -30,12 +33,11 @@ public class Map
|
|
|
|
|
|
|
|
public int ShortestPath()
|
|
public int ShortestPath()
|
|
|
{
|
|
{
|
|
|
- var n = 0;
|
|
|
|
|
var goal = _tiles[_width - 1, _height - 1];
|
|
var goal = _tiles[_width - 1, _height - 1];
|
|
|
var visited = new HashSet<string>();
|
|
var visited = new HashSet<string>();
|
|
|
var queue = new PriorityQueue<Path, int>();
|
|
var queue = new PriorityQueue<Path, int>();
|
|
|
- queue.Enqueue(new Path(null, _tiles[0, 0], new Vec(1, 0)), 0);
|
|
|
|
|
- queue.Enqueue(new Path(null, _tiles[0, 0], new Vec(0, 1)), 0);
|
|
|
|
|
|
|
+ queue.Enqueue(_pathFactory(null, _tiles[0, 0], new Vec(1, 0)), 0);
|
|
|
|
|
+ queue.Enqueue(_pathFactory(null, _tiles[0, 0], new Vec(0, 1)), 0);
|
|
|
|
|
|
|
|
while (queue.Count > 0)
|
|
while (queue.Count > 0)
|
|
|
{
|
|
{
|
|
@@ -51,8 +53,8 @@ public class Map
|
|
|
var pos = current.Tile.Position.Add(v);
|
|
var pos = current.Tile.Position.Add(v);
|
|
|
if (IsValid(pos))
|
|
if (IsValid(pos))
|
|
|
{
|
|
{
|
|
|
- var next = new Path(current, Tile(pos), v);
|
|
|
|
|
- if (next.Tile == goal)
|
|
|
|
|
|
|
+ var next = _pathFactory(current, Tile(pos), v);
|
|
|
|
|
+ if (next.Tile == goal && next.CanStop())
|
|
|
{
|
|
{
|
|
|
var cost = Backtrack(next);
|
|
var cost = Backtrack(next);
|
|
|
return cost;
|
|
return cost;
|
|
@@ -60,17 +62,16 @@ public class Map
|
|
|
queue.Enqueue(next, next.Cost);
|
|
queue.Enqueue(next, next.Cost);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- if (n % 100 == 0)
|
|
|
|
|
- {
|
|
|
|
|
- Console.WriteLine($"Queue: {queue.Count}");
|
|
|
|
|
- }
|
|
|
|
|
- n++;
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
return -1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ public void SetUltraPathMode()
|
|
|
|
|
+ {
|
|
|
|
|
+ _pathFactory = (path, tile, vRun) => new UltraPath(path, tile, vRun);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
private int Backtrack(Path path)
|
|
private int Backtrack(Path path)
|
|
|
{
|
|
{
|
|
|
var sum = 0;
|
|
var sum = 0;
|