Skip to content
Snippets Groups Projects
ArrayGrid.java 2.62 KiB
Newer Older
  • Learn to ignore specific revisions
  • dragapsy's avatar
    dragapsy committed
    package model;
    
    
    import java.util.Iterator;
    
    dragapsy's avatar
    dragapsy committed
    public class ArrayGrid implements Grid{
    
    
        protected Cell [][] cells;
    
    dragapsy's avatar
    dragapsy committed
        private int numberOfRows;
        private int numberOfColumns;
    
    
    
        public ArrayGrid(int numberOfRows, int numberOfColumns) throws Exception{
                if(numberOfRows <= 0 || numberOfColumns <= 0){ throw new IllegalArgumentException("taille nulle ou négative");}
                this.numberOfRows = numberOfRows;
                this.numberOfColumns = numberOfColumns;
                this.cells = new Cell[numberOfRows][numberOfColumns];
                for (int i = 0; i < numberOfRows; i++){
                    for (int j = 0; j < numberOfColumns; j++){
                        cells[i][j] = new SquareCell();
    
    dragapsy's avatar
    dragapsy committed
                    }
                }
    
    
                for (int i = 0; i < numberOfRows; i++) {
                    for (int j = 0; j < numberOfColumns; j++) {
                        calculateNeighbours(i,j);
                    }
                }
    
            }
        public void calculateNeighbours( int rowIndex, int columnIndex){
            List<Cell> neighbours = new ArrayList<>();
            if (rowIndex==0){ neighbours.add(getCell(rowIndex+1,columnIndex));}
            else if (rowIndex<this.getNumberOfRows()-1) {
                neighbours.add(getCell(rowIndex-1,columnIndex));
                neighbours.add(getCell(rowIndex+1,columnIndex));
            }
    
            if (rowIndex==this.getNumberOfRows()-1){  neighbours.add(getCell(rowIndex-1,columnIndex)); }
    
    
            if (columnIndex==0){ neighbours.add(getCell(rowIndex,columnIndex+1));}
            else if (columnIndex<this.getNumberOfColumns()-1 ) {
                neighbours.add(getCell(rowIndex,columnIndex-1));
                neighbours.add(getCell(rowIndex,columnIndex+1));
    
    dragapsy's avatar
    dragapsy committed
            }
    
            if (columnIndex==this.getNumberOfColumns()-1){ neighbours.add(getCell(rowIndex,columnIndex-1));}
    
    
            getCell(rowIndex,columnIndex).setNeighbours(neighbours);
    
    
    dragapsy's avatar
    dragapsy committed
        }
    
    
    
    
    dragapsy's avatar
    dragapsy committed
        @Override
        public Cell getCell(int row, int column) {
    
    dragapsy's avatar
    dragapsy committed
        }
    
        @Override
        public int getNumberOfRows() {
    
    dragapsy's avatar
    dragapsy committed
        }
    
        @Override
        public int getNumberOfColumns() {
    
    dragapsy's avatar
    dragapsy committed
        }
    
    dragapsy's avatar
    dragapsy committed
    
        @Override
        public void color(ColorGenerator colorGenerator) {
    
    //        for(Cell []cell1D:cells){
    //            for(Cell cell:cell1D){
    //                cell.setColor(colorGenerator.nextColor(cell));
    //            }
    //        }
            for(Cell cell:this){
                cell.setColor(colorGenerator.nextColor(cell));
    
    dragapsy's avatar
    dragapsy committed
            }
        }
    
        @Override
        public Iterator<Cell> iterator() {
            return new CellGridIterator(this);
        }
    
    dragapsy's avatar
    dragapsy committed
    }