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 (20)
Showing
with 339 additions and 108 deletions
......@@ -19,5 +19,5 @@ jeu de la vie.
## Membre du projet
- NOM, prénom, du participant
- SAHIN, Melis Damla
......@@ -75,3 +75,4 @@ public class SimulatorApplication extends Application {
}
}
......@@ -124,3 +124,4 @@ public class Controller {
timeline.pause();
}
}
package matrix;
public class ConstantMatrixInitializer<T> implements MatrixInitializer<T> {
// TODO: add instance variables
private final T constant ;
public ConstantMatrixInitializer(T constant) {
// TODO
}
this.constant = constant; }
@Override
public T initialValueAt(Coordinate coordinate) {
// TODO
return null;
return constant;
}
}
......@@ -15,8 +15,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);
}
/**
......@@ -25,8 +24,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(x-1,y);
}
/**
......@@ -35,8 +33,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(x+1,y);
}
/**
......@@ -45,8 +42,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(x,y+1);
}
/**
......@@ -55,8 +51,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(x,y-1);
}
/**
......@@ -73,8 +68,7 @@ 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(above(),right(),left(),below());
}
/**
......@@ -92,8 +86,7 @@ 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(new Coordinate(x-1,y-1),new Coordinate(x-1,y+1),new Coordinate(x+1,y-1),new Coordinate(x+1,y+1));
}
/**
......@@ -111,8 +104,7 @@ 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();
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));
}
@Override
......
......@@ -8,6 +8,9 @@ import java.util.NoSuchElementException;
* height range.
*/
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.
......@@ -16,7 +19,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 +30,7 @@ class CoordinateIterator implements Iterator<Coordinate> {
*/
@Override
public boolean hasNext() {
// TODO: à compléter
return false;
return current.y() < height;
}
/**
......@@ -38,7 +41,11 @@ class CoordinateIterator implements Iterator<Coordinate> {
*/
@Override
public Coordinate next() {
// TODO: à compléter
return null;
if (!hasNext()) throw new NoSuchElementException();
Coordinate next = current;
current = current.right();
if (current.x() == width) current = new Coordinate(0, current.y() + 1);
return next;
}
}
package matrix;
import java.util.ArrayList;
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}.
*/
public ListMatrix(int width, int height, MatrixInitializer<T> initializer) {
// TODO
this.width = 0;
this.height = 0;
this.matrix = null;
this.initializeWith(initializer); // fills the matrix using initializer
this.width = width;
this.height = height;
this.matrix = new ArrayList<>(width);
this.initializeWith(initializer);
}
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) {
// 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() {
// TODO
return 0;
return width;
}
public int height() {
// TODO
return 0;
return height;
}
@Override
public T get(int x, int y) {
// TODO
return null;
return matrix.get(x).get(y);
}
@Override
public void set(int x, int y, T newValue) {
// TODO
matrix.get(x).set(y,newValue);
}
}
......@@ -58,7 +58,8 @@ public interface Matrix<T> extends Iterable<T> {
}
default Matrix<T> subMatrix(Coordinate corner, int width, int height){
return null ;
return new ListMatrix<>(width, height, new SubMatrixInitializer(this, corner));
}
......
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());
}
}
......@@ -11,7 +11,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<>();
......@@ -21,7 +21,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.
......@@ -40,8 +40,12 @@ 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 : listeners) {
listener.valueChanged(oldValue, value);
}
}
/**
......@@ -50,7 +54,8 @@ 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 automaton.numberOfColumns();
}
@Override
public int numberOfRows() {
//TODO: à compléter
return 0;
return automaton.numberOfRows();
}
/**
......@@ -59,13 +57,16 @@ 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 grid.get(coordinate);
}
@Override
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
......@@ -74,23 +75,27 @@ public class CellularAutomatonSimulation<S extends State<S>>
* @return the states of each cell after one generation
*/
private ListMatrix<S> nextGenerationMatrix() {
//TODO: à compléter
return null;
return new ListMatrix<>(
numberOfColumns(),
numberOfRows(),
new NextGenerationInitializer<>(this)
);
}
@Override
public void next(Coordinate coordinate) {
//TODO: à compléter
Cell<S> cell = at(coordinate);
cell.set(cell.get().next());
}
@Override
public void copy(Coordinate source, Coordinate destination) {
//TODO: à compléter
Cell<S> sourceCell = at(source);
at(destination).set(sourceCell.get());
}
@Override
public Color getColor(Coordinate coordinate) {
//TODO: à compléter
return null;
return at(coordinate).get().getColor();
}
@Override
......@@ -108,17 +113,25 @@ 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 : grid.coordinates()) {
at(coordinate).set(automaton.defaultState());
}
generationNumber.set(0);
}
@Override
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
public Iterator<Coordinate> iterator() {
return this.grid.coordinates().iterator();
}
}
\ No newline at end of file
......@@ -11,20 +11,30 @@ import matrix.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
/** 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.
*
* @param defaultValue the value stored in each cell.
*/
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
public Cell<T> initialValueAt(Coordinate coordinate) {
//TODO: à compléter
return null;
return new Cell<>(defaultValue);
}
}
\ No newline at end of file
......@@ -4,6 +4,10 @@ import matrix.Coordinate;
import matrix.MatrixInitializer;
import matrix.ListMatrix;
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
......@@ -13,7 +17,8 @@ import controller.Simulation;
*/
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
* 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.
*/
public NextGenerationInitializer(CellularAutomatonSimulation<S> simulation) {
//TODO: à compléter
this.simulation = simulation;
}
@Override
public S initialValueAt(Coordinate coordinate) {
//TODO: à compléter
return null;
List<Coordinate> neighborCoordinates = coordinate.orthodiagonalNeighbours();
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
* 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
......@@ -39,10 +49,9 @@ 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(), simulation.numberOfColumns()), modulo(coordinate.y(), simulation.numberOfRows()));
}
/** The non-negative remainder of n divided by d.
......
......@@ -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 nb = 0;
for (T neighbour : neighbours) {
if (state.equals(neighbour)) {
nb++;
}
}
return nb;
}
}
\ No newline at end of file
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
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);
}
}
}
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);
}
}
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;
}
}
}
......@@ -5,32 +5,31 @@ import model.CellularAutomaton;
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,23 @@ public enum GameOfLifeState implements State<GameOfLifeState> {
@Override
public Color getColor() {
//TODO: à compléter
return Color.BLACK;
return this == ALIVE ? Color.RED : Color.WHITE;
}
@Override
public GameOfLifeState next() {
//TODO: à compléter
return null;
return this == ALIVE ? DEAD : ALIVE;
}
@Override
public GameOfLifeState update(List<GameOfLifeState> neighbours) {
//TODO: à compléter
return null;
int aliveNeighbors = State.count(ALIVE, neighbours);
if (this == ALIVE){
return (aliveNeighbors == 2 || aliveNeighbors == 3) ? ALIVE : DEAD;
} else {
return aliveNeighbors == 3 ? ALIVE : DEAD;
}
}
}