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

Tests and Javadox, thanks chatGPT

parent b3b75a8a
No related branches found
No related tags found
No related merge requests found
Showing
with 432 additions and 123 deletions
......@@ -7,16 +7,13 @@ import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import model.CellularAutomatonSimulation;
import model.automata.BriansBrainAutomaton;
import model.automata.GameOfLifeAutomaton;
import model.automata.BiColorAutomaton;
import model.automata.SeedsAutomaton;
import java.io.IOException;
import java.net.URL;
import java.util.Random;
import static java.util.Objects.requireNonNull;
/**
* Entry point for <i>The Game of Life</i> application.
*
......@@ -41,7 +38,7 @@ public class SimulatorApplication extends Application {
public SimulatorApplication() {
this.simulation =
new CellularAutomatonSimulation<>(
new SeedsAutomaton(NUMBER_OF_COLUMNS,NUMBER_OF_ROWS),
new BiColorAutomaton(NUMBER_OF_COLUMNS,NUMBER_OF_ROWS),
GENERATOR
);
}
......
......@@ -65,7 +65,14 @@ public class Controller {
}
private void setGenerationNumberLabelTextProperty() {
generationNumberLabel.textProperty().bind(simulation.generationNumberProperty().asString());
updateGenerationNumber(0);
this.simulation.setGenerationNumberChangeListener(
(oldValue, newValue) -> updateGenerationNumber(newValue)
);
}
private void updateGenerationNumber(int newValue) {
generationNumberLabel.textProperty().set(String.valueOf(newValue));
}
private void initializeMatrixPane() {
......
package controller;
import datastruct.Coordinate;
import javafx.beans.property.ReadOnlyLongProperty;
import javafx.scene.paint.Color;
import model.OnChangeListener;
/**
* Represents a simulation of a 2D cellular automaton, such as the Game of Life.
* Provides methods for updating the simulation, retrieving information, and managing listeners.
*/
public interface Simulation extends Iterable<Coordinate> {
/**
* Returns the number of columns in the simulation grid.
*
* @return The number of columns in the grid.
*/
int numberOfColumns();
/**
* Returns the number of rows in the simulation grid.
*
* @return The number of rows in the grid.
*/
int numberOfRows();
/**
* Updates the simulation to the next generation. This is done by computing, for each
* coordinate, a new state that depends on the states of its neighbours.
*/
void updateToNextGeneration();
/**
* Changes the state at a given {@link Coordinate}. This is used to edit the grid with the mouse. It
* is not part of the simulation of the cellular automaton.
*
* @param coordinate The {@link Coordinate} to advance to the next state.
*/
void next(Coordinate coordinate);
/**
* Copies the state from the source {@link Coordinate} to the destination {@link Coordinate}.
*
* @param source The source {@link Coordinate}.
* @param destination The destination {@link Coordinate}.
*/
void copy(Coordinate source, Coordinate destination);
/**
* Gets the {@link Color} associated with the state at the specified {@link Coordinate}.
*
* @param coordinate The {@link Coordinate} to retrieve the color for.
* @return The {@link Color} associated with the state at the specified {@link Coordinate}.
*/
Color getColor(Coordinate coordinate);
/**
* Sets a listener to be executed when the state at the specified {@link Coordinate} changes.
*
* @param coordinate The {@link Coordinate} to listen for changes.
* @param listener The listener to execute when the state changes.
*/
void setChangeListener(Coordinate coordinate, Runnable listener);
ReadOnlyLongProperty generationNumberProperty();
/**
* Sets a listener to be executed when the generation number changes.
*
* @param listener The listener to execute when the generation number changes.
*/
void setGenerationNumberChangeListener(OnChangeListener<Integer> listener);
/**
* Resets the simulation to random states.
*/
void reset();
/**
* Clears the simulation, setting all states to their default values.
*/
void clear();
}
......@@ -3,28 +3,63 @@ package datastruct;
import java.util.ArrayList;
import java.util.List;
/**
* Represents a 2D integer coordinate used to specify positions in a grid.
*/
public record Coordinate(int x, int y) {
/**
* Creates a new {@link Coordinate} instance with the given {@code x} and {@code y} values.
*
* @param x The x-coordinate value.
* @param y The y-coordinate value.
* @return A new {@link Coordinate} instance.
*/
public static Coordinate of(int x, int y) {
return new Coordinate(x, y);
}
/**
* Computes and returns the {@link Coordinate} to the left of this one.
*
* @return The left adjacent {@link Coordinate}.
*/
public Coordinate left() {
return new Coordinate(x - 1, y);
}
/**
* Computes and returns the {@link Coordinate} to the right of this one.
*
* @return The right adjacent {@link Coordinate}.
*/
public Coordinate right() {
return new Coordinate(x + 1, y);
}
/**
* Computes and returns the {@link Coordinate} above this one.
*
* @return The above adjacent {@link Coordinate}.
*/
public Coordinate above() {
return new Coordinate(x, y + 1);
}
/**
* Computes and returns the {@link Coordinate} below this one.
*
* @return The below adjacent {@link Coordinate}.
*/
public Coordinate below() {
return new Coordinate(x, y - 1);
}
/**
* Computes and returns a list of orthogonal (adjacent in horizontal or vertical direction) neighbors.
*
* @return A list of orthogonal neighboring {@link Coordinate}s.
*/
public List<Coordinate> orthogonalNeighbours() {
return List.of(
this.right(),
......@@ -34,6 +69,11 @@ public record Coordinate(int x, int y) {
);
}
/**
* Computes and returns a list of diagonal (adjacent in diagonal direction) neighbors.
*
* @return A list of diagonal neighboring {@link Coordinate}s.
*/
public List<Coordinate> diagonalNeighbours() {
return List.of(
this.right().above(),
......@@ -43,9 +83,19 @@ public record Coordinate(int x, int y) {
);
}
/**
* Computes and returns a list of all orthogonal and diagonal neighbors.
*
* @return A list of all neighboring {@link Coordinate}s.
*/
public List<Coordinate> orthodiagonalNeighbours() {
List<Coordinate> neighbours = new ArrayList<>(this.orthogonalNeighbours());
neighbours.addAll(this.diagonalNeighbours());
return neighbours;
}
@Override
public String toString() {
return "(" + this.x + "," + this.y + ")";
}
}
\ No newline at end of file
......@@ -3,33 +3,54 @@ package datastruct;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* An {@link Iterator} for generating 2D {@link Coordinate}s within a specified width and
* height range.
*/
class CoordinateIterator implements Iterator<Coordinate> {
private final int width;
private final int height;
private int x = 0;
private int y = 0;
/**
* Creates a new {@link CoordinateIterator} with the specified width and height.
*
* @param width The width of the coordinate range.
* @param height The height of the coordinate range.
*/
public CoordinateIterator(int width, int height) {
this.width = width;
this.height = height;
}
/**
* Checks if there are more {@link Coordinate}s to iterate over.
*
* @return true if there are more {@link Coordinate}s; otherwise, false.
*/
@Override
public boolean hasNext() {
return y < this.height;
}
/**
* Returns the next {@link Coordinate} in the iteration.
*
* @return The next {@link Coordinate} in the iteration.
* @throws NoSuchElementException if there are no more {@link Coordinate}s to iterate over.
*/
@Override
public Coordinate next() {
if (!this.hasNext()) {
throw new NoSuchElementException();
}
Coordinate coord = new Coordinate(this.x, this.y);
Coordinate coordinate = new Coordinate(this.x, this.y);
this.x = this.x + 1;
if (this.x == this.width) {
this.x = 0;
this.y = this.y + 1;
}
return coord;
return coordinate;
}
}
package datastruct;
/**
* A lens interface representing a view into a mutable state.
*
* @param <S> The type of the value stored in the lens.
*/
public interface Lens<S> {
/**
* Gets the value from the {@link Lens}.
*
* @return The value stored in the place designated by {@link Lens}.
*/
S get();
/**
* Sets a new value into the {@link Lens}.
*
* @param value The new value to set in the place designated by the {@link Lens}.
*/
void set(S value);
}
......@@ -4,14 +4,26 @@ import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* Represents a matrix, a rectangular array, with generic values in each cell.
*
* @param <T> The type of values stored in the matrix cells.
*/
public class Matrix<T> implements Iterable<T> {
private final List<List<T>> matrix;
private final int width;
private final int height;
/**
* Creates a new {@link Matrix} with the specified width, height, and an initializer to set
* values.
*
* @param width The width of the {@link Matrix}.
* @param height The height of the {@link Matrix}.
* @param initializer A matrix initializer to set values in the {@link Matrix}.
*/
public Matrix(int width, int height, MatrixInitializer<T> initializer) {
this.width = width;
this.height = height;
......@@ -19,6 +31,14 @@ public class Matrix<T> implements Iterable<T> {
this.initializeWith(initializer);
}
/**
* Creates a new {@link Matrix} with the specified width, height, and initial value for all
* cells.
*
* @param width The width of the {@link Matrix}.
* @param height The height of the {@link Matrix}.
* @param initialValue The initial value to set in all cells of the {@link Matrix}.
*/
public Matrix(int width, int height, T initialValue) {
this(width, height, new ConstantMatrixInitializer<>(initialValue));
}
......@@ -33,44 +53,107 @@ public class Matrix<T> implements Iterable<T> {
}
}
/**
* Returns the width of the {@link Matrix}.
*
* @return The width of the {@link Matrix}.
*/
public int width() {
return width;
}
/**
* Returns the height of the {@link Matrix}.
*
* @return The height of the {@link Matrix}.
*/
public int height() {
return height;
}
/**
* Gets the value at the specified coordinates (x, y) in the {@link Matrix}.
*
* @param x The x-coordinate.
* @param y The y-coordinate.
* @return The value at the specified coordinates.
*/
public T get(int x, int y) {
return this.matrix.get(x).get(y);
}
public T get(Coordinate coord) {
return this.get(coord.x(), coord.y());
/**
* Gets the value at the specified {@link Coordinate} in the {@link Matrix}.
*
* @param coordinate The {@link Coordinate}.
* @return The value at the specified {@link Coordinate}.
*/
public T get(Coordinate coordinate) {
return this.get(coordinate.x(), coordinate.y());
}
/**
* Sets the value at the specified coordinates (x, y) in the {@link Matrix}.
*
* @param x The x-coordinate.
* @param y The y-coordinate.
* @param value The value to set at the specified coordinates.
*/
public void set(int x, int y, T value) {
this.matrix.get(x).set(y, value);
}
public void set(Coordinate coord, T value) {
this.set(coord.x(), coord.y(), value);
}
/**
* Sets the value at the specified {@link Coordinate} in the {@link Matrix}.
*
* @param coordinate The {@link Coordinate}.
* @param value The value to set at the specified {@link Coordinate}.
*/
public void set(Coordinate coordinate, T value) {
this.set(coordinate.x(), coordinate.y(), value);
}
/**
* Returns an {@link Iterator} that allows iterating over the elements in the {@link Matrix} in
* row-major order.
*
* @return An {@link Iterator} for the {@link Matrix}.
*/
public Iterator<T> iterator() {
Iterator<Coordinate> coordIterator = this.coordinatesIterator();
return new MatrixIterator<>(this, coordIterator);
}
/**
* Returns an {@link Iterable} that provides access to the {@link Coordinate}s of the
* {@link Matrix} in row-major order. This means that a {@code for} loop on a {@link Matrix}
* will loop over the coordinates of the {@link Matrix}.
*
* @return An {@link Iterable} for the {@link Coordinate}s of the {@link Matrix}.
*/
public Iterable<Coordinate> coordinates() {
return this::coordinatesIterator;
}
/**
* Returns an {@link Iterator} that allows iterating over the {@link Coordinate}s in the
* {@link Matrix} in row-major order.
*
* @return An {@link Iterator} for the {@link Matrix}.
*/
private Iterator<Coordinate> coordinatesIterator() {
return new CoordinateIterator(this.width, this.height);
}
/**
* Returns a lens for accessing and modifying the value at the specified coordinates (x, y) in
* the {@link Matrix}.
*
* @param x The x-coordinate.
* @param y The y-coordinate.
* @return A lens for the specified coordinates.
*/
public Lens<T> at(int x, int y) {
return new Lens<T>() {
@Override
......@@ -85,8 +168,15 @@ public class Matrix<T> implements Iterable<T> {
};
}
public Lens<T> at(Coordinate coord) {
return this.at(coord.x(), coord.y());
/**
* Returns a lens for accessing and modifying the value at the specified coordinate in the
* {@link Matrix}.
*
* @param coordinate The {@link Coordinate}.
* @return A lens for the specified {@link Coordinate}.
*/
public Lens<T> at(Coordinate coordinate) {
return this.at(coordinate.x(), coordinate.y());
}
}
package datastruct;
/**
* An interface for initializing a {@link Matrix} by providing initial values for each cell.
*
* @param <T> The type of values to initialize the {@link Matrix} with.
*/
public interface MatrixInitializer<T> {
/**
* Returns the initial value to be set in a {@link Matrix} cell at the specified
* {@link Coordinate}.
*
* @param coordinate The {@link Coordinate} at which to set the initial value.
* @return The initial value for the specified cell.
*/
T initialValueAt(Coordinate coordinate);
}
......@@ -5,10 +5,12 @@ import datastruct.Lens;
import java.util.ArrayList;
import java.util.List;
/**
* {@link Cell} instances represent the cells of the grid in a simulation of cellular automata.
* A class representing a cell that holds a value and allows adding listeners to track value changes.
*
* @param <T> The type of value stored in the cell.
*/
public class Cell<T> implements Lens<T> {
private T content;
private final List<OnChangeListener<T>> listeners = new ArrayList<>();
......@@ -21,19 +23,26 @@ public class Cell<T> implements Lens<T> {
this.content = initialContent;
}
/** Add a {@link OnChangeListener} to react to any change of value in the cell.
*
* @param listener the {@link OnChangeListener} to activate when the value in the cell is
* changed.
*/
public void addOnChangeListener(OnChangeListener<T> listener) {
this.listeners.add(listener);
}
/**
* Sets the content of this {@link Cell}.
* Sets the content of this {@link Cell}. This will also call all the listeners that were
* registered by the method {@code addOnChangeListener}.
*
* @param value the new content of this {@link Cell}
*/
public void set(T value) {
T oldValue = this.content;
this.content = value;
for (OnChangeListener<T> listener : this.listeners) {
listener.valueChanged(this.content, value);
listener.valueChanged(oldValue, value);
}
}
......@@ -45,6 +54,4 @@ public class Cell<T> implements Lens<T> {
public T get(){
return this.content;
}
}
......@@ -2,9 +2,41 @@ package model;
import java.util.Random;
/**
* Represents a cellular automaton, which defines the main parameters of a cellular automaton.
* The rules for updating states are defined in the class used as {@code S}.
*
* @param <S> The type of state used in the cellular automaton.
*/
public interface CellularAutomaton<S extends State<S>> {
/**
* Returns the number of columns in the grid of the cellular automaton.
*
* @return The number of columns in the grid.
*/
int numberOfColumns();
/**
* Returns the number of rows in the grid of the cellular automaton.
*
* @return The number of rows in the grid.
*/
int numberOfRows();
/**
* Returns the default state that is used to initialize cells in the automaton.
*
* @return The default state for cells in the automaton.
*/
S defaultState();
/**
* Generates a random state using the specified random number generator.
*
* @param generator The random number generator to use.
* @return A randomly generated state.
*/
S randomState(Random generator);
}
\ No newline at end of file
......@@ -3,30 +3,31 @@ package model;
import controller.Simulation;
import datastruct.Coordinate;
import datastruct.Matrix;
import javafx.beans.property.ReadOnlyLongProperty;
import javafx.beans.property.ReadOnlyLongWrapper;
import javafx.scene.paint.Color;
import java.util.Iterator;
import java.util.Random;
import java.util.function.Supplier;
/**
* {@link CellularAutomatonSimulation} instances run <i>The Game of Life</i>.
*
* @param <S> The type of state used in the simulation.
*/
public class CellularAutomatonSimulation<S extends State<S>>
implements Simulation {
private final Matrix<Cell<S>> grid;
private final ReadOnlyLongWrapper generationNumber = new ReadOnlyLongWrapper();
private final Cell<Integer> generationNumber = new Cell<>(0);
private final CellularAutomaton<S> automaton;
private final Random generator;
/**
* Creates a new {@link CellularAutomatonSimulation} instance for a given automaton.
*
* @param automaton a description of the {@link CellularAutomaton}
* @param automaton A description of the {@link CellularAutomaton}.
* @param generator The {@link Random} instance used for random state generation.
*/
public CellularAutomatonSimulation(CellularAutomaton<S> automaton, Random generator) {
this.automaton = automaton;
......@@ -38,18 +39,6 @@ public class CellularAutomatonSimulation<S extends State<S>>
this.generator = generator;
}
/**
* Goes through each {@link Cell} in this {@code CellGrid} and sets it states with a
* state obtained from the supplier.
*
* @param generator {@link Random} instance used to generate a random state for each cell
* {@link Cell}.
*/
public void fillRandomly(Random generator) {
for (Cell<S> cell : this.grid) {
cell.set(this.automaton.randomState(generator));
}
}
@Override
public int numberOfColumns() {
......@@ -61,24 +50,32 @@ public class CellularAutomatonSimulation<S extends State<S>>
return this.grid.height();
}
/**
* Returns the {@link Cell} at the specified coordinate.
*
* @param coordinate The coordinate of the cell to retrieve.
* @return The cell at the specified coordinate.
*/
public Cell<S> at(Coordinate coordinate) {
return this.grid.get(coordinate);
}
@Override
public void updateToNextGeneration() {
this.generationNumber.set(getGenerationNumber() + 1);
this.generationNumber.set(this.generationNumber.get()+1);
Matrix<S> nextStates = this.nextGenerationMatrix();
for (Coordinate coordinate : this.grid.coordinates()) {
this.at(coordinate).set(nextStates.get(coordinate));
}
}
/** Computes the {link Matrix} of states obtained after a single step of updates
/** Computes the {@link Matrix} of states obtained after a single step of updates
* of the simulation.
*
* @return the states of each cell after one generation
*/
private Matrix<S> nextGenerationMatrix() {
return new Matrix<S>(
return new Matrix<>(
this.grid.width(),
this.grid.height(),
new NextGenerationInitializer<>(this)
......@@ -92,7 +89,6 @@ public class CellularAutomatonSimulation<S extends State<S>>
@Override
public void copy(Coordinate source, Coordinate destination) {
System.out.println("bip (" + source + ") (" + destination + ")");
S state = this.at(source).get();
this.at(destination).set(state);
}
......@@ -109,29 +105,13 @@ public class CellularAutomatonSimulation<S extends State<S>>
);
}
/**
* Returns the current generationNumber.
*
* @return the current generationNumber
*/
private long getGenerationNumber() {
return this.generationNumber.get();
}
/**
* Returns the generationNumber {@link ReadOnlyLongProperty}.
*
* @return the generationNumber {@link ReadOnlyLongProperty}
*/
public ReadOnlyLongProperty generationNumberProperty() {
return this.generationNumber.getReadOnlyProperty();
@Override
public void setGenerationNumberChangeListener(OnChangeListener<Integer> listener){
this.generationNumber.addOnChangeListener(listener);
}
/**
* Clears the current game.
*/
@Override
public void clear() {
for (Cell<S> cell : this.grid) {
cell.set(this.automaton.defaultState());
......@@ -139,12 +119,13 @@ public class CellularAutomatonSimulation<S extends State<S>>
this.generationNumber.set(0);
}
/**
* Clears the current game and randomly generates a new one.
*/
@Override
public void reset() {
this.clear();
this.fillRandomly(this.generator);
for (Cell<S> cell : this.grid) {
cell.set(this.automaton.randomState(generator));
}
this.generationNumber.set(0);
}
@Override
......
package model;
import datastruct.Coordinate;
import datastruct.Matrix;
import datastruct.MatrixInitializer;
/**
* An initializer for {@link Matrix} of {@link Cell}s, where each cell is initialized to the
* same value.
*
* @param <T> the type of content of each cell
*/
public class ConstantCellInitializer<T> implements MatrixInitializer<Cell<T>> {
private final T defaultValue;
/** Make a new {@link MatrixInitializer} with cells containing a {@link Cell} with the same
* value.
*
* @param defaultValue the value stored in each cell.
*/
public ConstantCellInitializer(T defaultValue) {
this.defaultValue = defaultValue;
}
......
package model;
import datastruct.Coordinate;
import datastruct.Matrix;
import datastruct.MatrixInitializer;
import datastruct.Matrix;
import controller.Simulation;
import java.util.ArrayList;
import java.util.List;
/**
* An initializer for a {@link Matrix} of states, where each state is computed based on the value
* of its neighbours in a {@link Simulation} of a cellular automaton.
*
* @param <S> the type of states in the simulation.
*/
public class NextGenerationInitializer<S extends State<S>> implements MatrixInitializer<S> {
private final CellularAutomatonSimulation<S> simulation;
/** Create a {@link MatrixInitializer} to compute the next generation in
* a 2D cellular automaton.
*
* @param simulation the {@link Simulation} representing the cellular automaton.
*/
public NextGenerationInitializer(CellularAutomatonSimulation<S> simulation) {
this.simulation = simulation;
}
......@@ -26,6 +37,14 @@ public class NextGenerationInitializer<S extends State<S>> implements MatrixInit
return state.update(neighbours);
}
/** Computes the grid {@link Coordinate} for an arbitrary {@link Coordinate}, even outside
* the grid. This is done by considering that the grid wraps over its edges, connecting the left side to the right
* side, and the top side to the bottom side. This way, every cell has 4 orthogonal
* neighbours and 4 diagonal neighbours.
*
* @param coordinate a {@link Coordinate} that may be outside the grid.
* @return a corresponding {@link Coordinate}, that is inside the grid.
*/
private Coordinate wrap(Coordinate coordinate) {
return new Coordinate(
modulo(coordinate.x(),this.simulation.numberOfColumns()),
......
......@@ -4,12 +4,51 @@ import javafx.scene.paint.Color;
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.
*/
public interface State<S> {
/**
* Returns the color associated with this state.
*
* @return The color representing this state.
*/
Color getColor();
/**
* Computes and returns the next state based on the rules of the cellular automaton.
*
* @return The next state.
*/
S next();
/**
* Updates the state based on the states of its neighboring cells.
*
* @param neighbours A list of neighboring cell states.
* @return The updated state based on the neighbors.
*/
S update(List<State<S>> neighbours);
/**
* Counts the occurrences of a specific state within a list of neighboring states.
*
* @param <T> The type of state to count.
* @param state The state to count occurrences of.
* @param neighbours A list of neighboring states to search within.
* @return The number of times the specified state appears in the list of neighbors.
*/
static <T> int count(T state, List<T> neighbours) {
int count = 0;
for (T neighbour : neighbours) {
if (neighbour.equals(state)) {
count++;
}
}
return count;
}
}
\ No newline at end of file
package model.automata;
import model.CellularAutomaton;
import java.util.Random;
public class BiColorAutomaton extends AbstractAutomaton<BiColorState> {
......
......@@ -4,7 +4,6 @@ import javafx.scene.paint.Color;
import model.State;
import java.util.List;
import java.util.Random;
public enum BiColorState implements State<BiColorState> {
BLUE, RED, DEAD;
......@@ -31,22 +30,15 @@ public enum BiColorState implements State<BiColorState> {
@Override
public BiColorState update(List<State<BiColorState>> neighbours) {
int countBlue = 0;
int countRed = 0;
for (State<BiColorState> neighbour : neighbours) {
if (neighbour == RED) {
countRed++;
}
if (neighbour == BLUE) {
countBlue++;
}
}
int countBlue = State.count(BLUE, neighbours);
int countRed = State.count(RED, neighbours);
int countAlive = countBlue + countRed;
if (this == DEAD) {
return (countBlue + countRed != 3)? DEAD:
return (countAlive != 3)? DEAD:
countBlue > countRed? BLUE:
RED;
}
return 2 <= countBlue + countRed && countBlue + countRed <= 3? this:
return 2 <= countAlive && countAlive <= 3? this:
DEAD;
}
......
......@@ -4,7 +4,6 @@ import javafx.scene.paint.Color;
import model.State;
import java.util.List;
import java.util.Random;
public enum BriansBrainState implements State<BriansBrainState> {
ON, OFF, DYING;
......@@ -33,19 +32,11 @@ public enum BriansBrainState implements State<BriansBrainState> {
case ON -> DYING;
case DYING -> OFF;
case OFF -> {
int count = countList(ON, neighbours);
int count = State.count(ON, neighbours);
yield count==2 ? ON : OFF;
}
};
}
static <T> int countList(T value, List<T> elements) {
int count = 0;
for (T v : elements) {
if (v.equals(value)) {
count++;
}
}
return count;
}
}
package model.automata;
import model.CellularAutomaton;
import java.util.Random;
public class GameOfLifeAutomaton extends AbstractAutomaton<GameOfLifeState> {
protected GameOfLifeAutomaton(int numberOfColumns, int numberOfRows) {
public GameOfLifeAutomaton(int numberOfColumns, int numberOfRows) {
super(numberOfColumns, numberOfRows);
}
......
......@@ -4,7 +4,6 @@ import javafx.scene.paint.Color;
import model.State;
import java.util.List;
import java.util.Random;
/**
* {@link GameOfLifeState} instances represent the possible states of a {@link GameOfLifeState}.
......@@ -31,12 +30,7 @@ public enum GameOfLifeState implements State<GameOfLifeState> {
@Override
public GameOfLifeState update(List<State<GameOfLifeState>> neighbours) {
int countAlive = 0;
for (State<GameOfLifeState> state : neighbours) {
if (state.equals(ALIVE)) {
countAlive++;
}
}
int countAlive = State.count(ALIVE, neighbours);
boolean isAlive =
(this == DEAD && 3 == countAlive)
|| (this == ALIVE && 2 <= countAlive && countAlive <= 3);
......
......@@ -14,7 +14,7 @@ public class SeedsAutomaton extends AbstractAutomaton<SeedsState> {
@Override
public SeedsState randomState(Random generator) {
return generator.nextInt() == 0?
return generator.nextInt(10) == 0?
SeedsState.ON:
SeedsState.OFF;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment