diff --git a/src/main/java/model/State.java b/src/main/java/model/State.java index 213723ba0f41e83066099b81e99058f78155930c..64529f0e089771e5fe58051ca7f6ff66f5de66aa 100644 --- a/src/main/java/model/State.java +++ b/src/main/java/model/State.java @@ -8,7 +8,8 @@ import java.util.List; * Represents a state of a cell in a cellular automaton, and the update rules for the cellular * automaton. * - * @param <S> The type of state used in the cellular automaton. + * @param <S> The type of the state itself, used for reflexivity: if a class {@code SomeState} + * wants to implement this interface, it should implement {@code State<SomeState>}. */ public interface State<S> { @@ -32,7 +33,7 @@ public interface State<S> { * @param neighbours A list of neighboring cell states. * @return The updated state based on the neighbors. */ - S update(List<State<S>> neighbours); + S update(List<S> neighbours); /** * Counts the occurrences of a specific state within a list of neighboring states. diff --git a/src/main/java/model/automata/BiColorState.java b/src/main/java/model/automata/BiColorState.java index 834429677919d14824deccca824629db6d9f20a5..5f74ad1015b979ac52612f458d92f126d7fb7eb4 100644 --- a/src/main/java/model/automata/BiColorState.java +++ b/src/main/java/model/automata/BiColorState.java @@ -29,7 +29,7 @@ public enum BiColorState implements State<BiColorState> { } @Override - public BiColorState update(List<State<BiColorState>> neighbours) { + public BiColorState update(List<BiColorState> neighbours) { int countBlue = State.count(BLUE, neighbours); int countRed = State.count(RED, neighbours); int countAlive = countBlue + countRed; diff --git a/src/main/java/model/automata/BriansBrainState.java b/src/main/java/model/automata/BriansBrainState.java index 565868366c40ebfc312e0bc5338f47b9f6b28ac4..3e68b250e9014939c038db85e0b0b5735f401998 100644 --- a/src/main/java/model/automata/BriansBrainState.java +++ b/src/main/java/model/automata/BriansBrainState.java @@ -27,7 +27,7 @@ public enum BriansBrainState implements State<BriansBrainState> { } @Override - public BriansBrainState update(List<State<BriansBrainState>> neighbours) { + public BriansBrainState update(List<BriansBrainState> neighbours) { return switch (this) { case ON -> DYING; case DYING -> OFF; diff --git a/src/main/java/model/automata/GameOfLifeState.java b/src/main/java/model/automata/GameOfLifeState.java index 1d8c75cd7062228220be0c25fcebdb447557e6fa..c100ad441c9875509eb74437f5b70b8aa9ca11be 100644 --- a/src/main/java/model/automata/GameOfLifeState.java +++ b/src/main/java/model/automata/GameOfLifeState.java @@ -29,12 +29,11 @@ public enum GameOfLifeState implements State<GameOfLifeState> { } @Override - public GameOfLifeState update(List<State<GameOfLifeState>> neighbours) { + public GameOfLifeState update(List<GameOfLifeState> neighbours) { int countAlive = State.count(ALIVE, neighbours); - boolean isAlive = - (this == DEAD && 3 == countAlive) - || (this == ALIVE && 2 <= countAlive && countAlive <= 3); - return isAlive ? ALIVE : DEAD; + return (countAlive == 3 || this == ALIVE && countAlive == 2)? + ALIVE: + DEAD; } } diff --git a/src/main/java/model/automata/SeedsState.java b/src/main/java/model/automata/SeedsState.java index c167fadb7c8466c2d383917a375ba5f59e0f82ef..c8b114aadf06ffc3b2207d7728745f1a2a8edc8e 100644 --- a/src/main/java/model/automata/SeedsState.java +++ b/src/main/java/model/automata/SeedsState.java @@ -25,7 +25,7 @@ public enum SeedsState implements State<SeedsState> { } @Override - public SeedsState update(List<State<SeedsState>> neighbours) { + public SeedsState update(List<SeedsState> neighbours) { return switch (this) { case ON -> OFF; case OFF -> State.count(ON,neighbours) == 2 ? ON: OFF; diff --git a/src/test/java/model/automata/GameOfLifeStateTest.java b/src/test/java/model/automata/GameOfLifeStateTest.java index 8c9f0d19fdcb829cd024e82853bb20b38867f8a1..ee988ff84a1937c6b86efab59de11ed21b30a223 100644 --- a/src/test/java/model/automata/GameOfLifeStateTest.java +++ b/src/test/java/model/automata/GameOfLifeStateTest.java @@ -25,22 +25,22 @@ class GameOfLifeStateTest { @Test public void testAliveUpdate() { // Test with three alive neighbors, should be ALIVE - List<State<GameOfLifeState>> aliveNeighbors = + List<GameOfLifeState> aliveNeighbors = List.of(ALIVE, DEAD, ALIVE, DEAD, ALIVE); assertEquals(ALIVE, ALIVE.update(aliveNeighbors)); // Test with two alive neighbors, should be ALIVE - List<State<GameOfLifeState>> twoAliveNeighbors = + List<GameOfLifeState> twoAliveNeighbors = List.of(ALIVE, DEAD, ALIVE, DEAD, DEAD); assertEquals(ALIVE, ALIVE.update(twoAliveNeighbors)); // Test with four alive neighbors, should be DEAD - List<State<GameOfLifeState>> fourAliveNeighbors = + List<GameOfLifeState> fourAliveNeighbors = List.of(ALIVE, ALIVE, DEAD, ALIVE, ALIVE); assertEquals(DEAD, ALIVE.update(fourAliveNeighbors)); // Test with zero alive neighbors, should be DEAD - List<State<GameOfLifeState>> zeroAliveNeighbors = + List<GameOfLifeState> zeroAliveNeighbors = List.of(DEAD, DEAD, DEAD, DEAD); assertEquals(DEAD, ALIVE.update(zeroAliveNeighbors)); } @@ -48,22 +48,22 @@ class GameOfLifeStateTest { @Test public void testDeadUpdate() { // Test with three alive neighbors, should be ALIVE - List<State<GameOfLifeState>> aliveNeighbors = + List<GameOfLifeState> aliveNeighbors = List.of(ALIVE, DEAD, ALIVE, DEAD, ALIVE); assertEquals(ALIVE, DEAD.update(aliveNeighbors)); // Test with two alive neighbors, should be DEAD - List<State<GameOfLifeState>> twoAliveNeighbors = + List<GameOfLifeState> twoAliveNeighbors = List.of(ALIVE, DEAD, ALIVE, DEAD, DEAD); assertEquals(DEAD, DEAD.update(twoAliveNeighbors)); // Test with four alive neighbors, should be DEAD - List<State<GameOfLifeState>> fourAliveNeighbors = + List<GameOfLifeState> fourAliveNeighbors = List.of(ALIVE, ALIVE, DEAD, ALIVE, ALIVE); assertEquals(DEAD, DEAD.update(fourAliveNeighbors)); // Test with zero alive neighbors, should be DEAD - List<State<GameOfLifeState>> zeroAliveNeighbors = + List<GameOfLifeState> zeroAliveNeighbors = List.of(DEAD, DEAD, DEAD, DEAD); assertEquals(DEAD, DEAD.update(zeroAliveNeighbors)); }