/*******************************************************************\

The Game-Of-Life (GOL) simulation runs in a 2-D array.
Each position in the array contains either 1 for a living cell,
or 0 for no living cell.  This simulation was invented
by Martin Gardner and has been thoroughly studied.
After each cycle (hour), the cells are updated, with new cells
growing at some positions, other cells dying, and some living
cells just continuing to live.

This program proposes a DIFFERENT set of rules for GOL simulation
(different than the standard simulation).

RULES
Start with some living cells (marked as 1).
The simulation loop runs once per second.
At each second :
- an empty position (0) will "give birth" to a new cell
  if any of the 8 neighbors contains a living cell,
  e.g. it gives birth if it has 1 or more living neighbors
- a living cell (1) will continue to live
  if it has either 0 or 1 living neighbor
- a living cell (1) dies if it has more than 1 living neighbor

Here is an example starting with one living cell:                                             
                                                                                                                             
     Time 0                  Time 1                Time 2
  0  0  0  0  0           0  0  0  0  0         1  1  1  1  1 
  0  0  0  0  0           0  1  1  1  0         1  0  0  0  1
  0  0  1  0  0           0  1  1  1  0         1  0  0  0  1
  0  0  0  0  0           0  1  1  1  0         1  0  0  0  1
  0  0  0  0  0           0  0  0  0  0         1  1  1  1  1

\*******************************************************************/


public class GameOfLife
{
  int[][] game = new int[7][7];
 
  public GameOfLife()
  {
    game[3][3] = 1;
    display();
    grow();
    display();
    grow();
    display();
  }
 
  void grow()
  {
    int[][] next = new int[7][7];
    for(int row = 1; row <= 5; row = row + 1)
    {
      for(int col = 1; col <= 5; col = col + 1)
      {
        int c = countNeighbors(row,col);
        if(game[row][col]==1 && c>1)
        { next[row][col] = 0; }
        else if(game[row][col]==0 && c>=1)
        { next[row][col] = 1; }
        else
        { next[row][col] = game[row][col]; }
      }
    }
    game = next;
  }
 
  int countNeighbors(int row, int col)
  {
    int count = game[row-1][col-1] + game[row-1][col] + game[row-1][col+1]
      + game[row][col-1] + game[row][col+1]
      + game[row+1][col-1] + game[row+1][col] + game[row+1][col+1];
    return count;          
  }
 
  void display()
  {
    String show = "";
    for(int row = 1; row <= 5; row = row+1)
    {
      show = show + game[row][1] + "  " + game[row][2] + "  "
        + game[row][3] + "  " + game[row][4] + "  " + game[row][5] + "\n";
    }
    output(show);
  }
 
  public static void main(String[] args)
  {  new GameOfLife(); }
 
  public void output(String message)
  {  javax.swing.JOptionPane.showMessageDialog(null,message);  }
 
  public String input(String prompt)
  {  return javax.swing.JOptionPane.showInputDialog(null,prompt);  }
}

/******************************************************************\
(1) Run the program and check that it produces the expected stages.

(2) Change the program to start with 2 living cells.
    Use a sheet of paper to predict what will happen.
    Then run the program to check your prediction.
   
(3) The array needs to contain 7 rows and 7 columns to properly
    represent a 5x5 board.  This is because the countNeighbors()
    method checks neighbors in each direction.  If we actually
    had cells at the edge, then an IndexOutOfBoundsException
    would occur at the edges.  This way, rows 0 and 6 and
    columns 0 and 6 always contain ZEROES, so they don't affect
    rows 1 and 5.
   
    Now change the board so that it stores 10x10 cells.
    That means the array must be 12x12.  You will need to change
    the program substantially, using nested loops for the display()
    method.  Then try various starting configurations.

(4) Change the setup() method to use a loop to continue running
    the simulation over and over again.
   
(5) These simplified rules don't make a very interesting sequence
    of frames.  The original rules make a much more interesting game.
    Change the program to implement the standard rules.
    You can find the original rules on Wikipedia at
    http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life

\******************************************************************/