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

GameOfLife.java

Blame
  • Forked from DAS Shantanu / Prog2Aix-tp3
    2 commits ahead of the upstream repository.
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    GameOfLife.java 1.72 KiB
    import java.util.Random;
    
    /**
     * {@link GameOfLife} instances run <i>The Game of Life</i>.
     */
    public class GameOfLife {
    
        private final Random random = new Random();
        private final Grid grid;
        private int generationNumber;
    
        /**
         * Creates a new {@code GameOfLife} instance given the underlying {@link Grid}.
         *
         * @param grid the underlying {@link Grid}
         * @throws NullPointerException if {@code grid} is {@code null}
         */
        public GameOfLife(Grid grid) {
    	//        this.grid = requireNonNull(grid, "grid is null");
    	this.grid = grid;
            grid.randomGeneration(random);
        }
    
        /**
         * Transitions into the next generationNumber.
         */
        public void next() {
            grid.nextGeneration();
    	generationNumber++;
        }
    
    
        /**
         * Returns the current generationNumber.
         *
         * @return the current generationNumber
         */
        private int getGenerationNumber() {
            return generationNumber;
        }
    
        /**
         * Returns the {@link Grid}.
         *
         * @return the {@link Grid}
         */
        public Grid getGrid() {
            return grid;
        }
        /**
         * Plays the game.
         */
        public void play(int maxGenerations) {
    	for(int generation =0; generation < maxGenerations; generation++){
    	    this.next();
    	    System.out.println("generation : " + generation);
    	}
    	
    	
        }
    
        /**
         * Pauses the game.
         */
        public void pause() {
    	//        timeline.pause();
        }
    
        /**
         * Clears the current game.
         */
        public void clear() {
            pause();
            grid.clear();
    	generationNumber = 0;
        }
    
        /**
         * Clears the current game and randomly generates a new one.
         */
        public void reset() {
            clear();
            grid.randomGeneration(random);
        }
    
    }