publicvoidgameOfLife(int[][] board) { for (inti=0; i < board.length; i++) { for (intj=0; j < board[0].length; j++) { intcount= calcLiveNeighbors(board, i, j); if (count == 2) { continue; } elseif (count == 3) { if (board[i][j] == 0) { board[i][j] = DIE_TO_LIVE; } } else { if (board[i][j] == 1) { board[i][j] = LIVE_TO_DIE; } } } }
for (inti=0; i < board.length; i++) { for (intj=0; j < board[0].length; j++) { if (board[i][j] == DIE_TO_LIVE) { board[i][j] = 1; } elseif (board[i][j] == LIVE_TO_DIE) { board[i][j] = 0; } } } }
privateintcalcLiveNeighbors(int[][] board, int i, int j) { intliveCount=0; if (isValidCellLive(board, i - 1, j)) { liveCount++; }
if (isValidCellLive(board, i - 1, j + 1)) { liveCount++; }
if (isValidCellLive(board, i, j + 1)) { liveCount++; }
if (isValidCellLive(board, i + 1, j + 1)) { liveCount++; }
if (isValidCellLive(board, i + 1, j)) { liveCount++; }
if (isValidCellLive(board, i + 1, j - 1)) { liveCount++; }
if (isValidCellLive(board, i, j - 1)) { liveCount++; }
if (isValidCellLive(board, i - 1, j - 1)) { liveCount++; }
return liveCount; }
privatebooleanisValidCellLive(int[][] board, int i, int j) { return i >=0 && i < board.length && j >= 0 && j < board[0].length && isLive(board[i][j]); }