diff --git a/Tests/TwoDimension/AreaTests.cs b/Tests/TwoDimension/AreaTests.cs index fa861bb..7a094d5 100644 --- a/Tests/TwoDimension/AreaTests.cs +++ b/Tests/TwoDimension/AreaTests.cs @@ -170,15 +170,36 @@ public void PositionSet() [Test] public void PositionGet() { - // TODO: Issue 6 (https://github.com/Wizcorp/TileSystem/issues/6) - Assert.Fail(); - } + // TODO: Issue 6 (https://github.com/Wizcorp/TileSystem/issues/6) + Area area = new Area(); + var mockPosition = new Mock(); + + // Test Null + Assert.That(() => area.Get(null), Throws.ArgumentNullException); + + /* + * Test position type + * Method is assuming IPosition2D, can be used if another type can be expected + * Can also be used for Issue 13 (https://github.com/Wizcorp/TileSystem/issues/13) + */ + //Assert.IsInstanceOf(mockPosition.Object); + + // Test Get Position Works + Assert.That(() => area.Get(mockPosition.Object), Throws.Nothing); + } [Test] public void PositionGetNeighbours() { - // TODO: Issue 6 (https://github.com/Wizcorp/TileSystem/issues/6) - Assert.Fail(); - } - } + // TODO: Issue 6 (https://github.com/Wizcorp/TileSystem/issues/6) + Area area = new Area(); + var mockTile = new Mock(); + + // Test Null + Assert.That(() => area.GetNeighbours(null), Throws.ArgumentNullException); + + // Test Get Neighbours Works + Assert.That(() => area.GetNeighbours(mockTile.Object), Throws.Nothing); + } + } } diff --git a/TileSystem/Implementation/TwoDimension/Area.cs b/TileSystem/Implementation/TwoDimension/Area.cs index 729705f..f26a7bd 100644 --- a/TileSystem/Implementation/TwoDimension/Area.cs +++ b/TileSystem/Implementation/TwoDimension/Area.cs @@ -1,6 +1,6 @@ using System; using System.Collections.Generic; - +using System.Linq; using TileSystem.Interfaces.Base; using TileSystem.Interfaces.Management; using TileSystem.Interfaces.TwoDimension; @@ -141,8 +141,14 @@ public virtual bool Remove(ITile tile) /// Tile instance or null public ITile Get(IPosition position) { - // TODO: Issue 6 (https://github.com/Wizcorp/TileSystem/issues/6) - throw new NotImplementedException(); + // TODO: Issue 6 (https://github.com/Wizcorp/TileSystem/issues/6) + //Assuming position is IPosition2D + var currentPosition = position as IPosition2D; + + return tiles?//Using Linq to simplify null check, foreach loop could be used instead. + .FirstOrDefault(tile => + (tile.Position as IPosition2D).X == currentPosition.X + && (tile.Position as IPosition2D).Y == currentPosition.Y); } /// @@ -152,9 +158,30 @@ public ITile Get(IPosition position) /// List of neighbours or null public List GetNeighbours(ITile tile) { - // TODO: Issue 6 (https://github.com/Wizcorp/TileSystem/issues/6) - throw new NotImplementedException(); - } + // TODO: Issue 6 (https://github.com/Wizcorp/TileSystem/issues/6) + List result = new List(); + + //Assuming position is IPosition2D + var currentTilePosition = tile.Position as IPosition2D; + + var maxRow = currentTilePosition.Y + 1; + var minRow = currentTilePosition.Y - 1; + + var maxColumn = currentTilePosition.X + 1; + var minColumn = currentTilePosition.X - 1; + + for (int row = minRow; row <= maxRow; row++) + { + for (int column = minColumn; column <= maxColumn; column++) + { + if (row == currentTilePosition.Y && column == currentTilePosition.X) + continue; + + result.Add(this.Get(new Position2D(column, row))); + } + } + return result.Any() ? result : null; + } /// /// Destroy this area and emit the event