Skip to content

Instantly share code, notes, and snippets.

@OlavHN
Created January 6, 2015 09:35
Show Gist options
  • Save OlavHN/644342fface11a146507 to your computer and use it in GitHub Desktop.
Save OlavHN/644342fface11a146507 to your computer and use it in GitHub Desktop.
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