Skip to content
Snippets Groups Projects
Cell.java 1.72 KiB
Newer Older
  • Learn to ignore specific revisions
  • Alexis Nasr's avatar
    Alexis Nasr committed
    /**
     * {@link Cell} instances represent the cells of <i>The Game of Life</i>.
     */
    public class Cell {
        private boolean isAlive;
    
    AZZOUG Lydia's avatar
    AZZOUG Lydia committed
        private boolean isRed; public Cell(){
            this.isAlive = false;
        }
    
        public void setisRed(boolean bool){
            this.isRed = bool;
        }
    
    AZZOUG Lydia's avatar
    AZZOUG Lydia committed
        public boolean isRed(){
            return this.isRed;
    
    AZZOUG Lydia's avatar
    AZZOUG Lydia committed
    
    
    Alexis Nasr's avatar
    Alexis Nasr committed
        /**
         * Determines whether this {@link Cell} is alive or not.
         *
         * @return {@code true} if this {@link Cell} is alive and {@code false} otherwise
         */
        public boolean isAlive() {
            return this.isAlive;
        }
    
        /**
         * Determines whether this {@link Cell} is dead or not.
         *
         * @return {@code true} if this {@link Cell} is dead and {@code false} otherwise
         */
        public boolean isDead() {
            return !this.isAlive;
        }
    
        /**
         * Sets the state of this {@link Cell} to alive.
         *
         */
        public void setAlive() {
    
    AZZOUG Lydia's avatar
    AZZOUG Lydia committed
            this.isAlive = true;
    
    Alexis Nasr's avatar
    Alexis Nasr committed
        }
    
        /**
         * Sets the state of this {@link Cell} to dead.
         *
         */
        public void setDead() {
    
    AZZOUG Lydia's avatar
    AZZOUG Lydia committed
            this.isAlive = false;
    
    Alexis Nasr's avatar
    Alexis Nasr committed
        }
    
        /**
         * Change the state of this {@link Cell} from ALIVE to DEAD or from DEAD to ALIVE.
         */
        public void toggleState() {
    
    AZZOUG Lydia's avatar
    AZZOUG Lydia committed
            if(this.isAlive)
                this.isAlive = false;
            else
                this.isAlive = true;
    
    Alexis Nasr's avatar
    Alexis Nasr committed
        }
    
        public boolean isAliveInNextState(int numberOfAliveNeighbours) {
    
    AZZOUG Lydia's avatar
    AZZOUG Lydia committed
            if(isAlive()){
                if ((numberOfAliveNeighbours == 2) ||  (numberOfAliveNeighbours == 3))
                    return true;
                else
                    return false;
            }
            else{
                if (numberOfAliveNeighbours == 3)
                    return true;
                else
                    return false;
            }
    
    AZZOUG Lydia's avatar
    AZZOUG Lydia committed