public IActionResult GetArea([FromBody] CalculatingSurfaceIncomeModel model) { if (!ModelState.IsValid) { throw new ArgumentException("ModelState is not valid."); } return(Ok(_surfaceAreasService.CalculateSurfaceAreas(model))); }
public void IsAllowed_Map1x2_False() { var areaSurface = new AreaSurfaceCalculationService(); var model = new CalculatingSurfaceIncomeModel { Coordinates = new[] { 0, 0 }, Map = new[, ] { { "#", "#" } } }; Assert.AreEqual("The Operation can not be completed. The size of the map should be 2x2 and bigger.", areaSurface.CalculateSurfaceAreas(model)); }
public void SurfaceAreaOfWater_Map2x2_3() { var areaSurface = new AreaSurfaceCalculationService(); var model = new CalculatingSurfaceIncomeModel { Coordinates = new[] { 0, 0 }, Map = new[, ] { { "O", "O" }, { "#", "O" } } }; Assert.AreEqual("The given map has surface area of 3 square meters.", areaSurface.CalculateSurfaceAreas(model)); }
public void SurfaceAreaOfWater_Empty() { var areaSurface = new AreaSurfaceCalculationService(); var model = new CalculatingSurfaceIncomeModel { Coordinates = new[] { 0, 0 }, Map = new[, ] { { "#", "#", "#", "#" }, { "#", "#", "#", "#" }, { "#", "#", "#", "#" }, { "#", "#", "#", "#" } } }; Assert.AreEqual("The given map does not have surface area of water.", areaSurface.CalculateSurfaceAreas(model)); }
public string CalculateSurfaceAreas(CalculatingSurfaceIncomeModel model) { var border = new[] { model.Map.GetLength(0) - 1, model.Map.GetLength(1) - 1 }; var ignoreList = new List <int[]>(); var waterChunks = new List <int>(); if (model.Map.GetLength(0) < 2 || model.Map.GetLength(1) < 2) { return("The Operation can not be completed. The size of the map should be 2x2 and bigger."); } for (var y = model.Coordinates[0]; y <= model.Map.GetLength(0) - 1; y++) { for (var x = model.Coordinates[1]; x <= model.Map.GetLength(1) - 1; x++) { if (model.Map[y, x].Equals("O")) { var checker = FindElementsInListOfArrays(new[] { y, x }, ignoreList); if (!checker) { ignoreList.Add(new[] { y, x }); } if (y + 1 <= border[0]) { if (model.Map[y + 1, x].Equals("O")) { checker = FindElementsInListOfArrays(new[] { y + 1, x }, ignoreList); if (!checker) { ignoreList.Add(new[] { y + 1, x }); } } } if (x + 1 <= border[1]) { if (model.Map[y, x + 1].Equals("O")) { checker = FindElementsInListOfArrays(new[] { y, x + 1 }, ignoreList); if (!checker) { ignoreList.Add(new[] { y, x + 1 }); } } } if (x == border[1] && ignoreList.Count != 0 && model.Map[y, x].Equals("O")) { var isThereWaterBehind = CheckCoordsBehind(new[] { y, x }, ignoreList, border[1]); if (!isThereWaterBehind) { ignoreList = SortListByMakingUniqueItemsOnly(ignoreList, waterChunks); } } } else if (y == border[0] && x == border[1]) { ignoreList = SortListByMakingUniqueItemsOnly(ignoreList, waterChunks); } else if (x == border[1] && ignoreList.Count != 0) { var isThereWaterBehind = CheckCoordsBehind(new[] { y, x }, ignoreList, border[1]); if (!isThereWaterBehind) { ignoreList = SortListByMakingUniqueItemsOnly(ignoreList, waterChunks); } } } } string providedInformation; if (waterChunks.Count == 0 || waterChunks[0] == 0) { providedInformation = "The given map does not have surface area of water."; } else { providedInformation = "The given map has surface area of " + string.Join(", ", waterChunks) + " square meters."; } return(providedInformation); }