0 Replies - 762 Views - Last Post: 27 July 2009 - 12:15 AM 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 initial seed Generation

Posted 27 July 2009 - 12:15 AM

I need the initial seed For Game Of Life for any number of rows and columns

I had generated a one in Game Of Life two argument constructor, but it had not come upto my expectation.

here is my code.........

<code>
/**
*
*/
package puzzle.gameoflife;


/**
* The Class Game Of Life
*
* @author Charles Green Way
*
*/
public class GameOfLife
{
static int noOfrow;
static int noOfcolumn;
boolean currentGen[][];// = new boolean[noOfrow][noOfcolumn];
boolean nextGen[][]; //= new boolean[noOfrow][noOfcolumn];


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

int neighbours[][];

public GameOfLife() {
noOfrow = 12;
noOfcolumn =36;
boolean temp[][] = {
{ DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,true,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD },
{ DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,true,DEAD,true,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD },
{ DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,true,true,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,true,true,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,true,true },
{ DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD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true,true,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD},
{DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,true,true,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD},
{DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,true,true,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD},
{DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,true,true,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD},
{true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true},
{true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true},
{DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,true,true,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD},
{DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,true,true,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD},
{DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,true,true,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD},
{DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,true,true,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD},
{DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,true,true,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD},
{DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,true,true,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD},
{DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,true,true,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD},
{DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,true,true,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD},
{DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,true,true,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD,DEAD},
};
currentGen = new boolean[12][36];
for (int row = 0; row < noOfrow ; row++ )
{
for (int col = 0; col < noOfcolumn ; col++)
{
currentGen[row][col] = temp[row][col]; //Array Copy
}
}
nextGen = new boolean[12][36];
neighbours = new int[12][36];
}

public GameOfLife(int row, int col)
{
noOfrow = row;
noOfcolumn = col;
currentGen = new boolean[row][col];
nextGen = new boolean[row][col];
neighbours = new int[row][col];

/*
* Initial Seed generation..
* <p>
* I need a help on this so that it generates a better seed
* for all inputs
*/
for (row = 0 ; row < noOfrow; row++)
{
for (col = 0 ; col < noOfcolumn; col++)
{

double rand = Math.random();
int c;
rand *= noOfrow;
int randInt = (int)rand;
if(noOfcolumn < noOfrow)
{
c = noOfrow / noOfcolumn + 3;
}
else
{
c= noOfcolumn / noOfrow + 3;
}
//generating random
if( randInt < c )

{
currentGen[row][col] = LIVING; //populating dead
}
else
{
currentGen[row][col] = DEAD; //populating living
}
}

}
}


public void showGameOfLife() throws InterruptedException
{
DisplayOutput dout = new DisplayOutput();
dout.createAndShowGUI(currentGen,noOfrow, noOfcolumn);
dout.updateGUI(currentGen,noOfrow, noOfcolumn);

while (this.checkGen())
{
this.generateNextGen();
Thread.currentThread();
Thread.sleep(50);
dout.updateGUI(nextGen,noOfrow, noOfcolumn);
this.replaceGen();

}
}

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

public int countNeighbours(int row, int col)
{
int neighbour = 0;
if(row > 0 && row < noOfrow-1 && col > 0 && col < noOfcolumn-1)
{
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+1]) neighbour++;
if(currentGen[row+1][col-1]) neighbour++;
if(currentGen[row+1][col]) neighbour++;
if(currentGen[row+1][col+1]) neighbour++;
}
else if ( row == 0 && col == 0 )
{ //top left
if (currentGen[row][col+1]) neighbour++;
if (currentGen[row+1][col+1]) neighbour++;
if (currentGen[row+1][col]) neighbour++;
}
else if ( row == 0 && col == noOfcolumn-1) //topright corner
{
if (currentGen[row+1][col]) neighbour++;
if (currentGen[row+1][col-1]) neighbour++;
if (currentGen[row][col-1]) neighbour++;
}
else if ( row == noOfrow-1 && col == 0 ) //bottom left corner
{
if (currentGen[row-1][col]) neighbour++;
if (currentGen[row-1][col+1]) neighbour++;
if (currentGen[row][col+1]) neighbour++;
}
else if ( row == noOfrow-1 && col == noOfcolumn-1 ) //bottom right
{
if (currentGen[row][col-1]) neighbour++;
if (currentGen[row-1][col-1]) neighbour++;
if (currentGen[row-1][col]) neighbour++;
}
else if ( row == 0 && (col > 0 && col < noOfcolumn-1)) //Top edge of Board not including first and last element
{
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++;
if (currentGen[row][col-1]) neighbour++;
}
else if ( row == noOfrow -1 && (col > 0 && col < noOfcolumn-1))
//Bottom edge of Board not including first and last element
{
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++;
if (currentGen[row][col+1]) neighbour++;
}
else if (( row > 0 && row < noOfrow-1) && col == 0)
//Left edge of Board not including first and last element
{
if (currentGen[row-1][col]) neighbour++;
if (currentGen[row-1][col+1]) neighbour++;
if (currentGen[row][col+1]) neighbour++;
if (currentGen[row+1][col+1]) neighbour++;
if (currentGen[row+1][col]) neighbour++;
}
else if (( row > 0 && row < noOfrow-1) && col == noOfcolumn-1)
{ //Right edge of Board not including first and last element
if (currentGen[row+1][col]) neighbour++;
if (currentGen[row+1][col-1]) neighbour++;
if (currentGen[row][col-1]) neighbour++;
if (currentGen[row-1][col-1]) neighbour++;
if (currentGen[row-1][col]) neighbour++;
}
return neighbour;
}


