/** * Copy constructor. */ public ConwayBoard(ConwayBoard other) { // Deep copy board array board = new bool[other.board.GetLength(0), other.board.GetLength(1)]; Array.Copy(other.board, board, other.board.Length); }
/** * Advances the board state by a tick, following the rules of the game. */ public void runTick() { // Create copy of the board's current state to reference while we // update the actual board ConwayBoard oldBoard = new ConwayBoard(this); // Iterate over all cells in old board for (int j = 0; j < height; j++) { for (int i = 0; i < width; i++) { // Count adjacent live cells int neighborCount = 0; // UL if (oldBoard.cell(i - 1, j - 1)) { ++neighborCount; } // U if (oldBoard.cell(i, j - 1)) { ++neighborCount; } // UR if (oldBoard.cell(i + 1, j - 1)) { ++neighborCount; } // L if (oldBoard.cell(i - 1, j)) { ++neighborCount; } // R if (oldBoard.cell(i + 1, j)) { ++neighborCount; } // DL if (oldBoard.cell(i - 1, j + 1)) { ++neighborCount; } // D if (oldBoard.cell(i, j + 1)) { ++neighborCount; } // DR if (oldBoard.cell(i + 1, j + 1)) { ++neighborCount; } if (oldBoard.cell(i, j)) { // Live cell // 1. Any live cell with fewer than two neighbors dies if (neighborCount < 2) { setCell(i, j, false); } // 2. Any live cell with two or three neighbors lives on else if (neighborCount <= 3) { } // 3. Any live cell with more than three neighbors dies else if (neighborCount > 3) { setCell(i, j, false); } } else { // Dead cell // 4. Any dead cell with three live neighbors becomes a live cell if (neighborCount == 3) { setCell(i, j, true); } } } } }
//=================================================================== // Public //=================================================================== /** * Constructor. */ public ConwayRunner(ConwayBoard board, int msPerTick) { this.board = board; this.msPerTick = msPerTick; }