From 24a30137746506540dd107e379302178aa52f486 Mon Sep 17 00:00:00 2001 From: Artem Votintsev Date: Sun, 19 May 2024 22:22:24 +0200 Subject: [PATCH] Add negative borders for grid graph --- BrainAI.Tests/GridGraphTest.cs | 113 ++++++++++++++++++++++++ BrainAI/Pathfinding/Graphs/GridGraph.cs | 20 +++-- 2 files changed, 126 insertions(+), 7 deletions(-) create mode 100644 BrainAI.Tests/GridGraphTest.cs diff --git a/BrainAI.Tests/GridGraphTest.cs b/BrainAI.Tests/GridGraphTest.cs new file mode 100644 index 0000000..b0c596c --- /dev/null +++ b/BrainAI.Tests/GridGraphTest.cs @@ -0,0 +1,113 @@ +using System.Collections.Generic; +using System.Linq; +using NUnit.Framework; + +namespace BrainAI.Pathfinding +{ + [TestFixture] + public class GridGraphTest + { + [Test] + public void GetNeighboursForTopLeftCorner_WidthHeightSet_Two() + { + var graph = new GridGraph(10, 10, false); + var result = new List(); + graph.GetNeighbors(new Point(0, 0), result); + + CollectionAssert.AreEquivalent(new[] { new Point(1, 0), new Point(0, 1) }, result); + } + + [Test] + public void GetNeighboursForOutsideTopLeftCorner_WidthHeightSet_None() + { + var graph = new GridGraph(10, 10, false); + var result = new List(); + graph.GetNeighbors(new Point(-1, -1), result); + + CollectionAssert.AreEquivalent(new Point[] { }, result); + } + + + [Test] + public void GetNeighboursForBottomRightCorner_WidthHeightSet_Two() + { + var graph = new GridGraph(10, 10, false); + var result = new List(); + graph.GetNeighbors(new Point(9, 9), result); + + CollectionAssert.AreEquivalent(new[] { new Point(9, 8), new Point(8, 9) }, result); + } + + [Test] + public void GetNeighboursForOutsideBottomRight_WidthHeightSet_None() + { + var graph = new GridGraph(10, 10, false); + var result = new List(); + graph.GetNeighbors(new Point(10, 10), result); + + CollectionAssert.AreEquivalent(new Point[] { }, result); + } + + [Test] + public void GetNeighboursForInside_WidthHeightSet_None() + { + var graph = new GridGraph(10, 10, false); + var result = new List(); + graph.GetNeighbors(new Point(5, 5), result); + + CollectionAssert.AreEquivalent(new[] { new Point(4, 5), new Point(5, 4), new Point(5, 6), new Point(6, 5) }, result); + } + + + [Test] + public void GetNeighboursForTopLeftCorner_BordersSet_Two() + { + var graph = new GridGraph(-10, -10, 10, 10, false); + var result = new List(); + graph.GetNeighbors(new Point(-10, -10), result); + + CollectionAssert.AreEquivalent(new[] { new Point(-10, -9), new Point(-9, -10) }, result); + } + + [Test] + public void GetNeighboursForOutsideTopLeftCorner_BordersSet_None() + { + var graph = new GridGraph(-10, -10, 10, 10, false); + var result = new List(); + graph.GetNeighbors(new Point(-11, -11), result); + + CollectionAssert.AreEquivalent(new Point[] { }, result); + } + + + [Test] + public void GetNeighboursForBottomRightCorner_BordersSet_Two() + { + var graph = new GridGraph(-10, -10, 10, 10, false); + var result = new List(); + graph.GetNeighbors(new Point(10, 10), result); + + CollectionAssert.AreEquivalent(new[] { new Point(9, 10), new Point(10, 9) }, result); + } + + [Test] + public void GetNeighboursForOutsideBottomRight_BordersSet_None() + { + var graph = new GridGraph(-10, -10, 10, 10, false); + var result = new List(); + graph.GetNeighbors(new Point(11, 11), result); + + CollectionAssert.AreEquivalent(new Point[] { }, result); + } + + [Test] + public void GetNeighboursForInside_BordersSet_None() + { + var graph = new GridGraph(-10, -10, 10, 10, false); + var result = new List(); + graph.GetNeighbors(new Point(0, 0), result); + + CollectionAssert.AreEquivalent(new[] { new Point(-1, 0), new Point(1, 0), new Point(0, -1), new Point(0, 1) }, result); + } + } +} diff --git a/BrainAI/Pathfinding/Graphs/GridGraph.cs b/BrainAI/Pathfinding/Graphs/GridGraph.cs index f11059c..a128389 100644 --- a/BrainAI/Pathfinding/Graphs/GridGraph.cs +++ b/BrainAI/Pathfinding/Graphs/GridGraph.cs @@ -31,14 +31,20 @@ public class GridGraph : IAstarGraph public readonly Dictionary Weights = new Dictionary(); public int DefaultWeight = 1; - public readonly int Width, Height; + public readonly int Left, Top, Right, Bottom; private readonly Point[] dirs; - public GridGraph(int width, int height, bool allowDiagonalSearch = false) + public GridGraph(int width, int height, bool allowDiagonalSearch = false) : this(0, 0, width - 1, height - 1, allowDiagonalSearch) { - this.Width = width; - this.Height = height; + } + + public GridGraph(int left, int top, int right, int bottom, bool allowDiagonalSearch = false) + { + this.Left = Math.Min(left, right); + this.Top = Math.Min(top, bottom); + this.Right = Math.Max(left, right); + this.Bottom = Math.Max(top, bottom); this.dirs = allowDiagonalSearch ? CompassDirs : CardinalDirs; } @@ -68,7 +74,7 @@ public int Heuristic(Point node, Point goal) private bool IsNodeInBounds(Point node) { - return 0 <= node.X && node.X < this.Width && 0 <= node.Y && node.Y < this.Height; + return this.Left <= node.X && node.X <= this.Right && this.Top <= node.Y && node.Y <= this.Bottom; } private bool IsNodePassable(Point node) @@ -82,9 +88,9 @@ public override string ToString() { sb.Clear(); - for (var y = 0; y < this.Height; y++) + for (var y = Left; y <= this.Right; y++) { - for (var x = 0; x < this.Width; x++) + for (var x = Top; x <= this.Bottom; x++) { var pos = new Point(x, y); var isWall = this.Walls.Contains(pos);