import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
public class Board extends Panel
{
// creates an 10x10 array of instances of the Cell class.

 private Cell [][] game_board = new Cell [10][10];
   
// constructor to set up board with its layout and add the instances of // Cell to it.   

  public Board (Interface game)
   {
      int row, column; 

      setLayout (new GridLayout (10,10));


      for (row = 0; row < game_board.length; row++)
         for (column = 0; column < game_board[0].length; column++)
         {
            game_board[row][column] = new Cell (game);
            add (game_board[row][column]);
         }
   }  

// method that goes through and runs the Cell class's clear method for // each cell. (reseting the state of the cell to dead)

 public void clear ()
   {
      int row, column;  
      for (row = 0; row < game_board.length; row++)
         for (column = 0; column < game_board[0].length; column++)
            game_board[row][column].clear ();

   }  
 
// when a cell has been clicked, goes through each cell in the array // and when it gets to the clicked one, it changes its state.

 public void activity (ActionEvent event)
   {
      int row, column; 
      for (row = 0; row < game_board.length; row++)
         for (column = 0; column < game_board[0].length; column++)
         {
            if (event.getSource() == game_board[row][column])
               (game_board[row][column].change()); 
             
         }

   }
 public void runGame (int times, int birth, int crowd, int lonely)
   {
     
     int row, column, total, count;
     total = 0; 
     for (count = 0; count < times; count++)
      { 

// in a for loop, runs through each square adjacent to the current // square selected by the loop and adds up the alive ones.

       for (row = 0; row < game_board.length; row++)
         for (column = 0; column < game_board[0].length; column++)
         {
             if((!((row-1)<0))&&(!((column-1)<0)))       
                total = game_board[row-1][column-1].getState() + total; 
             if(!((column-1)<0))
                total = game_board[row][column-1].getState() + total;
             if((!((row+1)>9))&&(!((column-1)<0)))
                total = game_board[row+1][column-1].getState() + total;   
             if(!((row-1)<0))             
                total = game_board[row-1][column].getState() + total;
             if(!((row+1)>9))
                total = game_board[row+1][column].getState() + total;
             if((!((row-1)<0))&&(!((column+1)>9)))
                total = game_board[row-1][column+1].getState() + total;
             if(!((column+1)>9))
                total = game_board[row][column+1].getState() + total;
             if((!((row+1)>9))&&(!((column+1)>9)))
                total = game_board[row+1][column+1].getState() + total;

// if the current cell is alive or dead, it compares the total live // cells from above with the appropriate current rules passed in by the // Interface class, and marks the cell to be changed accordingly.

             if(game_board[row][column].getState()==1)
             {
               if ((total > crowd)||(total < lonely))                                   game_board[row][column].change2();  
             }
             else
             if(game_board[row][column].getState()==0)
             {   
               if (total > birth) game_board[row][column].change2();
             }
          total = 0;  
          }

// changes all the state of all marked cells to be that of the mark.

       for (row = 0; row < game_board.length; row++)
         for (column = 0; column < game_board[0].length; column++)
               game_board[row][column].switchem();
          
     }
   }
}
