mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-23 05:08:34 +01:00
74 lines
2.7 KiB
C#
74 lines
2.7 KiB
C#
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using Microsoft.Xna.Framework;
|
|||
|
using MLEM.Pathfinding;
|
|||
|
using NUnit.Framework;
|
|||
|
|
|||
|
namespace Tests {
|
|||
|
public class PathfindingTests {
|
|||
|
|
|||
|
[Test]
|
|||
|
public void TestConsistency() {
|
|||
|
var area = new[] {
|
|||
|
"XXXX",
|
|||
|
"X X",
|
|||
|
"X X",
|
|||
|
"XXXX"
|
|||
|
};
|
|||
|
|
|||
|
var noDiagonals = FindPathInArea(new Point(1, 1), new Point(2, 2), area, false).ToArray();
|
|||
|
Assert.AreEqual(noDiagonals.Length, 3);
|
|||
|
Assert.AreEqual(noDiagonals[0], new Point(1, 1));
|
|||
|
Assert.AreEqual(noDiagonals[2], new Point(2, 2));
|
|||
|
|
|||
|
var diagonals = FindPathInArea(new Point(1, 1), new Point(2, 2), area, true).ToArray();
|
|||
|
Assert.AreEqual(diagonals.Length, 2);
|
|||
|
Assert.AreEqual(diagonals[0], new Point(1, 1));
|
|||
|
Assert.AreEqual(diagonals[1], new Point(2, 2));
|
|||
|
}
|
|||
|
|
|||
|
[Test]
|
|||
|
public void TestPathCost() {
|
|||
|
var area = new[] {
|
|||
|
"XXXXXXXX",
|
|||
|
"X 5 X",
|
|||
|
"X 5 X",
|
|||
|
"XXXXXXXX"
|
|||
|
};
|
|||
|
|
|||
|
var firstPath = FindPathInArea(new Point(1, 1), new Point(3, 1), area, false).ToArray();
|
|||
|
var firstExpected = new[] {new Point(1, 1), new Point(1, 2), new Point(2, 2), new Point(3, 2), new Point(3, 1)};
|
|||
|
Assert.AreEqual(firstPath, firstExpected);
|
|||
|
|
|||
|
var secondPath = FindPathInArea(new Point(1, 1), new Point(5, 2), area, false).ToArray();
|
|||
|
var secondExpected = firstExpected.Concat(new[] {new Point(4, 1), new Point(5, 1), new Point(5, 2)}).ToArray();
|
|||
|
Assert.AreEqual(secondPath, secondExpected);
|
|||
|
}
|
|||
|
|
|||
|
[Test]
|
|||
|
public void TestBlocked() {
|
|||
|
var area = new[] {
|
|||
|
"XXXX",
|
|||
|
"X XX",
|
|||
|
"XX X",
|
|||
|
"X X",
|
|||
|
"XXXX"
|
|||
|
};
|
|||
|
// non-diagonal pathfinding should get stuck in the corner
|
|||
|
Assert.IsNull(FindPathInArea(new Point(1, 1), new Point(2, 3), area, false));
|
|||
|
// diagonal pathfinding should be able to cross the diagonal gap
|
|||
|
Assert.IsNotNull(FindPathInArea(new Point(1, 1), new Point(2, 3), area, true));
|
|||
|
}
|
|||
|
|
|||
|
private static Stack<Point> FindPathInArea(Point start, Point end, IEnumerable<string> area, bool allowDiagonals) {
|
|||
|
var costs = area.Select(s => s.Select(c => c switch {
|
|||
|
' ' => 1,
|
|||
|
'X' => AStar2.InfiniteCost,
|
|||
|
_ => (float) char.GetNumericValue(c)
|
|||
|
}).ToArray()).ToArray();
|
|||
|
var pathFinder = new AStar2((p1, p2) => costs[p2.Y][p2.X], allowDiagonals);
|
|||
|
return pathFinder.FindPath(start, end);
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
}
|