// Ryan Kightlinger
// April 20, 2002
// This Program will display a conway's game of life


// Get the necessary Java library methods
import java.awt.*;
import java.applet.*;
import java.awt.event.*;


public class gameOfLife extends Applet implements ActionListener, MouseListener {

	private Button nextGeneration;
	private Table cells;


	public void init() {

		cells = new Table();
		nextGeneration = new Button("Next Generation");
		add(nextGeneration);
		nextGeneration.addActionListener(this);
	}


	public void paint (Graphics g) {

		for (int yCoord=20; yCoord <= 520; yCoord+=20) {
			g.drawLine(20,yCoord,520,yCoord);
		}

		for (int xCoord=20; xCoord <= 520; xCoord+=20) {
			g.drawLine(xCoord,20,xCoord,520);
		}

		cells.display(g);

	}


	public void actionPerformed(ActionEvent event) {

		if (event.getSource() == nextGeneration) {
			cells.nextGen()
		}

		repaint();
	}





	// mouse click actions
	public void mouseClicked( MouseEvent event) { 
    		int xCoord = event.getX(); 
  	        int yCoord = event.getY(); 
 	        cells.gridSelect(yCoord,xCoord);
                repaint(); 
        } 


	public void mouseReleased(MouseEvent e) {} 
	public void mousePressed(MouseEvent e) {} 
	public void mouseEntered(MouseEvent e) {} 
	public void mouseExited(MouseEvent e ) {} 


}



class Table {

	public int [][] neighbors = new int [27][27];
	public int [][] life = new int [27][27];

	public Table() {
		
		for (int i=0; i < 27; i++) {
			for (int j=0; j < 27; j++) {
				neighbors[i][j] = 0;
				life[i][j] = 0;
			}
		}
	}




	public void display(Graphics g) {
		
		int xCoord = 29;
		int yCoord = 37;

		// g.drawString( something

		for (int i=1; i < 26; i++) {
			for (int j=1; j <26; j++) {
				if (life[i][j] == 1)
					g.drawString("*",xCoord,yCoord);
					yCoord +=20;
                        }
			
			xCoord +=20;
			yCoord = 37;
		}

	}




	public void changeValue(int x, int y) {

		life[x][y] = (life[x][y] + 1) % 2;
	}



	public void gridSelect(int y, int x) {

		int row = (y-20) / 20;
		int column = (x - 20) / 20;
	
		life[row][column] = (life[row][column] + 1 % 2);
	}





	public void nextGen() {


		for (int i = 0; i < 27; i++)
			for (int j = 0; j < 27; j++)
				life[i][j] = neighbors[i][j]



		for (int i = 1; i < 26, i++)
			for (int j=1; j < 26; j++) {
				int sumOfNeighbors = life[i-1][j-1] + life[i-1][j] + life[i-1][j+1] ;

				if (life[i][j] = 1 && (sumOfNeighbors == 2 || sumOfNeighbors = 3)
					neighbors[i][j] == 1;

				else //die
					neighbors[i][j] = 0;

				
				if (life[i][j] = 0 && (sumOfNeighbors == 3)
					neighbors[i][j] = 1;

			}


	}



}		
