Skip to content
Snippets Groups Projects
Commit 5ff138c7 authored by Guyslain's avatar Guyslain
Browse files

Simplify update in interface State

parent b9754f9b
Branches
Tags
No related merge requests found
...@@ -8,7 +8,8 @@ import java.util.List; ...@@ -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 * Represents a state of a cell in a cellular automaton, and the update rules for the cellular
* automaton. * 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> { public interface State<S> {
...@@ -32,7 +33,7 @@ public interface State<S> { ...@@ -32,7 +33,7 @@ public interface State<S> {
* @param neighbours A list of neighboring cell states. * @param neighbours A list of neighboring cell states.
* @return The updated state based on the neighbors. * @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. * Counts the occurrences of a specific state within a list of neighboring states.
......
...@@ -29,7 +29,7 @@ public enum BiColorState implements State<BiColorState> { ...@@ -29,7 +29,7 @@ public enum BiColorState implements State<BiColorState> {
} }
@Override @Override
public BiColorState update(List<State<BiColorState>> neighbours) { public BiColorState update(List<BiColorState> neighbours) {
int countBlue = State.count(BLUE, neighbours); int countBlue = State.count(BLUE, neighbours);
int countRed = State.count(RED, neighbours); int countRed = State.count(RED, neighbours);
int countAlive = countBlue + countRed; int countAlive = countBlue + countRed;
......
...@@ -27,7 +27,7 @@ public enum BriansBrainState implements State<BriansBrainState> { ...@@ -27,7 +27,7 @@ public enum BriansBrainState implements State<BriansBrainState> {
} }
@Override @Override
public BriansBrainState update(List<State<BriansBrainState>> neighbours) { public BriansBrainState update(List<BriansBrainState> neighbours) {
return switch (this) { return switch (this) {
case ON -> DYING; case ON -> DYING;
case DYING -> OFF; case DYING -> OFF;
......
...@@ -29,12 +29,11 @@ public enum GameOfLifeState implements State<GameOfLifeState> { ...@@ -29,12 +29,11 @@ public enum GameOfLifeState implements State<GameOfLifeState> {
} }
@Override @Override
public GameOfLifeState update(List<State<GameOfLifeState>> neighbours) { public GameOfLifeState update(List<GameOfLifeState> neighbours) {
int countAlive = State.count(ALIVE, neighbours); int countAlive = State.count(ALIVE, neighbours);
boolean isAlive = return (countAlive == 3 || this == ALIVE && countAlive == 2)?
(this == DEAD && 3 == countAlive) ALIVE:
|| (this == ALIVE && 2 <= countAlive && countAlive <= 3); DEAD;
return isAlive ? ALIVE : DEAD;
} }
} }
...@@ -25,7 +25,7 @@ public enum SeedsState implements State<SeedsState> { ...@@ -25,7 +25,7 @@ public enum SeedsState implements State<SeedsState> {
} }
@Override @Override
public SeedsState update(List<State<SeedsState>> neighbours) { public SeedsState update(List<SeedsState> neighbours) {
return switch (this) { return switch (this) {
case ON -> OFF; case ON -> OFF;
case OFF -> State.count(ON,neighbours) == 2 ? ON: OFF; case OFF -> State.count(ON,neighbours) == 2 ? ON: OFF;
......
...@@ -25,22 +25,22 @@ class GameOfLifeStateTest { ...@@ -25,22 +25,22 @@ class GameOfLifeStateTest {
@Test @Test
public void testAliveUpdate() { public void testAliveUpdate() {
// Test with three alive neighbors, should be ALIVE // Test with three alive neighbors, should be ALIVE
List<State<GameOfLifeState>> aliveNeighbors = List<GameOfLifeState> aliveNeighbors =
List.of(ALIVE, DEAD, ALIVE, DEAD, ALIVE); List.of(ALIVE, DEAD, ALIVE, DEAD, ALIVE);
assertEquals(ALIVE, ALIVE.update(aliveNeighbors)); assertEquals(ALIVE, ALIVE.update(aliveNeighbors));
// Test with two alive neighbors, should be ALIVE // Test with two alive neighbors, should be ALIVE
List<State<GameOfLifeState>> twoAliveNeighbors = List<GameOfLifeState> twoAliveNeighbors =
List.of(ALIVE, DEAD, ALIVE, DEAD, DEAD); List.of(ALIVE, DEAD, ALIVE, DEAD, DEAD);
assertEquals(ALIVE, ALIVE.update(twoAliveNeighbors)); assertEquals(ALIVE, ALIVE.update(twoAliveNeighbors));
// Test with four alive neighbors, should be DEAD // Test with four alive neighbors, should be DEAD
List<State<GameOfLifeState>> fourAliveNeighbors = List<GameOfLifeState> fourAliveNeighbors =
List.of(ALIVE, ALIVE, DEAD, ALIVE, ALIVE); List.of(ALIVE, ALIVE, DEAD, ALIVE, ALIVE);
assertEquals(DEAD, ALIVE.update(fourAliveNeighbors)); assertEquals(DEAD, ALIVE.update(fourAliveNeighbors));
// Test with zero alive neighbors, should be DEAD // Test with zero alive neighbors, should be DEAD
List<State<GameOfLifeState>> zeroAliveNeighbors = List<GameOfLifeState> zeroAliveNeighbors =
List.of(DEAD, DEAD, DEAD, DEAD); List.of(DEAD, DEAD, DEAD, DEAD);
assertEquals(DEAD, ALIVE.update(zeroAliveNeighbors)); assertEquals(DEAD, ALIVE.update(zeroAliveNeighbors));
} }
...@@ -48,22 +48,22 @@ class GameOfLifeStateTest { ...@@ -48,22 +48,22 @@ class GameOfLifeStateTest {
@Test @Test
public void testDeadUpdate() { public void testDeadUpdate() {
// Test with three alive neighbors, should be ALIVE // Test with three alive neighbors, should be ALIVE
List<State<GameOfLifeState>> aliveNeighbors = List<GameOfLifeState> aliveNeighbors =
List.of(ALIVE, DEAD, ALIVE, DEAD, ALIVE); List.of(ALIVE, DEAD, ALIVE, DEAD, ALIVE);
assertEquals(ALIVE, DEAD.update(aliveNeighbors)); assertEquals(ALIVE, DEAD.update(aliveNeighbors));
// Test with two alive neighbors, should be DEAD // Test with two alive neighbors, should be DEAD
List<State<GameOfLifeState>> twoAliveNeighbors = List<GameOfLifeState> twoAliveNeighbors =
List.of(ALIVE, DEAD, ALIVE, DEAD, DEAD); List.of(ALIVE, DEAD, ALIVE, DEAD, DEAD);
assertEquals(DEAD, DEAD.update(twoAliveNeighbors)); assertEquals(DEAD, DEAD.update(twoAliveNeighbors));
// Test with four alive neighbors, should be DEAD // Test with four alive neighbors, should be DEAD
List<State<GameOfLifeState>> fourAliveNeighbors = List<GameOfLifeState> fourAliveNeighbors =
List.of(ALIVE, ALIVE, DEAD, ALIVE, ALIVE); List.of(ALIVE, ALIVE, DEAD, ALIVE, ALIVE);
assertEquals(DEAD, DEAD.update(fourAliveNeighbors)); assertEquals(DEAD, DEAD.update(fourAliveNeighbors));
// Test with zero alive neighbors, should be DEAD // Test with zero alive neighbors, should be DEAD
List<State<GameOfLifeState>> zeroAliveNeighbors = List<GameOfLifeState> zeroAliveNeighbors =
List.of(DEAD, DEAD, DEAD, DEAD); List.of(DEAD, DEAD, DEAD, DEAD);
assertEquals(DEAD, DEAD.update(zeroAliveNeighbors)); assertEquals(DEAD, DEAD.update(zeroAliveNeighbors));
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment