From c0e5765e78328d67ec1c8855f17c6df8b78669fa Mon Sep 17 00:00:00 2001 From: b22015696 <maxence.balme@etu.univ-amu.fr> Date: Tue, 26 Nov 2024 14:17:56 +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/State.java | 9 +++--- .../model/automata/GameOfLifeAutomaton.java | 3 +- .../java/model/automata/GameOfLifeState.java | 30 +++++++++++++++---- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/src/main/java/model/State.java b/src/main/java/model/State.java index 1954558..33928c0 100644 --- a/src/main/java/model/State.java +++ b/src/main/java/model/State.java @@ -25,8 +25,6 @@ public interface State<S> { * * @return The next state. */ - S next(); - /** * Updates the state based on the states of its neighboring cells. * @@ -44,7 +42,10 @@ 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) { - //TODO: à compléter - return 0; + int nb = 0; + for (T n : neighbours){ + if (n.equals(state)) nb++ ; + } + return nb; } } \ No newline at end of file diff --git a/src/main/java/model/automata/GameOfLifeAutomaton.java b/src/main/java/model/automata/GameOfLifeAutomaton.java index 7159813..72b0854 100644 --- a/src/main/java/model/automata/GameOfLifeAutomaton.java +++ b/src/main/java/model/automata/GameOfLifeAutomaton.java @@ -24,8 +24,7 @@ public class GameOfLifeAutomaton implements CellularAutomaton<GameOfLifeState> { @Override public GameOfLifeState defaultState() { - //TODO: à compléter - return null; + return GameOfLifeState.DEAD; } @Override diff --git a/src/main/java/model/automata/GameOfLifeState.java b/src/main/java/model/automata/GameOfLifeState.java index 12ee370..8f64cd8 100644 --- a/src/main/java/model/automata/GameOfLifeState.java +++ b/src/main/java/model/automata/GameOfLifeState.java @@ -14,20 +14,40 @@ public enum GameOfLifeState implements State<GameOfLifeState> { @Override public Color getColor() { - //TODO: à compléter + switch(this) { + case ALIVE: + return Color.RED; + case DEAD: + return Color.WHITE; + } return Color.BLACK; } @Override public GameOfLifeState next() { - //TODO: à compléter - return null; + if (this == GameOfLifeState.ALIVE) { + return GameOfLifeState.DEAD; + } + else if (this == GameOfLifeState.DEAD) { + return GameOfLifeState.ALIVE; + } + } @Override public GameOfLifeState update(List<GameOfLifeState> neighbours) { - //TODO: à compléter - return null; + 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(); } } -- GitLab