Java Programming The universe of the Game of Life is an infinite two-dimensional
ID: 3767053 • Letter: J
Question
Java Programming
The universe of the Game of Life is an infinite two-dimensional orthogonal grid of square cells, each of which is in one of two possible states, alive or dead. Every cell interacts with its eight neighbours, which are the cells that are horizontally, vertically, or diagonally adjacent. At each step in time, the following transitions occur: 1. Any live cell with fewer than two live neighbours dies, as if caused by under-population. 2. Any live cell with two or three live neighbours lives on to the next generation. 3. Any live cell with more than three live neighbours dies, as if by over-population. 4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction. Write an application using a two dimensional integer array of size 30 X 30 with some initial values (up to you) of 1s and zeroes. Fill in the entire array!!! Then write a method which implements one generational change (using the rules of the Game of life as explained above) on the array. Write another print method which prints the entire array out as a grid. Loop the method, printing out the entire array (as a grid) at the beginning and end. Be prepared to show me your output.
Explanation / Answer
// filename should be GameOfLife.java
// import Random class to generate numbers
import java.util.Random;
// import Scanner class to read input from user
import java.util.Scanner;
// Grid class which defines a grid of cells
class Grid
{
// variable to keep track of size of grid
private int SIZE = 30;
// two dimensional array to store grid contents
private int grid[][] = new int[SIZE][SIZE];
// Constructor which initializes the grid to random 0's and 1's
Grid()
{
Random random = new Random();
for(int i=0; i<SIZE; i++)
for(int j=0; j<SIZE; j++)
grid[i][j] = random.nextInt(2);
}
// generationChange() method updates the levliness of all chells for one cycle
public void generationChange()
{
for(int i=0; i<SIZE; i++)
{
for(int j=0; j<SIZE; j++)
{
// initialize neighbour_live_count of current cell to 0
int neighbour_live_count = 0;
// try to add the value of all the eight neighbours to neighbour_live_count
// if the cell is in the corner or edge of the grid, ArrayIndexOutOfBoundsException is raised
// Catch the exception and don't bother about it
try{
neighbour_live_count += grid[i-1][j-1];
}
catch(ArrayIndexOutOfBoundsException ref){}
try{
neighbour_live_count += grid[i-1][j];
}
catch(ArrayIndexOutOfBoundsException ref){}
try{
neighbour_live_count += grid[i-1][j+1];
}
catch(ArrayIndexOutOfBoundsException ref){}
try{
neighbour_live_count += grid[i][j-1];
}
catch(ArrayIndexOutOfBoundsException ref){}
try{
neighbour_live_count += grid[i][j+1];
}
catch(ArrayIndexOutOfBoundsException ref){}
try{
neighbour_live_count += grid[i+1][j-1];
}
catch(ArrayIndexOutOfBoundsException ref){}
try{
neighbour_live_count += grid[i+1][j];
}
catch(ArrayIndexOutOfBoundsException ref){}
try{
neighbour_live_count += grid[i+1][j+1];
}
catch(ArrayIndexOutOfBoundsException ref){}
// if the current cell is alive
if(grid[i][j] == 1)
// if the less than 2 or more than 3 neighbours are alive then this cell dies
if(neighbour_live_count < 2 || neighbour_live_count >3)
grid[i][j] = 0;
//else it remains same
else // if cell is dead
// if 3 neighbours are alive then current cell come to lifee
if(neighbour_live_count ==3)
grid[i][j] = 1;
// else remains dead
}
}
}
// displayGrid() method to print the status of all cells in grid form
public void displayGrid()
{
for(int i=0; i<SIZE; i++)
{
for(int j=0; j<SIZE; j++)
System.out.print(grid[i][j] + " ");
System.out.println();
}
System.out.println();
}
}
// GameOfLife class which creates an object of Grid and call methods of Grid
class GameOfLife
{
public static void main(String[] args)
{
// create an objecg of Scanner
Scanner scanner = new Scanner(System.in);
// create an object of Grid
// Grid's constructor will initialize the cell values to 0 or 1
Grid g = new Grid();
int input = 1;
// display initial grid
g.displayGrid();
// loop as long as input is 1
while(input == 1)
{
// change one generation
g.generationChange();
// display grid after changing generation
g.displayGrid();
// Ask user if another generational change is to be carried out
System.out.println("Do you want go for another Generatinal change??");
// Ask user to enter 1 to continue and 0 to exit
System.out.print("Enter Yes or No (1|0) : ");
// read user input
input = scanner.nextInt();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.