Skip to content

Instantly share code, notes, and snippets.

@zevaverbach
Created September 27, 2018 04:06
Show Gist options
  • Save zevaverbach/d8dfcb7b586ad7dec930657137af6ca2 to your computer and use it in GitHub Desktop.
Save zevaverbach/d8dfcb7b586ad7dec930657137af6ca2 to your computer and use it in GitHub Desktop.
get_neighbors function for Conway's Game of Life
const getNeighbors = (rowNum, colNum, board) => {
const neighbors = [];
for (let r of [rowNum - 1, rowNum, rowNum + 1]) {
if (board[r] === undefined) {
continue;
}
for (let c of [colNum - 1, colNum, colNum + 1]) {
if (board[r][c] === undefined) {
continue;
}
if (r === rowNum && c === colNum) {
continue;
}
neighbors.push(board[r][c])
}
}
return neighbors;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment