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

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
Show changes
Commits on Source (5)
Showing with 122 additions and 76 deletions
......@@ -19,5 +19,5 @@ jeu de la vie.
## Membre du projet
- NOM, prénom, du participant
- Balme,Maxence
......@@ -2,15 +2,15 @@ package matrix;
public class ConstantMatrixInitializer<T> implements MatrixInitializer<T> {
// TODO: add instance variables
private 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;
}
}
}
\ No newline at end of file
package matrix;
import java.util.ArrayList;
import java.util.List;
......@@ -23,10 +24,9 @@ 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.width = width;
this.height = height;
this.matrix = new ArrayList<>(width);
this.initializeWith(initializer); // fills the matrix using initializer
}
......@@ -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,7 @@ 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> {
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 initalValueAt(Coordinate coordinate) {
return matrix.get(coordinate.plus(corner));
}
}
......@@ -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,13 @@ 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 L :listeners) {
L.valueChanged(initialContent,value);
initialContent = value ;
}
}
/**
......@@ -50,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;
}
}
......@@ -11,7 +11,7 @@ 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
private T defaultValue;
/** Make a new {@link MatrixInitializer} with cells containing a {@link Cell} with the same
* value.
......@@ -19,12 +19,12 @@ public class ConstantCellInitializer<T> implements MatrixInitializer<Cell<T>> {
* @param defaultValue the value stored in each cell.
*/
public ConstantCellInitializer(T defaultValue) {
//TODO: à compléter
this.defaultValue = defaultValue;
}
@Override
public Cell<T> initialValueAt(Coordinate coordinate) {
//TODO: à compléter
return null;
return new Cell<>(defaultValue);
}
}
......@@ -5,6 +5,9 @@ import matrix.MatrixInitializer;
import matrix.ListMatrix;
import controller.Simulation;
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
* of its neighbours in a {@link Simulation} of a cellular automaton.
......@@ -12,8 +15,7 @@ import controller.Simulation;
* @param <S> the type of states in the simulation.
*/
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.
......@@ -21,15 +23,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> coordinates = coordinate.orthogonalNeighbours();
List<S> states = new ArrayList<>();
for (Coordinate coor : coordinates) {
states.add(simulation.at(this.wrap(coordinate)).get());
}
return simulation.at(coordinate).get().update(states);
}
/** 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 +46,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(),this.simulation.numberOfColumns()), modulo(coordinate.y(),this.simulation.numberOfRows()));
}
/** The non-negative remainder of n divided by d.
......
......@@ -25,8 +25,6 @@ public interface State<S> {
*
* @return The next state.
*/
S next();
/**
* Updates the state based on the states of its neighboring cells.
*
......@@ -44,7 +42,10 @@ 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 n : neighbours){
if (n.equals(state)) nb++ ;
}
return nb;
}
}
\ No newline at end of file
......@@ -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,32 @@ public enum GameOfLifeState implements State<GameOfLifeState> {
@Override
public Color getColor() {
//TODO: à compléter
switch(this) {
case ALIVE:
return Color.RED;
case DEAD:
return Color.WHITE;
}
return Color.BLACK;
}
@Override
public GameOfLifeState next() {
//TODO: à compléter
return null;
if (this == GameOfLifeState.ALIVE) {
return GameOfLifeState.DEAD;
}
else if (this == GameOfLifeState.DEAD) {
return GameOfLifeState.ALIVE;
}
}
@Override
public GameOfLifeState update(List<GameOfLifeState> neighbours) {
//TODO: à compléter
return null;
int count = State.count(ALIVE, neighbours);
if (count == 3) return ALIVE ;
if ((count == 2) && this.equals(ALIVE))return ALIVE ;
return DEAD ;
}
}