Skip to content
Snippets Groups Projects
CellularAutomatonSimulation.java 3.24 KiB
Newer Older
  • Learn to ignore specific revisions
  • Guyslain's avatar
    Guyslain committed
    package model;
    
    import controller.Simulation;
    
    Guyslain's avatar
    Guyslain committed
    import matrix.Coordinate;
    import matrix.ListMatrix;
    
    Guyslain's avatar
    Guyslain committed
    import javafx.scene.paint.Color;
    
    Guyslain's avatar
    Guyslain committed
    import java.util.Iterator;
    
    Guyslain's avatar
    Guyslain committed
    import java.util.Random;
    
    Guyslain's avatar
    Guyslain committed
    
    
    Guyslain's avatar
    Guyslain committed
    
    /**
    
     * {@link CellularAutomatonSimulation} instances run <i>The Game of Life</i>.
    
     *
     *  @param <S> The type of state used in the simulation.
    
    Guyslain's avatar
    Guyslain committed
     */
    
    public class CellularAutomatonSimulation<S extends State<S>>
    
    Guyslain's avatar
    Guyslain committed
            implements Simulation {
    
    
    Guyslain's avatar
    Guyslain committed
        private final ListMatrix<Cell<S>> grid;
    
        private final Cell<Integer> generationNumber = new Cell<>(0);
    
    Guyslain's avatar
    Guyslain committed
        private final CellularAutomaton<S> automaton;
        private final Random generator;
    
    Guyslain's avatar
    Guyslain committed
    
        /**
    
    Guyslain's avatar
    Guyslain committed
         * Creates a new {@link CellularAutomatonSimulation} instance for a given automaton.
    
    Guyslain's avatar
    Guyslain committed
         *
    
         * @param automaton  A description of the {@link CellularAutomaton}.
         * @param generator  The {@link Random} instance used for random state generation.
    
    Guyslain's avatar
    Guyslain committed
         */
    
    Guyslain's avatar
    Guyslain committed
        public CellularAutomatonSimulation(CellularAutomaton<S> automaton, Random generator) {
            this.automaton = automaton;
    
    Guyslain's avatar
    Guyslain committed
            this.grid = new ListMatrix<>(
    
    Guyslain's avatar
    Guyslain committed
                    automaton.numberOfColumns(),
                    automaton.numberOfRows(),
                    new ConstantCellInitializer<>(automaton.defaultState())
            );
            this.generator = generator;
    
    Guyslain's avatar
    Guyslain committed
        }
    
    
        @Override
        public int numberOfColumns() {
    
    Guyslain's avatar
    Guyslain committed
            //TODO: à compléter
            return 0;
    
    Guyslain's avatar
    Guyslain committed
        }
    
        @Override
        public int numberOfRows() {
    
    Guyslain's avatar
    Guyslain committed
            //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.
         */
    
    Guyslain's avatar
    Guyslain committed
        public Cell<S> at(Coordinate coordinate) {
    
    Guyslain's avatar
    Guyslain committed
            //TODO: à compléter
            return null;
    
    Guyslain's avatar
    Guyslain committed
        }
    
    
        @Override
    
    Guyslain's avatar
    Guyslain committed
        public void updateToNextGeneration() {
    
    Guyslain's avatar
    Guyslain committed
            //TODO: à compléter, en utilisant nextGenerationMatrix()
    
    Guyslain's avatar
    Guyslain committed
        }
    
    Guyslain's avatar
    Guyslain committed
        /** Computes the {@link ListMatrix} of states obtained after a single step of updates
    
    Guyslain's avatar
    Guyslain committed
         * of the simulation.
         *
         * @return the states of each cell after one generation
         */
    
    Guyslain's avatar
    Guyslain committed
        private ListMatrix<S> nextGenerationMatrix() {
    
    Guyslain's avatar
    Guyslain committed
            //TODO: à compléter
            return null;
    
    Guyslain's avatar
    Guyslain committed
        }
        @Override
        public void next(Coordinate coordinate) {
    
    Guyslain's avatar
    Guyslain committed
            //TODO: à compléter
    
    Guyslain's avatar
    Guyslain committed
        }
    
        @Override
        public void copy(Coordinate source, Coordinate destination) {
    
    Guyslain's avatar
    Guyslain committed
            //TODO: à compléter
    
    Guyslain's avatar
    Guyslain committed
        }
    
        @Override
        public Color getColor(Coordinate coordinate) {
    
    Guyslain's avatar
    Guyslain committed
            //TODO: à compléter
            return null;
    
    Guyslain's avatar
    Guyslain committed
        }
    
        @Override
    
    Guyslain's avatar
    Guyslain committed
        public void setChangeListener(Coordinate coordinate, Runnable listener) {
            this.at(coordinate).addOnChangeListener(
                    (oldValue, newValue) -> listener.run()
    
    Guyslain's avatar
    Guyslain committed
            );
        }
    
    
        @Override
        public void setGenerationNumberChangeListener(OnChangeListener<Integer> listener){
            this.generationNumber.addOnChangeListener(listener);
    
    Guyslain's avatar
    Guyslain committed
        }
    
    
    
        @Override
    
    Guyslain's avatar
    Guyslain committed
        public void clear() {
    
    Guyslain's avatar
    Guyslain committed
            //TODO: à compléter (penser à remettre le nombre de génération à 0)
    
    Guyslain's avatar
    Guyslain committed
        }
    
    
    Guyslain's avatar
    Guyslain committed
        public void reset() {
    
    Guyslain's avatar
    Guyslain committed
            //TODO: à compléter (penser à remettre le nombre de génération à 0)
    
    Guyslain's avatar
    Guyslain committed
        }
    
        @Override
        public Iterator<Coordinate> iterator() {
            return this.grid.coordinates().iterator();
        }
    }