From 78e8daf7a5cca17ce25e25000788a35162b0eacd Mon Sep 17 00:00:00 2001 From: Hai Dang <hai-dang.le@etu.univ-amu.fr> Date: Mon, 28 Sep 2020 17:52:03 +0200 Subject: [PATCH] Updated Grid Modification des methodes getNeighbours, countAlivesNeighbours, calculateNextState, calculateNextStates, goToNextStates. --- tp3/Grid.java | 46 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 8 deletions(-) diff --git a/tp3/Grid.java b/tp3/Grid.java index 939e05f..22ca2a6 100644 --- a/tp3/Grid.java +++ b/tp3/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,61 @@ public class Grid implements Iterable<Cell> { } private boolean[][] calculateNextStates() { - return null; + boolean[][] array = new boolean[getNumberOfRows()][getNumberOfColumns()]; + for (int collum = 0; collum < getNumberOfColumns(); collum++) { + for (int row = 0; row < getNumberOfRows(); row++) { + array[row][collum] = calculateNextState(row,collum,getCell(row,collum)); + } + } + return array; } private boolean calculateNextState(int rowIndex, int columnIndex, Cell cell) { - return false; + return cell.isAliveInNextState(countAliveNeighbours(rowIndex,columnIndex)); } private int countAliveNeighbours(int rowIndex, int columnIndex) { - return 0; + int numCellAlive = 0; + for (Cell cell : getNeighbours(rowIndex,columnIndex)){ + if(cell.isAlive()){ + numCellAlive ++; + } + } + return numCellAlive; } private List<Cell> getNeighbours(int rowIndex, int columnIndex) { - return null; + List<Cell> neighboursList = new ArrayList<>(); + neighboursList.add(getCell(rowIndex+1,columnIndex)); + neighboursList.add(getCell(rowIndex-1,columnIndex)); + neighboursList.add(getCell(rowIndex,columnIndex+1)); + neighboursList.add(getCell(rowIndex,columnIndex-1)); + neighboursList.add(getCell(rowIndex+1,columnIndex+1)); + neighboursList.add(getCell(rowIndex-1,columnIndex-1)); + neighboursList.add(getCell(rowIndex-1,columnIndex+1)); + neighboursList.add(getCell(rowIndex+1,columnIndex-1)); + return neighboursList; + } private void goToNextState(boolean[][] nextState) { + boolean[][] array = nextState; + for (int collum = 0; collum < getNumberOfColumns(); collum++) { + for (int row = 0; row < getNumberOfRows(); row++) { + if(array[row][collum]){ + cells[row][collum].setAlive(); + } + else cells[row][collum].setDead(); + } + } } /** * Sets all {@link Cell}s in this {@code Grid} as dead. */ void clear() { + } /** @@ -131,6 +160,7 @@ public class Grid implements Iterable<Cell> { */ void randomGeneration(Random random) { + } } -- GitLab