title: Conway game of life in javascript( best sol
body: <p>i am writing a code for conway game of life...... i am taking 2> arrays one fr old generation. and one for 2 nd genration. </p>
  
  <p>**rules are : The universe of the Game of Life is an infinite two-dimensional orthogonal grid of square cells, each of which is in one of two possible states, alive or dead. Every cell interacts with its eight neighbours, which are the cells that are horizontally, vertically, or diagonally adjacent. At each step in time, the following transitions occur: **1.Any live cell with fewer than two live neighbours dies, as if caused by under-population.</p>
  
  <p>2.Any live cell with two or three live neighbours lives on to the next generation.</p>
  
  <p>3.Any live cell with more than three live neighbours dies, as if by overcrowding.</p>
  
  <p>4.Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.**</p>
  
  <p>The initial pattern constitutes the seed of the system. The first generation is created by applying the above rules simultaneously to every cell in the seed—births and deaths occur simultaneously, and the discrete moment at which this happens is sometimes called a tick (in other words, each generation is a pure function of the preceding one). The rules continue to be applied repeatedly to create further generations.**</p>
  
  <p>here is the code</p>
  
  <p>I am getting a soln but i guess its not giving me the correct solution becuase its not checking the neighbors of the corners. i have marked that part</p>
  
  <pre><code>**
  
      window.conway =
      {
      };
      window.conway.maingame =
      {
      };
      conway.maingame = function(width, height)
      {
          window.a = [];
          this.width = width;
          this.height = height;
          this.map = new Array(width);
          for( i = 0; i &lt; this.width; i++)
          {
              this.map[i] = new Array(height);
          }
          console.log(this.map, "map")
      }
      conway.maingame.prototype.randomize = function()
      {
          for( y = 0; y &lt; this.height; y++)
          {
              //console.log("enter for loop")
              for( x = 0; x &lt; this.width; x++)
              {
                  if(Math.random() &gt; .5)
                  {
                      i =true;
                  }
                  else
                  {
                      i = false;
                  }
                  //console.log("enter function")
                  this.set(x, y, i);
              }
          }
      }
      conway.maingame.prototype.set = function(x, y, val)
      {
          x = x % this.width;
          y = y % this.height;
          this.map[x][y] = val;
          console.log(this.map, "map2");
      }
      conway.maingame.prototype.get = function(x, y)
      {
          x = x % this.width;
          y = y % this.height;
          return this.map[x][y];
      }
      *********************************************************************************
  conway.maingame.prototype.neighbors = function(x, y)
      {
          count = 0;
          if(x &gt; 0 &amp;&amp; y &gt; 0 &amp;&amp; this.get(x + 1, y + 1))
          {
              console.log(this.get(x + 1, y + 1), "value neighbor");
              count++;
              console.log(count);
          }
          if(x &gt; 0 &amp;&amp; y &gt; 0 &amp;&amp; this.get(x + 1, y))
          {
              console.log(this.get(x + 1, y), "vallue neighbor");
              count++;
              console.log(count);
          }
  
          if(x &gt; 0 &amp;&amp; y &gt; 0 &amp;&amp; this.get(x + 1, y - 1))
          {
              console.log(this.get(x + 1, y - 1), "vallue neighbor");
              count++;
              console.log(count);
          }
  
          if(x &gt; 0 &amp;&amp; y &gt;=0 &amp;&amp; this.get(x, y - 1))
          {
              console.log(this.get(x + 1, y - 1), "vallue neighbor");
              count++;
              console.log(count);
          }
  
          if(x &gt; 0 &amp;&amp; y &gt; 0 &amp;&amp; this.get(x - 1, y - 1))
          {
              console.log(this.get(x + 1, y - 1), "vallue neighbor");
              count++;
              console.log(count);
          }
  
          if(x &gt; 0 &amp;&amp; y &gt; 0 &amp;&amp; this.get(x - 1, y))
          {
              console.log(this.get(x + 1, y - 1), "vallue neighbor");
              count++;
              console.log(count);
          }
  
          if(x &gt; 0 &amp;&amp; y &gt; 0 &amp;&amp; this.get(x - 1, y + 1))
          {
              console.log(this.get(x + 1, y - 1), "vallue neighbor");
              count++;
              console.log(count);
          }
  
          if(x &gt; 0 &amp;&amp; y &gt; 0 &amp;&amp;this.get(x, y + 1))
          {
              console.log(this.get(x + 1, y - 1), "vallue neighbor");
              count++;
              console.log(count);
          }
  
          return count;
      }***
      conway.maingame.prototype.newgeneration = function()
      {
          var newMap = new Array(this.width);
          for( i = 0; i &lt; this.width; i++)
          {
              newMap[i] = new Array(this.height);
          }
          for(var y = 0; y &lt; this.height; y++)
          {
              for(var x = 0; x &lt; this.width; x++)
              {
                  console.log("enter all for");
                  newMap[x][y] = this.get(x, y);
                  console.log(newMap, "newarray");
                  //Rule 1: any live cell with fewer than two live neighbors dies
                  if(this.get(x, y) == true &amp;&amp; this.neighbors(x, y) &lt; 2)
                  {
                      newMap[x][y] = false;
                      console.log("rule1");
                  }
                  //Rule 2: Any live cell with two or three live neighbours lives on to the next generation
                  if(this.get(x, y) == true &amp;&amp; this.neighbors(x, y) == 2 || this.neighbors(x, y) == 3)
                  {
                      newMap[x][y] = true
                      console.log("rule2");
                  }
                  //Rule 3: any live cell with more than three live neighbors dies
                  if(this.get(x, y) == true &amp;&amp; this.neighbors(x, y) &gt; 3)
                  {
                      newMap[x][y] = false;
                      console.log("rule3");
                  }
                  //Rule 4: any dead cell with exactly three live neighbors becomes a live cell
                  if(this.get(x, y) == false &amp;&amp; this.neighbors(x, y) == 3)
                  {
                      newMap[x][y] = true;
                      console.log("rule4");
                  }
              }
          }
          this.map = newMap;
      console.log(this.map,"new generation")
      }
  
  **
  </code></pre>