Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Assignment overview John Conway invented his Game of Life in 1970. It is an example of a finite automaton where simple rules are used to

Assignment overview

John Conway invented his Game of Life in 1970. It is an example of a finite automaton where simple rules are used to create complex behavior. See

http://en.wikipedia.org/wiki/Conways_Game_of_Life

The game (actually more like a simulation) is played on a board consisting of a square grid of cells. Each cell can be alive or dead (empty). Well draw small black circles for living cells, and leave dead cells blank. So a small board might look like this:

A new generation of cells is created from the current generation as follows. The eight cells surrounding a given cell (vertically, horizontally, or diagonally) are its neighbours. The fate of any cell on the board is controlled by how many living neighbours it has, using these rules:

1. (Survival rule) If a cell is alive, then it stays alive in the next generation if it has either 2 or 3 living neighbours. (It dies of loneliness if it has 0 or 1, and dies of overcrowding if it has 4 or more.)

2. (Birth rule) If a dead (empty) cell has exactly 3 living neighbours, then it becomes a living cell in the next generation.

The next generation after the board shown above would be:

The living cell in the top left died of loneliness. The four in the top right survived, because they each have 3 living neighbours. In the middle, two cells died of loneliness, three survived, and two new cells were born because there were exactly 3 living neighbours in those places.

Note that the transition from one generation to the next must happen simultaneously for all cells. You cannot change the cells one at a time, or it will not work properly. You must pre-calculate what every cell will do in the next generation, and then change them all at once.

In this assignment, you will implement a simulation of Conways Game of Life. You must do it as specified in the questions below. Other styles (for example, anything that you might find on the internet) will be worth 0.

Phase 1: A Cell class

Implement a class that will represent a single cell (square) on the board. Your cells must have instance variables to store the following information:

The current state of the cell (alive or dead) (boolean)

The future state of the cell in the next generation (boolean)

A list of references to all of the Cell objects that are neighbours of this cell. This must be based on a Cell[] array. Most cells will have exactly 8 neighbours, but cells on the edge of the board will only have 5, and the corner cells will only have 3. You can use whatever method you like to allow for this.

A cell must not store any information about where it is on the board (row or column information). The list of neighbours should be all that it needs.

You may implement whatever methods you need. All instance variables must be private. Most methods will be public, but you can have small private methods, too, if that is appropriate. All of the rules for life and death that control the game must be implemented in this class.

Phase 2: A LifeBoard class

Implement a class that will represent the entire Game of Life board. The following instance variables and public methods must be implemented:

The board must be stored as a private 2D array of Cell objects (Cell[][]).

Provide two types of constructor:

o LifeBoard(int w, int h) should create a board with w columns and h rows. (Note the order columns first and rows second.) All cells should initially be dead (blank).

o LifeBoard(boolean[][] state) should create a board with the same number of rows and columns as the state parameter. The cells should be alive if the corresponding entry in the state parameter is true, and dead otherwise.

o These two constructors will share a lot of code. Use private methods to avoid writing the same code twice.

o When the boards are constructed, you must also create all of the Cell objects needed, and you must ensure that each Cell knows what its neighbour cells are.

Provide a method public void nextGeneration() which will advance the board to the next generation. The rules for life and death must be handled in the Cell class, not in this class.

Provide a method public void setState(boolean[][] newState) which will update the status (alive or dead) of every cell on the board. You may assume that the newState parameter will always have exactly the same size as the board.

Provide a method public void getState(boolean[][] currentState) which will fill in the currentState parameter to match every cell on the board (alive or dead). Note that it will update the existing array. It will not create and return a new one.

Provide a method public void toggleState(int r, int c) which will change the status of the cell at row r and column c on the board (from alive to dead, or dead to alive).

You may choose to implement other methods if you wish. For debugging purposes, a standard toString method would be a good idea, but it is not required.

Testing

Two files are provided: LifeTestCases.java and LifeWindow.java. Put these in the same folder as your two files, and compile them. Do not change them. If there are errors, it will be because you have not implemented your classes and methods properly. If you run LifeWindow it will give you a user interface to test your program. It should be fairly self-explanatory, and it will be demonstrated in class. The window is resizable. Click on a cell to change it from alive to dead or the reverse. For all other actions, press a key on the keyboard. Pressing 1 to 5 will give one of five different sample configurations which should act as follows:

1. This one should die out in exactly 8 generations. It is a very small one, but it will test all of the rules.

2. This is a glider gun. Small gliders should be generated that move toward the top right, one every 30 generations.

3. This is a set of 8 gliders that collide to form a glider gun.

4. This is a harvester. The block of living cells will crawl along the line of cells, turning them into little 2x2 blocks (bales).

5. This is the r pentomino. It is a simple group of 5 cells, but it generates very complex behaviour. On an infinitely large board, it will change for 1,103 generations before it settles into a stable or repeating pattern.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems 22nd European Conference Adbis 2018 Budapest Hungary September 2 5 2018 Proceedings Lncs 11019

Authors: Andras Benczur ,Bernhard Thalheim ,Tomas Horvath

1st Edition

3319983970, 978-3319983974

More Books

Students also viewed these Databases questions