From 661b897cefec02c5b51dd44a3a20e32e6076484a Mon Sep 17 00:00:00 2001 From: b22015696 <maxence.balme@etu.univ-amu.fr> Date: Tue, 26 Nov 2024 16:29:51 +0100 Subject: [PATCH] =?UTF-8?q?avanc=C3=A9e?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/model/Cell.java | 16 +++++++++------ .../java/model/ConstantCellInitializer.java | 8 ++++---- .../java/model/NextGenerationInitializer.java | 20 ++++++++++++------- .../model/automata/GameOfLifeAutomaton.java | 14 ++++++------- .../java/model/automata/GameOfLifeState.java | 14 +++---------- 5 files changed, 37 insertions(+), 35 deletions(-) diff --git a/src/main/java/model/Cell.java b/src/main/java/model/Cell.java index f7f6274..c845233 100644 --- a/src/main/java/model/Cell.java +++ b/src/main/java/model/Cell.java @@ -11,7 +11,7 @@ import java.util.List; */ public class Cell<T> implements Lens<T> { - //TODO: ajouter la ou les propriétés nécessaires + private T content; // la liste des objets écoutant les modifications du contenu de la cellule private final List<OnChangeListener<T>> listeners = new ArrayList<>(); @@ -21,7 +21,7 @@ public class Cell<T> implements Lens<T> { * @param initialContent the value initially stored by the cell. */ public Cell(T initialContent) { - //TODO: à compléter + this.content = initialContent; } /** Add a {@link OnChangeListener} to react to any change of value in the cell. @@ -40,8 +40,13 @@ public class Cell<T> implements Lens<T> { * @param value the new content of this {@link Cell} */ public void set(T value) { - //TODO: modifier le contenu de la cellule, puis appeler les méthodes valueChanged des - // listeners + T oldValue = this.content; + this.content = value; + for (OnChangeListener L :listeners) { + L.valueChanged(initialContent,value); + initialContent = value ; + } + } /** @@ -50,7 +55,6 @@ public class Cell<T> implements Lens<T> { * @return the current content of this {@link Cell} */ public T get(){ - //TODO: à compléter - return null; + return this.content; } } diff --git a/src/main/java/model/ConstantCellInitializer.java b/src/main/java/model/ConstantCellInitializer.java index 39a5b71..3a099c4 100644 --- a/src/main/java/model/ConstantCellInitializer.java +++ b/src/main/java/model/ConstantCellInitializer.java @@ -11,7 +11,7 @@ import matrix.MatrixInitializer; * @param <T> the type of content of each cell */ public class ConstantCellInitializer<T> implements MatrixInitializer<Cell<T>> { - //TODO: ajouter la/les propriétes nécessaires + private T defaultValue; /** Make a new {@link MatrixInitializer} with cells containing a {@link Cell} with the same * value. @@ -19,12 +19,12 @@ public class ConstantCellInitializer<T> implements MatrixInitializer<Cell<T>> { * @param defaultValue the value stored in each cell. */ public ConstantCellInitializer(T defaultValue) { - //TODO: à compléter + this.defaultValue = defaultValue; } @Override public Cell<T> initialValueAt(Coordinate coordinate) { - //TODO: à compléter - return null; + + return new Cell<>(defaultValue); } } diff --git a/src/main/java/model/NextGenerationInitializer.java b/src/main/java/model/NextGenerationInitializer.java index f9462bf..bdc3f22 100644 --- a/src/main/java/model/NextGenerationInitializer.java +++ b/src/main/java/model/NextGenerationInitializer.java @@ -5,6 +5,9 @@ import matrix.MatrixInitializer; import matrix.ListMatrix; import controller.Simulation; +import java.util.ArrayList; +import java.util.List; + /** * An initializer for a {@link ListMatrix} of states, where each state is computed based on the value * of its neighbours in a {@link Simulation} of a cellular automaton. @@ -12,8 +15,7 @@ import controller.Simulation; * @param <S> the type of states in the simulation. */ public class NextGenerationInitializer<S extends State<S>> implements MatrixInitializer<S> { - - //TODO: ajouter les propriétés nécessaires + private final CellularAutomatonSimulation<S> simulation; /** Create a {@link MatrixInitializer} to compute the next generation in * a 2D cellular automaton. @@ -21,15 +23,20 @@ public class NextGenerationInitializer<S extends State<S>> implements MatrixInit * @param simulation the {@link Simulation} representing the cellular automaton. */ public NextGenerationInitializer(CellularAutomatonSimulation<S> simulation) { - //TODO: à compléter + this.simulation = simulation; } @Override public S initialValueAt(Coordinate coordinate) { - //TODO: à compléter - return null; + List <Coordinate> coordinates = coordinate.orthogonalNeighbours(); + List<S> states = new ArrayList<>(); + for (Coordinate coor : coordinates) { + states.add(simulation.at(this.wrap(coordinate)).get()); + } + return simulation.at(coordinate).get().update(states); } + /** Computes the grid {@link Coordinate} for an arbitrary {@link Coordinate}, even outside * the grid. This is done by considering that the grid wraps over its edges, connecting the left side to the right * side, and the top side to the bottom side. This way, every cell has 4 orthogonal @@ -39,10 +46,9 @@ public class NextGenerationInitializer<S extends State<S>> implements MatrixInit * @return a corresponding {@link Coordinate}, that is inside the grid. */ Coordinate wrap(Coordinate coordinate) { - //TODO: à compléter //Il faut recalculer les coordonnées x et y modulo les dimensions de la grille. //Pour le modulo, utiliser la fonction ci-dessous, qui s'assure que le résultat est positif. - return null; + return new Coordinate(modulo(coordinate.x(),this.simulation.numberOfColumns()), modulo(coordinate.y(),this.simulation.numberOfRows())); } /** The non-negative remainder of n divided by d. diff --git a/src/main/java/model/automata/GameOfLifeAutomaton.java b/src/main/java/model/automata/GameOfLifeAutomaton.java index 72b0854..0db4800 100644 --- a/src/main/java/model/automata/GameOfLifeAutomaton.java +++ b/src/main/java/model/automata/GameOfLifeAutomaton.java @@ -5,21 +5,22 @@ import model.CellularAutomaton; import java.util.Random; public class GameOfLifeAutomaton implements CellularAutomaton<GameOfLifeState> { + private final int numberOfColumns; + private final int numberOfRows; public GameOfLifeAutomaton(int numberOfColumns, int numberOfRows) { - //TODO: à compléter + this.numberOfColumns = numberOfColumns; + this.numberOfRows = numberOfRows ; } @Override public int numberOfColumns() { - //TODO: à compléter - return 0; + return numberOfColumns; } @Override public int numberOfRows() { - //TODO: à compléter - return 0; + return numberOfRows; } @Override @@ -29,7 +30,6 @@ public class GameOfLifeAutomaton implements CellularAutomaton<GameOfLifeState> { @Override public GameOfLifeState randomState(Random generator) { - //TODO: à compléter - return null; + return generator.nextBoolean() ? GameOfLifeState.ALIVE : GameOfLifeState.DEAD; } } diff --git a/src/main/java/model/automata/GameOfLifeState.java b/src/main/java/model/automata/GameOfLifeState.java index 8f64cd8..e3f0693 100644 --- a/src/main/java/model/automata/GameOfLifeState.java +++ b/src/main/java/model/automata/GameOfLifeState.java @@ -37,17 +37,9 @@ public enum GameOfLifeState implements State<GameOfLifeState> { @Override public GameOfLifeState update(List<GameOfLifeState> neighbours) { int count = State.count(ALIVE, neighbours); - if this == ALIVE { - if(count == 2 || count == 3) { - return GameOfLifeState.ALIVE; - } - } - else if this == DEAD { - if (count ==3) { - return GameOfLifeState.ALIVE; - } - } - return this.next(); + if (count == 3) return ALIVE ; + if ((count == 2) && this.equals(ALIVE))return ALIVE ; + return DEAD ; } } -- GitLab