Skip to content
Snippets Groups Projects
Select Git revision
  • c7e2506ba0e0ff81c2f82014a4cf319b2aeef489
  • master default protected
2 results

Main.java

Blame
  • Forked from NAVES Guyslain / clock-template
    Source project has a limited visibility.
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    GameOfLifeState.java 1.08 KiB
    package model.automata;
    
    import javafx.scene.paint.Color;
    import model.State;
    
    import java.util.List;
    import java.util.Random;
    
    /**
     * {@link GameOfLifeState} instances represent the possible states of a {@link GameOfLifeState}.
     */
    public enum GameOfLifeState implements State<GameOfLifeState> {
        ALIVE(Color.RED),
        DEAD(Color.WHITE);
    
        public final Color color;
    
        GameOfLifeState(Color color) {
            this.color = color;
        }
    
        @Override
        public Color getColor() {
            return this.color;
        }
    
        @Override
        public GameOfLifeState next() {
            return GameOfLifeState.values()[1 - this.ordinal()];
        }
    
        @Override
        public GameOfLifeState update(List<State<GameOfLifeState>> neighbours) {
            int countAlive = 0;
            for (State<GameOfLifeState> state : neighbours) {
                if (state.equals(ALIVE)) {
                    countAlive++;
                }
            }
            boolean isAlive =
                    (this == DEAD && 3 == countAlive)
                    || (this == ALIVE && 2 <= countAlive && countAlive <= 3);
            return isAlive ? ALIVE : DEAD;
        }
    
    }