// Title	: GameOfLife						
// Programmer	: Abdulla Al-mutawa						        
// Date Written	: 11/15/00					
// Purpose	: Game of life applet , which includes a board which the user
//              : would change by turning the cells into dead or alive ones, 
//              : then by choosing a cycles , they will go through that number 
//              : of cycles and each time through a set of rules , cells would 
//              : die, be born, stay alive or stay dead. The end results are
//              : always facinating


//import libraries

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

//main class

  public class GameOfLife extends Applet implements ActionListener
    {
// Variables used in program

      int x=200, y=200, start=1;
      int count,count2,count3,times,maincount,action;
      int[][] counter         = new int[14][14]; 
      Examinar exam1          = new Examinar();
      
      Button times1_button    = new Button ("1");
      Button times2_button    = new Button ("10");
      Button times3_button    = new Button ("100");
      Button times4_button    = new Button ("1000");
      Button[][] board_button = new Button[14][14];
      Button clear_button     = new Button ("clear");
      Button example_button   = new Button ("example");      

      Panel a_panel           = new Panel();
      Panel b_panel           = new Panel();      
      Panel c_panel           = new Panel ();
   
      Label start_lable       = new Label ("Start");
  
  public void init()
    {
// the panel that will contain the board buttons, adding the board in the 
// center of the applet, adding the menu at the top , cycle choices
// adding the menu at the buttom      
     
      setLayout (new BorderLayout ());
      add ("Center", a_panel);
      a_panel.setLayout (new GridLayout (14,14));
      a_panel.setBackground (Color.cyan);

      add ("North", b_panel);
      b_panel.setLayout(new GridLayout(1,5));
      b_panel.add(start_lable);
      b_panel.add(times1_button);
      b_panel.add(times2_button);
      b_panel.add(times3_button);
      b_panel.add(times4_button);
      times1_button.addActionListener(this);
      times2_button.addActionListener(this);
      times3_button.addActionListener(this);
      times4_button.addActionListener(this);
      
      add ("South",c_panel);
      c_panel.setLayout (new GridLayout(1,2));
      c_panel.add(clear_button);
      c_panel.add(example_button);
      clear_button.addActionListener(this);
      example_button.addActionListener(this);

// these two nested loops will initiate all the button
// and set them at standard condition ( blue ) and it will
// also reset the counter variable
    
    count = 0;
    while (count<14)
      {
        count2 = 0;     
        while (count2<14)
          {
            counter[count][count2] = 0;
            board_button[count][count2] = new Button ("");
            board_button[count][count2].setBackground (Color.blue);
            board_button[count][count2].addActionListener (this);
            a_panel.add (board_button[count][count2]);
            count2 ++; 
          }
            count ++;
       }

   }// end init
      
      
  public void actionPerformed (ActionEvent event)
    {
      
// determing how many times the cycle would run
      
       if (event.getSource() == times1_button) 
          {
             times  = 1; 
             action = 1;
          }
         
         if (event.getSource() == times2_button) 
            {
             times  = 10;
             action = 1;
            }
         
            if ((event.getSource() == times3_button)||(start == 0)) 
                {
                  times  = 100;
                  action = 1;
                  start  = 1;
                }
      
                if (event.getSource() == times4_button) 
                   {
                     times = 1000; 
                     action = 1;
                   }

// for clearing the board
                  if (event.getSource() == clear_button)
                     {
                       Clear();
                     }

// will set an example, a shape 
                    if (event.getSource() == example_button)
                       {
                          Example();
                       }    
      
      
// using a nested loop to get the responce to pressing any of the
// board buttons
                     count = 0;
                     while (count<14)
                       {
                         count2=0;
                         while (count2<14)
                           {
             
                             if (event.getSource() == board_button[count][count2])
                                {

// an alive cell has a value of 1 , adead one is zero
               
                               if (counter[count][count2] == 0)counter[count][count2] = 1;
                
                               else counter[count][count2] = 0;
  
// the builder is the method that translates the numbers
// zeros and ones into the button colors
               
                               Builder (counter);
                  
                              }
                                count2 ++;
                          }
                                count ++;
                      }


         
// will run after one of the cycle buttons is pressed
                    if (action == 1)
                       {
                          maincount = 0;
                          while (maincount<times)
                            {
              
// will send the counter values to the Examinar class which 
// will scan the numbers and determine the future condition of the cell
            
                              exam1.Results (counter);
             
// Builder will again show the results
                              Builder (counter);
                              maincount ++ ;
                             }
                               action = 0;
                        }
      
      
      
         
              repaint();   
         
   }//end actionPerformed
   

// the builder methos will use the counter numbers to determine
// if the cell is dead or alive and show the corresponding color
   
  public void Builder (int[][] x)
    {
      count = 0;
      while (count<14)
        {
           count2 = 0;
           while (count2<14)
             {
            
             if (x[count][count2] == 0) board_button[count][count2].setBackground (Color.blue);
             else board_button[count][count2].setBackground (Color.green);
            
             count2 ++;
             }
         
              count ++;
        }
   }//end Builder

// the clear method will reset the applet by returning all values to 
// their standard condition

  public void Clear ()
    {
       count = 0;
       while (count<14)
         {
            count2=0;
            while (count2<14)
              {
                board_button[count][count2].setBackground (Color.blue);
                counter[count][count2] = 0;
                count2 ++;
              } 
                count ++;
         } 
     
    } //end Clear
      
// this method will clear the board first then construct a shape
// and will run 100 cycles on it
   
  public void Example ()
    {
       Clear();
       board_button[5][6].setBackground (Color.green);
       board_button[6][5].setBackground (Color.green);
       board_button[6][7].setBackground (Color.green);
       board_button[7][6].setBackground (Color.green);
       counter[5][6] = 1;
       counter[6][5] = 1;
       counter[6][7] = 1;
       counter[7][6] = 1;
       start = 0;
   }//end Example
   
   }//end main class
   

