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
  • s20026898/tp-6
  • boukenze.b/jeu-de-la-vie-tp-3
  • b22015696/game-of-life-template
  • s23026062/sahin-game-of-life-template
  • m22023183/game-of-life-MALEK
  • z23012739/game-of-life-template
  • p23021107/poussardin-malo-game-of-life-template
  • o21225801/game-of-life-template
  • alaboure/game-fo-life-template
  • t22007439/game-of-life-toullec
  • b23021750/game-of-life
  • c22029830/game-of-life-template-rafi
  • b23025683/game-of-life-template-tp-6
  • gnaves/game-of-life-template
  • a22025223/game-of-life-template-cristel
  • f22024692/game-of-life-template-paolo-mathis-erwan
  • t21233923/game-fo-life-template
  • h21231335/game-fo-life-template
  • l22023519/game-of-life-template-salma
  • p23020787/game-of-life-template
  • b21232450/game-of-life-template
  • s22031458/game-of-life
  • n21223697/tp-4-ngom
  • a22027291/game-of-life-of-salim
  • k22029508/tp-4
  • s19033421/game-of-life-template
  • b21229750/jeu-de-la-vie-tp-3
  • saddem.r/game-of-life-template
  • l3_s3_infoamu/s3/programmation-2/game-fo-life-template
29 results
Select Git revision
  • ImprovedMouseInteraction
  • correction_video
  • going_further
  • main
  • ModifGUI
  • final2023
  • template
7 results
Show changes

Commits on Source 10

