Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
  • ImprovedMouseInteraction
  • correction_video
  • going_further
  • main
  • ModifGUI
  • final2023
  • template
7 results

Target

Select target project
No results found
Select Git revision
  • ImprovedMouseInteraction
  • correction_video
  • going_further
  • main
  • ModifGUI
  • final2023
  • template
7 results
Show changes

Commits on Source 20

20 files
+ 339
108
Compare changes
  • Side-by-side
  • Inline

Files

+1 −1
Original line number Original line Diff line number Diff line
@@ -19,5 +19,5 @@ jeu de la vie.


## Membre du projet
## Membre du projet


- NOM, prénom, du participant
- SAHIN, Melis Damla
Original line number Original line Diff line number Diff line
@@ -75,3 +75,4 @@ public class SimulatorApplication extends Application {
  }
  }


}
}
Original line number Original line Diff line number Diff line
@@ -124,3 +124,4 @@ public class Controller {
        timeline.pause();
        timeline.pause();
    }
    }
}
}
Original line number Original line Diff line number Diff line
package matrix;
package matrix;


public class ConstantMatrixInitializer<T> implements MatrixInitializer<T> {
public class ConstantMatrixInitializer<T> implements MatrixInitializer<T> {

  private final T constant ;
  // TODO: add instance variables


  public ConstantMatrixInitializer(T constant) {
  public ConstantMatrixInitializer(T constant) {
    // TODO
    this.constant = constant;  }
  }


