Skip to content
Snippets Groups Projects
Grid.java 5.79 KiB
Newer Older
  • Learn to ignore specific revisions
  • Mattéo's avatar
    Mattéo committed
    import java.util.*;
    
    Mattéo's avatar
    Mattéo committed
    import static com.sun.tools.doclint.Entity.ne;
    import static com.sun.tools.doclint.Entity.or;
    
    
    Alexis Nasr's avatar
    Alexis Nasr committed
    /**
     * {@code Grid} instances represent the grid in <i>The Game of Life</i>.
     */
    public class Grid implements Iterable<Cell> {
    
        private final int numberOfRows;
        private final int numberOfColumns;
        private final Cell[][] cells;
    
        /**
         * Creates a new {@code Grid} instance given the number of rows and columns.
         *
         * @param numberOfRows    the number of rows
         * @param numberOfColumns the number of columns
         * @throws IllegalArgumentException if {@code numberOfRows} or {@code numberOfColumns} are
         *                                  less than or equal to 0
         */
        public Grid(int numberOfRows, int numberOfColumns) {
            this.numberOfRows = numberOfRows;
            this.numberOfColumns = numberOfColumns;
            this.cells = createCells();
        }
    
        @Override
        public Iterator<Cell> iterator() {
            return new GridIterator(this);
        }
    
        private Cell[][] createCells() {
            Cell[][] cells = new Cell[getNumberOfRows()][getNumberOfColumns()];
            for (int rowIndex = 0; rowIndex < getNumberOfRows(); rowIndex++) {
                for (int columnIndex = 0; columnIndex < getNumberOfColumns(); columnIndex++) {
                    cells[rowIndex][columnIndex] = new Cell();
                }
            }
            return cells;
        }
    
        /**
         * Returns the {@link Cell} at the given index.
         *
         * <p>Note that the index is wrapped around so that a {@link Cell} is always returned.
         *
         * @param rowIndex    the row index of the {@link Cell}
         * @param columnIndex the column index of the {@link Cell}
         * @return the {@link Cell} at the given row and column index
         */
        public Cell getCell(int rowIndex, int columnIndex) {
            return cells[getWrappedRowIndex(rowIndex)][getWrappedColumnIndex(columnIndex)];
        }
    
        private int getWrappedRowIndex(int rowIndex) {
            return (rowIndex + getNumberOfRows()) % getNumberOfRows();
        }
    
        private int getWrappedColumnIndex(int columnIndex) {
            return (columnIndex + getNumberOfColumns()) % getNumberOfColumns();
        }
    
        /**
         * Returns the number of rows in this {@code Grid}.
         *
         * @return the number of rows in this {@code Grid}
         */
        public int getNumberOfRows() {
            return numberOfRows;
        }
    
        /**
         * Returns the number of columns in this {@code Grid}.
         *
         * @return the number of columns in this {@code Grid}
         */
        public int getNumberOfColumns() {
            return numberOfColumns;
        }
    
        /**
         * Transitions all {@link Cell}s in this {@code Grid} to the next generation.
         *
         * <p>The following rules are applied:
         * <ul>
         * <li>Any live {@link Cell} with fewer than two live neighbours dies, i.e. underpopulation.</li>
         * <li>Any live {@link Cell} with two or three live neighbours lives on to the next
         * generation.</li>
         * <li>Any live {@link Cell} with more than three live neighbours dies, i.e. overpopulation.</li>
         * <li>Any dead {@link Cell} with exactly three live neighbours becomes a live cell, i.e.
         * reproduction.</li>
         * </ul>
         */
        void nextGeneration() {
            goToNextState(calculateNextStates());
        }
    
        private boolean[][] calculateNextStates() {
    
    Mattéo's avatar
    Mattéo committed
            boolean[][] nextStates = new boolean[numberOfRows][numberOfColumns];
            for (Cell cell : this) {
                for (int i = 0; i == numberOfRows - 1; i++) {
                    for (int j = 0; j == numberOfColumns - 1; j++) {
                        nextStates[i][j] = calculateNextState(i, j, cell);
                    }
                }
            }
            return nextStates;
    
    Alexis Nasr's avatar
    Alexis Nasr committed
        }
    
        private boolean calculateNextState(int rowIndex, int columnIndex, Cell cell) {
    
    Mattéo's avatar
    Mattéo committed
            if (cell.isAlive()) {
                return (countAliveNeighbours(rowIndex, columnIndex) == 2) || (countAliveNeighbours(rowIndex, columnIndex) == 3);
            }
            else return false;
    
    Alexis Nasr's avatar
    Alexis Nasr committed
        }
    
        private int countAliveNeighbours(int rowIndex, int columnIndex) {
    
    Mattéo's avatar
    Mattéo committed
            int aliveNeighbours = 0;
            for (Cell cell : getNeighbours(rowIndex,columnIndex)) {
                if (cell.isAlive()) {aliveNeighbours++;}
            }
            return aliveNeighbours;
    
    Alexis Nasr's avatar
    Alexis Nasr committed
        }
    
    
        private List<Cell> getNeighbours(int rowIndex, int columnIndex) {
    
    Mattéo's avatar
    Mattéo committed
            List<Cell> neighbours = new ArrayList<>();
            neighbours.add(getCell(rowIndex-1,columnIndex-1));
            neighbours.add(getCell(rowIndex,columnIndex-1));
            neighbours.add(getCell(rowIndex+1,columnIndex-1));
            neighbours.add(getCell(rowIndex-1,columnIndex));
            neighbours.add(getCell(rowIndex+1,columnIndex));
            neighbours.add(getCell(rowIndex-1,columnIndex+1));
            neighbours.add(getCell(rowIndex,columnIndex+1));
            neighbours.add(getCell(rowIndex+1,columnIndex+1));
            return neighbours;
    
    Alexis Nasr's avatar
    Alexis Nasr committed
        }
    
        private void goToNextState(boolean[][] nextState) {
    
    Mattéo's avatar
    Mattéo committed
            for (Cell cell : this) {
                for (int i = 0; i == numberOfRows - 1; i++) {
                    for (int j = 0; j == numberOfColumns - 1; j++) {
                        if (nextState[i][j]) {
                            cell.setAlive();
                        } else cell.setDead();
                    }
                }
    
    Mattéo's avatar
    Mattéo committed
            }
    
    Alexis Nasr's avatar
    Alexis Nasr committed
        }
    
        /**
         * Sets all {@link Cell}s in this {@code Grid} as dead.
         */
        void clear() {
    
    Mattéo's avatar
    Mattéo committed
            for (Cell cell : this) {
                cell.setDead();
            }
    
    Alexis Nasr's avatar
    Alexis Nasr committed
        }
    
        /**
         * Goes through each {@link Cell} in this {@code Grid} and randomly sets it as alive or dead.
         *
         * @param random {@link Random} instance used to decide if each {@link Cell} is alive or dead
         * @throws NullPointerException if {@code random} is {@code null}
         */
     
        void randomGeneration(Random random) {
    
    Mattéo's avatar
    Mattéo committed
            for (Cell cell : this) {
                if (random.nextBoolean()) {cell.setAlive();}
                else cell.setDead();
            }