diff --git a/src/main/java/SimulatorApplication.java b/src/main/java/SimulatorApplication.java index 1e2f7b34dadbe4ce66bfd4f13200e43cdef09ff5..7e2a01d75586b977138a32a64624b3c4879f71a6 100644 --- a/src/main/java/SimulatorApplication.java +++ b/src/main/java/SimulatorApplication.java @@ -7,8 +7,7 @@ import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage; import model.CellularAutomatonSimulation; -import model.automata.BiColorAutomaton; -import model.automata.SeedsAutomaton; +import model.automata.GameOfLifeAutomaton; import java.io.IOException; import java.net.URL; @@ -38,7 +37,7 @@ public class SimulatorApplication extends Application { public SimulatorApplication() { this.simulation = new CellularAutomatonSimulation<>( - new BiColorAutomaton(NUMBER_OF_COLUMNS,NUMBER_OF_ROWS), + new GameOfLifeAutomaton(NUMBER_OF_COLUMNS,NUMBER_OF_ROWS), GENERATOR ); } diff --git a/src/main/java/controller/Controller.java b/src/main/java/controller/Controller.java index 0e8cf64017bd9532e0b2dd930c69b71b2e0d0dc7..32e565779e3dad6299bcfe582e6f5a85816ffd20 100644 --- a/src/main/java/controller/Controller.java +++ b/src/main/java/controller/Controller.java @@ -51,13 +51,6 @@ public class Controller { } - /** - * Sets {@link CellularAutomatonSimulation} instance. - * - * @param simulation {@link CellularAutomatonSimulation} instance - * @throws NullPointerException if {@code gameOfLife} is {@code null} - */ - public void setSimulation(Simulation simulation) { this.simulation = requireNonNull(simulation, "game of life is null"); setGenerationNumberLabelTextProperty(); diff --git a/src/main/java/datastruct/Coordinate.java b/src/main/java/datastruct/Coordinate.java index 18282bc801ba34a0517152502bd175dbd19d14c3..f7f7e22f440efd4cb15793ca3d1b4ecc8d5bda0c 100644 --- a/src/main/java/datastruct/Coordinate.java +++ b/src/main/java/datastruct/Coordinate.java @@ -16,7 +16,8 @@ public record Coordinate(int x, int y) { * @return A new {@link Coordinate} instance. */ public static Coordinate of(int x, int y) { - return new Coordinate(x, y); + // TODO: compléter ce fabriquant + return null; } /** @@ -25,7 +26,8 @@ public record Coordinate(int x, int y) { * @return The left adjacent {@link Coordinate}. */ public Coordinate left() { - return new Coordinate(x - 1, y); + // TODO: à compléter + return null; } /** @@ -34,7 +36,8 @@ public record Coordinate(int x, int y) { * @return The right adjacent {@link Coordinate}. */ public Coordinate right() { - return new Coordinate(x + 1, y); + // TODO: à compléter + return null; } /** @@ -43,7 +46,8 @@ public record Coordinate(int x, int y) { * @return The above adjacent {@link Coordinate}. */ public Coordinate above() { - return new Coordinate(x, y + 1); + // TODO: à compléter + return null; } /** @@ -52,7 +56,8 @@ public record Coordinate(int x, int y) { * @return The below adjacent {@link Coordinate}. */ public Coordinate below() { - return new Coordinate(x, y - 1); + // TODO: à compléter + return null; } /** @@ -61,12 +66,8 @@ public record Coordinate(int x, int y) { * @return A list of orthogonal neighboring {@link Coordinate}s. */ public List<Coordinate> orthogonalNeighbours() { - return List.of( - this.right(), - this.left(), - this.above(), - this.below() - ); + // TODO: à compléter + return List.of(); } /** @@ -75,12 +76,8 @@ public record Coordinate(int x, int y) { * @return A list of diagonal neighboring {@link Coordinate}s. */ public List<Coordinate> diagonalNeighbours() { - return List.of( - this.right().above(), - this.left().above(), - this.left().below(), - this.right().below() - ); + // TODO: à compléter + return List.of(); } /** @@ -89,9 +86,8 @@ public record Coordinate(int x, int y) { * @return A list of all neighboring {@link Coordinate}s. */ public List<Coordinate> orthodiagonalNeighbours() { - List<Coordinate> neighbours = new ArrayList<>(this.orthogonalNeighbours()); - neighbours.addAll(this.diagonalNeighbours()); - return neighbours; + // TODO: à compléter + return List.of(); } @Override diff --git a/src/main/java/datastruct/CoordinateIterator.java b/src/main/java/datastruct/CoordinateIterator.java index 312d2de44356ad8974160f83d4d5e46d4762de84..37360d0720a983441f3d37d3d2e45550c727c30a 100644 --- a/src/main/java/datastruct/CoordinateIterator.java +++ b/src/main/java/datastruct/CoordinateIterator.java @@ -8,10 +8,6 @@ import java.util.NoSuchElementException; * height range. */ class CoordinateIterator implements Iterator<Coordinate> { - private final int width; - private final int height; - private int x = 0; - private int y = 0; /** * Creates a new {@link CoordinateIterator} with the specified width and height. @@ -20,8 +16,7 @@ class CoordinateIterator implements Iterator<Coordinate> { * @param height The height of the coordinate range. */ public CoordinateIterator(int width, int height) { - this.width = width; - this.height = height; + // TODO: à compléter } /** @@ -31,7 +26,8 @@ class CoordinateIterator implements Iterator<Coordinate> { */ @Override public boolean hasNext() { - return y < this.height; + // TODO: à compléter + return false; } /** @@ -42,15 +38,7 @@ class CoordinateIterator implements Iterator<Coordinate> { */ @Override public Coordinate next() { - if (!this.hasNext()) { - throw new NoSuchElementException(); - } - Coordinate coordinate = new Coordinate(this.x, this.y); - this.x = this.x + 1; - if (this.x == this.width) { - this.x = 0; - this.y = this.y + 1; - } - return coordinate; + // TODO: à compléter + return null; } } diff --git a/src/main/java/model/Cell.java b/src/main/java/model/Cell.java index 35022e794c14515447f284343801cb33f29409d8..03780346a5a4b46bb5c2b61d704ec27ee00bef63 100644 --- a/src/main/java/model/Cell.java +++ b/src/main/java/model/Cell.java @@ -12,7 +12,10 @@ import java.util.List; * @param <T> The type of value stored in the cell. */ public class Cell<T> implements Lens<T> { - private T content; + + //TODO: ajouter la ou les propriétés nécessaires + + // la liste des objets écoutant les modifications du contenu de la cellule private final List<OnChangeListener<T>> listeners = new ArrayList<>(); /** Initialize a new cell with a given value. @@ -20,7 +23,7 @@ public class Cell<T> implements Lens<T> { * @param initialContent the value initially stored by the cell. */ public Cell(T initialContent) { - this.content = initialContent; + //TODO: à compléter } /** Add a {@link OnChangeListener} to react to any change of value in the cell. @@ -39,11 +42,8 @@ public class Cell<T> implements Lens<T> { * @param value the new content of this {@link Cell} */ public void set(T value) { - T oldValue = this.content; - this.content = value; - for (OnChangeListener<T> listener : this.listeners) { - listener.valueChanged(oldValue, value); - } + //TODO: modifier le contenu de la cellule, puis appeler les méthodes valueChanged des + // listeners } /** @@ -52,6 +52,7 @@ public class Cell<T> implements Lens<T> { * @return the current content of this {@link Cell} */ public T get(){ - return this.content; + //TODO: à compléter + return null; } } diff --git a/src/main/java/model/CellularAutomatonSimulation.java b/src/main/java/model/CellularAutomatonSimulation.java index 0604e8f94125498202801b825faa5e85f5eba5c1..4f51254692cefda4e95edb8be3de494242c387da 100644 --- a/src/main/java/model/CellularAutomatonSimulation.java +++ b/src/main/java/model/CellularAutomatonSimulation.java @@ -42,12 +42,14 @@ public class CellularAutomatonSimulation<S extends State<S>> @Override public int numberOfColumns() { - return this.grid.width(); + //TODO: à compléter + return 0; } @Override public int numberOfRows() { - return this.grid.height(); + //TODO: à compléter + return 0; } /** @@ -57,16 +59,13 @@ public class CellularAutomatonSimulation<S extends State<S>> * @return The cell at the specified coordinate. */ public Cell<S> at(Coordinate coordinate) { - return this.grid.get(coordinate); + //TODO: à compléter + return null; } @Override public void updateToNextGeneration() { - this.generationNumber.set(this.generationNumber.get()+1); - Matrix<S> nextStates = this.nextGenerationMatrix(); - for (Coordinate coordinate : this.grid.coordinates()) { - this.at(coordinate).set(nextStates.get(coordinate)); - } + //TODO: à compléter, en utilisant nextGenerationMatrix() } /** Computes the {@link Matrix} of states obtained after a single step of updates @@ -75,27 +74,23 @@ public class CellularAutomatonSimulation<S extends State<S>> * @return the states of each cell after one generation */ private Matrix<S> nextGenerationMatrix() { - return new Matrix<>( - this.grid.width(), - this.grid.height(), - new NextGenerationInitializer<>(this) - ); + //TODO: à compléter + return null; } @Override public void next(Coordinate coordinate) { - S oldState = this.grid.get(coordinate).get(); - this.at(coordinate).set(oldState.next()); + //TODO: à compléter } @Override public void copy(Coordinate source, Coordinate destination) { - S state = this.at(source).get(); - this.at(destination).set(state); + //TODO: à compléter } @Override public Color getColor(Coordinate coordinate) { - return this.at(coordinate).get().getColor(); + //TODO: à compléter + return null; } @Override @@ -113,19 +108,13 @@ public class CellularAutomatonSimulation<S extends State<S>> @Override public void clear() { - for (Cell<S> cell : this.grid) { - cell.set(this.automaton.defaultState()); - } - this.generationNumber.set(0); + //TODO: à compléter (penser à remettre le nombre de génération à 0) } @Override public void reset() { - for (Cell<S> cell : this.grid) { - cell.set(this.automaton.randomState(generator)); - } - this.generationNumber.set(0); + //TODO: à compléter (penser à remettre le nombre de génération à 0) } @Override diff --git a/src/main/java/model/ConstantCellInitializer.java b/src/main/java/model/ConstantCellInitializer.java index 1016b57944589521427e57bab3f446fbd1e9d1ff..c44714566bb35f8b7eaacc90d7acc321d27f8cc8 100644 --- a/src/main/java/model/ConstantCellInitializer.java +++ b/src/main/java/model/ConstantCellInitializer.java @@ -11,7 +11,7 @@ import datastruct.MatrixInitializer; * @param <T> the type of content of each cell */ public class ConstantCellInitializer<T> implements MatrixInitializer<Cell<T>> { - private final T defaultValue; + //TODO: ajouter la/les propriétes nécessaires /** Make a new {@link MatrixInitializer} with cells containing a {@link Cell} with the same * value. @@ -19,11 +19,12 @@ public class ConstantCellInitializer<T> implements MatrixInitializer<Cell<T>> { * @param defaultValue the value stored in each cell. */ public ConstantCellInitializer(T defaultValue) { - this.defaultValue = defaultValue; + //TODO: à compléter } @Override public Cell<T> initialValueAt(Coordinate coordinate) { - return new Cell<>(defaultValue); + //TODO: à compléter + return null; } } diff --git a/src/main/java/model/NextGenerationInitializer.java b/src/main/java/model/NextGenerationInitializer.java index 1242f43362c6a7765311b9897f371a877335ba7f..ad03fdf0a14dc41e72f1bfbd4beb976bcf212a9f 100644 --- a/src/main/java/model/NextGenerationInitializer.java +++ b/src/main/java/model/NextGenerationInitializer.java @@ -15,7 +15,7 @@ import java.util.List; */ public class NextGenerationInitializer<S extends State<S>> implements MatrixInitializer<S> { - private final CellularAutomatonSimulation<S> simulation; + //TODO: ajouter les propriétés nécessaires /** Create a {@link MatrixInitializer} to compute the next generation in * a 2D cellular automaton. @@ -23,18 +23,13 @@ public class NextGenerationInitializer<S extends State<S>> implements MatrixInit * @param simulation the {@link Simulation} representing the cellular automaton. */ public NextGenerationInitializer(CellularAutomatonSimulation<S> simulation) { - this.simulation = simulation; + //TODO: à compléter } @Override public S initialValueAt(Coordinate coordinate) { - List<State<S>> neighbours = new ArrayList<>(); - for (Coordinate neighbourCoord : coordinate.orthodiagonalNeighbours()) { - Coordinate wrapped = wrap(neighbourCoord); - neighbours.add(this.simulation.at(wrapped).get()); - } - S state = this.simulation.at(coordinate).get(); - return state.update(neighbours); + //TODO: à compléter + return null; } /** Computes the grid {@link Coordinate} for an arbitrary {@link Coordinate}, even outside @@ -45,11 +40,11 @@ public class NextGenerationInitializer<S extends State<S>> implements MatrixInit * @param coordinate a {@link Coordinate} that may be outside the grid. * @return a corresponding {@link Coordinate}, that is inside the grid. */ - private Coordinate wrap(Coordinate coordinate) { - return new Coordinate( - modulo(coordinate.x(),this.simulation.numberOfColumns()), - modulo(coordinate.y(),this.simulation.numberOfRows()) - ); + 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; } /** The non-negative remainder of n divided by d. @@ -58,7 +53,7 @@ public class NextGenerationInitializer<S extends State<S>> implements MatrixInit * @param d a non-zero integer. * @return the remainder of {@code n/d}, between {@code 0} and {@code n-1}. */ - private static int modulo(int n, int d) { + static int modulo(int n, int d) { int result = n % d; return n < 0 ? result + d : result; } diff --git a/src/main/java/model/State.java b/src/main/java/model/State.java index 64529f0e089771e5fe58051ca7f6ff66f5de66aa..1954558460d6969d09544c64c40152d97c1a736e 100644 --- a/src/main/java/model/State.java +++ b/src/main/java/model/State.java @@ -44,12 +44,7 @@ public interface State<S> { * @return The number of times the specified state appears in the list of neighbors. */ static <T> int count(T state, List<T> neighbours) { - int count = 0; - for (T neighbour : neighbours) { - if (neighbour.equals(state)) { - count++; - } - } - return count; + //TODO: à compléter + return 0; } } \ No newline at end of file diff --git a/src/main/java/model/automata/AbstractAutomaton.java b/src/main/java/model/automata/AbstractAutomaton.java deleted file mode 100644 index 818197ea65111fa8b59df1fbbaa6fb75691b4d04..0000000000000000000000000000000000000000 --- a/src/main/java/model/automata/AbstractAutomaton.java +++ /dev/null @@ -1,24 +0,0 @@ -package model.automata; - -import model.CellularAutomaton; -import model.State; - -public abstract class AbstractAutomaton<S extends State<S>> implements CellularAutomaton<S> { - - private final int numberOfColumns; - private final int numberOfRows; - - - protected AbstractAutomaton(int numberOfColumns, int numberOfRows) { - this.numberOfColumns = numberOfColumns; - this.numberOfRows = numberOfRows; - } - - public int numberOfColumns() { - return numberOfColumns; - } - - public int numberOfRows() { - return numberOfRows; - } -} diff --git a/src/main/java/model/automata/BiColorAutomaton.java b/src/main/java/model/automata/BiColorAutomaton.java deleted file mode 100644 index da2c939ff54abb18130a84f4a1b0424da7905056..0000000000000000000000000000000000000000 --- a/src/main/java/model/automata/BiColorAutomaton.java +++ /dev/null @@ -1,23 +0,0 @@ -package model.automata; - -import java.util.Random; - -public class BiColorAutomaton extends AbstractAutomaton<BiColorState> { - - public BiColorAutomaton(int numberOfColumns, int numberOfRows) { - super(numberOfColumns, numberOfRows); - } - - @Override - public BiColorState defaultState() { - return BiColorState.DEAD; - } - - @Override - public BiColorState randomState(Random generator) { - return generator.nextBoolean()? BiColorState.DEAD: - generator.nextBoolean()? BiColorState.RED: - BiColorState.BLUE; - } - -} diff --git a/src/main/java/model/automata/BiColorState.java b/src/main/java/model/automata/BiColorState.java deleted file mode 100644 index 5f74ad1015b979ac52612f458d92f126d7fb7eb4..0000000000000000000000000000000000000000 --- a/src/main/java/model/automata/BiColorState.java +++ /dev/null @@ -1,45 +0,0 @@ -package model.automata; - -import javafx.scene.paint.Color; -import model.State; - -import java.util.List; - -public enum BiColorState implements State<BiColorState> { - BLUE, RED, DEAD; - - - - @Override - public Color getColor() { - return switch (this) { - case BLUE -> Color.BLUE; - case RED -> Color.RED; - case DEAD -> Color.WHITE; - }; - } - - @Override - public BiColorState next() { - return switch (this) { - case BLUE -> RED; - case RED -> DEAD; - case DEAD -> BLUE; - }; - } - - @Override - public BiColorState update(List<BiColorState> neighbours) { - int countBlue = State.count(BLUE, neighbours); - int countRed = State.count(RED, neighbours); - int countAlive = countBlue + countRed; - if (this == DEAD) { - return (countAlive != 3)? DEAD: - countBlue > countRed? BLUE: - RED; - } - return 2 <= countAlive && countAlive <= 3? this: - DEAD; - } - -} diff --git a/src/main/java/model/automata/BriansBrainAutomaton.java b/src/main/java/model/automata/BriansBrainAutomaton.java deleted file mode 100644 index f78771a7e5bb3ee4bd8dc9dfcc526cecb8331bff..0000000000000000000000000000000000000000 --- a/src/main/java/model/automata/BriansBrainAutomaton.java +++ /dev/null @@ -1,22 +0,0 @@ -package model.automata; - -import java.util.Random; - -public class BriansBrainAutomaton extends AbstractAutomaton<BriansBrainState> { - - public BriansBrainAutomaton(int numberOfColumns, int numberOfRows) { - super(numberOfColumns, numberOfRows); - } - - @Override - public BriansBrainState defaultState() { - return BriansBrainState.OFF; - } - - @Override - public BriansBrainState randomState(Random generator) { - return generator.nextInt(10) == 0 ? - BriansBrainState.ON: - BriansBrainState.OFF; - } -} diff --git a/src/main/java/model/automata/BriansBrainState.java b/src/main/java/model/automata/BriansBrainState.java deleted file mode 100644 index 3e68b250e9014939c038db85e0b0b5735f401998..0000000000000000000000000000000000000000 --- a/src/main/java/model/automata/BriansBrainState.java +++ /dev/null @@ -1,42 +0,0 @@ -package model.automata; - -import javafx.scene.paint.Color; -import model.State; - -import java.util.List; - -public enum BriansBrainState implements State<BriansBrainState> { - ON, OFF, DYING; - - @Override - public Color getColor() { - return switch (this) { - case ON -> Color.WHITE; - case OFF -> Color.BLACK; - case DYING -> Color.BLUE; - }; - } - - @Override - public BriansBrainState next() { - return switch (this) { - case ON -> DYING; - case OFF -> ON; - case DYING -> OFF; - }; - } - - @Override - public BriansBrainState update(List<BriansBrainState> neighbours) { - return switch (this) { - case ON -> DYING; - case DYING -> OFF; - case OFF -> { - int count = State.count(ON, neighbours); - yield count==2 ? ON : OFF; - } - }; - } - - -} diff --git a/src/main/java/model/automata/GameOfLifeAutomaton.java b/src/main/java/model/automata/GameOfLifeAutomaton.java index a25c577efe4a9443b6586af9c499edadd662c53b..7159813139faea3941dc095e09dda0e8a8894a00 100644 --- a/src/main/java/model/automata/GameOfLifeAutomaton.java +++ b/src/main/java/model/automata/GameOfLifeAutomaton.java @@ -1,22 +1,36 @@ package model.automata; +import model.CellularAutomaton; + import java.util.Random; -public class GameOfLifeAutomaton extends AbstractAutomaton<GameOfLifeState> { +public class GameOfLifeAutomaton implements CellularAutomaton<GameOfLifeState> { public GameOfLifeAutomaton(int numberOfColumns, int numberOfRows) { - super(numberOfColumns, numberOfRows); + //TODO: à compléter + } + + @Override + public int numberOfColumns() { + //TODO: à compléter + return 0; + } + + @Override + public int numberOfRows() { + //TODO: à compléter + return 0; } @Override public GameOfLifeState defaultState() { - return GameOfLifeState.DEAD; + //TODO: à compléter + return null; } @Override public GameOfLifeState randomState(Random generator) { - return generator.nextBoolean()? - GameOfLifeState.ALIVE: - GameOfLifeState.DEAD; + //TODO: à compléter + return null; } } diff --git a/src/main/java/model/automata/GameOfLifeState.java b/src/main/java/model/automata/GameOfLifeState.java index c100ad441c9875509eb74437f5b70b8aa9ca11be..12ee370cb07f75700dbf5b688c5a89a627f89515 100644 --- a/src/main/java/model/automata/GameOfLifeState.java +++ b/src/main/java/model/automata/GameOfLifeState.java @@ -9,31 +9,25 @@ import java.util.List; * {@link GameOfLifeState} instances represent the possible states of a {@link GameOfLifeState}. */ public enum GameOfLifeState implements State<GameOfLifeState> { - ALIVE(Color.RED), - DEAD(Color.WHITE); + ALIVE, DEAD; - public final Color color; - - GameOfLifeState(Color color) { - this.color = color; - } @Override public Color getColor() { - return this.color; + //TODO: à compléter + return Color.BLACK; } @Override public GameOfLifeState next() { - return GameOfLifeState.values()[1 - this.ordinal()]; + //TODO: à compléter + return null; } @Override public GameOfLifeState update(List<GameOfLifeState> neighbours) { - int countAlive = State.count(ALIVE, neighbours); - return (countAlive == 3 || this == ALIVE && countAlive == 2)? - ALIVE: - DEAD; + //TODO: à compléter + return null; } } diff --git a/src/main/java/model/automata/SeedsAutomaton.java b/src/main/java/model/automata/SeedsAutomaton.java deleted file mode 100644 index c7204df48d2c126ee3f70c1f2208dc7de3128beb..0000000000000000000000000000000000000000 --- a/src/main/java/model/automata/SeedsAutomaton.java +++ /dev/null @@ -1,21 +0,0 @@ -package model.automata; - -import java.util.Random; - -public class SeedsAutomaton extends AbstractAutomaton<SeedsState> { - public SeedsAutomaton(int numberOfColumns, int numberOfRows) { - super(numberOfColumns, numberOfRows); - } - - @Override - public SeedsState defaultState() { - return SeedsState.OFF; - } - - @Override - public SeedsState randomState(Random generator) { - return generator.nextInt(10) == 0? - SeedsState.ON: - SeedsState.OFF; - } -} diff --git a/src/main/java/model/automata/SeedsState.java b/src/main/java/model/automata/SeedsState.java deleted file mode 100644 index c8b114aadf06ffc3b2207d7728745f1a2a8edc8e..0000000000000000000000000000000000000000 --- a/src/main/java/model/automata/SeedsState.java +++ /dev/null @@ -1,35 +0,0 @@ -package model.automata; - -import javafx.scene.paint.Color; -import model.State; - -import java.util.List; - - -public enum SeedsState implements State<SeedsState> { - ON, OFF; - @Override - public Color getColor() { - return switch (this) { - case ON -> Color.GOLD; - case OFF -> Color.BLACK; - }; - } - - @Override - public SeedsState next() { - return switch (this) { - case ON -> OFF; - case OFF -> ON; - }; - } - - @Override - public SeedsState update(List<SeedsState> neighbours) { - return switch (this) { - case ON -> OFF; - case OFF -> State.count(ON,neighbours) == 2 ? ON: OFF; - }; - } - -}