Created
March 3, 2018 22:32
-
-
Save uncompiled/2e3e1739f776ee650fbc0ab3c1c93b3b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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