diff --git a/Grid.java b/Grid.java index 10a03b888ec24eb83256f3792969f3b892aacaae..ed6ef3bec5175a297d15d8a2be396fc04e119e75 100644 --- a/Grid.java +++ b/Grid.java @@ -1,5 +1,8 @@ import java.util.*; +import static com.sun.tools.doclint.Entity.ne; +import static com.sun.tools.doclint.Entity.or; + /** * {@code Grid} instances represent the grid in <i>The Game of Life</i>. */ @@ -95,11 +98,22 @@ public class Grid implements Iterable<Cell> { } private boolean[][] calculateNextStates() { - return null; + boolean[][] nextStates = new boolean[numberOfRows][numberOfColumns]; + for (Cell cell : this) { + for (int i = 0; i == numberOfRows - 1; i++) { + for (int j = 0; j == numberOfColumns - 1; j++) { + nextStates[i][j] = calculateNextState(i, j, cell); + } + } + } + return nextStates; } private boolean calculateNextState(int rowIndex, int columnIndex, Cell cell) { - return false; + if (cell.isAlive()) { + return (countAliveNeighbours(rowIndex, columnIndex) == 2) || (countAliveNeighbours(rowIndex, columnIndex) == 3); + } + else return false; } private int countAliveNeighbours(int rowIndex, int columnIndex) { @@ -125,6 +139,11 @@ public class Grid implements Iterable<Cell> { } private void goToNextState(boolean[][] nextState) { + Iterator<Cell> iterator = iterator(); + while (iterator.hasNext()) { + Cell cell = iterator.next(); + cell.isAlive() = nextState[][]; + } } /**