From 8e12530ad4ccdf806d4dcc46ee332ebe83f3e398 Mon Sep 17 00:00:00 2001 From: CHERIFI Khadidja <khadidja.cherifi@etu.univ-amu.fr> Date: Sun, 18 Oct 2020 22:27:57 +0200 Subject: [PATCH] Replace Grid.java --- Grid.java | 79 +++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 68 insertions(+), 11 deletions(-) diff --git a/Grid.java b/Grid.java index 939e05f..3fd3342 100644 --- a/Grid.java +++ b/Grid.java @@ -1,12 +1,9 @@ -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>. */ -public class Grid implements Iterable<Cell> { +class Grid implements Iterable<Cell> { private final int numberOfRows; private final int numberOfColumns; @@ -15,7 +12,7 @@ public class Grid implements Iterable<Cell> { /** * Creates a new {@code Grid} instance given the number of rows and columns. * - * @param numberOfRows the number of rows + * @param numberOfRows the number of rows * @param numberOfColumns the number of columns * @throws IllegalArgumentException if {@code numberOfRows} or {@code numberOfColumns} are * less than or equal to 0 @@ -77,6 +74,7 @@ public class Grid implements Iterable<Cell> { * @return the number of columns in this {@code Grid} */ public int getNumberOfColumns() { + return numberOfColumns; } @@ -98,29 +96,78 @@ public class Grid implements Iterable<Cell> { } private boolean[][] calculateNextStates() { - return null; + boolean [][] new_stat = new boolean[getNumberOfRows()][getNumberOfColumns()]; + for(int row=0; row<getNumberOfRows(); row++){ + for (int col = 0; col < getNumberOfColumns(); col++) + new_stat [row][col] = calculateNextState(row, col); + } + + return new_stat; } - private boolean calculateNextState(int rowIndex, int columnIndex, Cell cell) { - return false; + + + private boolean calculateNextState(int rows, int columns) { + Cell cellule = getCell(rows, columns); + int nbOfVoisinesVivantes = countAliveNeighbours(rows, columns); + return cellule.isAliveInNextState(nbOfVoisinesVivantes); + } + + + + private int countAliveNeighbours(int rowIndex, int columnIndex) { - return 0; + List<Cell> voisine =getNeighbours( rowIndex, columnIndex); + Iterator<Cell> iterator = voisine.iterator(); + int voisine_vivant = 0; + while(iterator.hasNext()) { + if(iterator.next().isAlive()) + voisine_vivant = voisine_vivant+1; + } + return voisine_vivant; } + private List<Cell> getNeighbours(int rowIndex, int columnIndex) { - return null; + List<Cell> voisine = new LinkedList<Cell>(); + voisine.add(getCell(rowIndex, columnIndex-1)); + voisine.add(getCell(rowIndex, columnIndex+1)); + voisine.add(getCell(rowIndex+1, columnIndex)); + voisine.add(getCell(rowIndex-1, columnIndex)); + voisine.add(getCell(rowIndex+1, columnIndex+1)); + voisine.add(getCell(rowIndex+1, columnIndex-1)); + voisine.add(getCell(rowIndex-1, columnIndex-1)); + voisine.add(getCell(rowIndex-1, columnIndex+1)); + + + return voisine; } private void goToNextState(boolean[][] nextState) { + for(int row = 0 ; row<this.getNumberOfRows();row++){ + for(int col = 0 ; col<this.getNumberOfColumns(); col++){ + Cell stat = getCell(row,col); + if (nextState[row][col]){ + stat.setAlive(); + } + else{ + stat.setDead(); + } + } + } } /** * Sets all {@link Cell}s in this {@code Grid} as dead. */ void clear() { + for(Iterator<Cell> gril = iterator(); gril.hasNext();){ + Cell cel = gril.next(); + cel.setDead(); + } } /** @@ -131,6 +178,16 @@ public class Grid implements Iterable<Cell> { */ void randomGeneration(Random random) { + Iterator<Cell> gril = iterator(); + while (gril.hasNext()){ + Cell cel = gril.next(); + boolean aleatoire = random.nextBoolean(); + if(aleatoire) + cel.setAlive(); + else + cel.setDead(); + } + } } -- GitLab