From 3578ad05315b77ab2ae4b01e19e72b120ba537f1 Mon Sep 17 00:00:00 2001 From: Radoslaw Kosminski Date: Mon, 20 Aug 2018 23:29:49 +0100 Subject: [PATCH] Implemented Get and GetNeighbours in Level.cs as well as their Tests in LevelTests.cs --- Tests/TwoDimension/LevelTests.cs | 63 +++++++++++++++++-- .../Implementation/TwoDimension/Level.cs | 58 +++++++++++++++-- 2 files changed, 113 insertions(+), 8 deletions(-) diff --git a/Tests/TwoDimension/LevelTests.cs b/Tests/TwoDimension/LevelTests.cs index 641e2c0..4f89ffe 100644 --- a/Tests/TwoDimension/LevelTests.cs +++ b/Tests/TwoDimension/LevelTests.cs @@ -6,6 +6,7 @@ using TileSystem.Interfaces.TwoDimension; using TileSystem.Implementation.TwoDimension; using TileSystem.Interfaces.Creation; +using System.Collections.Generic; namespace Tests.TwoDimension { @@ -112,15 +113,69 @@ public void AreaRemove() [Test] public void PositionGet() { - // TODO: Issue 15 (https://github.com/Wizcorp/TileSystem/issues/15) - Assert.Fail(); + Level level = new Level(); + var mockArea = new Mock(); + + mockArea.Object.SetPosition(level, new Position2D(5, 5)); + + //Add Area to Level + level.Add(mockArea.Object); + + IPosition2D position2D = new Position2D(5, 5); + + //Grab the Area by position + IArea area = level.Get(position2D); + + //Test that area isn't null + Assert.IsNotNull(area); + + //Test that wrong position throws error + IPosition2D wrongPosition2D = new Position2D(3, 2); + Assert.That(() => level.Get(wrongPosition2D), Throws.ArgumentNullException); } [Test] public void PositionGetNeighbours() { - // TODO: Issue 15 (https://github.com/Wizcorp/TileSystem/issues/15) - Assert.Fail(); + Level level = new Level(); + var mockArea1 = new Mock(); + var mockArea2 = new Mock(); + var mockArea3 = new Mock(); + var mockArea4 = new Mock(); + var mockArea5 = new Mock(); + var mockArea6 = new Mock(); + + //3 Areas that will be each other neighbours + mockArea1.Object.SetPosition(level, new Position2D(2, 2)); + mockArea2.Object.SetPosition(level, new Position2D(1, 2)); + mockArea3.Object.SetPosition(level, new Position2D(2, 3)); + + //2 Areas that will be each other neighbours + mockArea4.Object.SetPosition(level, new Position2D(8, 8)); + mockArea5.Object.SetPosition(level, new Position2D(8, 9)); + + //Single Area with no neighbours + mockArea3.Object.SetPosition(level, new Position2D(0, 0)); + + //Add Areas to the level + level.Add(mockArea1.Object); + level.Add(mockArea2.Object); + level.Add(mockArea3.Object); + level.Add(mockArea4.Object); + level.Add(mockArea5.Object); + level.Add(mockArea6.Object); + + //Test that mockArea2 has 2 neighbours + List neighbours1 = level.GetNeighbours(mockArea2.Object); + Assert.IsTrue(neighbours1.Count == 2); + + //Test that mockArea4 has 1 neighbour + List neighbours2 = level.GetNeighbours(mockArea4.Object); + Assert.IsTrue(neighbours2.Count == 1); + + //Test that mockArea6 has no neighbours + List neighbours3 = level.GetNeighbours(mockArea6.Object); + Assert.IsTrue(neighbours3.Count == 0); } } } diff --git a/TileSystem/Implementation/TwoDimension/Level.cs b/TileSystem/Implementation/TwoDimension/Level.cs index eccd3f3..589cc94 100644 --- a/TileSystem/Implementation/TwoDimension/Level.cs +++ b/TileSystem/Implementation/TwoDimension/Level.cs @@ -4,6 +4,7 @@ using TileSystem.Interfaces.Base; using TileSystem.Interfaces.Creation; using TileSystem.Interfaces.Management; +using TileSystem.Interfaces.TwoDimension; namespace TileSystem.Implementation.TwoDimension { @@ -232,8 +233,30 @@ public bool Remove(IArea area) /// Reference to the area public IArea Get(IPosition position) { - // TODO: Issue 15 (https://github.com/Wizcorp/TileSystem/issues/15) - throw new NotImplementedException(); + IArea currentArea = null; + + //Assuming it's 2D for comparison sake + IPosition2D position2D = position as IPosition2D; + + //Looking through all areas in current level + foreach (Area area in areas) + { + IPosition2D areaPosition2D = area.Position as IPosition2D; + + if (areaPosition2D.X == position2D.X && areaPosition2D.Y == position2D.Y) + { + currentArea = area; + //We can stop searching since there won't be any areas with duplicate positions + break; + } + } + + if (currentArea == null) + { + throw new ArgumentNullException("area", "No Area found at position"); + } + + return currentArea; } /// @@ -243,8 +266,35 @@ public IArea Get(IPosition position) /// List of IArea which are next to the supplied area public List GetNeighbours(IArea area) { - // TODO: Issue 15 (https://github.com/Wizcorp/TileSystem/issues/15) - throw new NotImplementedException(); + //The method will return maximum of 4 neighbours in the 4 cardinal directions, doesn't take into account diagonal neighbours + List neighbours = new List(); + + //Assuming it's 2D for ease of comparison + IPosition2D areaPosition2D = area.Position as IPosition2D; + + foreach (Area a in areas) + { + IPosition2D potentialNeighbourPosition2D = a.Position as IPosition2D; + + //Assuming negative positions in areas are possible + for (int x = -1; x < 2; x++) + { + for (int y = -1; y < 2; y++) + { + //If the position is 0, 0 offset from current area, ignore it, since it's the current area + if (x == 0 && y == 0) + continue; + + if (potentialNeighbourPosition2D.X + x == areaPosition2D.X && potentialNeighbourPosition2D.Y + y == areaPosition2D.Y) + { + //If potential Neighbour Position + offset equals the current area position, add it to our list, since it's a neighbour + neighbours.Add(a); + } + } + } + } + + return neighbours; } #endregion