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

Implemented features found in issues #6 #18

Open
wants to merge 7 commits 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
56 changes: 52 additions & 4 deletions Tests/TwoDimension/AreaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,63 @@ public void PositionSet()
[Test]
public void PositionGet()
{
// TODO: Issue 6 (https://github.com/Wizcorp/TileSystem/issues/6)
Assert.Fail();
Area area = new Area();
var mockTile = new Mock<Tile>();
var tilePosition = new Position2D(0, 0);
var tilePositionNotFound = new Position2D(1, 0);

mockTile.Object.SetPosition(area, tilePosition);
area.Add(mockTile.Object);

// Test Nulls
Assert.That(() => area.Get(null), Throws.ArgumentNullException);

// Test Same Tile
Assert.AreSame(mockTile.Object, area.Get(tilePosition));

// Test Tile Not Found
Assert.AreNotSame(mockTile.Object, area.Get(tilePositionNotFound));
}

[Test]
public void PositionGetNeighbours()
{
// TODO: Issue 6 (https://github.com/Wizcorp/TileSystem/issues/6)
Assert.Fail();
Area area = new Area();
var mockTile = new Mock<Tile>();
var tilePosition = new Position2D(0, 0);
var mockTileNotInArea = new Mock<Tile>();
var mockFirstNeighbourTile = new Mock<Tile>();
var firstNeighbourTilePosition = new Position2D(1, 0);
var mockSecondNeighbourTile = new Mock<Tile>();
var secondNeighbourTilePosition = new Position2D(1, 1);
var mockNotANeighbourTile = new Mock<Tile>();
var notANeighbourTilePosition = new Position2D(4, 0);

mockTile.Object.SetPosition(area, tilePosition);
area.Add(mockTile.Object);

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

// Test Tile Not In Area
Assert.That(() => area.GetNeighbours(mockTileNotInArea.Object), Throws.ArgumentException);

// Test No Neighbours
Assert.Null(area.GetNeighbours(mockTile.Object));

// Test One Or More Neighbours
mockFirstNeighbourTile.Object.SetPosition(area, firstNeighbourTilePosition);
area.Add(mockFirstNeighbourTile.Object);
mockSecondNeighbourTile.Object.SetPosition(area, secondNeighbourTilePosition);
area.Add(mockSecondNeighbourTile.Object);
mockNotANeighbourTile.Object.SetPosition(area, notANeighbourTilePosition);
area.Add(mockNotANeighbourTile.Object);

var neighbourTiles = area.GetNeighbours(mockTile.Object);

Assert.IsTrue(neighbourTiles.Contains(mockFirstNeighbourTile.Object));
Assert.IsTrue(neighbourTiles.Contains(mockSecondNeighbourTile.Object));
Assert.IsFalse(neighbourTiles.Contains(mockNotANeighbourTile.Object));
}
}
}
32 changes: 28 additions & 4 deletions TileSystem/Implementation/TwoDimension/Area.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,12 @@ 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();
if (position == null)
{
throw new ArgumentNullException("position", "Position can not be null");
}

return tiles.Find(tile => tile.Position.CompareTo(position) == 0);
}

/// <summary>
Expand All @@ -152,8 +156,28 @@ 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();
if (tile == null)
{
throw new ArgumentNullException("tile", "Tile can not be null");
}

if (tiles.Contains(tile) == false)
{
throw new ArgumentException("Tile not in Area", "tile");
}

List<ITile> neigbourTiles = tiles.FindAll(t =>
Math.Abs(((Position2D)t.Position).X - ((Position2D)tile.Position).X) == 1 ||
Math.Abs(((Position2D)t.Position).Y - ((Position2D)tile.Position).Y) == 1);

if (neigbourTiles.Count <= 0)
{
return null;
}
else
{
return neigbourTiles;
}
}

/// <summary>
Expand Down