Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

The file ConwaysLife.java has a partial implementation of Conway's game of life that you are asked to complete. The game simulates the evolution of a population by calculatinga sequence of generations...

1 answer below »
The file ConwaysLife.java has a partial implementation of Conway's
game of life that you are asked to complete.
The game simulates the evolution of a population by calculatinga sequence of generations according to some simple rules. Thecalculation requires two arrays that are each two dimensional,because the next generation must be calculated into a secondarray, which then becomes the current generation.
You must complete the code needed to perform that update.
WHAT YOU HAVE TO DO
1. create a project in Eclipse for this lab
2. within that project create a Java class, ConwaysLife.java, and copy that file from the archive to the Eclipse class(you will need to delete the text Eclipse puts in the class).
3. In the class
a. code the neighborCount method as specified
b. code the cellIsOccupiedInNextGen as specified
c. complete the coding of the calculateNextGeneration method as specified
All methods you must code are marked
YOU MUST CODE THIS
4. Try running your program on the .txt input files below or by keying in the contents of one of the .txt input files in the archive. To read from the file directly, you would need to download it from the archive into Eclipse.
blinker.txt toad.txt glider.txt pulsar.txt
The gridsize should be at least 17 for pulsar.txt.

5. If they appear to run correctly, submit the ConwaysLife.java source code file to Blackboard.

There is more information about the game at the Wikipedia article
https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life







Answered Same Day Jul 28, 2021

Solution

Aditya answered on Jul 29 2021
133 Votes
import java.util.*;
import java.io.*;
******************************************************************
Lab Exercise II
Code the methods below that you are asked to code, and submit the
modified class file to Blackboard. Include the names of the people
on the team right below.
Team members:
This program plays Conway's famous Game of Life, which is discussed at
http:
en.wikipedia.org/wiki/Conway%27s_Game_of_Life
The game requires a 2D a
ay of int(or boolean), n by n, for some n >= 3.
Each cell that is not on the border of the a
ay will have EIGHT
neighboring cells. Cells in the corner will have only three neighbors,
and cells on the border but not in the corner will have five neighbors.
Briefly, the initial configuration is a grid of cells such that each
cell is either occupied with a single organism, or not occupied. The next
generation, which also will have each cell occupied by a single
organism or not occupied, is determined from the cu
ent by the following rules
(suppose the cell is indexed by [i][j])
If the cell is cu
ently occupied then
if the number of occupied neighboring cells is < 2
the organism in cell[i][j] dies from loneliness, so
cell[i][j] becomes unoccupied
else if the number of occupied neighboring cells is > 3
the organism in cell[i][j] dies from overcrowding, so
cell[i][j] becomes unoccupied
else
there are 2 or 3 occupied neig
oring cells
the organism in cell[i][j] survives, so cell[i][j]
remains occupied
else
the cell is cu
ently unoccupied
if the number of occupied neighboring cells is exactly 3,
a new organism is born into cell[i][j], so it becomes
occupied
else
cell [i][j] remains unoccupied
Note, you cannot update a single a
ay in place, since if a
cell changes, it will affect the calculation for its neighboring
cells. To calculate the change of a single generation, YOU MUST
USE TWO ARRAYS, one for the cu
ent generation and a second for the
next generation that is calculated from the cu
ent generation.
For convenience, the a
ay has extra rows and columns so that a
cell that might actually hold an organism always has eight neighboring
cells within the a
ay, but the outermost border of the a
ay(topmost and
ottommost rows and leftmost and rightmost columns) should never be
loaded with organisms.
This implementation allows the user to select the size of the board
and key in cell indices that are occupied or read the indices from a text file
and other run parameters.
************************************************************************
public class ConwaysLife{

private static Scanne

for reading from the keyboard
stdIn = new Scanner(System.in),

for reading from the file(optional)
fileScanner;

private static final int

values to control the size of the grid
MIN_GRID_SIZE = 6,
MAX_GRID_SIZE = 100;

private static int

the size of the non-border a
ay; user selects
gridSize;

private static int[][]

NOTE: if GRID_SIZE is n, that a
ays will be (n+2) by (n+2)

to hold the extra rows and columns on the borde

a
ay for the cu
ent generation
cu
entGeneration,

and an a
ay for calculating the next generation
auxA
ay;

private static boolean

true when the user wants to load the grid randomly
loadRandomly;

private static boolean

true when the user wants to pause between generations
pauseBetweenGenerations;

private static double

when the grid is loaded randomly, the probability that

a cell will be occupied
probOfOccupancyInInitialGrid;

private static int

maximum number of generations to calculate
maxGenerations;
private static void getConfigurationParameters(){
/*
Prompts the user and reads in
gridSize
whether to load the grid randomly or from user keyed cell indices
(y or Y means yes, load the grid randomly)
number of generations to print(will quit if the populations dies out)
whether to pause between printing each generation
***************************************************************

System.out.println("Enter a size n for the grid(" +
MIN_GRID_SIZE + " <= n <= " + MAX_GRID_SIZE + ')');


this illustrates using a try-catch block to avoid throwing

an exception
try{
gridSize = stdIn.nextInt();
while (gridSize < MIN_GRID_SIZE || gridSize > MAX_GRID_SIZE){
System.out.println("Value must be >= " + MIN_GRID_SIZE +
" and <= " + MAX_GRID_SIZE + ". Please try again.");
gridSize = stdIn.nextInt();
}
}
catch (Exception e){
System.out.println("E
or on the input for grid size.\n" + e.toString());
System.out.println("\nProgram terminating.");
e.printStackTrace();
System.exit(0);
}


add rows and columns for the border area
cu
entGeneration = new int[gridSize + 2][gridSize + 2];
auxA
ay = new int[gridSize + 2] [gridSize + 2];

System.out.println("Do you want...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here