From 82d0af347be2fe81143a0d874fc7ec733b68f7d1 Mon Sep 17 00:00:00 2001 From: FERAUD Guillaume <guillaume.feraud.2@etu.univ-amu.fr> Date: Sun, 4 Oct 2020 22:29:46 +0200 Subject: [PATCH] Completing all methods of Grid.java --- Grid.java | 65 ++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 55 insertions(+), 10 deletions(-) diff --git a/Grid.java b/Grid.java index 939e05f..a9e9be3 100644 --- a/Grid.java +++ b/Grid.java @@ -1,7 +1,4 @@ -import java.util.Arrays; -import java.util.Iterator; -import java.util.List; -import java.util.Random; +import java.util.*; /** * {@code Grid} instances represent the grid in <i>The Game of Life</i>. @@ -98,29 +95,73 @@ public class Grid implements Iterable<Cell> { } private boolean[][] calculateNextStates() { - return null; + boolean [][] state = new boolean[numberOfRows][numberOfColumns]; + for (int i = 0; i < state.length; i++) { + for (int j = 0; j < state[i].length; j++) { + if (calculateNextState(i, j, cells[i][j] )) + state[i][j] = true; + else + state[i][j] = false; + } + } + return state; } private boolean calculateNextState(int rowIndex, int columnIndex, Cell cell) { - return false; + int aliveNeighbours = countAliveNeighbours(rowIndex, columnIndex); + if (cell.isDead() && aliveNeighbours == 3) + return true; + if (cell.isAlive() && aliveNeighbours == 2 || aliveNeighbours == 3) + return true; + else + return false; } private int countAliveNeighbours(int rowIndex, int columnIndex) { - return 0; + int aliveNeighbours = 0; + for (int i = 0; i < getNeighbours(rowIndex, columnIndex).size(); i++) { + Cell cell = getNeighbours(rowIndex, columnIndex).get(i); + boolean cellState = cell.isAlive(); + if (cellState) + aliveNeighbours++; + } + return aliveNeighbours; } private List<Cell> getNeighbours(int rowIndex, int columnIndex) { - return null; + List<Cell> neighbours = new ArrayList<>(); + neighbours.add(getCell(rowIndex, columnIndex + 1)); + neighbours.add(getCell(rowIndex + 1, columnIndex + 1)); + neighbours.add(getCell(rowIndex + 1, columnIndex)); + neighbours.add(getCell(rowIndex + 1, columnIndex -1)); + neighbours.add(getCell(rowIndex, columnIndex - 1)); + neighbours.add(getCell(rowIndex - 1, columnIndex - 1)); + neighbours.add(getCell(rowIndex - 1, columnIndex)); + neighbours.add(getCell(rowIndex - 1, columnIndex + 1)); + return neighbours; } private void goToNextState(boolean[][] nextState) { + for (int i = 0; i < nextState.length; i++) { + for (int j = 0; j < nextState[i].length; j++) { + if (nextState[i][j]) + getCell(i, j).setAlive(); + else + getCell(i, j).setDead(); + } + } } /** * Sets all {@link Cell}s in this {@code Grid} as dead. */ void clear() { + boolean [][] clearState = new boolean[numberOfRows][numberOfColumns]; + for (int i = 0; i < clearState.length; i++) { + Arrays.fill(clearState[i], false); + } + goToNextState(clearState); } /** @@ -131,6 +172,10 @@ public class Grid implements Iterable<Cell> { */ void randomGeneration(Random random) { + boolean [][] randomState = new boolean[getNumberOfRows()][getNumberOfColumns()]; + for (int i = 0; i < randomState.length; i++) { + Arrays.fill(randomState[i], random.nextBoolean()); + } + goToNextState(randomState); } - -} +} \ No newline at end of file -- GitLab