Description
You were uploading some changes to the Matrix (fixing some bugs regarding fidget spinners) when finally instead of clicking “submit” you accidentally clicked “delete everything”. You had mentioned in a team meeting that it really doesn’t make sense to have a “delete everything” button, but your boss Carl has a sense of flare and insisted on keeping it for dramatic effect. And so, just like that, the Matrix disapeared with billions of humans now without a simulation to live in. To prevent the humans from waking up and
revolting, program a simple simulation to keep things under control.
Solution Description
Your task will be to simulate a population of cells. Your simulation will consist of a 2D array that we will call our universe. This 2D array will contain all of the Cells that make up our basic universe. For this assignment you have been provided Cell.java, a simple class that will represent a cell. Like all cells, a Cell is either dead or alive.
You will also be provided part of the Board.java class – to complete this assignment you should correctly fill in the body of each method as specified. You may create more methods and fields if you like but you may not modify the existing method headers.
The following fields are included in the provided code and should remain as is:
private Cell[][] universe; private int dimensionLength = 0; private int generationCount = 0;
The following are methods for you to complete:
- public Board(int dimensionLength, int[][] seed)
This constructor should do the following: 1. Assign instance variable dimensionLength the value of the respective parameter. 2. Instantiate universe to be a 2D Cell array of size dimensionLength x dimensionLength 3. Loop through each Cell in universe and instantiate them with the default constructor 4. Call method applySeed with the seed array (more on this later – see the applySeed section)
For this homework you may assume dimensionLength will be at least 4, and `seed` will contain at least o
- private void applySeed(int[][] seed)
This method determines what cells will start off alive in universe. Loop through universe and make the Cells “alive” where the corresponding entry in the seed array is not 0. You may assume that seed has the same dimensions of universe.
Ex) if seed[3][2] == 3, then universe[3][2].setAlive(true)
- private int getCellStatus(Cell[][] arr, int x, int y)
Return 0 if arr[x][y] is out of bounds or the Cell at that location is dead; otherwise return 1. This method is used by the provided getAliveNeighbors method.
- public void tick()
This method should progress the universe one step forward and increment generationCount by one accordingly. During this method you should iterate through universe and for each Cell decide if it should be dead or alive based on the following criteria. Note: You should consider these factors only for the current generation of cells. The updated values of cells should not be taken into account when computing the next generation. (Hint: it might be helpful to store a temporary universe array to temporarily store the updated cells)
Be sure to use the provided getAliveNeighbors method to help you implement this method.
- Underpopulation: Any living cell with less than two neighbors dies
- Overpopulation: Any living cell with more than three neighbors dies
- Continuation: Any living cell with either two or three neighbors continues to live
- Reproduction: Any dead cell with exactly three neigbors becomes alive The following methods have been provided to you:
public String toString()
Will return a simple visualization of universe and generationCount private int getAliveNeighbors(Cell[][] arr, int x, int y)
This method depends on your correct implementation of the above getCellStatus method. It will return the number of living neighbors for any given cell. You need not do anything for this method, however
understanding it is crucial for this assignment.
Testing
We encourage you to write your own tests for this assignment. Create a Test.java file, instantiate a Board and try out different seeds. You can use something like the following:
| public class Test { public static void main(String[] args) { int[][] seed = {
{0, 1, 0, 0, 1}, {0, 0, 1, 1, 1}, {0, 0, 0, 0, 0}, {0, 0, 0, 0, 1}, {1, 1, 0, 0, 0} }; Board b = new Board(5, seed); System.out.println(b); for (int i = 0; i < 10; i++) { b.tick(); System.out.println(b); } } } |
Inspiration
This problem is an implementation of Conway’s Game Of Life. It’s just a peak into the world of cellular automata!




