Created
January 6, 2015 09:35
-
-
Save OlavHN/644342fface11a146507 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 gameoflife(matrix) { | |
| var population = []; | |
| for (var i = 0; i < matrix.length; i++) { | |
| population[i] = []; | |
| for (var j = 0; j < matrix[0].length; j++) | |
| population[i][j] = 0; | |
| } | |
| matrix.forEach(function(row, i) { | |
| row.forEach(function(col, j) { | |
| if (col) { | |
| if (i>0) | |
| population[i-1][j] += 1; | |
| if (i<matrix.length-1) | |
| population[i+1][j] +=1; | |
| if (j>0) | |
| population[i][j-1] += 1; | |
| if (j<matrix[0].length-1) | |
| population[i][j+1] += 1; | |
| } | |
| }); | |
| }); | |
| population.forEach(function(row, i) { | |
| row.forEach(function(col, j) { | |
| matrix[i][j] = col > 1 && col < 4 ? true : false; | |
| }); | |
| }); | |
| return matrix; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment