Skip to content

Commit

Permalink
v1.4.14 add exclusions to GetAdjacentCells()
Browse files Browse the repository at this point in the history
  • Loading branch information
smabuk committed Dec 17, 2023
1 parent 6f9f2f2 commit bb9f478
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
20 changes: 12 additions & 8 deletions src/Smab.Helpers/GridHelpers/Walk.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Smab.Helpers;
using System.Data;

namespace Smab.Helpers;

public static partial class ArrayHelpers {
/// <summary>
Expand All @@ -24,7 +26,7 @@ public static IEnumerable<Cell<T>> Walk2dArrayWithValues<T>(this T[,] array) {



public static IEnumerable<Cell<T>> GetAdjacentCells<T>(this T[,] array, int x, int y, bool includeDiagonals = false) {
public static IEnumerable<Cell<T>> GetAdjacentCells<T>(this T[,] array, int x, int y, bool includeDiagonals = false, IEnumerable<(int dX, int dY)>? exclude = null) {
int cols = array.GetUpperBound(0);
int rows = array.GetUpperBound(1);

Expand All @@ -34,14 +36,16 @@ public static IEnumerable<Cell<T>> GetAdjacentCells<T>(this T[,] array, int x, i
};

foreach ((int dX, int dY) in DIRECTIONS) {
int newX = x + dX;
int newY = y + dY;
if (newX >= 0 && newX <= cols && newY >= 0 && newY <= rows) {
yield return new(newX, newY, array[newX, newY]);
if (exclude is null || !exclude.Contains((dX, dY))) {
int newX = x + dX;
int newY = y + dY;
if (array.InBounds(newX, newY)) {
yield return new(newX, newY, array[newX, newY]);
}
}
}
}

public static IEnumerable<Cell<T>> GetAdjacentCells<T>(this T[,] array, (int x, int y) point, bool includeDiagonals = false)
=> GetAdjacentCells<T>(array, point.x, point.y, includeDiagonals);
public static IEnumerable<Cell<T>> GetAdjacentCells<T>(this T[,] array, (int x, int y) point, bool includeDiagonals = false, IEnumerable<(int dX, int dY)>? exclude = null)
=> GetAdjacentCells<T>(array, point.x, point.y, includeDiagonals, exclude);
}
2 changes: 1 addition & 1 deletion src/Smab.Helpers/Smab.Helpers.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<PropertyGroup>
<PackageReleaseNotes>.NET 8.0</PackageReleaseNotes>
<VersionPrefix>1.4.13</VersionPrefix>
<VersionPrefix>1.4.14</VersionPrefix>
<Preview></Preview>
<VersionSuffix Condition="'$(Preview)' != '' And '$(BUILD_BUILDNUMBER)' == ''">$(Preview).$([System.DateTime]::get_Now().get_Year())$([System.DateTime]::get_Now().get_Month().ToString("D2"))$([System.DateTime]::get_Now().get_Day().ToString("D2"))-$([System.DateTime]::get_Now().get_Hour().ToString("D2"))$([System.DateTime]::get_Now().get_Minute().ToString("D2"))</VersionSuffix>
<VersionSuffix Condition="'$(Preview)' != '' And '$(BUILD_BUILDNUMBER)' != ''">$(Preview).$(BUILD_BUILDNUMBER)</VersionSuffix>
Expand Down
16 changes: 15 additions & 1 deletion test/Smab.Helpers.Test/HelperMethodTests/ArrayHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,21 @@ public void GetAdjacentCells_Should_Have(int X, int Y, bool includeDiagonals, in
}
(char, int)[,] array = input.To2dArray<(char, int)>(5);
var points = array.GetAdjacentCells((X, Y), includeDiagonals: includeDiagonals);
Assert.Equal(points.Count(), expected);
points.Count().ShouldBe(expected);
}

[Theory]
[InlineData(0, 0, false, 1)]
[InlineData(1, 1, false, 3)]
[InlineData(1, 1, true, 6)]
public void GetAdjacentCells_Should_Not_Have(int X, int Y, bool includeDiagonals, int expected) {
(char, int)[] input = new (char, int)[26];
for (int i = 0; i < input.GetUpperBound(0); i++) {
input[i] = new((char)(65 + i), i + 1);
}
(char, int)[,] array = input.To2dArray(5);
var points = array.GetAdjacentCells((X, Y), includeDiagonals: includeDiagonals, exclude: [ArrayHelpers.RIGHT, ArrayHelpers.NORTH_EAST]);
points.Count().ShouldBe(expected);
}

[Theory]
Expand Down

0 comments on commit bb9f478

Please sign in to comment.