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

Grid.java

Blame
  • Forked from NAVES Guyslain / Game of life Template
    Source project has a limited visibility.
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    Grid.java 4.76 KiB
    package model;
    
    import java.util.Iterator;
    import java.util.List;
    import java.util.Random;
    
    
    /**
     * {@link 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();
        }
    
        /**
         * Returns an iterator over the cells in this {@code Grid}.
         *
         * @return an iterator over the cells in this {@code Grid}
         */
    
        @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();