13 files + 122 − 76 Side-by-side Compare changes Side-by-side Inline Show whitespace changes Files 13 README.md +1 −1 Original line number Original line Diff line number Diff line Loading @@ -19,5 +19,5 @@ jeu de la vie. ## Membre du projet ## Membre du projet - NOM, prénom, du participant - Balme,Maxence src/main/java/matrix/ConstantMatrixInitializer.java +4 −4 Original line number Original line Diff line number Diff line Loading @@ -2,15 +2,15 @@ package matrix; public class ConstantMatrixInitializer<T> implements MatrixInitializer<T> { public class ConstantMatrixInitializer<T> implements MatrixInitializer<T> { // TODO: add instance variables private T constant ; 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 null; return constant; } } } } src/main/java/matrix/Coordinate.java +8 −16 Original line number Original line Diff line number Diff line Loading @@ -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; } } /** /** Loading @@ -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; } } /** /** Loading @@ -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; } } /** /** Loading @@ -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; } } /** /** Loading @@ -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; } } /** /** Loading @@ -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(); } } /** /** Loading @@ -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(); } } /** /** Loading @@ -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 Loading src/main/java/matrix/CoordinateIterator.java +13 −6 Original line number Original line Diff line number Diff line Loading @@ -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. Loading @@ -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; } } /** /** Loading @@ -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; } } /** /** Loading @@ -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; } } } } No newline at end of file src/main/java/matrix/ListMatrix.java +20 −13 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; Loading @@ -23,10 +24,9 @@ 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); // fills the matrix using initializer this.initializeWith(initializer); // fills the matrix using initializer } } Loading @@ -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); } } } } src/main/java/matrix/Matrix.java +1 −1 Original line number Original line Diff line number Diff line Loading @@ -58,7 +58,7 @@ 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)); } } Loading src/main/java/matrix/SubMatrixInitializer.java 0 → 100644 +18 −0 Original line number Original line Diff line number Diff line 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)); } } src/main/java/model/Cell.java +10 −6 Original line number Original line Diff line number Diff line Loading @@ -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<>(); Loading @@ -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. Loading @@ -40,8 +40,13 @@ 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 L :listeners) { L.valueChanged(initialContent,value); initialContent = value ; } } } /** /** Loading @@ -50,7 +55,6 @@ 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; } } } } src/main/java/model/ConstantCellInitializer.java +4 −4 Original line number Original line Diff line number Diff line Loading @@ -11,7 +11,7 @@ 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 private T defaultValue; /** Make a new {@link MatrixInitializer} with cells containing a {@link Cell} with the same /** Make a new {@link MatrixInitializer} with cells containing a {@link Cell} with the same * value. * value. Loading @@ -19,12 +19,12 @@ public class ConstantCellInitializer<T> implements MatrixInitializer<Cell<T>> { * @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; } } @Override @Override public Cell<T> initialValueAt(Coordinate coordinate) { public Cell<T> initialValueAt(Coordinate coordinate) { //TODO: à compléter return null; return new Cell<>(defaultValue); } } } } src/main/java/model/NextGenerationInitializer.java +13 −7 Original line number Original line Diff line number Diff line Loading @@ -5,6 +5,9 @@ import matrix.MatrixInitializer; import matrix.ListMatrix; import matrix.ListMatrix; import controller.Simulation; 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 * 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. * of its neighbours in a {@link Simulation} of a cellular automaton. Loading @@ -12,8 +15,7 @@ import controller.Simulation; * @param <S> the type of states in the simulation. * @param <S> the type of states in the simulation. */ */ public class NextGenerationInitializer<S extends State<S>> implements MatrixInitializer<S> { public class NextGenerationInitializer<S extends State<S>> implements MatrixInitializer<S> { private final CellularAutomatonSimulation<S> simulation; //TODO: ajouter les propriétés nécessaires /** 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. Loading @@ -21,14 +23,19 @@ 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> coordinates = coordinate.orthogonalNeighbours(); return null; 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 /** 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 Loading @@ -39,10 +46,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(),this.simulation.numberOfColumns()), modulo(coordinate.y(),this.simulation.numberOfRows())); } } /** The non-negative remainder of n divided by d. /** The non-negative remainder of n divided by d. Loading src/main/java/model/State.java +5 −4 Original line number Original line Diff line number Diff line Loading @@ -25,8 +25,6 @@ public interface State<S> { * * * @return The next state. * @return The next state. */ */ S next(); /** /** * Updates the state based on the states of its neighboring cells. * Updates the state based on the states of its neighboring cells. * * Loading @@ -44,7 +42,10 @@ 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 n : neighbours){ if (n.equals(state)) nb++ ; } return nb; } } } } No newline at end of file src/main/java/model/automata/GameOfLifeAutomaton.java +8 −9 Original line number Original line Diff line number Diff line Loading @@ -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; } } } } src/main/java/model/automata/GameOfLifeState.java +17 −5 Original line number Original line Diff line number Diff line Loading @@ -14,20 +14,32 @@ public enum GameOfLifeState implements State<GameOfLifeState> { @Override @Override public Color getColor() { public Color getColor() { //TODO: à compléter switch(this) { case ALIVE: return Color.RED; case DEAD: return Color.WHITE; } return Color.BLACK; return Color.BLACK; } } @Override @Override public GameOfLifeState next() { public GameOfLifeState next() { //TODO: à compléter if (this == GameOfLifeState.ALIVE) { return null; return GameOfLifeState.DEAD; } else if (this == GameOfLifeState.DEAD) { return GameOfLifeState.ALIVE; } } } @Override @Override public GameOfLifeState update(List<GameOfLifeState> neighbours) { public GameOfLifeState update(List<GameOfLifeState> neighbours) { //TODO: à compléter int count = State.count(ALIVE, neighbours); return null; if (count == 3) return ALIVE ; if ((count == 2) && this.equals(ALIVE))return ALIVE ; return DEAD ; } } } }
README.md +1 −1 Original line number Original line Diff line number Diff line Loading @@ -19,5 +19,5 @@ jeu de la vie. ## Membre du projet ## Membre du projet - NOM, prénom, du participant - Balme,Maxence
src/main/java/matrix/ConstantMatrixInitializer.java +4 −4 Original line number Original line Diff line number Diff line Loading @@ -2,15 +2,15 @@ package matrix; public class ConstantMatrixInitializer<T> implements MatrixInitializer<T> { public class ConstantMatrixInitializer<T> implements MatrixInitializer<T> { // TODO: add instance variables private T constant ; 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 null; return constant; } } } }
src/main/java/matrix/Coordinate.java +8 −16 Original line number Original line Diff line number Diff line Loading @@ -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; } } /** /** Loading @@ -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; } } /** /** Loading @@ -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; } } /** /** Loading @@ -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; } } /** /** Loading @@ -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; } } /** /** Loading @@ -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(); } } /** /** Loading @@ -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(); } } /** /** Loading @@ -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 Loading
src/main/java/matrix/CoordinateIterator.java +13 −6 Original line number Original line Diff line number Diff line Loading @@ -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. Loading @@ -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; } } /** /** Loading @@ -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; } } /** /** Loading @@ -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; } } } } No newline at end of file
src/main/java/matrix/ListMatrix.java +20 −13 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; Loading @@ -23,10 +24,9 @@ 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); // fills the matrix using initializer this.initializeWith(initializer); // fills the matrix using initializer } } Loading @@ -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); } } } }
src/main/java/matrix/Matrix.java +1 −1 Original line number Original line Diff line number Diff line Loading @@ -58,7 +58,7 @@ 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)); } } Loading
src/main/java/matrix/SubMatrixInitializer.java 0 → 100644 +18 −0 Original line number Original line Diff line number Diff line 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)); } }
src/main/java/model/Cell.java +10 −6 Original line number Original line Diff line number Diff line Loading @@ -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<>(); Loading @@ -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. Loading @@ -40,8 +40,13 @@ 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 L :listeners) { L.valueChanged(initialContent,value); initialContent = value ; } } } /** /** Loading @@ -50,7 +55,6 @@ 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; } } } }
src/main/java/model/ConstantCellInitializer.java +4 −4 Original line number Original line Diff line number Diff line Loading @@ -11,7 +11,7 @@ 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 private T defaultValue; /** Make a new {@link MatrixInitializer} with cells containing a {@link Cell} with the same /** Make a new {@link MatrixInitializer} with cells containing a {@link Cell} with the same * value. * value. Loading @@ -19,12 +19,12 @@ public class ConstantCellInitializer<T> implements MatrixInitializer<Cell<T>> { * @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; } } @Override @Override public Cell<T> initialValueAt(Coordinate coordinate) { public Cell<T> initialValueAt(Coordinate coordinate) { //TODO: à compléter return null; return new Cell<>(defaultValue); } } } }
src/main/java/model/NextGenerationInitializer.java +13 −7 Original line number Original line Diff line number Diff line Loading @@ -5,6 +5,9 @@ import matrix.MatrixInitializer; import matrix.ListMatrix; import matrix.ListMatrix; import controller.Simulation; 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 * 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. * of its neighbours in a {@link Simulation} of a cellular automaton. Loading @@ -12,8 +15,7 @@ import controller.Simulation; * @param <S> the type of states in the simulation. * @param <S> the type of states in the simulation. */ */ public class NextGenerationInitializer<S extends State<S>> implements MatrixInitializer<S> { public class NextGenerationInitializer<S extends State<S>> implements MatrixInitializer<S> { private final CellularAutomatonSimulation<S> simulation; //TODO: ajouter les propriétés nécessaires /** 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. Loading @@ -21,14 +23,19 @@ 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> coordinates = coordinate.orthogonalNeighbours(); return null; 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 /** 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 Loading @@ -39,10 +46,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(),this.simulation.numberOfColumns()), modulo(coordinate.y(),this.simulation.numberOfRows())); } } /** The non-negative remainder of n divided by d. /** The non-negative remainder of n divided by d. Loading
src/main/java/model/State.java +5 −4 Original line number Original line Diff line number Diff line Loading @@ -25,8 +25,6 @@ public interface State<S> { * * * @return The next state. * @return The next state. */ */ S next(); /** /** * Updates the state based on the states of its neighboring cells. * Updates the state based on the states of its neighboring cells. * * Loading @@ -44,7 +42,10 @@ 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 n : neighbours){ if (n.equals(state)) nb++ ; } return nb; } } } } No newline at end of file
src/main/java/model/automata/GameOfLifeAutomaton.java +8 −9 Original line number Original line Diff line number Diff line Loading @@ -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; } } } }
src/main/java/model/automata/GameOfLifeState.java +17 −5 Original line number Original line Diff line number Diff line Loading @@ -14,20 +14,32 @@ public enum GameOfLifeState implements State<GameOfLifeState> { @Override @Override public Color getColor() { public Color getColor() { //TODO: à compléter switch(this) { case ALIVE: return Color.RED; case DEAD: return Color.WHITE; } return Color.BLACK; return Color.BLACK; } } @Override @Override public GameOfLifeState next() { public GameOfLifeState next() { //TODO: à compléter if (this == GameOfLifeState.ALIVE) { return null; return GameOfLifeState.DEAD; } else if (this == GameOfLifeState.DEAD) { return GameOfLifeState.ALIVE; } } } @Override @Override public GameOfLifeState update(List<GameOfLifeState> neighbours) { public GameOfLifeState update(List<GameOfLifeState> neighbours) { //TODO: à compléter int count = State.count(ALIVE, neighbours); return null; if (count == 3) return ALIVE ; if ((count == 2) && this.equals(ALIVE))return ALIVE ; return DEAD ; } } } }