diff --git a/src/main/java/model/State.java b/src/main/java/model/State.java index 1954558460d6969d09544c64c40152d97c1a736e..33928c02e4cd6655ed0a3b90a8768b8f6d55bf6b 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 7159813139faea3941dc095e09dda0e8a8894a00..72b0854181d9be458b6fc85b6f69297ca8692ff5 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 12ee370cb07f75700dbf5b688c5a89a627f89515..8f64cd8b046a3692fddf17fa2a0ca33a664b20e3 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(); } }