Skip to content
Snippets Groups Projects
Commit 6adaf247 authored by SAHIN Melis damla's avatar SAHIN Melis damla
Browse files

TP

parent 446c447e
Branches
No related tags found
No related merge requests found
Pipeline #45490 failed
...@@ -75,3 +75,4 @@ public class SimulatorApplication extends Application { ...@@ -75,3 +75,4 @@ public class SimulatorApplication extends Application {
} }
} }
...@@ -124,3 +124,4 @@ public class Controller { ...@@ -124,3 +124,4 @@ public class Controller {
timeline.pause(); timeline.pause();
} }
} }
...@@ -68,7 +68,7 @@ public record Coordinate(int x, int y) { ...@@ -68,7 +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() {
return List.of(left(),right(), above(), below()); return List.of(above(),right(),left(),below());
} }
/** /**
...@@ -86,10 +86,7 @@ public record Coordinate(int x, int y) { ...@@ -86,10 +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() {
return List.of(new Coordinate(x-1, y-1), 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));
new Coordinate(x-1, y+1),
new Coordinate(x+1, y-1),
new Coordinate(x+1, y+1));
} }
/** /**
...@@ -107,10 +104,7 @@ public record Coordinate(int x, int y) { ...@@ -107,10 +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() {
return List.of(left(),right(), above(), below(), new Coordinate(x-1, y-1), 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));
new Coordinate(x-1, y+1),
new Coordinate(x+1, y-1),
new Coordinate(x+1, y+1));
} }
@Override @Override
......
...@@ -11,6 +11,7 @@ class CoordinateIterator implements Iterator<Coordinate> { ...@@ -11,6 +11,7 @@ class CoordinateIterator implements Iterator<Coordinate> {
private final int width; private final int width;
private final int height; private final int height;
private Coordinate current = new Coordinate(0, 0); 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.
* *
...@@ -41,16 +42,10 @@ class CoordinateIterator implements Iterator<Coordinate> { ...@@ -41,16 +42,10 @@ class CoordinateIterator implements Iterator<Coordinate> {
@Override @Override
public Coordinate next() { public Coordinate next() {
if (!hasNext()) throw new NoSuchElementException(); if (!hasNext()) throw new NoSuchElementException();
Coordinate next = current; Coordinate next = current;
// on crée un méthode update
update();
return next;
}
private void update(){
current = current.right(); current = current.right();
if (current.x() == width) if (current.x() == width) current = new Coordinate(0, current.y() + 1);
current = new Coordinate(0, current.y()+1);
return next;
} }
} }
...@@ -27,7 +27,7 @@ public class ListMatrix<T> implements Matrix<T> { ...@@ -27,7 +27,7 @@ public class ListMatrix<T> implements Matrix<T> {
this.width = width; this.width = width;
this.height = height; this.height = height;
this.matrix = new ArrayList<>(width); this.matrix = new ArrayList<>(width);
this.initializeWith(initializer); // fills the matrix using initializer this.initializeWith(initializer);
} }
public ListMatrix(int width, int height, T constant) { public ListMatrix(int width, int height, T constant) {
...@@ -37,6 +37,7 @@ public class ListMatrix<T> implements Matrix<T> { ...@@ -37,6 +37,7 @@ public class ListMatrix<T> implements Matrix<T> {
private void initializeWith(MatrixInitializer<T> initializer) { private void initializeWith(MatrixInitializer<T> initializer) {
for (int x=0; x<width; x++) { for (int x=0; x<width; x++) {
List<T> column = new ArrayList<>(height); List<T> column = new ArrayList<>(height);
for (int y = 0; y < height; y++) { for (int y = 0; y < height; y++) {
column.add(initializer.initialValueAt(new Coordinate(x, y))); column.add(initializer.initialValueAt(new Coordinate(x, y)));
} }
...@@ -45,6 +46,7 @@ public class ListMatrix<T> implements Matrix<T> { ...@@ -45,6 +46,7 @@ public class ListMatrix<T> implements Matrix<T> {
} }
public int width() { public int width() {
return width; return width;
} }
...@@ -54,7 +56,8 @@ public class ListMatrix<T> implements Matrix<T> { ...@@ -54,7 +56,8 @@ public class ListMatrix<T> implements Matrix<T> {
} }
@Override @Override
public T get(int x, int y) { // une liste de listes public T get(int x, int y) {
return matrix.get(x).get(y); return matrix.get(x).get(y);
} }
...@@ -65,3 +68,4 @@ public class ListMatrix<T> implements Matrix<T> { ...@@ -65,3 +68,4 @@ public class ListMatrix<T> implements Matrix<T> {
} }
} }
...@@ -4,19 +4,19 @@ import controller.Simulation; ...@@ -4,19 +4,19 @@ import controller.Simulation;
import matrix.Coordinate; import matrix.Coordinate;
import matrix.ListMatrix; import matrix.ListMatrix;
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
import model.*;
import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List;
import java.util.Random; import java.util.Random;
/** /**
* {@link CellularAutomatonSimulation} instances run <i>The Game of Life</i>. * {@link CellularAutomatonSimulation} instances run <i>The Game of Life</i>.
* *
* @param <S> The type of state used in the simulation. * @param <S> The type of state used in the simulation.
*/ */
public class CellularAutomatonSimulation<S extends State<S>> public class CellularAutomatonSimulation<S extends State<S>> implements Simulation {
implements Simulation {
private final ListMatrix<Cell<S>> grid; private final ListMatrix<Cell<S>> grid;
private final Cell<Integer> generationNumber = new Cell<>(0); private final Cell<Integer> generationNumber = new Cell<>(0);
...@@ -39,15 +39,14 @@ public class CellularAutomatonSimulation<S extends State<S>> ...@@ -39,15 +39,14 @@ public class CellularAutomatonSimulation<S extends State<S>>
this.generator = generator; this.generator = generator;
} }
@Override @Override
public int numberOfColumns() { public int numberOfColumns() {
return numberOfColumns(); return this.grid.width();
} }
@Override @Override
public int numberOfRows() { public int numberOfRows() {
return numberOfRows(); return this.grid.height();
} }
/** /**
...@@ -57,37 +56,86 @@ public class CellularAutomatonSimulation<S extends State<S>> ...@@ -57,37 +56,86 @@ 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) {
return null; return this.grid.get(coordinate.x(), coordinate.y());
} }
@Override @Override
public void updateToNextGeneration() { public void updateToNextGeneration() {
//TODO: à compléter, en utilisant nextGenerationMatrix() ListMatrix<Cell<S>> nextMatrix = nextGenerationMatrix();
for (Coordinate coordinate : this.grid.coordinates()) {
this.grid.get(coordinate.x(), coordinate.y())
.set(nextMatrix.get(coordinate.x(), coordinate.y()).get());
}
this.generationNumber.set(this.generationNumber.get() + 1); // Increment generation number
} }
/** 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
* of the simulation. * of the simulation.
* *
* @return the states of each cell after one generation * @return the states of each cell after one generation
*/ */
private ListMatrix<S> nextGenerationMatrix() { private ListMatrix<Cell<S>> nextGenerationMatrix() {
//TODO: à compléter ListMatrix<Cell<S>> nextGrid = new ListMatrix<>(
return null; this.grid.width(),
this.grid.height(),
new ConstantCellInitializer<>(automaton.defaultState()) // Pass the state directly
);
for (Coordinate coordinate : this.grid.coordinates()) {
S currentState = this.grid.get(coordinate.x(), coordinate.y()).get();
List<S> neighbors = getNeighbors(coordinate);
S nextState = currentState.update(neighbors);
nextGrid.get(coordinate.x(), coordinate.y()).set(nextState);
}
return nextGrid;
}
/**
* Calculates the neighbors of a cell at the given coordinate.
*
* @param coordinate The coordinate of the cell.
* @return A list of states representing the neighbors of the cell.
*/
private List<S> getNeighbors(Coordinate coordinate) {
List<S> neighbors = new ArrayList<>();
for (Coordinate neighbor : coordinate.orthodiagonalNeighbours()) {
if (isValidCoordinate(neighbor)) {
neighbors.add(this.grid.get(neighbor.x(), neighbor.y()).get());
}
} }
return neighbors;
}
/**
* Validates if the given coordinate is within the bounds of the grid.
*
* @param coordinate The coordinate to validate.
* @return True if the coordinate is valid; false otherwise.
*/
private boolean isValidCoordinate(Coordinate coordinate) {
return coordinate.x() >= 0 && coordinate.x() < this.grid.width()
&& coordinate.y() >= 0 && coordinate.y() < this.grid.height();
}
@Override @Override
public void next(Coordinate coordinate) { public void next(Coordinate coordinate) {
//TODO: à compléter Cell<S> cell = this.at(coordinate);
List<S> neighbors = getNeighbors(coordinate);
S nextState = cell.get().update(neighbors);
cell.set(nextState);
} }
@Override @Override
public void copy(Coordinate source, Coordinate destination) { public void copy(Coordinate source, Coordinate destination) {
//TODO: à compléter S sourceState = this.at(source).get();
this.at(destination).set(sourceState);
} }
@Override @Override
public Color getColor(Coordinate coordinate) { public Color getColor(Coordinate coordinate) {
//TODO: à compléter return this.at(coordinate).get().getColor();
return null;
} }
@Override @Override
...@@ -102,16 +150,20 @@ public class CellularAutomatonSimulation<S extends State<S>> ...@@ -102,16 +150,20 @@ public class CellularAutomatonSimulation<S extends State<S>>
this.generationNumber.addOnChangeListener(listener); this.generationNumber.addOnChangeListener(listener);
} }
@Override @Override
public void clear() { public void clear() {
//TODO: à compléter (penser à remettre le nombre de génération à 0) for (Coordinate coordinate : this.grid.coordinates()) {
this.at(coordinate).set(this.automaton.defaultState());
}
this.generationNumber.set(0); // Reset generation counter
} }
@Override @Override
public void reset() { public void reset() {
//TODO: à compléter (penser à remettre le nombre de génération à 0) for (Coordinate coordinate : this.grid.coordinates()) {
this.grid.set(coordinate.x(), coordinate.y(), new Cell<>(this.automaton.defaultState()));
}
this.generationNumber.set(0);
} }
@Override @Override
......
...@@ -11,21 +11,30 @@ import matrix.MatrixInitializer; ...@@ -11,21 +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.
*/ */
private T defaultValue;
public ConstantCellInitializer(T defaultValue) { public ConstantCellInitializer(T defaultValue) {
this.defaultValue = defaultValue; 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) {
return new Cell(defaultValue); return new Cell<>(defaultValue);
} }
} }
\ No newline at end of file
...@@ -8,7 +8,6 @@ public class GameOfLifeAutomaton implements CellularAutomaton<GameOfLifeState> { ...@@ -8,7 +8,6 @@ public class GameOfLifeAutomaton implements CellularAutomaton<GameOfLifeState> {
private final int numberOfColumns; private final int numberOfColumns;
private final int numberOfRows; private final int numberOfRows;
//(j'ai ecrit un message a moi-meme) return tipine dikkat et
public GameOfLifeAutomaton(int numberOfColumns, int numberOfRows) { public GameOfLifeAutomaton(int numberOfColumns, int numberOfRows) {
this.numberOfColumns = numberOfColumns; this.numberOfColumns = numberOfColumns;
...@@ -33,6 +32,5 @@ public class GameOfLifeAutomaton implements CellularAutomaton<GameOfLifeState> { ...@@ -33,6 +32,5 @@ public class GameOfLifeAutomaton implements CellularAutomaton<GameOfLifeState> {
@Override @Override
public GameOfLifeState randomState(Random generator) { public GameOfLifeState randomState(Random generator) {
return generator.nextBoolean() ? GameOfLifeState.ALIVE : GameOfLifeState.DEAD; return generator.nextBoolean() ? GameOfLifeState.ALIVE : GameOfLifeState.DEAD;
// message a moi-meme : dikkat et!!
} }
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment