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

GameOfLifeState.java

  • Forked from YAGOUBI Rim / Game of life Template
    17 commits ahead of the upstream repository.
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    GameOfLifeState.java 1004 B
    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, DEAD;
    
        @Override
        public Color getColor() {
            return this == ALIVE ? Color.RED : Color.WHITE;
        }
    
        @Override
        public GameOfLifeState next() {
            return this == ALIVE ? DEAD : ALIVE;
        }
    
        @Override
        public GameOfLifeState update(List<GameOfLifeState> neighbors) {
            int aliveCount = State.count(ALIVE, neighbors);
    
            if (this == ALIVE) {
                return (aliveCount == 2 || aliveCount == 3) ? ALIVE : DEAD;
            } else {
                return (aliveCount == 3) ? ALIVE : DEAD;
            }
        }
        public GameOfLifeState randomState(Random generator) {
            return generator.nextBoolean() ? GameOfLifeState.ALIVE : GameOfLifeState.DEAD;
        }
    }