title: How to generate new grid - the game of life
body: <p>Hi everyone I am trying to simulate a simple Conway's game of life, these are the rules of the game:</p>
  
  <p>Any live cell with fewer than two live neighbours dies, as if caused by under-population.
  Any live cell with two or three live neighbours lives on to the next generation.
  Any live cell with more than three live neighbours dies, as if by overcrowding.
  Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.</p>
  
  <p>Now the problem is that I need to modify my actual grid in order to print it and then re-change it as its previously value before any rules is applied, however I cannot find any system to accomplish this task, I hope someone can help me.
  Here my code:</p>
  
  <pre><code>import java.util.Scanner;
  import java.io.*;
  
  class LifeGrid
  {
  private int[][] grid, newGrid;
  private int generation = 0;
  
  public LifeGrid(int x, int y, String filename) throws FileNotFoundException 
  { 
      grid = new int[x][y];
      newGrid = new int[x][y];
  
      int j = 0;
  
      Scanner scanner = new Scanner(new File(filename));
  
      while(scanner.hasNextLine() &amp;&amp; j &lt; x)
      {
          String line = scanner.nextLine();
  
          for(int i=0; i&lt;line.length() &amp;&amp; i&lt;y; i++)
          {
              if(line.charAt(i) == '*')
                  grid[j][i] = 1;
              else
                  grid[j][i] = 0;
          }
          j++;
      }
      scanner.close();
  }
  
  public void show()
  {
  
      for(int i=0; i&lt;grid.length; i++)
      {
          for(int j=0; j&lt;grid[i].length; j++)
          {
              if(grid[i][j] == 1)
                  System.out.print("*");
              else
                  System.out.print(" "); 
          }
          System.out.println();
      }
      System.out.println("Generation:" + generation);
  }
  
  //Getter methods
  
  public int getWidth()             { return grid[0].length;  }
  public int getHeight()            { return grid.length;     }
  public int getGeneration()        { return this.generation; }
  public int getCell(int x, int y)  { return grid[x][y];      }
  
  
  public static void main(String[] args)throws FileNotFoundException 
  {
      LifeGrid life = new LifeGrid(6, 10, args[0]);
      life.run(); 
      }
  
  //Check neighbours
  
      public int neighbours(int x, int y)
      {
          int neighbours = 0;
  
          if(x == 0 &amp;&amp; y == 0)
          {
          if(grid[x][y+1] == 1)       {neighbours++;} 
          if(grid[x+1][y] == 1)       {neighbours++;}
          if(grid[x+1][y+1] == 1)     {neighbours++;}
          }
          else if(x == 0 &amp;&amp; y &gt;= 1 &amp;&amp; y &lt; getWidth() -1)
          {
          if(grid[x][y+1] == 1)       {neighbours++;} 
          if(grid[x][y-1] == 1)       {neighbours++;}
          if(grid[x+1][y] == 1)       {neighbours++;}
          if(grid[x+1][y+1] == 1)     {neighbours++;} 
          if(grid[x+1][y-1] == 1)     {neighbours++;}
          }
          else if(x &gt;= 1 &amp;&amp; x &lt; getHeight() -1 &amp;&amp; y == 0)
          {
          if(grid[x][y+1] == 1)       {neighbours++;} 
          if(grid[x+1][y] == 1)       {neighbours++;}
          if(grid[x+1][y+1] == 1)     {neighbours++;} 
          if(grid[x-1][y+1] == 1)     {neighbours++;}
          }
          else if(x == getHeight()-1 &amp;&amp; y &gt;= 1 &amp;&amp; y &lt; getWidth() - 1)
          {
          if(grid[x][y+1] == 1)       {neighbours++;} 
          if(grid[x][y-1] == 1)       {neighbours++;}
          if(grid[x-1][y-1] == 1)     {neighbours++;}
          if(grid[x-1][y+1] == 1)     {neighbours++;}
          }
          else if(x &gt;=1 &amp;&amp; x &lt; getHeight() - 1 &amp;&amp; y == getWidth()-1 )
          {
          if(grid[x][y-1] == 1)       {neighbours++;}
          if(grid[x+1][y] == 1)       {neighbours++;}
          if(grid[x+1][y-1] == 1)     {neighbours++;}
          if(grid[x-1][y-1] == 1)     {neighbours++;}
          }
          else if(x == 0 &amp;&amp; y == getWidth()-1 )
          {   
          if(grid[x][y-1] == 1)       {neighbours++;}
          if(grid[x+1][y] == 1)       {neighbours++;}
          if(grid[x+1][y-1] == 1)     {neighbours++;}
          }
          else if(x == getHeight()-1  &amp;&amp; y == 0)
          {
          if(grid[x-1][y] == 1)       {neighbours++;}
          if(grid[x][y+1] == 1)       {neighbours++;}
          if(grid[x-1][y+1] == 1)     {neighbours++;}
          }   
          else if(x == getHeight()-1  &amp;&amp; y == getWidth()-1)
          {
          if(grid[x][y-1] == 1)       {neighbours++;}
          if(grid[x-1][y] == 1)       {neighbours++;}
          if(grid[x-1][y-1] == 1)     {neighbours++;}
          }
          else
          {
                      if(grid[x][y+1] == 1)           {neighbours++;}
                      if(grid[x][y-1] == 1)           {neighbours++;}
                      if(grid[x+1][y] == 1)           {neighbours++;}
                      if(grid[x+1][y+1] == 1)         {neighbours++;}
                      if(grid[x+1][y-1] == 1)         {neighbours++;}
                      if(grid[x-1][y-1] == 1)         {neighbours++;}
                      if(grid[x-1][y+1] == 1)         {neighbours++;}
                  }
          return neighbours;
      }
      public void run()
      {
          int n;
          int[][] old;
  
          for(int i=0; i&lt;grid.length; i++)
          {
              for(int j=0; j&lt;grid[i].length; j++)
              {
                  n = neighbours(i,j);
  
                  if(grid[i][j] == 1)
                  {
                      if(n &lt; 2 || n &gt; 3)      {generation = 0;}
                      if(n == 2 || n == 3)    {generation = 1;}
                  }
                  else 
                  {
                      if(n == 3)          {generation = 1;}
                      else            {generation = 0;}
                  }
  
                  if(generation == 1)
                  {
                      old = grid;
                      newGrid[i][j] = 1;
                      grid = newGrid;
                      show();
                      grid = old;
                  }
                  else 
                  {   
                      old = grid;
                      newGrid[i][j] = 0;
                      grid = newGrid;
                      show();
                      grid = old;
                                  }
  
              }
          }
          }
  }       
  </code></pre>
  
  <p>File:</p>
  
  <pre><code>* * *
  </code></pre>
  
  <p>Expected output:</p>
  
  <pre><code>*
  *
  *
  </code></pre>
  
  <p>The new run() method is:</p>
  
  <pre><code>public void run()
  {
      int n;
  
      for(int i=0; i&lt;grid.length; i++)
      {
          for(int j=0; j&lt;grid[i].length; j++)
          {
              n = neighbours(i,j);
  
              if(grid[i][j] == 1)
              {
                  if(n &lt; 2 || n &gt; 3)  {generation = 0;}
                  if(n == 2 || n == 3)    {generation = 1;}
              }
              else 
              {
                  if(n == 3)      {generation = 1;}
                  else            {generation = 0;}
              }
  
              newGrid[i][j] = generation;
          }
      }
      grid = newGrid.clone();
      show();
  }
  </code></pre>
  
  <p>Now instead of getting as output:</p>
  
  <pre><code>*
  *
  *
  </code></pre>
  
  <p>I get:</p>
  
  <pre><code>*
  *
  </code></pre>
  
  <p>Can someone help me to figure out why?</p>