function LifeGame(){}
LifeGame.prototype.init = function(w,h){
this.w=w;
this.h=h;
this.buf1 = new Array(h);
this.buf2 = new Array(h);
this.bufS = true;
for(var y=0;y<h;y++){
this.buf1[y]=new Array(w);
this.buf2[y]=new Array(w);
}
return this;
}
LifeGame.prototype.set = function(a){
this.w=0;
this.h=a.length;
this.buf1 = a;
this.buf2 = new Array(a.length);
for(var y=0;y<this.h;y++){
this.buf2[y] = new Array(a[y].length);
this.w = Math.max(this.w, a[y].length);
}
this.bufS = true;
return this;
}
LifeGame.prototype.sum = function(a,x,y){
var n=0;
for(var y2=-1;y2<=1;y2++){
for(var x2=-1;x2<=1;x2++){
var ay=(y+y2+a.length)%a.length;
var ax=(x+x2+a[ay].length)%a[ay].length;
if(!(y2==0 && x2==0) && a[ay][ax]){n++;}
}
}
return n;
};
LifeGame.prototype.upd = function(a,b,x,y){
var s = this.sum(a,x,y);
var r;
if(s<=1){r=0;}
else if(s==2){r=a[y][x];}
else if(s==3){r=1;}
else {r=0;}
b[y][x]=r;
};
LifeGame.prototype.life = function(n){
if(n){ for(var i=0;i<n;i++){this.life1();} }
else{ this.life1(); }
return this;
}
LifeGame.prototype.life1 = function(){
var a = this.bufS ? this.buf1 : this.buf2;
var b = this.bufS ? this.buf2 : this.buf1;
this.bufS = !this.bufS;
var h=this.h;
var w=this.w;
for(var y=0;y<h;y++){
for(var x=0;x<w;x++){
this.upd(a,b,x,y);
}
}
return this;
};
LifeGame.prototype.buf = function(){
return this.bufS ? this.buf1 : this.buf2;
};
LifeGame.prototype.rnd = function(){
var a = this.buf();
for(var y=0;y<this.h;y++){
for(var x=0;x<this.w;x++){
a[y][x]=Math.random()<0.5?0:1;
}
}
return this;
};
LifeGame.prototype.toString = function(){
return this.buf().join("\n");
}
if(typeof(console)=="undefined" && typeof(print)!="undefined"){
console = new Object();
console.log = function(o){print(o);};
console.log("use print");
}
function test(){
var lg = new LifeGame().set([
[0,1,1,1,0],
[0,1,0,0,0],
[0,0,1,0,0],
[0,0,0,0,0],
[0,0,0,0,0]
]);
console.log(""+lg);
console.log("---------------");
lg.life();
console.log(""+lg);
console.log("---------------");
var st = new Date();
lg.init(500,500).rnd();
console.log("create :" + (new Date()-st) + " msec");
lg.life(1);
console.log("life1:" + (new Date()-st) + " msec");
}
test();
CmZ1bmN0aW9uIExpZmVHYW1lKCl7fQpMaWZlR2FtZS5wcm90b3R5cGUuaW5pdCA9IGZ1bmN0aW9uKHcsaCl7Cgl0aGlzLnc9dzsKCXRoaXMuaD1oOwoJdGhpcy5idWYxID0gbmV3IEFycmF5KGgpOwoJdGhpcy5idWYyID0gbmV3IEFycmF5KGgpOwoJdGhpcy5idWZTID0gdHJ1ZTsKCWZvcih2YXIgeT0wO3k8aDt5KyspewoJCXRoaXMuYnVmMVt5XT1uZXcgQXJyYXkodyk7CgkJdGhpcy5idWYyW3ldPW5ldyBBcnJheSh3KTsKCX0KCXJldHVybiB0aGlzOwp9CkxpZmVHYW1lLnByb3RvdHlwZS5zZXQgPSBmdW5jdGlvbihhKXsKCXRoaXMudz0wOwoJdGhpcy5oPWEubGVuZ3RoOwoJdGhpcy5idWYxID0gYTsKCXRoaXMuYnVmMiA9IG5ldyBBcnJheShhLmxlbmd0aCk7Cglmb3IodmFyIHk9MDt5PHRoaXMuaDt5KyspewoJCXRoaXMuYnVmMlt5XSA9IG5ldyBBcnJheShhW3ldLmxlbmd0aCk7CgkJdGhpcy53ID0gTWF0aC5tYXgodGhpcy53LCBhW3ldLmxlbmd0aCk7Cgl9Cgl0aGlzLmJ1ZlMgPSB0cnVlOwoJcmV0dXJuIHRoaXM7Cn0KTGlmZUdhbWUucHJvdG90eXBlLnN1bSA9IGZ1bmN0aW9uKGEseCx5KXsKCXZhciBuPTA7Cglmb3IodmFyIHkyPS0xO3kyPD0xO3kyKyspewoJCWZvcih2YXIgeDI9LTE7eDI8PTE7eDIrKyl7CgkJCXZhciBheT0oeSt5MithLmxlbmd0aCklYS5sZW5ndGg7CgkJCXZhciBheD0oeCt4MithW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,0,0,0
---------------
0,1,1,0,0
0,1,0,1,0
0,0,0,0,0
0,0,0,0,0
0,0,1,0,0
---------------
create :270 msec
life1:4104 msec