diff --git a/FloodPipeWPF/MVVM/Model/Game/GameField/CellFunctions.cs b/FloodPipeWPF/MVVM/Model/Game/GameField/CellFunctions.cs index 502d8a8..d0308cf 100644 --- a/FloodPipeWPF/MVVM/Model/Game/GameField/CellFunctions.cs +++ b/FloodPipeWPF/MVVM/Model/Game/GameField/CellFunctions.cs @@ -55,5 +55,23 @@ public static bool IsCellEmpty(Cell cell) { return cell.CellState == CellState.EMPTY; } + + public static List GetConnectedEmptyCells(Cell cell, List> cells) + { + var connectedEmptyCells = new List(); + + foreach (var connection in cell.RelativeConnections) + { + var position = cell.Position + connection; + var connectedCell = cells[(int)position.X][(int)position.Y]; + + if (IsCellEmpty(connectedCell)) + { + connectedEmptyCells.Add(connectedCell); + } + } + + return connectedEmptyCells; + } } } diff --git a/FloodpipeWPF.UnitTests/CellFunctionsTest.cs b/FloodpipeWPF.UnitTests/CellFunctionsTest.cs index 095843e..64d00d0 100644 --- a/FloodpipeWPF.UnitTests/CellFunctionsTest.cs +++ b/FloodpipeWPF.UnitTests/CellFunctionsTest.cs @@ -250,5 +250,39 @@ public void IsCellEmpty_FilledCell_False() var cell = new Cell(CellType.CROSS, new Vector2(0, 0), 0, CellState.FULL); Assert.IsFalse(CellFunctions.IsCellEmpty(cell)); } + + [TestMethod] + public void GetConnectedEmptyCells_EmptyCell_EmptyList() + { + var cells = new List>(); + CellFunctions.CreateField(cells, 2, 2); + var connectedEmptyCells = CellFunctions.GetConnectedEmptyCells(cells[0][0], cells); + Assert.AreEqual(connectedEmptyCells.Count, 0); + } + + [TestMethod] + public void GetConnectedEmptyCells_FilledCell_EmptyList() + { + var cells = new List>(); + CellFunctions.CreateField(cells, 2, 2); + cells[0][0].CellState = CellState.FULL; + var connectedEmptyCells = CellFunctions.GetConnectedEmptyCells(cells[0][0], cells); + Assert.AreEqual(connectedEmptyCells.Count, 0); + } + + [TestMethod] + public void GetConnectedEmptyCells_EmptyCellWithConnectedEmptyCells_ConnectedEmptyCells() + { + var cells = new List>(); + CellFunctions.CreateField(cells, 2, 2); + cells[0][0].CellState = CellState.EMPTY; + cells[0][1].CellState = CellState.EMPTY; + cells[1][0].CellState = CellState.EMPTY; + var connectedEmptyCells = CellFunctions.GetConnectedEmptyCells(cells[0][0], cells); + Assert.AreEqual(connectedEmptyCells.Count, 3); + Assert.IsTrue(connectedEmptyCells.Contains(cells[0][1])); + Assert.IsTrue(connectedEmptyCells.Contains(cells[1][0])); + Assert.IsTrue(connectedEmptyCells.Contains(cells[1][1])); + } } }