public void Find_ArrayWithCell_ReportsProperCoordinates() { var cellToFind = new GameBoardCell(); GameBoardCell[,] cells = { { new GameBoardCell(), new GameBoardCell(), new GameBoardCell() }, { new GameBoardCell(), new GameBoardCell(), new GameBoardCell() }, { cellToFind, new GameBoardCell(), new GameBoardCell() }, { new GameBoardCell(), new GameBoardCell(), new GameBoardCell() }, { new GameBoardCell(), new GameBoardCell(), new GameBoardCell() } }; var subject = new CellCoordinatesFinder(); CellCoordinates coordinates = subject.Find( cells, cellToFind ); coordinates.X.Should().Be( 2 ); coordinates.Y.Should().Be( 0 ); }
public int Count( GameBoardCell[,] gameBoardCells, CellCoordinates coordinates ) { int numberOfLivingNeighbors = 0; numberOfLivingNeighbors += PlusOneIfCellIsAlive( gameBoardCells, coordinates.X - 1, coordinates.Y - 1 ); numberOfLivingNeighbors += PlusOneIfCellIsAlive( gameBoardCells, coordinates.X - 1, coordinates.Y ); numberOfLivingNeighbors += PlusOneIfCellIsAlive( gameBoardCells, coordinates.X - 1, coordinates.Y + 1 ); numberOfLivingNeighbors += PlusOneIfCellIsAlive( gameBoardCells, coordinates.X , coordinates.Y - 1 ); numberOfLivingNeighbors += PlusOneIfCellIsAlive( gameBoardCells, coordinates.X, coordinates.Y + 1 ); numberOfLivingNeighbors += PlusOneIfCellIsAlive( gameBoardCells, coordinates.X + 1, coordinates.Y - 1 ); numberOfLivingNeighbors += PlusOneIfCellIsAlive( gameBoardCells, coordinates.X + 1, coordinates.Y ); numberOfLivingNeighbors += PlusOneIfCellIsAlive( gameBoardCells, coordinates.X + 1, coordinates.Y + 1 ); return numberOfLivingNeighbors; }
private int PlusOneIfCellIsAlive( GameBoardCell[,] gameBoardCells, int xPosition, int yPosition ) { int height = gameBoardCells.GetLength( 0 ); int width = gameBoardCells.GetLength( 1 ); if ( xPosition < 0 | xPosition > height | yPosition < 0 | yPosition > width) { return 0; // out of bounds } if ( gameBoardCells[xPosition, yPosition].IsAlive ) { return 1; } return 0; }
public GameBoardCell SetLife( int numberOfLivingNeighbors, GameBoardCell cell ) { bool wasAlive = cell.IsAlive; if ( numberOfLivingNeighbors <= 2 | numberOfLivingNeighbors > 3 ) { cell.IsAlive = false; } if ( wasAlive && numberOfLivingNeighbors == 2 ) { cell.IsAlive = true; } if ( numberOfLivingNeighbors == 3 ) { cell.IsAlive = true; } return cell; }
public CellCoordinates Find( GameBoardCell[,] gameBoardCells, GameBoardCell cell ) { int w = gameBoardCells.GetLength( 0 ); // width int h = gameBoardCells.GetLength( 1 ); // height for ( int x = 0; x < w; ++x ) { for ( int y = 0; y < h; ++y ) { if ( gameBoardCells[x, y].Equals( cell ) ) { return new CellCoordinates { X = x, Y = y }; } } } throw new KeyNotFoundException(); }
public GameBoardCell RandomizeCell( GameBoardCell cell ) { var random = new Random(); cell.IsAlive = random.Next(100) < _aliveLikelihood; return cell; }