Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 6 Added Area -> Get and GetNeighbours methods and test #19

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions Tests/TwoDimension/AreaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IPosition2D>();

// 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<IPosition2D>(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<ITile>();

// Test Null
Assert.That(() => area.GetNeighbours(null), Throws.ArgumentNullException);

// Test Get Neighbours Works
Assert.That(() => area.GetNeighbours(mockTile.Object), Throws.Nothing);
}
}
}
39 changes: 33 additions & 6 deletions TileSystem/Implementation/TwoDimension/Area.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -141,8 +141,14 @@ public virtual bool Remove(ITile tile)
/// <returns>Tile instance or null</returns>
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);
}

/// <summary>
Expand All @@ -152,9 +158,30 @@ public ITile Get(IPosition position)
/// <returns>List of neighbours or null</returns>
public List<ITile> 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<ITile> result = new List<ITile>();

//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;
}

/// <summary>
/// Destroy this area and emit the event
Expand Down