import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;

/* Requires GofLinterface to run it
*/

public class GameOfLife extends JPanel implements Runnable
{
    //applet member data
    JButton [] a; //1D array to simulate a 2D one
    int row = 20;
    int col = 20;
    int total = row * col;
    Color tracker [] []; //2D array to track colours in 1D button array
    Color dead = Color.white;
    Color alive = Color.blue;
    volatile boolean stop = false;

    public GameOfLife ()
    {
	setLayout (new GridLayout (row, col, 0, 0));

	//initialize tracking array to have all black backgrounds
	tracker = new Color [row] [col];
	initializeTracker ();
	//set up a blinker
	tracker [8] [8] = alive;
	tracker [9] [8] = alive;
	tracker [10] [8] = alive;

	//declare a new array of buttons
	a = new JButton [total];

	//initialize each of the buttons in the array
	//with an empty label
	for (int nNum = 0 ; nNum < total ; nNum++)
	{
	    a [nNum] = new JButton ("");
	    add (a [nNum]);
	    //each button will have an action listener
	    //a [nNum].addActionListener (this);
	    a [nNum].setBackground (dead);
	    //each button will send a message with its number
	    a [nNum].setActionCommand ("" + nNum);
	}

	resize (400, 400);

    }


    public void drawArray ()
    {
	//copy tracker's colours out into the button array
	int move = 0;
	for (int i = 0 ; i < row ; i++)
	{
	    for (int j = 0 ; j < col ; j++)
	    {
		a [move].setBackground (tracker [i] [j]);
		move++;
	    }
	}

    }


    public void stopit ()
    {
	stop = true;
    }


    public void initializeTracker ()
    {
	for (int i = 0 ; i < row ; i++)
	{
	    for (int j = 0 ; j < col ; j++)
	    {
		tracker [i] [j] = dead;
	    }
	}
    }


    public void updateScreen (String name)
    {
	stop = true;
	initializeTracker ();
	if (name.equals ("Blinker"))
	{
	    tracker [8] [8] = alive;
	    tracker [9] [8] = alive;
	    tracker [10] [8] = alive;
	}
	else if (name.equals ("Pi"))
	{
	    tracker [9] [8] = alive;
	    tracker [10] [8] = alive;
	    tracker [11] [8] = alive;
	    tracker [9] [9] = alive;
	    tracker [9] [10] = alive;
	    tracker [10] [10] = alive;
	    tracker [11] [10] = alive;
	}
	else if (name.equals ("R-Pentamino"))
	{
	    tracker [11] [10] = alive;
	    tracker [10] [11] = alive;
	    tracker [11] [11] = alive;
	    tracker [11] [12] = alive;
	    tracker [12] [10] = alive;
	}

	else //if (name.equals ("Beacon"))
	{
	    tracker [11] [9] = alive;
	    tracker [12] [9] = alive;
	    tracker [12] [10] = alive;
	    tracker [9] [11] = alive;
	    tracker [9] [12] = alive;
	    tracker [10] [12] = alive;
	}

	drawArray ();
    }


    public void run ()
    {
	stop = false;
	while (!stop)
	{
	    nextgen ();
	    drawArray ();
	    try
	    {
		Thread.sleep (150);
	    }
	    catch (InterruptedException x)
	    {
		;
	    }

	}


    }


    public void nextgen ()
    {
	Color next [] [] = new Color [row] [col];
	for (int i = 0 ; i < row ; i++)
	{
	    for (int j = 0 ; j < col ; j++)
	    {
		next [i] [j] = dead;

	    }
	}
	int count = 0;
	for (int i = 0 ; i < row ; i++)
	{
	    for (int j = 0 ; j < col ; j++)
	    {
		count = 0;
		if (i - 1 > 0 && j - 1 > 0 && tracker [i - 1] [j - 1].equals (alive))
		    count++;
		if (i - 1 > 0 && tracker [i - 1] [j].equals (alive))
		    count++;
		if (i - 1 > 0 && j + 1 < col && tracker [i - 1] [j + 1].equals (alive))
		    count++;

		if (j - 1 > 0 && tracker [i] [j - 1].equals (alive))
		    count++;
		if (j + 1 < col && tracker [i] [j + 1].equals (alive))
		    count++;

		if (i + 1 < row && j - 1 > 0 && tracker [i + 1] [j - 1].equals (alive))
		    count++;
		if (i + 1 < row && tracker [i + 1] [j].equals (alive))
		    count++;
		if (i + 1 < row && j + 1 < col && tracker [i + 1] [j + 1].equals (alive))
		    count++;

		if (tracker [i] [j].equals (alive) && (count == 2 || count == 3))
		    next [i] [j] = alive;
		else if (tracker [i] [j].equals (dead) && (count == 3))
		    next [i] [j] = alive;
	    }
	}

	for (int i = 0 ; i < row ; i++)
	{
	    for (int j = 0 ; j < col ; j++)
	    {
		tracker [i] [j] = next [i] [j];

	    }
	}
    }
}


