Game of Life

how to handle mouse listener so that it gets updated in the initialisa

Page 1 of 1

2 Replies - 1268 Views - Last Post: 23 July 2009 - 09:05 PM Rate Topic: -----

#1 greenwaycharlessiva   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 5
  • Joined: 16-December 07

Game of Life

Post icon  Posted 22 July 2009 - 10:30 PM

 
/**
 * 
 */
package puzzle.gameoflife;

/**
 * @author charles
 *
 */
public class GameOfLife 
{
	int noOfrow; 
	int noOfcolumn;
	boolean currentGen[][] = new boolean[3][5];
	boolean nextGen[][] =  new boolean[noOfrow][noOfcolumn];

	boolean LIVING = true;
	boolean DEAD = false;
	boolean REGENERATED = true;

	int neighbours[][] = new int[noOfrow][noOfcolumn];

	public GameOfLife(int row, int col)
	{
		
		this.noOfrow = row;
		this.noOfcolumn = col;
		
		for (row = 0; row < noOfrow; row++)
		{
			for (col = 0; col < noOfrow; col++)
			{

				currentGen[row][col] = DEAD;		//initially dead
				System.out.println(row + col);
			}
		}

	}



	public void populateGameOfLife()
	{
		DisplayOutput.createAndShowGUI(noOfrow, noOfcolumn);
		//this.generateNextGen();
	}

	public void generateNextGen()
	{

		for (int row = 0; row < noOfrow; row++ )
		{
			for (int col = 0; col < noOfcolumn; col++)
			{
				nextGen[row][col] = currentGen[row][col];	//Array Copy
				neighbours[row][col] = this.countNeighbours(row, col);
				if(neighbours[row][col] < 2)		//under population 
				{
					nextGen[row][col] = DEAD;
				}
				if(neighbours[row][col] > 3)		//over Crowding	
				{
					nextGen[row][col] = DEAD;
				}
				if(neighbours[row][col] == 2 && 
						neighbours[row][col] == 3)	//
				{
					nextGen[row][col] = LIVING;
				}
				if(currentGen[row][col] == DEAD &&
						neighbours[row][col] == 3)	//regeneration
				{
					nextGen[row][col] = REGENERATED;
				}
			}
		}

	}

	public int countNeighbours(int row, int col)
	{
		int neighbour = 0;


		if(currentGen[row-1][col-1])
		{
			neighbour++;
		}
		if(currentGen[row-1][col])
		{
			neighbour++;
		}

		if(currentGen[row-1][col+1])
		{
			neighbour++;
		}

		if(currentGen[row][col-1])
		{
			neighbour++;
		}
		if(currentGen[row][col])
		{
			neighbour++;
		}
		if(currentGen[row][col+1])
		{
			neighbour++;
		}
		if(currentGen[row+1][col-1])
		{
			neighbour++;
		}
		if(currentGen[row+1][col])
		{
			neighbour++;
		}
		if(currentGen[row+1][col+1])
		{
			neighbour++;
		}



		return neighbour;
	}

	public void showGameOfLife()
	{
		//DisplayOutput dOut = new DisplayOutput(noOfrow,noOfcolumn);
		DisplayOutput.showOutput(nextGen);


		//System.out.println("randomC" + randomNumberforColumn + "randomR" + randomNumberforRow);
	}
}





package puzzle.gameoflife;

import java.io.IOException;


public class GolImplementation extends DisplayOutput
{

	
	public GolImplementation(int noOfrow, int noOfcolumn) {
		super(noOfrow, noOfcolumn);
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException
	{
		try
		{
			final int noRows = Integer.parseInt(args[0]);
			final int noCols = Integer.parseInt(args[1]);
					
			GameOfLife gol = new GameOfLife(noRows ,noCols);	
			gol.populateGameOfLife();

		}
		
		catch(ArrayIndexOutOfBoundsException ae)
		{
			//System.out.println("Array index out of memory");
		}
	}
}





package puzzle.gameoflife;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class DisplayOutput  implements ActionListener, MouseListener
{ 
	static int row;
	static int column;
	
	static JFrame golFrame = new JFrame("Game Of Life");
	static JPanel golPanel = new JPanel();
	static JButton golButton[][] = new JButton[row][column];


	public DisplayOutput(int noOfrow, int noOfcolumn) 
	{
		row = noOfrow;
		column = noOfcolumn;

	}

	
	public static void createAndShowGUI(int row, int column) {


		
		golFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		golFrame.setLocation(50, 30);
		//golFrame.setSize(1000, 800);
		golFrame.setResizable(false);

		
		golPanel.setLayout(new GridLayout(row,column));
		golFrame.add(golPanel);

		
		for ( int i = 0; i < row; i++)
			for(int j = 0; j < column; j++)
			{	

				{
					golButton[i][j] = new JButton();
					golPanel.add(golButton[i][j]);

				}
			}
		
		golFrame.pack();
		golFrame.setVisible(true);
	}

	public void actionPerformed(ActionEvent event) {
		// TODO Auto-generated method stub
		
		

	}

	public void mouseClicked(MouseEvent event) {
		// TODO Auto-generated method stub
		System.out.println("nnnnnnnnnn");
	}

	public void mouseEntered(MouseEvent event) {
		// TODO Auto-generated method stub

	}

	public void mouseExited(MouseEvent event) {
		// TODO Auto-generated method stub

	}

	public void mousePressed(MouseEvent event) {
		// TODO Auto-generated method stub
		System.out.println("pppppppp");
	}

	public void mouseReleased(MouseEvent event) {
		// TODO Auto-generated method stub

	}
	
	public static void showOutput(boolean[][] nextGen)
	{
		for(int r = 0; r < row ; r++)
		{
			for(int c = 0; c < column; c++ )
			{

				if(nextGen[r][c])
				{
					golButton[r][c].setForeground(Color.BLACK);
				}
			}
		}
		golFrame.pack();
		golFrame.setVisible(true);
	}


}

Attached File  gameoflife.zip (2.46K)
Number of downloads: 73Attached File  gameoflife.zip (2.46K)
Number of downloads: 73

Is This A Good Question/Topic? 0
  • +

Replies To: Game of Life

#2 syfran   User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: Game of Life

Posted 22 July 2009 - 10:32 PM

What is your question?
Was This Post Helpful? 0
  • +
  • -

#3 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Game of Life

Posted 23 July 2009 - 09:05 PM

Coded a Game of Life in Fortran 35 years ago
Don't even remember how it works
What is your question ?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1