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

Simplify update in interface State

parent b9754f9b
No related branches found
No related tags found
No related merge requests found
......@@ -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.
......
......@@ -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;
......
......@@ -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;
......
......@@ -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;
}
}
......@@ -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;
......
......@@ -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));
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment