/**
* Write a description of class Cell here.
*
* @author Mark Goddard
* @version 20/11/2011
*/
public class Cell
{
// instance variables
private final int ROW = 5;
private final int COL = 4;
int[][] currentGeneration = new int[ROW+1][COL+1];
int[][] nextGeneration = new int[ROW+1][COL+1];
int row = 0;
int col = 0;
/**
* Constructor for objects of class Cell
*/
public Cell()
{
// initialise instance variables
}
/**
* Intialising/Setting generation 1
*
* @param None
* @return None
*/
public void generation1()
{
for (int row = 0; row < currentGeneration.length; row++)
for(int col = 0; col < currentGeneration[row].length; col++)
{
{
currentGeneration[1][1] = 1;
currentGeneration[2][3] = 1;
currentGeneration[3][2] = 1;
currentGeneration[4][1] = 1;
currentGeneration[3][3] = 1;
currentGeneration[2][3] = 1;
currentGeneration[3][1] = 1;
currentGeneration[row][col] = 0;
}
}
for (int row = 0; row < currentGeneration.length; row++)
{
for(int col = 0; col < currentGeneration[row].length; col++)
{
System.out.print(currentGeneration[row][col]);
}
System.out.println();
}
}
/**
* Game of Life Rules
*
* @param None
* @return None
*/
public int countNeighbour(int count)
{
count = 0;
if (currentGeneration[row-1][col] == 1)
{
count++;
}
if (currentGeneration[row][col-1] == 1)
{
count++;
}
if (currentGeneration[row-1][col-1] == 1)
{
count++;
}
if (currentGeneration[row-1][col+1] ==1)
{
count++;
}
if (currentGeneration[row+1][col] ==1)
{
count++;
}
if (currentGeneration[row+1][col+1] ==1)
{
count++;
}
if (currentGeneration[row][col+1] ==1)
{
count++;
}
if (currentGeneration[row-1][col+1] ==1)
{
count++;
}
return count;
}
/**
* Game of Life Rules
*
* @param None
* @return None
*/
public void rules(int count)
{
for (int row = 1; row < currentGeneration.length - 1; row++)
{
for (int col = 1; col < currentGeneration.length - 1; col++)
{
if(currentGeneration[row][col] == 1)
{
// Any live cell with fewer than two live neighbours dies
if(countNeighbour(count) < 2)
{
nextGeneration[row][col] = 0;
}
// Any live cell with more than three live neighbours dies
if(countNeighbour(count) > 3)
{
nextGeneration[row][col] = 0;
}
}
else
{
// Any dead cell with exactly three live neighbours becomes a live cell
if(countNeighbour(count) == 3)
{
nextGeneration[row][col] = 1;
}
}
}
}
}
/**
* Game of Life Rules
*
* @param None
* @return None
*/
private void copyBoard()
{
// copies all the nextBoard values to the currBoard values
for (int row = 0; row < nextGeneration.length - 1; row++)
{
for (int col = 0; col < nextGeneration.length - 1; col++)
{
nextGeneration[row][col] = currentGeneration[row][col];
}
}
}
/**
* Game of Life Rules
*
* @param None
* @return None
*/
public void print()
{
for (int row = 0; row <= row; row++)
{
for (int col = 0; col <= col; col++)
{
System.out.println(nextGeneration);
}
}
}
}
/**
* A class which presents the user with a menu from which
* they can choose and demonstrates the game of life.
* @author Mark Goddard
* @version 31/10/2011
*/
public class Menu
{
// instance variables
/**
* Constructor for objects of class Menu
*/
public Menu()
{
// initialise instance variables
}
/**
* Method to display the menu
*
* @param None
* @return None
*/
public static void displayMenu()
{
// Printing the individual choices to the screen
System.out.println("GAME OF LIFE");
System.out.println("");
System.out.println("Main Menu: ");
System.out.println("1 - Load a Generation");
System.out.println("2 - Save game");
System.out.println("3 - Load saved game");
System.out.println("0 - Exit");
// Class the menu method as it defines what happens for each number chosen by the user
Menu();
}
/**
* Method to define the menu choices
*
* @param None
* @return None
*/
public static void Menu()
{
// Creating a local variable
int choice;
// Printing a message to the user
System.out.println("");
System.out.print("Please enter either number 1, 2, 3 or 4 for a particular option: ");
// Using Genio method to get choice
choice = Genio.getInteger();
// Code for defining the choices available
do if (choice == 1)
{
Cell Menu = new Cell();
Menu.generation1();
Menu1();
}
else if (choice == 2)
{
}
else if (choice == 3)
{
}
else if (choice == 4)
{
}
else if (choice == 0)
{
System.out.println("Goodbye, program is closing");
}
else
{
System.out.println("Command not recognised");
}
while (false);
}
/**
* Method to define the menu choices
*
* @param None
* @return None
*/
public static void Menu1()
{
// Creating a local variable
int choice;
// Printing a message to the user
System.out.println("");
System.out.println("Please 1 for the next generation: ");
// Using Genio method to get choice
choice = Genio.getInteger();
// Code for defining the choices available
do if (choice == 1)
{
Cell Menu = new Cell();
Menu.print();
}
else
{
System.out.println("Command not recognised");
}
while (false);
}
}

New Topic/Question
Reply



MultiQuote





|