Skip to content
Snippets Groups Projects
CellularAutomatonSimulation.java 4.38 KiB
Newer Older
  • Learn to ignore specific revisions
  • Guyslain's avatar
    Guyslain committed
    package model;
    
    import controller.Simulation;
    import datastruct.Coordinate;
    
    Guyslain's avatar
    Guyslain committed
    import datastruct.Matrix;
    
    Guyslain's avatar
    Guyslain committed
    import javafx.beans.property.ReadOnlyLongProperty;
    import javafx.beans.property.ReadOnlyLongWrapper;
    import javafx.scene.paint.Color;
    import java.util.Iterator;
    
    Guyslain's avatar
    Guyslain committed
    import java.util.Random;
    
    Guyslain's avatar
    Guyslain committed
    import java.util.function.Supplier;
    
    
    Guyslain's avatar
    Guyslain committed
    
    /**
    
     * {@link CellularAutomatonSimulation} instances run <i>The Game of Life</i>.
    
    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 Matrix<Cell<S>> grid;
    
    Guyslain's avatar
    Guyslain committed
        private final ReadOnlyLongWrapper generationNumber = new ReadOnlyLongWrapper();
    
    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
         *
    
    Guyslain's avatar
    Guyslain committed
         * @param automaton         a description of the {@link CellularAutomaton}
    
    Guyslain's avatar
    Guyslain committed
         */
    
    Guyslain's avatar
    Guyslain committed
        public CellularAutomatonSimulation(CellularAutomaton<S> automaton, Random generator) {
            this.automaton = automaton;
            this.grid = new Matrix<>(
                    automaton.numberOfColumns(),
                    automaton.numberOfRows(),
                    new ConstantCellInitializer<>(automaton.defaultState())
            );
            this.generator = generator;
    
    Guyslain's avatar
    Guyslain committed
        }
    
    
    Guyslain's avatar
    Guyslain committed
        /**
         * Goes through each {@link Cell} in this {@code CellGrid} and sets it states with a
         * state obtained from the supplier.
         *
         * @param generator {@link Random} instance used to generate a random state for each cell
         *                  {@link Cell}.
         */
        public void fillRandomly(Random generator) {
            for (Cell<S> cell : this.grid) {
                cell.set(this.automaton.randomState(generator));
            }
        }
    
    Guyslain's avatar
    Guyslain committed
    
        @Override
        public int numberOfColumns() {
    
    Guyslain's avatar
    Guyslain committed
            return this.grid.width();
    
    Guyslain's avatar
    Guyslain committed
        }
    
        @Override
        public int numberOfRows() {
    
    Guyslain's avatar
    Guyslain committed
            return this.grid.height();
        }
    
        public Cell<S> at(Coordinate coordinate) {
            return this.grid.get(coordinate);
    
    Guyslain's avatar
    Guyslain committed
        }
    
        public void updateToNextGeneration() {
    
            this.generationNumber.set(getGenerationNumber() + 1);
    
    Guyslain's avatar
    Guyslain committed
            Matrix<S> nextStates = this.nextGenerationMatrix();
            for (Coordinate coordinate : this.grid.coordinates()) {
                this.at(coordinate).set(nextStates.get(coordinate));
            }
        }
        /** Computes the {link Matrix} of states obtained after a single step of updates
         * of the simulation.
         *
         * @return the states of each cell after one generation
         */
        private Matrix<S> nextGenerationMatrix() {
            return new Matrix<S>(
                    this.grid.width(),
                    this.grid.height(),
                    new NextGenerationInitializer<>(this)
            );
    
    Guyslain's avatar
    Guyslain committed
        }
        @Override
        public void next(Coordinate coordinate) {
    
    Guyslain's avatar
    Guyslain committed
            S oldState = this.grid.get(coordinate).get();
            this.at(coordinate).set(oldState.next());
    
    Guyslain's avatar
    Guyslain committed
        }
    
        @Override
        public void copy(Coordinate source, Coordinate destination) {
    
    Guyslain's avatar
    Guyslain committed
            System.out.println("bip (" + source + ") (" + destination + ")");
            S state = this.at(source).get();
            this.at(destination).set(state);
    
    Guyslain's avatar
    Guyslain committed
        }
    
        @Override
        public Color getColor(Coordinate coordinate) {
    
    Guyslain's avatar
    Guyslain committed
            return this.at(coordinate).get().getColor();
    
    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
            );
        }
    
    
        /**
         * Returns the current generationNumber.
         *
         * @return the current generationNumber
         */
        private long getGenerationNumber() {
    
            return this.generationNumber.get();
    
    Guyslain's avatar
    Guyslain committed
        }
    
        /**
         * Returns the generationNumber {@link ReadOnlyLongProperty}.
         *
         * @return the generationNumber {@link ReadOnlyLongProperty}
         */
        public ReadOnlyLongProperty generationNumberProperty() {
    
            return this.generationNumber.getReadOnlyProperty();
    
    Guyslain's avatar
    Guyslain committed
        }
    
    
        /**
         * Clears the current game.
         */
        public void clear() {
    
    Guyslain's avatar
    Guyslain committed
            for (Cell<S> cell : this.grid) {
                cell.set(this.automaton.defaultState());
            }
    
            this.generationNumber.set(0);
    
    Guyslain's avatar
    Guyslain committed
        }
    
        /**
         * Clears the current game and randomly generates a new one.
         */
        public void reset() {
    
    Guyslain's avatar
    Guyslain committed
            this.fillRandomly(this.generator);
    
    Guyslain's avatar
    Guyslain committed
        }
    
        @Override
        public Iterator<Coordinate> iterator() {
            return this.grid.coordinates().iterator();
        }
    }