    @Override
    @Override
    public T initialValueAt(Coordinate coordinate) {
    public T initialValueAt(Coordinate coordinate) {
      // TODO
      return constant;
      return null;
    }
    }
}
}
Original line number Original line Diff line number Diff line
@@ -15,8 +15,7 @@ public record Coordinate(int x, int y) {
     * @return A new {@link Coordinate} instance.
     * @return A new {@link Coordinate} instance.
     */
     */
    public static Coordinate of(int x, int y) {
    public static Coordinate of(int x, int y) {
        // TODO: compléter ce fabriquant
        return new Coordinate(x,y);
        return null;
    }
    }


    /**
    /**
@@ -25,8 +24,7 @@ public record Coordinate(int x, int y) {
     * @return The left adjacent {@link Coordinate}.
     * @return The left adjacent {@link Coordinate}.
     */
     */
    public Coordinate left() {
    public Coordinate left() {
        // TODO: à compléter
        return new Coordinate(x-1,y);
        return null;
    }
    }


    /**
    /**
@@ -35,8 +33,7 @@ public record Coordinate(int x, int y) {
     * @return The right adjacent {@link Coordinate}.
     * @return The right adjacent {@link Coordinate}.
     */
     */
    public Coordinate right() {
    public Coordinate right() {
        // TODO: à compléter
        return new Coordinate(x+1,y);
        return null;
    }
    }


    /**
    /**
@@ -45,8 +42,7 @@ public record Coordinate(int x, int y) {
     * @return The above adjacent {@link Coordinate}.
     * @return The above adjacent {@link Coordinate}.
     */
     */
    public Coordinate above() {
    public Coordinate above() {
        // TODO: à compléter
        return new Coordinate(x,y+1);
        return null;
    }
    }


    /**
    /**
@@ -55,8 +51,7 @@ public record Coordinate(int x, int y) {
     * @return The below adjacent {@link Coordinate}.
     * @return The below adjacent {@link Coordinate}.
     */
     */
    public Coordinate below() {
    public Coordinate below() {
        // TODO: à compléter
        return new Coordinate(x,y-1);
        return null;
    }
    }


    /**
    /**
@@ -73,8 +68,7 @@ public record Coordinate(int x, int y) {
     * @return A list of orthogonal neighboring {@link Coordinate}s.
     * @return A list of orthogonal neighboring {@link Coordinate}s.
     */
     */
    public List<Coordinate> orthogonalNeighbours() {
    public List<Coordinate> orthogonalNeighbours() {
        // TODO: à compléter
        return List.of(above(),right(),left(),below());
        return List.of();
    }
    }


    /**
    /**
@@ -92,8 +86,7 @@ public record Coordinate(int x, int y) {
     * @return A list of diagonal neighboring {@link Coordinate}s.
     * @return A list of diagonal neighboring {@link Coordinate}s.
     */
     */
    public List<Coordinate> diagonalNeighbours() {
    public List<Coordinate> diagonalNeighbours() {
        // TODO: à compléter
        return List.of(new Coordinate(x-1,y-1),new Coordinate(x-1,y+1),new Coordinate(x+1,y-1),new Coordinate(x+1,y+1));
        return List.of();
    }
    }


    /**
    /**
@@ -111,8 +104,7 @@ public record Coordinate(int x, int y) {
     * @return A list of all neighboring {@link Coordinate}s.
     * @return A list of all neighboring {@link Coordinate}s.
     */
     */
    public List<Coordinate> orthodiagonalNeighbours() {
    public List<Coordinate> orthodiagonalNeighbours() {
        // TODO: à compléter
        return List.of(above(),right(),left(),below(),new Coordinate(x-1,y-1),new Coordinate(x-1,y+1),new Coordinate(x+1,y-1),new Coordinate(x+1,y+1));
        return List.of();
    }
    }


    @Override
    @Override
Original line number Original line Diff line number Diff line
@@ -8,6 +8,9 @@ import java.util.NoSuchElementException;
 * height range.
 * height range.
 */
 */
class CoordinateIterator implements Iterator<Coordinate> {
class CoordinateIterator implements Iterator<Coordinate> {
    private final int width;
    private final int height;
    private Coordinate current = new Coordinate(0, 0);


    /**
    /**
     * Creates a new {@link CoordinateIterator} with the specified width and height.
     * Creates a new {@link CoordinateIterator} with the specified width and height.
@@ -16,7 +19,8 @@ class CoordinateIterator implements Iterator<Coordinate> {
     * @param height The height of the coordinate range.
     * @param height The height of the coordinate range.
     */
     */
    public CoordinateIterator(int width, int height) {
    public CoordinateIterator(int width, int height) {
        // TODO: à compléter
        this.width = width;
        this.height = height;
    }
    }


    /**
    /**
@@ -26,8 +30,7 @@ class CoordinateIterator implements Iterator<Coordinate> {
     */
     */
    @Override
    @Override
    public boolean hasNext() {
    public boolean hasNext() {
        // TODO: à compléter
        return current.y() < height;
        return false;
    }
    }


    /**
    /**
@@ -38,7 +41,11 @@ class CoordinateIterator implements Iterator<Coordinate> {
     */
     */
    @Override
    @Override
    public Coordinate next() {
    public Coordinate next() {
        // TODO: à compléter
        if (!hasNext()) throw new NoSuchElementException();
        return null;
        Coordinate next = current;
        current = current.right();
        if (current.x() == width) current = new Coordinate(0, current.y() + 1);

        return next;
    }
    }
}
}
Original line number Original line Diff line number Diff line
package matrix;
package matrix;


import java.util.ArrayList;
import java.util.List;
import java.util.List;




@@ -23,11 +24,10 @@ public class ListMatrix<T> implements Matrix<T> {
   * @param initializer A matrix initializer to set values in the {@link ListMatrix}.
   * @param initializer A matrix initializer to set values in the {@link ListMatrix}.
   */
   */
  public ListMatrix(int width, int height, MatrixInitializer<T> initializer) {
  public ListMatrix(int width, int height, MatrixInitializer<T> initializer) {
    // TODO
    this.width = width;
    this.width = 0;
    this.height = height;
    this.height = 0;
    this.matrix = new ArrayList<>(width);
    this.matrix = null;
    this.initializeWith(initializer);
    this.initializeWith(initializer); // fills the matrix using initializer
  }
  }


  public ListMatrix(int width, int height, T constant) {
  public ListMatrix(int width, int height, T constant) {
@@ -35,29 +35,36 @@ public class ListMatrix<T> implements Matrix<T> {
  }
  }


  private void initializeWith(MatrixInitializer<T> initializer) {
  private void initializeWith(MatrixInitializer<T> initializer) {
    // TODO initialize each cell of the matrix, with a value determined by initializer
    for (int x = 0; x < width; x++) {
      List<T> column = new ArrayList<>(height);

      for (int y = 0; y < height; y++) {
        column.add(initializer.initialValueAt(new Coordinate(x, y)));
      }
      matrix.add(column);
    }
  }
  }



  public int width() {
  public int width() {
    // TODO
    return width;
    return 0;
  }
  }


  public int height() {
  public int height() {
    // TODO
    return height;
    return 0;
  }
  }


  @Override
  @Override
  public T get(int x, int y) {
  public T get(int x, int y) {
    // TODO

    return null;
    return matrix.get(x).get(y);
  }
  }




  @Override
  @Override
  public void set(int x, int y, T newValue) {
  public void set(int x, int y, T newValue) {
    // TODO
    matrix.get(x).set(y,newValue);
  }
  }


}
}
Original line number Original line Diff line number Diff line
@@ -58,7 +58,8 @@ public interface Matrix<T> extends Iterable<T> {
  }
  }


  default Matrix<T> subMatrix(Coordinate corner, int width, int height){
  default Matrix<T> subMatrix(Coordinate corner, int width, int height){
    return null ;

    return new ListMatrix<>(width, height, new SubMatrixInitializer(this, corner));
  }
  }




