Created
September 27, 2018 04:06
-
-
Save zevaverbach/d8dfcb7b586ad7dec930657137af6ca2 to your computer and use it in GitHub Desktop.
get_neighbors function for Conway's Game of Life
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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