Showing
with 219 additions and 83 deletions
......@@ -8,6 +8,7 @@ import javafx.scene.Scene;
import javafx.stage.Stage;
import model.CellularAutomatonSimulation;
import model.automata.GameOfLifeAutomaton;
import model.automata.WireWorldAutomaton;
import java.io.IOException;
import java.net.URL;
......@@ -37,7 +38,7 @@ public class SimulatorApplication extends Application {
public SimulatorApplication() {
this.simulation =
new CellularAutomatonSimulation<>(
new GameOfLifeAutomaton(NUMBER_OF_COLUMNS,NUMBER_OF_ROWS),
new WireWorldAutomaton(NUMBER_OF_COLUMNS,NUMBER_OF_ROWS),
GENERATOR
);
}
......
......@@ -37,7 +37,7 @@ public interface Simulation extends Iterable<Coordinate> {
*
* @param coordinate The {@link Coordinate} to advance to the next state.
*/
void next(Coordinate coordinate);
void cycleThrough(Coordinate coordinate);
/**
* Copies the state from the source {@link Coordinate} to the destination {@link Coordinate}.
......
......@@ -16,8 +16,7 @@ public record Coordinate(int x, int y) {
* @return A new {@link Coordinate} instance.
*/
public static Coordinate of(int x, int y) {
// TODO: compléter ce fabriquant
return null;
return new Coordinate(x,y);
}
/**
......@@ -26,8 +25,7 @@ public record Coordinate(int x, int y) {
* @return The left adjacent {@link Coordinate}.
*/
public Coordinate left() {
// TODO: à compléter
return null;
return new Coordinate(this.x - 1, this.y);
}
/**
......@@ -36,8 +34,7 @@ public record Coordinate(int x, int y) {
* @return The right adjacent {@link Coordinate}.
*/
public Coordinate right() {
// TODO: à compléter
return null;
return new Coordinate(this.x + 1, this.y);
}
/**
......@@ -46,8 +43,7 @@ public record Coordinate(int x, int y) {
* @return The above adjacent {@link Coordinate}.
*/
public Coordinate above() {
// TODO: à compléter
return null;
return new Coordinate(this.x, this.y + 1);
}
/**
......@@ -56,8 +52,7 @@ public record Coordinate(int x, int y) {
* @return The below adjacent {@link Coordinate}.
*/
public Coordinate below() {
// TODO: à compléter
return null;
return new Coordinate(this.x, this.y - 1);
}
/**
......@@ -66,8 +61,12 @@ public record Coordinate(int x, int y) {
* @return A list of orthogonal neighboring {@link Coordinate}s.
*/
public List<Coordinate> orthogonalNeighbours() {
// TODO: à compléter
return List.of();
return List.of(
this.right(),
this.above(),
this.left(),
this.below()
);
}
/**
......@@ -76,8 +75,12 @@ public record Coordinate(int x, int y) {
* @return A list of diagonal neighboring {@link Coordinate}s.
*/
public List<Coordinate> diagonalNeighbours() {
// TODO: à compléter
return List.of();
return List.of(
this.right().above(),
this.left().above(),
this.left().below(),
this.right().below()
);
}
/**
......@@ -86,8 +89,10 @@ public record Coordinate(int x, int y) {
* @return A list of all neighboring {@link Coordinate}s.
*/
public List<Coordinate> orthodiagonalNeighbours() {
// TODO: à compléter
return List.of();
List<Coordinate> neighbours = new ArrayList<>();
neighbours.addAll(this.orthogonalNeighbours());
neighbours.addAll(this.diagonalNeighbours());
return neighbours;
}
@Override
......
......@@ -9,6 +9,10 @@ import java.util.NoSuchElementException;
*/
class CoordinateIterator implements Iterator<Coordinate> {
private Coordinate current = new Coordinate(0,0);
private final int width;
private final int height;
/**
* Creates a new {@link CoordinateIterator} with the specified width and height.
*
......@@ -16,7 +20,8 @@ class CoordinateIterator implements Iterator<Coordinate> {
* @param height The height of the coordinate range.
*/
public CoordinateIterator(int width, int height) {
// TODO: à compléter
this.width = width;
this.height = height;
}
/**
......@@ -26,8 +31,7 @@ class CoordinateIterator implements Iterator<Coordinate> {
*/
@Override
public boolean hasNext() {
// TODO: à compléter
return false;
return this.current.y() != height;
}
/**
......@@ -38,7 +42,18 @@ class CoordinateIterator implements Iterator<Coordinate> {
*/
@Override
public Coordinate next() {
// TODO: à compléter
return null;
if (!hasNext()) {
throw new NoSuchElementException();
}
Coordinate next = this.current;
this.update();
return next;
}
private void update() {
this.current = this.current.right();
if (this.current.x() == this.width) {
this.current = new Coordinate(0,this.current.y() + 1);
}
}
}
......@@ -13,7 +13,7 @@ import java.util.List;
*/
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
private final List<OnChangeListener<T>> listeners = new ArrayList<>();
......@@ -23,7 +23,7 @@ public class Cell<T> implements Lens<T> {
* @param initialContent the value initially stored by the cell.
*/
public Cell(T initialContent) {
//TODO: à compléter
this.content = initialContent;
}
/** Add a {@link OnChangeListener} to react to any change of value in the cell.
......@@ -42,8 +42,11 @@ public class Cell<T> implements Lens<T> {
* @param value the new content of this {@link Cell}
*/
public void set(T value) {
//TODO: modifier le contenu de la cellule, puis appeler les méthodes valueChanged des
// listeners
T oldValue = this.content;
this.content = value;
for (OnChangeListener<T> listener : this.listeners) {
listener.valueChanged(oldValue,value);
}
}
/**
......@@ -52,7 +55,6 @@ public class Cell<T> implements Lens<T> {
* @return the current content of this {@link Cell}
*/
public T get(){
//TODO: à compléter
return null;
return this.content;
}
}
......@@ -42,14 +42,12 @@ public class CellularAutomatonSimulation<S extends State<S>>
@Override
public int numberOfColumns() {
//TODO: à compléter
return 0;
return this.automaton.numberOfColumns();
}
@Override
public int numberOfRows() {
//TODO: à compléter
return 0;
return this.automaton.numberOfRows();
}
/**
......@@ -59,13 +57,18 @@ public class CellularAutomatonSimulation<S extends State<S>>
* @return The cell at the specified coordinate.
*/
public Cell<S> at(Coordinate coordinate) {
//TODO: à compléter
return null;
return this.grid.get(coordinate);
}
@Override
public void updateToNextGeneration() {
//TODO: à compléter, en utilisant nextGenerationMatrix()
Matrix<S> newStates = nextGenerationMatrix();
for (Coordinate coordinate : this) {
S newState = newStates.at(coordinate).get();
Cell<S> cell = this.at(coordinate);
cell.set(newState);
}
this.generationNumber.set(this.generationNumber.get() + 1);
}
/** Computes the {@link Matrix} of states obtained after a single step of updates
......@@ -74,23 +77,28 @@ public class CellularAutomatonSimulation<S extends State<S>>
* @return the states of each cell after one generation
*/
private Matrix<S> nextGenerationMatrix() {
//TODO: à compléter
return null;
return new Matrix<>(
this.numberOfColumns(),
this.numberOfRows(),
new NextGenerationInitializer<>(this)
);
}
@Override
public void next(Coordinate coordinate) {
//TODO: à compléter
public void cycleThrough(Coordinate coordinate) {
Cell<S> cell = this.at(coordinate);
S newState = cell.get().cycle();
cell.set(newState);
}
@Override
public void copy(Coordinate source, Coordinate destination) {
//TODO: à compléter
S state = this.at(source).get();
this.at(destination).set(state);
}
@Override
public Color getColor(Coordinate coordinate) {
//TODO: à compléter
return null;
return this.at(coordinate).get().getColor();
}
@Override
......@@ -108,13 +116,19 @@ public class CellularAutomatonSimulation<S extends State<S>>
@Override
public void clear() {
//TODO: à compléter (penser à remettre le nombre de génération à 0)
for (Coordinate coordinate : this) {
this.at(coordinate).set(this.automaton.defaultState());
}
this.generationNumber.set(0);
}
@Override
public void reset() {
//TODO: à compléter (penser à remettre le nombre de génération à 0)
for (Coordinate coordinate : this) {
this.at(coordinate).set(this.automaton.randomState(this.generator));
}
this.generationNumber.set(0);
}
@Override
......
......@@ -11,7 +11,8 @@ import datastruct.MatrixInitializer;
* @param <T> the type of content of each cell
*/
public class ConstantCellInitializer<T> implements MatrixInitializer<Cell<T>> {
//TODO: ajouter la/les propriétes nécessaires
private final T value;
/** Make a new {@link MatrixInitializer} with cells containing a {@link Cell} with the same
* value.
......@@ -19,12 +20,11 @@ public class ConstantCellInitializer<T> implements MatrixInitializer<Cell<T>> {
* @param defaultValue the value stored in each cell.
*/
public ConstantCellInitializer(T defaultValue) {
//TODO: à compléter
this.value = defaultValue;
}
@Override
public Cell<T> initialValueAt(Coordinate coordinate) {
//TODO: à compléter
return null;
return new Cell<>(value);
}
}
......@@ -15,7 +15,7 @@ import java.util.List;
*/
public class NextGenerationInitializer<S extends State<S>> implements MatrixInitializer<S> {
//TODO: ajouter les propriétés nécessaires
private final CellularAutomatonSimulation<S> simulation;
/** Create a {@link MatrixInitializer} to compute the next generation in
* a 2D cellular automaton.
......@@ -23,13 +23,17 @@ public class NextGenerationInitializer<S extends State<S>> implements MatrixInit
* @param simulation the {@link Simulation} representing the cellular automaton.
*/
public NextGenerationInitializer(CellularAutomatonSimulation<S> simulation) {
//TODO: à compléter
this.simulation = simulation;
}
@Override
public S initialValueAt(Coordinate coordinate) {
//TODO: à compléter
return null;
List<Coordinate> neighbours = coordinate.orthodiagonalNeighbours();
List<S> states = new ArrayList<>();
for (Coordinate neighbour : neighbours) {
states.add(this.simulation.at(wrap(neighbour)).get());
}
return this.simulation.at(coordinate).get().update(states);
}
/** Computes the grid {@link Coordinate} for an arbitrary {@link Coordinate}, even outside
......@@ -41,10 +45,10 @@ public class NextGenerationInitializer<S extends State<S>> implements MatrixInit
* @return a corresponding {@link Coordinate}, that is inside the grid.
*/
Coordinate wrap(Coordinate coordinate) {
//TODO: à compléter
//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.
return null;
return new Coordinate(
modulo(coordinate.x(),this.simulation.numberOfColumns()),
modulo(coordinate.y(),this.simulation.numberOfRows())
);
}
/** The non-negative remainder of n divided by d.
......
......@@ -21,11 +21,11 @@ public interface State<S> {
Color getColor();
/**
* Computes and returns the next state based on the rules of the cellular automaton.
* Computes and returns the states by cycling through each possible state
*
* @return The next state.
*/
S next();
S cycle();
/**
* Updates the state based on the states of its neighboring cells.
......@@ -44,7 +44,12 @@ public interface State<S> {
* @return The number of times the specified state appears in the list of neighbors.
*/
static <T> int count(T state, List<T> neighbours) {
//TODO: à compléter
return 0;
int count = 0;
for (T element : neighbours) {
if (element.equals(state)) {
count = count + 1;
}
}
return count;
}
}
\ No newline at end of file
......@@ -6,31 +6,33 @@ import java.util.Random;
public class GameOfLifeAutomaton implements CellularAutomaton<GameOfLifeState> {
private final int numberOfColumns;
private final int numberOfRows;
public GameOfLifeAutomaton(int numberOfColumns, int numberOfRows) {
//TODO: à compléter
this.numberOfColumns = numberOfColumns;
this.numberOfRows = numberOfRows;
}
@Override
public int numberOfColumns() {
//TODO: à compléter
return 0;
return numberOfColumns;
}
@Override
public int numberOfRows() {
//TODO: à compléter
return 0;
return numberOfRows;
}
@Override
public GameOfLifeState defaultState() {
//TODO: à compléter
return null;
return GameOfLifeState.DEAD;
}
@Override
public GameOfLifeState randomState(Random generator) {
//TODO: à compléter
return null;
return generator.nextBoolean()?
GameOfLifeState.ALIVE:
GameOfLifeState.DEAD;
}
}
......@@ -14,20 +14,26 @@ public enum GameOfLifeState implements State<GameOfLifeState> {
@Override
public Color getColor() {
//TODO: à compléter
return Color.BLACK;
return switch (this) {
case ALIVE -> Color.RED;
case DEAD -> Color.WHITE;
};
}
@Override
public GameOfLifeState next() {
//TODO: à compléter
return null;
public GameOfLifeState cycle() {
return switch (this) {
case ALIVE -> DEAD;
case DEAD -> ALIVE;
};
}
@Override
public GameOfLifeState update(List<GameOfLifeState> neighbours) {
//TODO: à compléter
return null;
int countAlive = State.count(ALIVE, neighbours);
return (countAlive == 3 || (countAlive == 2 && this == ALIVE))?
ALIVE:
DEAD;
}
}
package model.automata;
import model.CellularAutomaton;
import java.util.Random;
import static model.automata.WireWorldState.WIRE;
public class WireWorldAutomaton implements CellularAutomaton<WireWorldState> {
private final int numberOfColumns;
private final int numberOfRows;
public WireWorldAutomaton(int numberOfColumns, int numberOfRows) {
this.numberOfColumns = numberOfColumns;
this.numberOfRows = numberOfRows;
}
@Override
public int numberOfColumns() {
return numberOfColumns;
}
@Override
public int numberOfRows() {
return numberOfRows;
}
@Override
public WireWorldState defaultState() {
return WireWorldState.DEAD;
}
@Override
public WireWorldState randomState(Random generator) {
int r = generator.nextInt(10);
return r < 3 ? WireWorldState.WIRE: WireWorldState.DEAD;
}
}
package model.automata;
import javafx.scene.paint.Color;
import model.State;
import java.util.List;
public enum WireWorldState implements State<WireWorldState> {
DEAD, WIRE, BLUE, RED;
@Override
public Color getColor() {
return switch (this) {
case DEAD -> Color.BLACK;
case WIRE -> Color.GOLD;
case BLUE -> Color.BLUE;
case RED -> Color.RED;
};
}
@Override
public WireWorldState cycle() {
return switch (this) {
case DEAD -> WIRE;
case WIRE -> BLUE;
case BLUE -> RED;
case RED -> DEAD;
};
}
@Override
public WireWorldState update(List<WireWorldState> neighbours) {
return switch (this) {
case DEAD -> DEAD;
case WIRE -> {
int countBlues = State.count(BLUE, neighbours);
yield (countBlues == 1 || countBlues == 2)? BLUE: WIRE;
}
case BLUE -> RED;
case RED -> WIRE;
};
}
}
......@@ -29,7 +29,7 @@ public class FillingMouseListener implements MouseListener {
@Override
public void onMousePressed(MouseEvent event, Coordinate coordinate) {
this.matrix.getController().getSimulation().next(coordinate);
this.matrix.getController().getSimulation().cycleThrough(coordinate);
this.matrix.setMouseListener(
new FillingMouseListener(this.matrix, coordinate)
);
......
......@@ -14,7 +14,7 @@ class WaitingMouseListener implements MouseListener {
@Override
public void onMousePressed(MouseEvent event, Coordinate coordinate) {
this.matrix.getController().getSimulation().next(coordinate);
this.matrix.getController().getSimulation().cycleThrough(coordinate);
this.matrix.setMouseListener(new FillingMouseListener(this.matrix, coordinate));
}
......
......@@ -78,7 +78,7 @@ class CellularAutomatonSimulationTest {
Coordinate coordinate = Coordinate.of(1, 1);
Cell<GameOfLifeState> cell = simulation.at(coordinate);
GameOfLifeState oldState = cell.get();
simulation.next(coordinate);
simulation.cycleThrough(coordinate);
assertNotEquals(oldState, cell.get());
}
......
package model.automata;
import javafx.scene.paint.Color;
import model.State;
import org.junit.jupiter.api.Test;
import java.util.List;
......@@ -18,8 +17,8 @@ class GameOfLifeStateTest {
@Test
public void testNext() {
assertEquals(ALIVE.next(), DEAD);
assertEquals(DEAD.next(), ALIVE);
assertEquals(ALIVE.cycle(), DEAD);
assertEquals(DEAD.cycle(), ALIVE);
}
@Test
......