fork(1) download
  1.  
  2. function LifeGame(){}
  3. LifeGame.prototype.init = function(w,h){
  4. this.w=w;
  5. this.h=h;
  6. this.buf1 = new Array(h);
  7. this.buf2 = new Array(h);
  8. this.bufS = true;
  9. for(var y=0;y<h;y++){
  10. this.buf1[y]=new Array(w);
  11. this.buf2[y]=new Array(w);
  12. }
  13. return this;
  14. }
  15. LifeGame.prototype.set = function(a){
  16. this.w=0;
  17. this.h=a.length;
  18. this.buf1 = a;
  19. this.buf2 = new Array(a.length);
  20. for(var y=0;y<this.h;y++){
  21. this.buf2[y] = new Array(a[y].length);
  22. this.w = Math.max(this.w, a[y].length);
  23. }
  24. this.bufS = true;
  25. return this;
  26. }
  27. LifeGame.prototype.sum = function(a,x,y){
  28. var n=0;
  29. for(var y2=-1;y2<=1;y2++){
  30. for(var x2=-1;x2<=1;x2++){
  31. var ay=(y+y2+a.length)%a.length;
  32. var ax=(x+x2+a[ay].length)%a[ay].length;
  33. if(!(y2==0 && x2==0) && a[ay][ax]){n++;}
  34. }
  35. }
  36. return n;
  37. };
  38. LifeGame.prototype.upd = function(a,b,x,y){
  39. var s = this.sum(a,x,y);
  40. var r;
  41. if(s<=1){r=0;}
  42. else if(s==2){r=a[y][x];}
  43. else if(s==3){r=1;}
  44. else {r=0;}
  45. b[y][x]=r;
  46. };
  47. LifeGame.prototype.life = function(n){
  48. if(n){ for(var i=0;i<n;i++){this.life1();} }
  49. else{ this.life1(); }
  50. return this;
  51. }
  52. LifeGame.prototype.life1 = function(){
  53. var a = this.bufS ? this.buf1 : this.buf2;
  54. var b = this.bufS ? this.buf2 : this.buf1;
  55. this.bufS = !this.bufS;
  56. var h=this.h;
  57. var w=this.w;
  58. for(var y=0;y<h;y++){
  59. for(var x=0;x<w;x++){
  60. this.upd(a,b,x,y);
  61. }
  62. }
  63. return this;
  64. };
  65. LifeGame.prototype.buf = function(){
  66. return this.bufS ? this.buf1 : this.buf2;
  67. };
  68. LifeGame.prototype.rnd = function(){
  69. var a = this.buf();
  70. for(var y=0;y<this.h;y++){
  71. for(var x=0;x<this.w;x++){
  72. a[y][x]=Math.random()<0.5?0:1;
  73. }
  74. }
  75. return this;
  76. };
  77. LifeGame.prototype.toString = function(){
  78. return this.buf().join("\n");
  79. }
  80.  
  81. if(typeof(console)=="undefined" && typeof(print)!="undefined"){
  82. console = new Object();
  83. console.log = function(o){print(o);};
  84. console.log("use print");
  85. }
  86.  
  87. function test(){
  88. var lg = new LifeGame().set([
  89. [0,1,1,1,0],
  90. [0,1,0,0,0],
  91. [0,0,1,0,0],
  92. [0,0,0,0,0],
  93. [0,0,0,0,0]
  94. ]);
  95. console.log(""+lg);
  96. console.log("---------------");
  97. lg.life();
  98. console.log(""+lg);
  99. console.log("---------------");
  100.  
  101. var st = new Date();
  102. lg.init(500,500).rnd();
  103. console.log("create :" + (new Date()-st) + " msec");
  104. lg.life(1);
  105. console.log("life1:" + (new Date()-st) + " msec");
  106. }
  107.  
  108. test();
  109.  
Success #stdin #stdout 4.11s 5084KB
stdin
Standard input is empty
stdout
use print
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
---------------
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