Original line number Original line Diff line number Diff line
package matrix;

public class SubMatrixInitializer <T> implements MatrixInitializer<T> {
    // Matrix, corner
    private final Matrix<T> matrix;
    private final Coordinate corner;

    public SubMatrixInitializer(Matrix<T> matrix, Coordinate corner) {
        this.matrix = matrix;
        this.corner = corner;
    }
    @Override
    public T initialValueAt(Coordinate coordinate) {
        return matrix.get(coordinate.x()+corner.x(), coordinate.y()+corner.y());
    }
}
Original line number Original line Diff line number Diff line
@@ -11,7 +11,7 @@ import java.util.List;
 */
 */
public class Cell<T> implements Lens<T> {
public class Cell<T> implements Lens<T> {


    //TODO: ajouter la ou les propriétés nécessaires
    private T content;


    // la liste des objets écoutant les modifications du contenu de la cellule
    // la liste des objets écoutant les modifications du contenu de la cellule
    private final List<OnChangeListener<T>> listeners = new ArrayList<>();
    private final List<OnChangeListener<T>> listeners = new ArrayList<>();
@@ -21,7 +21,7 @@ public class Cell<T> implements Lens<T> {
     * @param initialContent the value initially stored by the cell.
     * @param initialContent the value initially stored by the cell.
     */
     */
    public Cell(T initialContent) {
    public Cell(T initialContent) {
        //TODO: à compléter
        this.content = initialContent;
    }
    }


    /** Add a {@link OnChangeListener} to react to any change of value in the cell.
    /** Add a {@link OnChangeListener} to react to any change of value in the cell.
@@ -40,8 +40,12 @@ public class Cell<T> implements Lens<T> {
     * @param value the new content of this {@link Cell}
     * @param value the new content of this {@link Cell}
     */
     */
    public void set(T value) {
    public void set(T value) {
        //TODO: modifier le contenu de la cellule, puis appeler les méthodes valueChanged des
        T oldValue = this.content;
        // listeners
        this.content = value;

        for (OnChangeListener<T> listener : listeners) {
            listener.valueChanged(oldValue, value);
        }
    }
    }


    /**
    /**
@@ -50,7 +54,8 @@ public class Cell<T> implements Lens<T> {
     * @return the current content of this {@link Cell}
     * @return the current content of this {@link Cell}
     */
     */
    public T get(){
    public T get(){
        //TODO: à compléter
        return this.content;
        return null;
    }
    }
}
}

Original line number Original line Diff line number Diff line
@@ -42,14 +42,12 @@ public class CellularAutomatonSimulation<S extends State<S>>


    @Override
    @Override
    public int numberOfColumns() {
    public int numberOfColumns() {
        //TODO: à compléter
        return automaton.numberOfColumns();
        return 0;
    }
    }


    @Override
    @Override
    public int numberOfRows() {
    public int numberOfRows() {
        //TODO: à compléter
        return automaton.numberOfRows();
        return 0;
    }
    }


