xxxxxxxxxx
let cellSize = 10;
let n = 80;
let m = 80;
var game;
function setup() {
createCanvas(800, 800);
background(100);
game = new Game(n,m);
//print("done setup");
// print(game.getCell(2,3))
// game.setCell(2,3,37);
// print(game.getCell(2,3))
}
function draw() {
print("looop");
game.update();
game.draw();
}
function Game(n,m){
var a = []
for(var i=0; i<n*m;i++){
let i = floor(random(2));
a.push(i);
}
this.state = a ;
this.tempState = a.slice(0);
this.setCell = function(i,j,value){
this.tempState[i*n + j] = value
}
this.getCell = function(i,j) {
i = (i+n)%n
j = (j+m)%m
return this.state[i*n + j];
}
this.update = function() {
print("update")
for(var i=0; i<n;i++){
for(var j=0; j<m;j++){
let t = this.getCell(i,j+1) +
this.getCell(i-1,j+1) +
this.getCell(i+1,j+1) +
this.getCell(i,j-1) +
this.getCell(i+1,j-1) +
this.getCell(i-1,j-1) +
this.getCell(i+1,j) +
this.getCell(i-1,j);
//print("t=" + t + "ij = " + i + "," + j)
if(this.getCell(i,j)==0){
if(t==3){
//print("change")
this.setCell(i,j,1)
}
}
else if (t>3 || t<2 ){
//print("change")
this.setCell(i,j,0)
}
}
}
this.state = this.tempState.slice(0);
}
this.draw = function() {
print("draw")
for(var i=0; i<n;i++){
for(var j=0; j<m;j++){
if(this.getCell(i,j)==0){
fill(0);
}
else {
fill(255);
}
rect(i*cellSize,j*cellSize,cellSize,cellSize);
}
}
}
}