/* 
This is the Game of Life, created 11/6/2000 by David Bierbaum.
The Game of Life is based on the interaction of adjacent 'cells' on a grid 'world' with two states (alive
or not alive). The basic idea is that the world starts with a population of certain cells being alive. The
next generation is calculated on three rules applied to the previous generation. The rules are: if a cell is empty and has at least three neighboring cells that are alive, then the empty cell becomes alive (it is born), otherwise it remains empty. if a live cell has more than three neighbors it dies due to overcrowding. if a live cell has less than two neighbors it dies due to loneliness. othwise a living cell stays alive. It consists of 3 classes, the Interface, Board, and Cell. 
*/


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

public class Interface extends Applet implements ActionListener
{
// Initialize Variables and set up button/field/panel/labels

int runtimes = 1;
int birthrule = 2;
int lonelyrule = 2;
int crowdrule = 3;
Label birthLabel = new Label("Birth> ");
Label crowdLabel = new Label("Crowding> ");
Label lonelyLabel = new Label("Lonely< ");
Label runLabel = new Label("# Runs= ");
Label title = new Label("GameLife Bierbaum 2000");
Button clear = new Button("Clear");
Button update = new Button("Update Rules");
Button reset = new Button("Reset Rules");
Button run = new Button("Run");
TextField runField = new TextField(1);
TextField birthField = new TextField(2);
TextField crowdField = new TextField(3);
TextField lonelyField = new TextField(2);
Board world = new Board(this);
Panel textpanel = new Panel();
Panel buttonpanel = new Panel();
Panel buttoncpanel = new Panel();
Panel eastpanel = new Panel();
Panel westpanel = new Panel();
Panel centerpanel = new Panel();
Panel textcpanel = new Panel();
Panel textepanel = new Panel();

public void init()
{

// set up User Interface

setLayout(new BorderLayout());
add("South", textpanel);
add("Center", world);
add("North", buttonpanel);
buttonpanel.setLayout(new BorderLayout());
buttonpanel.add("Center", buttoncpanel);
buttoncpanel.setLayout(new BorderLayout());
buttonpanel.add("West", clear);
buttonpanel.add("East", update);
buttoncpanel.add("East", reset);
buttoncpanel.add("Center", title);
buttoncpanel.add("West", run);
clear.addActionListener(this);
update.addActionListener(this);
reset.addActionListener(this);
run.addActionListener(this);
textpanel.setLayout(new BorderLayout());
textpanel.add("East", eastpanel);
eastpanel.setLayout(new BorderLayout());
eastpanel.add("East", textepanel);
textepanel.add("West", runLabel);
textepanel.add("East", runField);
eastpanel.add("West", birthField);
birthField.addActionListener(this);
textpanel.add("Center", centerpanel);
centerpanel.setLayout(new BorderLayout());
centerpanel.add("West", crowdLabel);
centerpanel.add("Center", textcpanel);
textcpanel.setLayout(new BorderLayout());
textcpanel.add("West", crowdField);
crowdField.addActionListener(this);
textpanel.add("West", westpanel);
westpanel.setLayout(new BorderLayout());
westpanel.add("West", lonelyLabel);
westpanel.add("East", lonelyField);
centerpanel.add("East", birthLabel);
lonelyField.addActionListener(this);
crowdField.setText("3");
birthField.setText("2");
lonelyField.setText("2");
runField.setText("1");
}

public void actionPerformed(ActionEvent event)
{
if (event.getSource() == run) world.runGame(runtimes, birthrule, crowdrule, lonelyrule);
if (event.getSource() == reset)
{
   runtimes = 1;
   birthrule = 2;
   crowdrule = 3;
   lonelyrule = 2;
   crowdField.setText("3");
   birthField.setText("2");
   lonelyField.setText("2");
   runField.setText("1");
}
if (event.getSource() == clear) world.clear();

// if update is clicked, all the values in the textfields get put into // their respective variables.

if (event.getSource() == update)
{
   runtimes = Integer.parseInt(runField.getText());
   birthrule = Integer.parseInt(birthField.getText());
   crowdrule = Integer.parseInt(crowdField.getText());
   lonelyrule = Integer.parseInt(lonelyField.getText());
}

// if no buttons have been pressed, the user must have clicked on a // cell. This method from the Board class toggles the cell dead/alive.

world.activity(event);
repaint();
}


}
