Game of Life: Assignment to complete one test method
Collaboration Complete this Assignment on your own. You may get help from section leaders.
The Game of Life was invented by John Conway to simulate the birth and death of cells in a society. The following rules govern the birth and/or death of cells between two consecutive time periods. At time
- A cell is born if there was none at time T-1 and exactly three of its neighbors were alive.
- An existing cell remains alive if at time T-1 there were either two or three neighbors.
- A cell dies from isolation if at time T-1 there were fewer than two neighbors.
- A cell dies from overcrowding if at time T-1 there were more than three neighbors.
A neighborhood consists of the eight elements around any element (N represents 1 neighbor):
NNN
N N
NNN
The neighborhood can extend to the other side of the society. For example, a location in the first row has a neighborhood that includes three locations in the last row. The following patterns would occur when T ranges from 1 to 5, with the initial society shown at T=1. O represents a live cell; a blank indicates that no cell exists at the particular location in the society.
T=0 T=1 T=2 T=3 T=4Society dies off at T=4
......
..O.O.. ..O.O......
..OOO.. ..O.O.. ..O.O.....O......
...... O...... O...... O......
......
Other societies may stabilize like this:
T=0 T=1 T=2 T=3 T=4 This pattern repeats
......
...... O...... O......
..OOO.. ...O.....OOO.. ...O.....OOO..
...... O...... O......
......
You will be implementing several methods. Here are two
/**
*Thereturnvaluesshouldalwaysbeintherangeof0through8.
*
*@returnThenumberofneighborsaroundanycellusingwraparound.
*/
publicint neighborCount(int row, int col) {
/**
*Updatethestatetorepresentthenextsociety.
*Typically,somecellswilldieoffwhileothersareborn.
*/
publicvoid update()
One simple test method for neighborCount might look like this:
@Test
publicvoid testNeighborsWrapping() {
society = new GameOfLife(10, 16);
society.growCellAt(3, 0);
society.growCellAt(3, 15);
assertEquals(1, society.neighborCount(3, 15));
assertEquals(1, society.neighborCount(3, 0));
assertEquals(2, society.neighborCount(2, 0));
assertEquals(2, society.neighborCount(4, 0));
assertEquals(2, society.neighborCount(2, 15));
}
Assignment due Wednesday, 24-Feb at 10:00 am Name ______
Online Student, email test method below to John by 21:00
Complete the test method to assert all new cells have grown and all old ones have died
@Test
publicvoid testNeighborsWrapping() {
society = new GameOfLife(10, 16);
society.growCellAt(3, 3);
society.growCellAt(3, 4);
society.growCellAt(3, 5);
society.growCellAt(4, 3);
society.growCellAt(4, 6);
society.growCellAt(2, 4);
society.update()
assertEquals(___, society.neighborCount(____ , ___));
assertEquals(___, society.neighborCount(____ , ___));
assertEquals(___, society.neighborCount(____ , ___));
assertEquals(___, society.neighborCount(____ , ___));
assertEquals(___, society.neighborCount(____ , ___));
assertEquals(___, society.neighborCount(____ , ___));
assertEquals(___, society.neighborCount(____ , ___));
assertEquals(___, society.neighborCount(____ , ___));
assertEquals(___, society.neighborCount(____ , ___));
}