Skip to content

Instantly share code, notes, and snippets.

@Andygmb
Created December 31, 2014 12:33
Show Gist options
  • Save Andygmb/4b0abf06da8f2b29f2d0 to your computer and use it in GitHub Desktop.
Save Andygmb/4b0abf06da8f2b29f2d0 to your computer and use it in GitHub Desktop.
wrap: function (x, min, max){
return x < 0 ? max + x : min
},
get_neighbour:function(x, y) {
if (g.grid[x] && g.grid[x][y]){
return g.grid[x][y].state
} else // else the number is out of bounds{
x = g.func.wrap(x, 0, 500);
y = g.func.wrap(y, 0, 500);
return g.grid[x][y].state
}
},
check_neighbours: function(){
for (var x = 0; x < g.canvas.width +10; x += 10) {
for (var y = 0; y < g.canvas.height +10; y +=10) {
g.grid[x][y].neighbours = 0
if (g.func.get_neighbour(x, y-10)){
g.grid[x][y].neighbours += 1;
};
if (g.func.get_neighbour(x, y+10)){
g.grid[x][y].neighbours += 1;
};
if (g.func.get_neighbour(x-10, y)){
g.grid[x][y].neighbours += 1;
};
if (g.func.get_neighbour(x+10, y)){
g.grid[x][y].neighbours += 1;
};
if (g.func.get_neighbour(x-10, y-10)){
g.grid[x][y].neighbours += 1;
};
if (g.func.get_neighbour(x+10, y+10)){
g.grid[x][y].neighbours += 1;
};
if (g.func.get_neighbour(x+10, y-10)){
g.grid[x][y].neighbours += 1;
};
if (g.func.get_neighbour(x-10, y+10)){
g.grid[x][y].neighbours += 1;
};
}
}
},
set_state: function(){
// for x in xrange(canvas.width -1, 10)
for (var x = 0; x < g.canvas.width +10; x += 10) {
// for y in xrange(canvas.height -1, 10)
for (var y = 0; y < g.canvas.height +10; y +=10) {
if (g.grid[x][y].state) {
if (g.grid[x][y].neighbours < 2){
g.grid[x][y].state = false;
g.grid[x][y].age = 0;
}
else if (g.grid[x][y].neighbours in [2, 3]){
g.grid[x][y].state = true;
}
else if (g.grid[x][y].neighbours > 3){
g.grid[x][y].state = false;
g.grid[x][y].age = 0;
}
}
else if (!g.grid[x][y].state){
if (g.grid[x][y].neighbours === 3){
g.grid[x][y].state = true;
}
else{
g.grid[x][y].state = false;
g.grid[x][y].age = 0;
}
}
}
}
}
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment