Skip to content

Commit

Permalink
adds GetConnectedEmptyCells function and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TheZitroX committed Jan 12, 2025
1 parent d09d932 commit 30c74cd
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
18 changes: 18 additions & 0 deletions FloodPipeWPF/MVVM/Model/Game/GameField/CellFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,23 @@ public static bool IsCellEmpty(Cell cell)
{
return cell.CellState == CellState.EMPTY;
}

public static List<Cell> GetConnectedEmptyCells(Cell cell, List<List<Cell>> cells)
{
var connectedEmptyCells = new List<Cell>();

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;
}
}
}
34 changes: 34 additions & 0 deletions FloodpipeWPF.UnitTests/CellFunctionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<Cell>>();
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<List<Cell>>();
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<List<Cell>>();
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]));
}
}
}

0 comments on commit 30c74cd

Please sign in to comment.