Skip to content

Instantly share code, notes, and snippets.

@uncompiled
Created March 3, 2018 21:00
Show Gist options
  • Save uncompiled/59135801fae5d3cc84a989927eaa7acb to your computer and use it in GitHub Desktop.
Save uncompiled/59135801fae5d3cc84a989927eaa7acb to your computer and use it in GitHub Desktop.
Starting Point for Conway's Game of Life
function gameOfLifeIterator(board) {
let returnBoard = [];
for (let i = 0; i < board.length; i++) {
returnBoard[i] = [];
for (let j = 0; j < board[i].length; j++) {
let neighborCount = getCellNeighborCount(i, j, board);
if (board[i][j] === 1) {
if (neighborCount > 1 && neighborCount < 4) {
returnBoard[i][j] = 1;
} else {
returnBoard[i][j] = 0;
}
} else {
returnBoard[i][j] = (neighborCount === 3) ? 1 : 0;
}
}
}
return returnBoard;
function getCellNeighborCount(x, y, board) {
let neighborCount = 0;
if (board[x - 1] !== undefined) {
if (board[x - 1][y - 1] === 1) {
neighborCount++;
}
if (board[x - 1][y] === 1) {
neighborCount++;
}
if (board[x - 1][y + 1] === 1) {
neighborCount++;
}
}
if (board[x] !== undefined) {
if (board[x][y - 1] === 1) {
neighborCount++;
}
if (board[x][y + 1] === 1) {
neighborCount++;
}
}
if (board[x + 1] !== undefined) {
if (board[x + 1][y - 1] === 1) {
neighborCount++;
}
if (board[x + 1][y] === 1) {
neighborCount++;
}
if (board[x + 1][y + 1] === 1) {
neighborCount++;
}
}
return neighborCount;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment