Answered step by step
Verified Expert Solution
Question
1 Approved Answer
use java code print out must be the same as the last page. thank you The Game of Life is a well-known mathematical game that
use java code
print out must be the same as the last page.
thank you
The Game of Life is a well-known mathematical game that gives rise to amazingly complex behavior, although it can be specified by a few simple rules. (It is not actually a game in the traditional sense, with players competing for a win.) Here are the rules. The game is played on a rectangular board. Each square can be either empty or occupied. At the beginning, you can specify empty and occupied cells in some way; then the game runs automatically. In each generation, the next generation is computed. A new cell is born on an empty square if it is surrounded by exactly three occupied neighbor cells. A cell dies of overcrowding if it is surrounded by four or more Neighbors neighbors, and it dies of loneliness if it is surrounded by zero or one neighbor. A neighbor is an occupant of an adjacent square to the left, Figur right, top, or bottom or in a diagonal direction. Figure 21 shows a cell Neighborhood of a Cell and its neighbor cells. Cell Generation 0 Generation 3 Generation 4 Generation 1 Generation 2 Figure 22 Glider Many configurations show interesting behavior when subjected to these rules. Figure 22 shows a glider, observed over five generations. After four generations, it is transformed into the identical shape, but located one square to the right and below. One of the more amazing configurations is the glider gun: a complex collection of cells that, after 30 moves, turns back into itself and a glider (see Figure 23). Figure 23 Glider Gun D D D 0 Generation Generation 30 Generation 60 Generation 90 Generation 120 Generation 150 Write a class called Gameoflife which perform the game of life with given pattern. The skeleton class is given as follow: public class GameofLife { private boolean[][] board; private final int NUMROW; private final int NUMCOL; public Gameoflife (boolean[][] initialBoard) { } public String toString() { } public void grow(int generation) { } private int checkNeighbor(int x, int y){ } } Instructions: 1. private Boolean[][] board: The board is the game board occupied blocks are represented with true empty blocks are represented with false. 2. public Gameoflife(Boolean[][] initialBoard){ } In the constructor, you should: Initiate NUMROW and NUMCOL. Use for loops to deep copy initialBoard to board. Do not directly assign initialBoard to board 3. public String toString(){ } This returns a string that represents the board state. For each occupied block, you should represent it as the lower-case letter "o". For each empty block, you should represent it as "+". For each block in a row, separate them with an empty space"". For example: + + + + o + + + + + + + + + + + + + + + + + + + OOO + + + + + + + + + + + + + + + + + + + + + + + + 4. public void grow(int generation, int freqPrint) { } This function modifies the board. It should also frequently print the entire board using toString() to show how much the board has changed. The input generation controls how many generations your board is going to run. The input freqPrint controls how frequently the board got printed. If freqPrint = 5, then the board is printed at every 5 generations. If freqPrint = 1, then the board is printed in each generation In each generation, the program should start from the top left most block, check & update all blocks. o When checking, please use the helper function checkNeighbor(x, y) to get the number of occupied blocks in (x, y)'s neighbors as M (you should have better naming in your code) o if Mis 3, set the block (x, y) to occupied. o If Mis 2 and (x, y) itself is occupied, the block (x, y) keeps occupied 0 If M is 1 or 0 (loneliness), or greater than 3 (overcrowding), set the block to empty. Time complexity is not considered. There could be certain algorithms that can speed up the game. Implementing such algorithm is not required. 5. private int checkNeighbor(int x, int y){ } The function returns the number of occupied blocks of the neighbors of the block (x, y) Neighbors are consisted by the blocks around the given block (x,y) Neighbors does not include (x, y) The maximum number of neighbors (x, y) could have is 8. If the given block (x, y) are on the edge, the neighbors is consisted by the possible surrounding block. For example, The neighbor of (0,0) is (0, 1), (1, 0), (1, 1). The neighbor of (1, 0) is (0,0), (0, 1), (1, 1), (1, 2), (0, 2). The neighbor of (1, 1) is (0,0), (0, 1), (0, 2), (1, 2), (2, 2), (2, 1), (2, 0), (1, 0). Testing: To test your class, you should select at least one shape from each category listed as following. For your selected shape, you should generate the Boolean[][] board and create distinct Gameoflife object using the board generated in your main methods). And then test your grow(generation, freq Print) method with generation = 100 and freqPrint = 20 Still Life: the following patterns stay unchanged no matter how many generations Oscillators: The following patterns repeat itself within a certain period. Try to find the period if you canStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started