Skip to content
Snippets Groups Projects
Cell.java 1.83 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;
    
    RANDRIANARISON John's avatar
    RANDRIANARISON John committed
        private String color = "Black";
    
    Alexis Nasr's avatar
    Alexis Nasr committed
    
        public Cell(){
    
    RANDRIANARISON John's avatar
    RANDRIANARISON John committed
            this.isAlive = false;
    
    RANDRIANARISON John's avatar
    RANDRIANARISON John committed
    
        public void setColor(String hisColor) { color = hisColor; }
    
        public String getColor() { return color; }
    
    
    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;
        }
    
    
    RANDRIANARISON John's avatar
    RANDRIANARISON John committed
    
    
    Alexis Nasr's avatar
    Alexis Nasr committed
        /**
         * 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.
         *
         * @param cellState the new state of this {@link Cell}
         */
    
        public void setAlive() {
    
    RANDRIANARISON John's avatar
    RANDRIANARISON John committed
            this.isAlive = true;
    
    Alexis Nasr's avatar
    Alexis Nasr committed
        }
    
        /**
         * Sets the state of this {@link Cell} to dead.
         *
         * @param cellState the new state of this {@link Cell}
         */
    
        public void setDead() {
    
    RANDRIANARISON John's avatar
    RANDRIANARISON John 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() {
    
    RANDRIANARISON John's avatar
    RANDRIANARISON John committed
            if(this.isAlive)
                this.isAlive = false;
            else
                this.isAlive = true;
    
    Alexis Nasr's avatar
    Alexis Nasr committed
        }
    
        public boolean isAliveInNextState(int numberOfAliveNeighbours) {
    
    RANDRIANARISON John's avatar
    RANDRIANARISON John committed
            if(isAlive()){
                if (numberOfAliveNeighbours == 2 ||  numberOfAliveNeighbours == 3)
                    return true;
                else
                    return false;
            }
            else{
                if (numberOfAliveNeighbours == 3)
                    return true;
                else
                    return false;
            }