public void replaceGen()
{
for (int row = 0; row < noOfrow ; row++ )
{
for (int col = 0; col < noOfcolumn ; col++)
{
currentGen[row][col] = nextGen[row][col];
if(currentGen[row][col])
System.out.print(" 1");
else
System.out.print(" 0");
}
System.out.println();
}
System.out.println();
System.out.println();
System.out.println();
}

public boolean checkGen()
{
for (int row = 0; row < noOfrow ; row++ )
{
for (int col = 0; col < noOfcolumn ; col++)
{
if(currentGen[row][col])
{
return true;
}
}
}
return false;
}
}

</code>

Here is the class which displays the Game Of life in swings

<code>

package puzzle.gameoflife;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;

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

public class DisplayOutput {
static JFrame golFrame = new JFrame("Game Of Life");
static JPanel golPanel = new JPanel();
static JButton golButton[][];

public void createAndShowGUI(boolean[][] nextGen, int row, int column) {

golFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
golFrame.setLocation(20, 30);
golFrame.setSize(row * 8,column * 8);
golFrame.setResizable(true);
golFrame.setVisible(true);

golPanel.setLayout(new GridLayout(row, column, 0, 0));
golPanel.setSize(new Dimension(row * 4, column * 4));
golFrame.getContentPane().add(golPanel);

golButton = new JButton[row][column];

for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
golButton[i][j] = new JButton();
golButton[i][j].setPreferredSize( new Dimension(15,15));
golButton[i][j].setBackground(Color.BLACK);
golPanel.add(golButton[i][j]);
golButton[i][j].setVisible(true);
}
}

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

public void updateGUI(boolean[][] nextGen, int row, int column)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < column; j++)
{
if (nextGen[i][j]== false)
{
golButton[i][j].setBackground(Color.BLACK);

}
else
{
golButton[i][j].setBackground(Color.YELLOW);

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

}
}

</code>

This is the code which uses Game Of Life

<code>
/**
*
*
*/

package puzzle.gameoflife;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
*
* Implements Game Of Life
* <p> If the user enter the value of the row or column less than 3
* The default game of life is shown
* Else a random seed is generated and and the corresponding
* Game Of Life is shown
*
* @author Charles Green Way
*
*/
public class GolImplementation
{
/**
* @param args
* @throws IOException
* @throws InterruptedException
*/
public static void main(String[] args) throws IOException, InterruptedException
{
System.out.println("Enter a value ");
System.out.println(" Greater than 3 for random Game Of life" );
System.out.println(" Less than 3 for DEFAULT Game Of life ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the row for the Grid :");
int noRows = Integer.parseInt(br.readLine());
System.out.println("Enter the column for the Grid :");
int noCols = Integer.parseInt(br.readLine());
if( noRows < 3 || noCols < 3)
{
System.out.println("Enter a value greater than 3");
GameOfLife gol = new GameOfLife();
gol.showGameOfLife();
}
else
{
GameOfLife gol = new GameOfLife(noRows ,noCols);
gol.showGameOfLife();
}
}
}

</code>

Is This A Good Question/Topic? 0
  • +

Page 1 of 1