    /**
    /**
@@ -59,13 +57,16 @@ public class CellularAutomatonSimulation<S extends State<S>>
     * @return The cell at the specified coordinate.
     * @return The cell at the specified coordinate.
     */
     */
    public Cell<S> at(Coordinate coordinate) {
    public Cell<S> at(Coordinate coordinate) {
        //TODO: à compléter
        return grid.get(coordinate);
        return null;
    }
    }


    @Override
    @Override
    public void updateToNextGeneration() {
    public void updateToNextGeneration() {
        //TODO: à compléter, en utilisant nextGenerationMatrix()
        ListMatrix<S> nextStates = nextGenerationMatrix();
        for (Coordinate coordinate : grid.coordinates()) {
            grid.get(coordinate).set(nextStates.get(coordinate));
        }
        generationNumber.set(generationNumber.get() + 1);
    }
    }


    /** Computes the {@link ListMatrix} of states obtained after a single step of updates
    /** Computes the {@link ListMatrix} of states obtained after a single step of updates
@@ -74,23 +75,27 @@ public class CellularAutomatonSimulation<S extends State<S>>
     * @return the states of each cell after one generation
     * @return the states of each cell after one generation
     */
     */
    private ListMatrix<S> nextGenerationMatrix() {
    private ListMatrix<S> nextGenerationMatrix() {
        //TODO: à compléter
        return new ListMatrix<>(
        return null;
                numberOfColumns(),
                numberOfRows(),
                new NextGenerationInitializer<>(this)
        );
    }
    }
    @Override
    @Override
    public void next(Coordinate coordinate) {
    public void next(Coordinate coordinate) {
        //TODO: à compléter
        Cell<S> cell = at(coordinate);
        cell.set(cell.get().next());
    }
    }


    @Override
    @Override
    public void copy(Coordinate source, Coordinate destination) {
    public void copy(Coordinate source, Coordinate destination) {
        //TODO: à compléter
        Cell<S> sourceCell = at(source);
        at(destination).set(sourceCell.get());
    }
    }


    @Override
    @Override
    public Color getColor(Coordinate coordinate) {
    public Color getColor(Coordinate coordinate) {
        //TODO: à compléter
        return at(coordinate).get().getColor();
        return null;
    }
    }


    @Override
    @Override
@@ -108,17 +113,25 @@ public class CellularAutomatonSimulation<S extends State<S>>


    @Override
    @Override
    public void clear() {
    public void clear() {
        //TODO: à compléter (penser à remettre le nombre de génération à 0)
        for(Coordinate coordinate : grid.coordinates()) {
            at(coordinate).set(automaton.defaultState());
        }
        generationNumber.set(0);
    }
    }




    @Override
    @Override
    public void reset() {
    public void reset() {
        //TODO: à compléter (penser à remettre le nombre de génération à 0)
        for(Coordinate coordinate : grid.coordinates()) {
            at(coordinate).set(automaton.randomState(generator));
        }
        generationNumber.set(0);
    }
    }


    @Override
    @Override
    public Iterator<Coordinate> iterator() {
    public Iterator<Coordinate> iterator() {
        return this.grid.coordinates().iterator();
        return this.grid.coordinates().iterator();
    }
    }


}
}
 No newline at end of file
Original line number Original line Diff line number Diff line
@@ -11,20 +11,30 @@ import matrix.MatrixInitializer;
 * @param <T> the type of content of each cell
 * @param <T> the type of content of each cell
 */
 */
public class ConstantCellInitializer<T> implements MatrixInitializer<Cell<T>> {
public class ConstantCellInitializer<T> implements MatrixInitializer<Cell<T>> {
    //TODO: ajouter la/les propriétes nécessaires


    /** Make a new {@link MatrixInitializer} with cells containing a {@link Cell} with the same
    /**
     * The default value to be stored in each cell.
     */
    private final T defaultValue;

    /**
     * Make a new {@link MatrixInitializer} with cells containing a {@link Cell} with the same
     * value.
     * value.
     *
     *
     * @param defaultValue the value stored in each cell.
     * @param defaultValue the value stored in each cell.
     */
     */
    public ConstantCellInitializer(T defaultValue) {
    public ConstantCellInitializer(T defaultValue) {
        //TODO: à compléter
        this.defaultValue = defaultValue;
    }
    }


