Skip to content

Commit

Permalink
Add negative borders for grid graph
Browse files Browse the repository at this point in the history
  • Loading branch information
ApmeM committed May 19, 2024
1 parent 58653c7 commit 24a3013
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 7 deletions.
113 changes: 113 additions & 0 deletions BrainAI.Tests/GridGraphTest.cs
Original file line number Diff line number Diff line change
@@ -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<Point>();
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<Point>();
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<Point>();
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<Point>();
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<Point>();
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<Point>();
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<Point>();
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<Point>();
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<Point>();
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<Point>();
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);
}
}
}
20 changes: 13 additions & 7 deletions BrainAI/Pathfinding/Graphs/GridGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,20 @@ public class GridGraph : IAstarGraph<Point>
public readonly Dictionary<Point, int> Weights = new Dictionary<Point, int>();
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;
}

Expand Down Expand Up @@ -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)
Expand All @@ -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);
Expand Down

0 comments on commit 24a3013

Please sign in to comment.