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
Loading items

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
Loading items
Show changes
Showing
with 829 additions and 27 deletions
package matrix;
import java.util.Iterator;
public interface Matrix<T> extends Iterable<T> {
/**
* Returns the width of the {@link Matrix}.
*
* @return The width of the {@link Matrix}.
*/
int width();
/**
* Returns the height of the {@link Matrix}.
*
* @return The height of the {@link Matrix}.
*/
int height();
/**
* Returns the value at the specified coordinates (x, y) in
* the {@link Matrix}.
*
* @param x The x-coordinate.
* @param y The y-coordinate.
* @return The content of the matrix at the coordinates (x,y).
*/
T get(int x, int y);
/**
* Returns the value at the specified coordinates (x, y) in
* the {@link Matrix}.
*
* @param coordinate The coordinates (x,y).
* @return The content of the matrix at the coordinates (x,y).
*/
default T get(Coordinate coordinate) {
return this.get(coordinate.x(), coordinate.y());
}
/**
* Changes the value at the specified coordinates (x,y) in the {@link Matrix}
*
* @param x the x-coordinate
* @param y the y-coordinate
* @param newValue the value to assign to coordinates (x,y).
*/
void set(int x, int y, T newValue);
/**
* Changes the value at the specified coordinates (x,y) in the {@link Matrix}
*
* @param coordinate The coordinates (x,y)
* @param newValue the value to assign to coordinates (x,y).
*/
default void set(Coordinate coordinate, T newValue) {
this.set(coordinate.x(), coordinate.y(), newValue);
}
Matrix<T> subMatrix(Coordinate corner, int width, int height);
/**
* Returns an {@link Iterable} that provides access to the {@link Coordinate}s of the
* {@link Matrix} in row-major order. This means that a {@code for} loop on a {@link Matrix}
* will loop over the coordinates of the {@link Matrix}.
*
* @return An {@link Iterable} for the {@link Coordinate}s of the {@link Matrix}.
*/
default Iterable<Coordinate> coordinates() {
return () -> new CoordinateIterator(this.width(), this.height());
}
/**
* Returns an {@link Iterator} that allows iterating over the elements in the {@link Matrix} in
* row-major order.
*
* @return An {@link Iterator} for the {@link Matrix}.
*/
default Iterator<T> iterator() {
Iterator<Coordinate> coords =
new CoordinateIterator(this.width(),this.height());
return new MatrixIterator<>(this, coords);
}
}
package matrix;
/**
* An interface for initializing a {@link ListMatrix} by providing initial values for each cell.
*
* @param <T> The type of values to initialize the {@link ListMatrix} with.
*/
public interface MatrixInitializer<T> {
/**
* Returns the initial value to be set in a {@link ListMatrix} cell at the specified
* {@link Coordinate}.
*
* @param coordinate The {@link Coordinate} at which to set the initial value.
* @return The initial value for the specified cell.
*/
T initialValueAt(Coordinate coordinate);
}
package matrix;
import java.util.Iterator;
import java.util.NoSuchElementException;
class MatrixIterator<T> implements Iterator<T> {
private final Iterator<Coordinate> coordIterator;
private final Matrix<T> matrix;
public MatrixIterator(Matrix<T> matrix, Iterator<Coordinate> coordIterator) {
this.coordIterator = coordIterator;
this.matrix = matrix;
}
@Override
public boolean hasNext() {
return coordIterator.hasNext();
}
@Override
public T next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
return matrix.get(coordIterator.next());
}
}
package model;
import java.util.ArrayList;
import java.util.List;
/**
* A class representing a cell that holds a value and allows adding listeners to track value changes.
*
* @param <T> The type of value stored in the cell.
*/
public class Cell<T> implements Lens<T> {
//TODO: ajouter la ou les propriétés nécessaires
// la liste des objets écoutant les modifications du contenu de la cellule
private final List<OnChangeListener<T>> listeners = new ArrayList<>();
/** Initialize a new cell with a given value.
*
* @param initialContent the value initially stored by the cell.
*/
public Cell(T initialContent) {
//TODO: à compléter
}
/** Add a {@link OnChangeListener} to react to any change of value in the cell.
*
* @param listener the {@link OnChangeListener} to activate when the value in the cell is
* changed.
*/
public void addOnChangeListener(OnChangeListener<T> listener) {
this.listeners.add(listener);
}
/**
* Sets the content of this {@link Cell}. This will also call all the listeners that were
* registered by the method {@code addOnChangeListener}.
*
* @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
}
/**
* Returns the current content of this {@link Cell}.
*
* @return the current content of this {@link Cell}
*/
public T get(){
//TODO: à compléter
return null;
}
}
package model;
import java.util.Random;
/**
* Represents a cellular automaton, which defines the main parameters of a cellular automaton.
* The rules for updating states are defined in the class used as {@code S}.
*
* @param <S> The type of state used in the cellular automaton.
*/
public interface CellularAutomaton<S extends State<S>> {
/**
* Returns the number of columns in the grid of the cellular automaton.
*
* @return The number of columns in the grid.
*/
int numberOfColumns();
/**
* Returns the number of rows in the grid of the cellular automaton.
*
* @return The number of rows in the grid.
*/
int numberOfRows();
/**
* Returns the default state that is used to initialize cells in the automaton.
*
* @return The default state for cells in the automaton.
*/
S defaultState();
/**
* Generates a random state using the specified random number generator.
*
* @param generator The random number generator to use.
* @return A randomly generated state.
*/
S randomState(Random generator);
}
\ No newline at end of file
package model;
import controller.Simulation;
import matrix.Coordinate;
import matrix.ListMatrix;
import javafx.scene.paint.Color;
import java.util.Iterator;
import java.util.Random;
/**
* {@link CellularAutomatonSimulation} instances run <i>The Game of Life</i>.
*
* @param <S> The type of state used in the simulation.
*/
public class CellularAutomatonSimulation<S extends State<S>>
implements Simulation {
private final ListMatrix<Cell<S>> grid;
private final Cell<Integer> generationNumber = new Cell<>(0);
private final CellularAutomaton<S> automaton;
private final Random generator;
/**
* Creates a new {@link CellularAutomatonSimulation} instance for a given automaton.
*
* @param automaton A description of the {@link CellularAutomaton}.
* @param generator The {@link Random} instance used for random state generation.
*/
public CellularAutomatonSimulation(CellularAutomaton<S> automaton, Random generator) {
this.automaton = automaton;
this.grid = new ListMatrix<>(
automaton.numberOfColumns(),
automaton.numberOfRows(),
new ConstantCellInitializer<>(automaton.defaultState())
);
this.generator = generator;
}
@Override
public int numberOfColumns() {
//TODO: à compléter
return 0;
}
@Override
public int numberOfRows() {
//TODO: à compléter
return 0;
}
/**
* Returns the {@link Cell} at the specified coordinate.
*
* @param coordinate The coordinate of the cell to retrieve.
* @return The cell at the specified coordinate.
*/
public Cell<S> at(Coordinate coordinate) {
//TODO: à compléter
return null;
}
@Override
public void updateToNextGeneration() {
//TODO: à compléter, en utilisant nextGenerationMatrix()
}
/** Computes the {@link ListMatrix} of states obtained after a single step of updates
* of the simulation.
*
* @return the states of each cell after one generation
*/
private ListMatrix<S> nextGenerationMatrix() {
//TODO: à compléter
return null;
}
@Override
public void next(Coordinate coordinate) {
//TODO: à compléter
}
@Override
public void copy(Coordinate source, Coordinate destination) {
//TODO: à compléter
}
@Override
public Color getColor(Coordinate coordinate) {
//TODO: à compléter
return null;
}
@Override
public void setChangeListener(Coordinate coordinate, Runnable listener) {
this.at(coordinate).addOnChangeListener(
(oldValue, newValue) -> listener.run()
);
}
@Override
public void setGenerationNumberChangeListener(OnChangeListener<Integer> listener){
this.generationNumber.addOnChangeListener(listener);
}
@Override
public void clear() {
//TODO: à compléter (penser à remettre le nombre de génération à 0)
}
@Override
public void reset() {
//TODO: à compléter (penser à remettre le nombre de génération à 0)
}
@Override
public Iterator<Coordinate> iterator() {
return this.grid.coordinates().iterator();
}
}
package model;
import matrix.Coordinate;
import matrix.ListMatrix;
import matrix.MatrixInitializer;
/**
* An initializer for {@link ListMatrix} of {@link Cell}s, where each cell is initialized to the
* same value.
*
* @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
* value.
*
* @param defaultValue the value stored in each cell.
*/
public ConstantCellInitializer(T defaultValue) {
//TODO: à compléter
}
@Override
public Cell<T> initialValueAt(Coordinate coordinate) {
//TODO: à compléter
return null;
}
}
package model;
/**
* A lens interface representing a view into a mutable state.
*
* @param <S> The type of the value stored in the lens.
*/
public interface Lens<S> {
/**
* Gets the value from the {@link Lens}.
*
* @return The value stored in the place designated by {@link Lens}.
*/
S get();
/**
* Sets a new value into the {@link Lens}.
*
* @param value The new value to set in the place designated by the {@link Lens}.
*/
void set(S value);
}
package model;
import matrix.Coordinate;
import matrix.MatrixInitializer;
import matrix.ListMatrix;
import controller.Simulation;
/**
* 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.
*
* @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
/** Create a {@link MatrixInitializer} to compute the next generation in
* a 2D cellular automaton.
*
* @param simulation the {@link Simulation} representing the cellular automaton.
*/
public NextGenerationInitializer(CellularAutomatonSimulation<S> simulation) {
//TODO: à compléter
}
@Override
public S initialValueAt(Coordinate coordinate) {
//TODO: à compléter
return null;
}
/** 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
* neighbours and 4 diagonal neighbours.
*
* @param coordinate a {@link Coordinate} that may be outside the grid.
* @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;
}
/** The non-negative remainder of n divided by d.
*
* @param n an arbitrary integer.
* @param d a non-zero integer.
* @return the remainder of {@code n/d}, between {@code 0} and {@code n-1}.
*/
static int modulo(int n, int d) {
int result = n % d;
return n < 0 ? result + d : result;
}
}
package model;
public interface OnChangeListener<T> {
void valueChanged(T oldValue, T newValue);
}
package model;
import javafx.scene.paint.Color;
import java.util.List;
/**
* Represents a state of a cell in a cellular automaton, and the update rules for the cellular
* automaton.
*
* @param <S> The type of the state itself, used for reflexivity: if a class {@code SomeState}
* wants to implement this interface, it should implement {@code State<SomeState>}.
*/
public interface State<S> {
/**
* Returns the color associated with this state.
*
* @return The color representing this state.
*/
Color getColor();
/**
* Computes and returns the next state based on the rules of the cellular automaton.
*
* @return The next state.
*/
S next();
/**
* Updates the state based on the states of its neighboring cells.
*
* @param neighbours A list of neighboring cell states.
* @return The updated state based on the neighbors.
*/
S update(List<S> neighbours);
/**
* Counts the occurrences of a specific state within a list of neighboring states.
*
* @param <T> The type of state to count.
* @param state The state to count occurrences of.
* @param neighbours A list of neighboring states to search within.
* @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;
}
}
\ No newline at end of file
package model.automata;
import model.CellularAutomaton;
import java.util.Random;
public class GameOfLifeAutomaton implements CellularAutomaton<GameOfLifeState> {
public GameOfLifeAutomaton(int numberOfColumns, int numberOfRows) {
//TODO: à compléter
}
@Override
public int numberOfColumns() {
//TODO: à compléter
return 0;
}
@Override
public int numberOfRows() {
//TODO: à compléter
return 0;
}
@Override
public GameOfLifeState defaultState() {
//TODO: à compléter
return null;
}
@Override
public GameOfLifeState randomState(Random generator) {
//TODO: à compléter
return null;
}
}
package model.automata;
import javafx.scene.paint.Color;
import model.State;
import java.util.List;
/**
* {@link GameOfLifeState} instances represent the possible states of a {@link GameOfLifeState}.
*/
public enum GameOfLifeState implements State<GameOfLifeState> {
ALIVE, DEAD;
@Override
public Color getColor() {
//TODO: à compléter
return Color.BLACK;
}
@Override
public GameOfLifeState next() {
//TODO: à compléter
return null;
}
@Override
public GameOfLifeState update(List<GameOfLifeState> neighbours) {
//TODO: à compléter
return null;
}
}
package view;
import matrix.Coordinate;
import javafx.scene.input.MouseEvent;
public class FillingMouseListener implements MouseListener {
private final MatrixPane matrix;
private final Coordinate source;
public FillingMouseListener(MatrixPane matrix, Coordinate source) {
this.matrix = matrix;
this.source = source;
}
@Override
public void onMouseReleased(MouseEvent event, Coordinate coordinate) {
this.matrix.resetWaitingListener();
}
@Override
public void onMouseEntered(MouseEvent event, Coordinate destination) {
if (!event.isPrimaryButtonDown()) {
this.matrix.resetWaitingListener();
return;
}
this.matrix.getController().getSimulation().copy(source, destination);
}
@Override
public void onMousePressed(MouseEvent event, Coordinate coordinate) {
this.matrix.getController().getSimulation().next(coordinate);
this.matrix.setMouseListener(
new FillingMouseListener(this.matrix, coordinate)
);
}
}
package view;
import controller.Controller;
import matrix.Coordinate;
import javafx.scene.input.MouseDragEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
/**
* Created by Arnaud Labourel on 22/11/2018.
*/
public class MatrixPane extends GridPane {
private static final double CELL_SIZE = 14;
public Controller getController() {
return controller;
}
private Controller controller;
public void initialize(Controller controller) {
this.controller = controller;
for (Coordinate coordinate : controller.coordinates()) {
addCellRectangle(coordinate);
}
}
private void addCellRectangle(Coordinate coord) {
Rectangle rectangleCell = new Rectangle(CELL_SIZE, CELL_SIZE);
addStatePropertyListener(rectangleCell, coord);
updateFill(rectangleCell, coord);
addEventHandler(rectangleCell, coord);
add(rectangleCell, coord);
}
private void add(Rectangle rectangleCell, Coordinate coord) {
this.add(rectangleCell, coord.x(), coord.y());
}
private void addStatePropertyListener(Rectangle cellRectangle, Coordinate coord) {
controller.getSimulation().setChangeListener(
coord,
() -> updateFill(cellRectangle, coord)
);
}
private void updateFill(Rectangle cellRectangle, Coordinate coord) {
Color color = this.controller.getSimulation().getColor(coord);
cellRectangle.setFill(color);
}
private void addEventHandler(Rectangle cellRectangle, Coordinate coord) {
cellRectangle.addEventHandler(
MouseEvent.MOUSE_PRESSED,
event -> mouseListener.onMousePressed(event, coord)
);
cellRectangle.addEventHandler(
MouseEvent.DRAG_DETECTED,
event -> this.startFullDrag()
);
cellRectangle.addEventHandler(
MouseDragEvent.MOUSE_DRAG_RELEASED,
event -> mouseListener.onMouseReleased(event, coord)
);
cellRectangle.addEventHandler(
MouseDragEvent.MOUSE_DRAG_ENTERED,
event -> mouseListener.onMouseEntered(event, coord)
);
}
private MouseListener mouseListener = new WaitingMouseListener(this);
void setMouseListener(MouseListener mouseListener) {
this.mouseListener = mouseListener;
}
void resetWaitingListener() {
this.mouseListener = new WaitingMouseListener(this);
}
}
package view;
import matrix.Coordinate;
import javafx.scene.input.MouseEvent;
interface MouseListener {
default void onMousePressed(MouseEvent event, Coordinate coordinate) {}
default void onMouseReleased(MouseEvent event, Coordinate coordinate) {}
default void onMouseEntered(MouseEvent event, Coordinate coordinate) {};
}
package view;
import matrix.Coordinate;
import javafx.scene.input.MouseEvent;
class WaitingMouseListener implements MouseListener {
private final MatrixPane matrix;
WaitingMouseListener(MatrixPane matrix) {
this.matrix = matrix;
}
@Override
public void onMousePressed(MouseEvent event, Coordinate coordinate) {
this.matrix.getController().getSimulation().next(coordinate);
this.matrix.setMouseListener(new FillingMouseListener(this.matrix, coordinate));
}
}
.root {
-fx-focus-color: transparent;
-fx-font-size: 13px;
}
.background {
-fx-background-color: #1d1d1d;
}
.separator .line {
-fx-border-color: #fff;
}
.button {
-fx-background-color: derive(#1d1d1d, 20%);
-fx-text-fill: #fff;
}
.button:hover,
.button:selected {
-fx-background-color: #fff;
-fx-background-insets: 0 0 -1px 0, 0, 1px, 2px;
-fx-background-radius: 5px, 5px, 4px, 3px;
-fx-text-fill: derive(#1d1d1d, 20%);
}
.label {
-fx-text-fill: #fff;
}
.cell-pane {
-fx-background-color: derive(#1d1d1d, 20%);
}
.alive {
-fx-background-color: #fff;
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import view.MatrixPane?>
<AnchorPane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308"
styleClass="background" stylesheets="@style.css"
xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="controller.Controller">
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0"/>
</padding>
<children>
<HBox alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" prefHeight="24.0"
prefWidth="980.0" spacing="10.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0"
AnchorPane.topAnchor="0.0">
<children>
<Separator maxHeight="-Infinity" maxWidth="-Infinity" orientation="VERTICAL"
prefHeight="24.0" prefWidth="6.0"/>
<ToggleButton fx:id="playToggleButton" maxHeight="-Infinity" maxWidth="-Infinity"
mnemonicParsing="false" onAction="#playToggleButtonAction" prefHeight="24.0"
prefWidth="62.0" styleClass="button" text="Play"/>
<ToggleButton fx:id="pauseToggleButton" maxHeight="-Infinity" maxWidth="-Infinity"
mnemonicParsing="false" onAction="#pauseToggleButtonAction" prefHeight="24.0"
prefWidth="71.0" styleClass="button" text="Pause"/>
<Button fx:id="resetButton" maxHeight="-Infinity" maxWidth="-Infinity"
mnemonicParsing="false" onAction="#resetButtonAction" prefHeight="24.0" prefWidth="70.0"
text="Reset"/>
<Button fx:id="clearButton" maxHeight="-Infinity" maxWidth="-Infinity"
mnemonicParsing="false" onAction="#clearButtonAction" prefHeight="24.0" prefWidth="70.0"
text="Clear"/>
<Separator maxHeight="-Infinity" maxWidth="-Infinity" orientation="VERTICAL"
prefHeight="24.0" prefWidth="6.0"/>
<Separator maxHeight="-Infinity" maxWidth="-Infinity" orientation="VERTICAL"
prefHeight="24.0" prefWidth="6.0"/>
<Label maxHeight="-Infinity" maxWidth="-Infinity" prefHeight="24.0" prefWidth="103.0"
text="Generation"/>
<Label fx:id="generationNumberLabel" alignment="CENTER_RIGHT" contentDisplay="TEXT_ONLY"
maxHeight="-Infinity" maxWidth="-Infinity" prefHeight="24.0" prefWidth="99.0"/>
<Separator maxHeight="-Infinity" maxWidth="-Infinity" orientation="VERTICAL"
prefHeight="24.0" prefWidth="6.0"/>
</children>
</HBox>
<MatrixPane fx:id="matrixPane" alignment="CENTER" hgap="1.0"
maxHeight="-Infinity" maxWidth="-Infinity" prefHeight="600.0" prefWidth="980.0" vgap="1.0"
AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0"
AnchorPane.topAnchor="35.0"/>
</children>
</AnchorPane>
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;
public class StandardOutputSandbox implements Runnable {
static String NEW_LINE = System.getProperty("line.separator");
private Runnable runnable;
private OutputStream outputStream;
StandardOutputSandbox(Runnable runnable) {
this.runnable = runnable;
}
public void run(){
outputStream = new ByteArrayOutputStream();
PrintStream printStream = new PrintStream(outputStream);
System.setOut(printStream);
runnable.run();
PrintStream originalOut = System.out;
System.setOut(originalOut);
}
String getProducedOutput() {
return outputStream.toString();
}
}