    /**
     * Returns a new {@link Cell} initialized with the default value for the specified coordinate.
     *
     * @param coordinate The {@link Coordinate} at which to set the initial value.
     * @return A new {@link Cell} containing the default value.
     */
    @Override
    @Override
    public Cell<T> initialValueAt(Coordinate coordinate) {
    public Cell<T> initialValueAt(Coordinate coordinate) {
        //TODO: à compléter
        return new Cell<>(defaultValue);
        return null;
    }
    }
}
}
 No newline at end of file
Original line number Original line Diff line number Diff line
@@ -4,6 +4,10 @@ import matrix.Coordinate;
import matrix.MatrixInitializer;
import matrix.MatrixInitializer;
import matrix.ListMatrix;
import matrix.ListMatrix;
import controller.Simulation;
import controller.Simulation;
import model.automata.GameOfLifeState;

import java.util.ArrayList;
import java.util.List;


/**
/**
 * An initializer for a {@link ListMatrix} of states, where each state is computed based on the value
 * An initializer for a {@link ListMatrix} of states, where each state is computed based on the value
@@ -13,7 +17,8 @@ import controller.Simulation;
 */
 */
public class NextGenerationInitializer<S extends State<S>> implements MatrixInitializer<S> {
public class NextGenerationInitializer<S extends State<S>> implements MatrixInitializer<S> {


    //TODO: ajouter les propriétés nécessaires
    CellularAutomatonSimulation<S> simulation;



    /** Create a {@link MatrixInitializer} to compute the next generation in
    /** Create a {@link MatrixInitializer} to compute the next generation in
     * a 2D cellular automaton.
     * a 2D cellular automaton.
@@ -21,15 +26,20 @@ public class NextGenerationInitializer<S extends State<S>> implements MatrixInit
     * @param simulation the {@link Simulation} representing the cellular automaton.
     * @param simulation the {@link Simulation} representing the cellular automaton.
     */
     */
    public NextGenerationInitializer(CellularAutomatonSimulation<S> simulation) {
    public NextGenerationInitializer(CellularAutomatonSimulation<S> simulation) {
        //TODO: à compléter
        this.simulation = simulation;
    }
    }


    @Override
    @Override
    public S initialValueAt(Coordinate coordinate) {
    public S initialValueAt(Coordinate coordinate) {
        //TODO: à compléter
        List<Coordinate> neighborCoordinates = coordinate.orthodiagonalNeighbours();
        return null;
        List<S> neighborStates = new ArrayList<>();

        for (Coordinate neighbor : neighborCoordinates) {
            neighborStates.add(simulation.at(this.wrap(neighbor)).get());
        }
        }


        return simulation.at(coordinate).get().update(neighborStates);
    }
    /** Computes the grid {@link Coordinate} for an arbitrary {@link Coordinate}, even outside
    /** 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
     * 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
     * side, and the top side to the bottom side. This way, every cell has 4 orthogonal
@@ -39,10 +49,9 @@ public class NextGenerationInitializer<S extends State<S>> implements MatrixInit
     * @return a corresponding {@link Coordinate}, that is inside the grid.
     * @return a corresponding {@link Coordinate}, that is inside the grid.
     */
     */
    Coordinate wrap(Coordinate coordinate) {
    Coordinate wrap(Coordinate coordinate) {
        //TODO: à compléter
        //Il faut recalculer les coordonnées x et y modulo les dimensions de la grille.
        //Il faut recalculer les coordonnées x et y modulo les dimensions de la grille.
        //Pour le modulo, utiliser la fonction ci-dessous, qui s'assure que le résultat est positif.
        //Pour le modulo, utiliser la fonction ci-dessous, qui s'assure que le résultat est positif.
        return null;
        return new Coordinate(modulo(coordinate.x(), simulation.numberOfColumns()), modulo(coordinate.y(), simulation.numberOfRows()));
    }
    }


