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

















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 array of int(or boolean), n by n, for some n >= 3. Each cell that is not on the border of the array 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 current by the following rules (suppose the cell is indexed by [i][j]) If the cell is currently 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 neigbboring cells the organism in cell[i][j] survives, so cell[i][j] remains occupied else // the cell is currently 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 array 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 current generation and a second for the next generation that is calculated from the current generation. For convenience, the array has extra rows and columns so that a cell that might actually hold an organism always has eight neighboring cells within the array, but the outermost border of the array(topmost and bottommost 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 Scanner // 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 array; user selects gridSize; private static int[][] // NOTE: if GRID_SIZE is n, that arrays will be (n+2) by (n+2) // to hold the extra rows and columns on the border // array for the current generation currentGeneration, // and an array for calculating the next generation auxArray; 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("error="" 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="" currentgeneration="new" int[gridsize="" +="" 2][gridsize="" +="" 2];="" auxarray="new" int[gridsize="" +="" 2]="" [gridsize="" +="" 2];="" system.out.println("do="" you="" want="" to="" load="" the="" initial="" grid="" randomly?"="" +="" "\nenter="" y="" or="" n.");="" this="" illustrates="" using="" the="" scanner's="" hasnext="" method="" to="" test="" for="" the="" availability="" of="" input,="" an="" alternative="" to="" the="" try-catch="" if="" (stdin.hasnext()){="" char="" reply="stdIn.next().charAt(0);" loadrandomly="reply" =='y' ||="" reply="=" 'y';="" }="" else{="" system.out.println("reached="" end="" of="" file="" without="" a="" response.");="" system.out.println("\nprogram="" terminating.");="" system.exit(0);="" }="" clear="" the="" line="" stdin.nextline();="" give="" user="" the="" opportunity="" to="" read="" from="" a="" file="" if="" (!loadrandomly){="" system.out.println("if="" you="" would="" like="" to="" read="" the="" initial="" configuration\n"="" +="" "from="" a="" file,="" enter="" the="" name="" of="" the="" file.="" if="" you="" want="" to="" enter\n"="" +="" "the="" data="" from="" the="" keyboard,="" just="" hit="" enter.");="" try{="" string="" filename="stdIn.nextLine();" if="" (!filename.equals(""))="" read="" from="" a="" file="" filescanner="new" scanner(new="" file(filename));="" }="" catch(exception="" e){="" system.out.println("some="" error="" on="" the="" input="" threw="" an="" exception.\n"="" +="" "program="" terminating.");="" e.printstacktrace();="" system.exit(0);="" }="" }="" system.out.println("enter="" the="" number="" of="" generations="" to="" print.");="" this="" uses="" the="" hasnextint="" method="" to="" skip="" over="" input="" that="" is="" not="" an="" integer="" value="" maxgenerations="-1;" while="" (maxgenerations="">< 0){="" while="" (!stdin.hasnextint()){="" system.out.println("next="" token="" is="" not="" an="" integer.="" skipping="" this="" line.");="" stdin.nextline();="" }="" maxgenerations="stdIn.nextInt();" if="" (maxgenerations="">< 0){="" system.out.println("value="" must="" be="">= 0. Please try again."); } } System.out.println("Do you want
Answered Same DayJul 28, 2021

Answer To: The file ConwaysLife.java has a partial implementation of Conway's game of life that you are asked...

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 array of int(or boolean), n by n, for some n >= 3.
Each cell that is not on the b
order of the array 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 current by the following rules
(suppose the cell is indexed by [i][j])
If the cell is currently 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 neigbboring cells
the organism in cell[i][j] survives, so cell[i][j]
remains occupied
else // the cell is currently 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 array 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 current generation and a second for the
next generation that is calculated from the current generation.
For convenience, the array has extra rows and columns so that a
cell that might actually hold an organism always has eight neighboring
cells within the array, but the outermost border of the array(topmost and
bottommost 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 Scanner
// 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 array; user selects
gridSize;

private static int[][]
// NOTE: if GRID_SIZE is n, that arrays will be (n+2) by (n+2)
// to hold the extra rows and columns on the border
// array for the current generation
currentGeneration,
// and an array for calculating the next generation
auxArray;

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("Error 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
currentGeneration = new int[gridSize + 2][gridSize + 2];
auxArray = 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