Section: GameOfLife

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.

•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.

1.Using the rules above, and the initial cells to the left, show the cells after one update to the right

2.Complete the unit test below so it grows the four cells on the left and makes sure the correct six expected cells exist after the update message.

GameOfLife society = newGameOfLife(6, 5);

society.growCellAt(__, __);

society.growCellAt(__, __);

society.growCellAt(__, __);

society.growCellAt(__, __);

society.update();

assertTrue(society.cellAt(__, __));

assertTrue(society.cellAt(__, __));

assertTrue(society.cellAt(__, __));

assertTrue(society.cellAt(__, __));

assertTrue(society.cellAt(__, __));

assertTrue(society.cellAt(__, __));

3.Using the rules above, and the cells to the left, show cells after one update message.

4.Complete the unit test below so it makes assertions about the two cells that are gone and the two new ones that grew (see previous societies above).

@Test

publicvoid testExample2() {

GameOfLife society = newGameOfLife(6, 5);

society.growCellAt(1, 1);

society.growCellAt(2, 2);

society.growCellAt(2, 3);

society.growCellAt(3, 1);

society.growCellAt(3, 2);

society.update();

// Assert the newly grown cells are there

assertTrue(society.cellAt(__, __));

assertTrue(society.cellAt(__, __));

// Assert the cells that aren't there anymore, aren't

assertFalse(society.cellAt(__, __));

assertFalse(society.cellAt(__, __));

}

5.Consider neighborCount with wraparound. First fill in the values below so the assertions pass for neighborCount. If time permits, show the next version of this society after one update() message.

The O has 8 neighbors labeled a..h. Wraparound is needed for d..h. The labels are repeated to show where they need to be checked.
f / g / h
e / O / a / e
d / c / b / d
g / h / f
/ Try to imagine the cells covering a torus:

society.growCellAt(0, 0);

society.growCellAt(0, 2);

society.growCellAt(0, 4);

society.growCellAt(5, 2);

society.growCellAt(5, 3);

society.growCellAt(5, 4);

assertEquals(__, society.neighborCount(0, 0));

assertEquals(__, society.neighborCount(0, 2));

assertEquals(__, society.neighborCount(0, 4));

assertEquals(__, society.neighborCount(5, 2));

assertEquals(__, society.neighborCount(5, 3));

assertEquals(__, society.neighborCount(5, 4)); // Lower right corner