    /** The non-negative remainder of n divided by d.
    /** The non-negative remainder of n divided by d.
Original line number Original line Diff line number Diff line
@@ -44,7 +44,12 @@ public interface State<S> {
     * @return The number of times the specified state appears in the list of neighbors.
     * @return The number of times the specified state appears in the list of neighbors.
     */
     */
    static <T> int count(T state, List<T> neighbours) {
    static <T> int count(T state, List<T> neighbours) {
        //TODO: à compléter
        int nb = 0;
        return 0;
        for (T neighbour : neighbours) {
            if (state.equals(neighbour)) {
                nb++;
            }
        }
        return nb;
    }
    }
}
}
 No newline at end of file
Original line number Original line Diff line number Diff line
package model.automata;
import model.CellularAutomaton;

import java.util.Random;

public class BiColorGameOfLifeAutomaton implements CellularAutomaton<BiColorGameOfLifeState> {
    private final int rows;
    private final int columns;

    public BiColorGameOfLifeAutomaton(int rows, int columns) {
        this.rows = rows;
        this.columns = columns;
    }

    @Override
    public int numberOfColumns() {
        return columns;
    }

    @Override
    public int numberOfRows() {
        return rows;
    }

    @Override
    public BiColorGameOfLifeState defaultState() {
        return new BiColorGameOfLifeState(BiColorGameOfLifeState.StateType.DEAD);
    }

    @Override
    public BiColorGameOfLifeState randomState(Random generator) {
        return generator.nextDouble() < 0.5
                ? new BiColorGameOfLifeState(BiColorGameOfLifeState.StateType.DEAD)
                : (generator.nextDouble() < 0.5
                ? new BiColorGameOfLifeState(BiColorGameOfLifeState.StateType.ALIVE_RED)
                : new BiColorGameOfLifeState(BiColorGameOfLifeState.StateType.ALIVE_BLUE));
    }
}
 No newline at end of file
Original line number Original line Diff line number Diff line
package model.automata;
import javafx.scene.paint.Color;
import model.State;

import java.util.List;
public class BiColorGameOfLifeState implements State<BiColorGameOfLifeState> {
    public enum StateType {
        DEAD,
        ALIVE_RED,
        ALIVE_BLUE }

    private final StateType currentState;

    public BiColorGameOfLifeState(StateType state) {
        this.currentState = state;
    }

    @Override
    public Color getColor() {
        return switch (currentState) {
            case DEAD -> Color.WHITE;
            case ALIVE_RED -> Color.RED;
            case ALIVE_BLUE -> Color.BLUE;
        };
    }
    @Override
    public BiColorGameOfLifeState next() {
        return new BiColorGameOfLifeState(currentState == StateType.ALIVE_RED ? StateType.ALIVE_BLUE : StateType.ALIVE_RED);
    }
    @Override
    public BiColorGameOfLifeState update(List<BiColorGameOfLifeState> neighbours) {
        int redCount = State.count(new BiColorGameOfLifeState(StateType.ALIVE_RED), neighbours);
        int blueCount = State.count(new BiColorGameOfLifeState(StateType.ALIVE_BLUE), neighbours);
        int liveCount = redCount + blueCount;

        if (currentState == StateType.DEAD && liveCount == 3) {
            return new BiColorGameOfLifeState(redCount > blueCount ? StateType.ALIVE_RED : StateType.ALIVE_BLUE);
        } else if (currentState != StateType.DEAD && (liveCount == 2 || liveCount == 3)) {
            return this;
        } else {
            return new BiColorGameOfLifeState(StateType.DEAD);
        }
    }
}
Original line number Original line Diff line number Diff line
package model.automata;
import model.CellularAutomaton;

import java.util.Random;

public class BrianBrainAutomaton implements CellularAutomaton<BrianBrainState> {
    private final int rows;
    private final int columns;

    public BrianBrainAutomaton(int rows, int columns) {
        this.rows = rows;
        this.columns = columns;
    }

    @Override
    public int numberOfColumns() {
        return columns;
    }

    @Override
    public int numberOfRows() {
        return rows;
    }

    @Override
    public BrianBrainState defaultState() {
        return new BrianBrainState(BrianBrainState.StateType.DEAD);
    }

