Skip to content

Instantly share code, notes, and snippets.

@uncompiled
Created March 3, 2018 22:32
Show Gist options
  • Save uncompiled/2e3e1739f776ee650fbc0ab3c1c93b3b to your computer and use it in GitHub Desktop.
Save uncompiled/2e3e1739f776ee650fbc0ab3c1c93b3b to your computer and use it in GitHub Desktop.
function gameOfLifeIterator(board) {
const isAlive = (x, y) => board[x] && board[x][y]
return board.map((row, x) =>
row.map((_, y) => {
let n = getCellNeighborCount(x, y)
return (isAlive(x, y) ? n > 1 && n < 4 : n === 3) ? 1 : 0
}))
function getCellNeighborCount (x, y) {
let neighborCount = 0
if (isAlive(x - 1, y - 1)) neighborCount++
if (isAlive(x - 1, y)) neighborCount++
if (isAlive(x - 1, y + 1)) neighborCount++
if (isAlive(x, y - 1)) neighborCount++
if (isAlive(x, y + 1)) neighborCount++
if (isAlive(x + 1, y - 1)) neighborCount++
if (isAlive(x + 1, y)) neighborCount++
if (isAlive(x + 1, y + 1)) neighborCount++
return neighborCount
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment