Skip to content
Snippets Groups Projects
Select Git revision
  • b3b75a8a3e25879de2a5e368583fc286b6e6531a
  • main default protected
  • correction_video
  • going_further
  • ImprovedMouseInteraction
  • final2023
  • template
  • ModifGUI
8 results

GameOfLifeState.java

Blame
  • Forked from NAVES Guyslain / Game of life Template
    7 commits behind the upstream repository.
    user avatar
    Guyslain authored
    b3b75a8a
    History
    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;
        }
    
    }