    @Override
    public BrianBrainState randomState(Random generator) {
        return generator.nextDouble() < 0.5
                ? new BrianBrainState(BrianBrainState.StateType.DEAD)
                : new BrianBrainState(BrianBrainState.StateType.READY_TO_FIRE);
    }
}
Original line number Original line Diff line number Diff line
package model.automata;

import javafx.scene.paint.Color;
import model.State;

import java.util.List;

public class BrianBrainState implements State<BrianBrainState> {
    enum StateType { DEAD,
        FIRE,
        READY_TO_FIRE }

    private final StateType currentState;

    public BrianBrainState(StateType state) {
        this.currentState = state;
    }

    @Override
    public Color getColor() {
        return switch (currentState) {
            case DEAD -> Color.WHITE;
            case FIRE -> Color.RED;
            case READY_TO_FIRE -> Color.YELLOW;
        };
    }
    @Override
    public BrianBrainState next() {
        return new BrianBrainState(currentState == StateType.FIRE ? StateType.DEAD : StateType.FIRE);
    }
    @Override
    public BrianBrainState update(List<BrianBrainState> neighbours) {
        int fireCount = (int) neighbours.stream().filter(n -> n.currentState == StateType.FIRE).count();

        if (currentState == StateType.READY_TO_FIRE && fireCount > 0) {
            return new BrianBrainState(StateType.FIRE);
        } else if (currentState == StateType.FIRE) {
            return new BrianBrainState(StateType.DEAD);
        } else {
            return this;
        }
    }
}
Original line number Original line Diff line number Diff line
@@ -5,32 +5,31 @@ import model.CellularAutomaton;
import java.util.Random;
import java.util.Random;


public class GameOfLifeAutomaton implements CellularAutomaton<GameOfLifeState> {
public class GameOfLifeAutomaton implements CellularAutomaton<GameOfLifeState> {
    private final int numberOfColumns;
    private final int numberOfRows;


    public GameOfLifeAutomaton(int numberOfColumns, int numberOfRows) {
    public GameOfLifeAutomaton(int numberOfColumns, int numberOfRows) {
        //TODO: à compléter
        this.numberOfColumns = numberOfColumns;
        this.numberOfRows = numberOfRows;
    }
    }


    @Override
    @Override
    public int numberOfColumns() {
    public int numberOfColumns() {
        //TODO: à compléter
        return numberOfColumns;
        return 0;
    }
    }


    @Override
    @Override
    public int numberOfRows() {
    public int numberOfRows() {
        //TODO: à compléter
        return numberOfRows;
        return 0;
    }
    }


    @Override
    @Override
    public GameOfLifeState defaultState() {
    public GameOfLifeState defaultState() {
        //TODO: à compléter
        return GameOfLifeState.DEAD;
        return null;
    }
    }


    @Override
    @Override
    public GameOfLifeState randomState(Random generator) {
    public GameOfLifeState randomState(Random generator) {
        //TODO: à compléter
        return generator.nextBoolean() ? GameOfLifeState.ALIVE : GameOfLifeState.DEAD;
        return null;
    }
    }
}
}
Original line number Original line Diff line number Diff line
@@ -14,20 +14,23 @@ public enum GameOfLifeState implements State<GameOfLifeState> {


    @Override
    @Override
    public Color getColor() {
    public Color getColor() {
        //TODO: à compléter
        return this == ALIVE ? Color.RED : Color.WHITE;
        return Color.BLACK;
    }
    }


    @Override
    @Override
    public GameOfLifeState next() {
    public GameOfLifeState next() {
        //TODO: à compléter
        return this == ALIVE ? DEAD : ALIVE;
        return null;
    }
    }


    @Override
    @Override
    public GameOfLifeState update(List<GameOfLifeState> neighbours) {
    public GameOfLifeState update(List<GameOfLifeState> neighbours) {
        //TODO: à compléter
        int aliveNeighbors = State.count(ALIVE, neighbours);
        return null;
        if (this == ALIVE){
            return (aliveNeighbors == 2 || aliveNeighbors == 3) ? ALIVE : DEAD;
        } else {
            return aliveNeighbors == 3 ? ALIVE : DEAD;
        }
    }
    }


}
}