/// <summary> /// Simulates the state of the cell at the given position. /// </summary> /// <param name="x">The position of the cell on the x-axis.</param> /// <param name="y">The position of the cell on the y-axis.</param> /// <param name="gameField">The IGameField to pull the data from.</param> /// <returns> /// Whether the given cell is 'dead' or 'alive'. /// </returns> private bool SimulateCell( int x, int y, IGameField gameField ) { int neighborCount = this.GetAliveNeighborCount( x, y, gameField ); if( gameField.GetCellStateStrict( x, y ) ) { // For a space that is 'populated': // Each cell with one or no neighbors dies, as if by loneliness. // Each cell with four or more neighbors dies, as if by overpopulation. // Each cell with two or three neighbors survives. return neighborCount == 2 || neighborCount == 3; } else { // For a space that is 'empty' or 'unpopulated' // Each cell with three neighbors becomes populated. return neighborCount == 3; } }
/// <summary> /// Gets whether the given cell is considered 'alive'. /// </summary> /// <param name="x">The position of the cell on the x-axis.</param> /// <param name="y">The position of the cell on the y-axis.</param> /// <param name="gameField">The IGameField to pull the data from.</param> /// <returns> /// Returns 0 if the index is out of valid range or the cell is 'dead'; /// returns 1 if the cell is 'alive'. /// </returns> private int GetCellState( int x, int y, IGameField gameField ) { if( this.hasSolidWalls ) { if( x < 0 || x >= gameField.Width ) return 0; if( y < 0 || y >= gameField.Height ) return 0; return gameField.GetCellStateStrict( x, y ) ? 1 : 0; } else { return gameField.GetCellState( x, y ) ? 1 : 0; } }
/// <summary> /// Refreshes the cell at the given position. /// </summary> /// <param name="x">The position of the cell on the x-axis.</param> /// <param name="y">The position of the cell on the y-axis.</param> /// <param name="gameField">The IGameField to pull the data from.</param> private void RefreshCell( int x, int y, IGameField gameField ) { bool isAlive = gameField.GetCellStateStrict( x, y ); Brush brush = this.GetBrush( isAlive ); Rectangle rect = this.cellField[x, y]; if( rect.Fill != brush ) { rect.Fill = brush; } }