Adding pieces of JAVA CODE TO The Game of Life The Game of Life Place all the fi
ID: 3764954 • Letter: A
Question
Adding pieces of JAVA CODE TO The Game of Life
The Game of Life
Place all the files for this project together into one .jar file or .zip file, including the ones you didn't change. Please following our naming style of LastNameHW6.extension When you create the .jar file, if you specify which class has the main method; then you can run it from your desktop by double clicking on the icon.
Life is a mathematical “game” invented by mathematician, John Conway. It became widely popular after it was published in a column in Scientific American in 1970. It is one of the most commonly programmed games on the computer.
Life is an example of a cellular automaton, a system in which rules are applied to cells and their neighbors in a regular grid. Life is played on a rectangular grid of square cells. Each of these cells is either dead or alive. This is its state. Here are the rules for Life:
Once started, the state of a cell depends on its current state and the current state of its neighbors.
These rules are applied to all of the cells at one time. That is, the new state of each cell is determined before changing the state of any of the cells.
Click here to see some additional notes on Life.
Objectives
Working with 2-dimensional arrays
Reading, understanding, and modifying existing code
Creating an executable jar file
Specification
Life is designed to run in a grid of infinite size. John Conway initially created Life on a Go board, with 19 rows and 19 columns. We will use this simplification for our implementation.
You are given starter code here: LifeStarter.zip. This .zip file contains 3 .java files; they should all compile. You will not have to write any new classes. Your task will be to implement the needed methods in the Life class in order for the program to run properly.
You will need to provide an initial state for the game. Your initial state must include some static patterns (like the block and beehive), some patterns that will die off , and some alternating patterns (like the blinker). You can include a glider as well, if you like. For more information about these patterns, see the additional notes about Life.
You need to determine the values for the next generation of all the cells completely before changing the state of the cells. Use two arrays, one for the current generation and another for the next generation.
Since our game is not of infinite size, we do have to think about the "boundary cells"; those on the edge of the grid. These are more challenging because they don't have 8 neighbors: the corners have only 3, the cells along the edges have just 5. For this assignment, all boundary cells are required to start off dead and stay dead.
Documentation
You get off easy this time. The JavaDocs are all in place (just make sure to add your name to the file you modify). What I'll be looking for here are good algorithm comments and good style. Don't forget them. Make sure to add your name to the Life class.
public class Life {
// public constants
/** Number of rows/columns in Life Grid */
public static final int NROWSCOLS = 19;
// private instance variables
private State[][] board; // game board
private int generations; // how many generations have happened
/** Construct and initialize new game board
* @param display The LifeGUI display object for this game */
public Life() {
board = new State[NROWSCOLS][NROWSCOLS];
newGame();
}
/** initialize new game
* */
public void newGame() {
/*****************************************************
* STUDENTS: setup your initial Life environment here
*****************************************************/
}
/** Returns the number of generations
* @return The number of generations that have been run so far.
*/
public int getGenerationCount() {
return generations;
}
/** Return the current state of game board cell at given row/column
* (Squares numbered from 0 to NROWSCOLS-1).
* @throws IllegalArgumentException for bad row/col
* @param row The row of the deisred cell.
* @param col The column of the desired cell.
*/
public State getCell(int row, int col) {
return State.DEAD; // stub value
/***************************************
* STUDENTS: fix this
**************************************/
}
/** Process one life cycle of the cellular automaton
*
*/
public void lifeCycle() {
/***************************************
* STUDENTS: implement this
**************************************/
}
}
Life is a mathematical “game” invented by mathematician, John Conway. It became widely popular after it was published in a column in Scientific American in 1970. It is one of the most commonly programmed games on the computer.
Life is an example of a cellular automaton, a system in which rules are applied to cells and their neighbors in a regular grid. Life is played on a rectangular grid of square cells. Each of these cells is either dead or alive. This is its state. Here are the rules for Life:
Each cell has eight (8) neighbors.
Once started, the state of a cell depends on its current state and the current state of its neighbors.
If a dead cell has exactly three (3) live neighbors, it becomes alive. This is a birth.
If a dead cell does not have exactly three (3) live neighbors, it remains dead.
If a live cell has two (2) or three (3) live neighbors, it remains alive. This is survival.
If a live cell has zero (0) or one (1) live neighbors, it dies. This is loneliness.
If a live cell has four (4) or more live neighbors, it dies. This is overcrowding.
These rules are applied to all of the cells at one time. That is, the new state of each cell is determined before changing the state of any of the cells.
Click here to see some additional notes on Life.
Explanation / Answer
I DONT HAVE IDE IN MY SYSTEM RIGHT NOW,PLEASE CHECK THE BELOW CODE
Listing 1. HandleMouseDown.
In random mode, cells are birthed randomly by the program at the start of each cycle (length set via the Generations menu). That code is shown in Listing 2.
Listing 2. NewCell and FillRandom.
The FillRandom procedure calls the NewCell procedure in a loop until the required number of randomly-located cells have been birthed. NewCell generates two random integers, scales and converts them to a location in the 60 by 100 matrix, then tests to see if that location holds a live cell. If so, the selection process is repeated; otherwise a new cell is birthed at the selected location and the matrix location marked TRUE.
Once Life begins applying Conway's rules the program can be paused at any time by clicking the Pause button. During pauses cells can be killed or birthed by clicking their locations regardless of the manual/random mode setting.
In applying Conway's rules to the matrix, there is a choice of two approaches: static and dynamic. Both methods count the number of live cell neighbors had by each location in the 6000-cell matrix using the code in Listing 3 from the Action unit.
Listing 3. CountNeighbors.
The code uses a series of conditionals to test matrix positions neighboring the location passed by row and column numbers, while accounting for boundary conditions.
The static mode code that calls the code in Listing 3 is given in Listing 4. A local array is used to store changes until the scan of the matrix is completed. Then the field is updated, along with the global array. The SetMaxCol and SetMaxRow procedures called near the beginning of Listing 4 set local variables maxCol and maxRow equal to global constants ggcMaxCol and ggcMaxRow. CodeWarrior Pascal won't let me use the global constants directly in the for loop statements, demanding local variables or constants. CodeWarrior Pascal also won't allow a simple assignment such as: maxCol := ggcMaxCol. I wrote procedures, included in the Miscunit, to do the assignments by incrementing or decrementing the variable passed to it in a loop until it equals the global constant.
Listing 4. CheckStaticNeighborhood.
Listing 5 and Listing 6 perform similar functions for the dynamic mode, the key differences being that, in dynamic mode, the relative positions of the row and column loops is significant, whereas in static mode the order makes no difference. The global boolean variable ggColumnIndexFlag, toggled by clicking the Row/Column button, determines the execution order of the loops.
In dynamic mode, the matrix is updated immediately after counting neighbors. Listing 5 calls Listing 3 to do the count, then calls Listing 6 to perform the update.
Listing 5. CheckDynamicNeighborhood.
With the default static approach, which conforms more closely to the previously published versions and discussions of Conway's game that I have seen, changes are not made to the matrix until it has been completely scanned. The dynamic approach makes changes immediately. The default mode combination is manual-static.
Playing the Game of Life
The opening screen is a blank field with a control panel to the right. The control panel shows two data windows, cell count and generations. There are six buttons initially labeled Row,Clear, Dynamic, Random, Go and Quit. In manual mode, cells are birthed or killed by clicking their locations with the mouse. When the desired cell arrangement is in place, clicking the Go button begins execution. Go changes to Pause and is used to temporarilly interrupt execution. The Clear button, enabled only in manual mode, removes all living cells from the game field.
In random mode, the Random button is relabeled to Manual, and clicking the Go button initiates automatic operation that continues uninterrupted until manual mode is reselected by clicking the Manual button, the Pause button is clicked, or operation is terminated by clicking Quit, typing Command-Q, or selecting Quit from the File menu.
In Dynamic mode, the button inially labelled Row is enabled. It is used to switch the relative positions of nested row and column loops. In the default state, the outer loop is indexed by columns, the inner by rows. Clicking the Row button reverses the two and relabels the button to read Column.
The static approach yields more oscillator and glider patterns. The button initially labelledDynamic is used to toggle between the two approaches. In Dynamic mode the button is relabeled Static and can be clicked to reselect that operating mode.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.