diff --git a/app/src/main/java/model/ArrayGrid.java b/app/src/main/java/model/ArrayGrid.java index 3fd9a6b30fdc72e936359606087e74c4721a0296..826cf474404fd73d4781b4da5f1140245e47a847 100644 --- a/app/src/main/java/model/ArrayGrid.java +++ b/app/src/main/java/model/ArrayGrid.java @@ -1,10 +1,9 @@ package model; -<<<<<<< HEAD -import java.util.Iterator; +import java.util.ArrayList; +import java.util.List; + -======= ->>>>>>> origin/main public class ArrayGrid implements Grid{ private Cell [][] cells; @@ -12,41 +11,64 @@ public class ArrayGrid implements Grid{ private int numberOfColumns; - public ArrayGrid(int numberOfRows,int numberOfColumns){ - this.numberOfColumns=numberOfColumns; - this.numberOfRows=numberOfRows; - if(numberOfRows==0 | numberOfRows<0 | numberOfColumns==0 | numberOfRows<0 | numberOfColumns<0) { - throw new IllegalArgumentException("numberOfRows or numberOfColumns can't be equal to zero or a negative value"); - } - else { - this.cells=new Cell[numberOfRows][numberOfColumns]; - Cell cell=new SquareCell(); - for (int i=0; i<numberOfRows;i++){ - for(int j=0; j<numberOfColumns;j++){ - cells[i][j]=cell; + 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(); } } + 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)); } + if (columnIndex==this.getNumberOfColumns()-1){ neighbours.add(getCell(rowIndex,columnIndex-1));} + + + getCell(rowIndex,columnIndex).setNeighbours(neighbours); + } + @Override public Cell getCell(int row, int column) { - return null; + return this.cells[row][column]; } @Override public int getNumberOfRows() { - return 0; + return this.numberOfRows; } @Override public int getNumberOfColumns() { - return 0; + return this.numberOfColumns; } -<<<<<<< HEAD @Override public void color(ColorGenerator colorGenerator) { diff --git a/app/src/main/java/model/SquareCell.java b/app/src/main/java/model/SquareCell.java index 87656a156dc802354d2d4eb715604a812482b9e9..7869fe787cceda7a31982ee3c4f722ab401f4a93 100644 --- a/app/src/main/java/model/SquareCell.java +++ b/app/src/main/java/model/SquareCell.java @@ -13,12 +13,14 @@ public class SquareCell extends AbstractCell{ public SquareCell(){ setColor(AbstractCell.DEFAULT_CELL_COLOR); - setNeighbours(null); + this.neighbours= new ArrayList<Cell>(); + } public SquareCell( Color color){ setColor(color); - setNeighbours(null); + this.neighbours= new ArrayList<Cell>(); + } public SquareCell(Color color,List<Cell>neighbours){ @@ -45,17 +47,8 @@ public class SquareCell extends AbstractCell{ */ @Override public void setNeighbours(List<Cell> cells) { - if (cells==null){ - neighbours=new ArrayList<>(); - } - else { - - neighbours=new ArrayList<>(); - for (Cell value:cells) - { - neighbours.add(value); - } - } + this.neighbours=cells; + } diff --git a/app/src/main/java/view/MatrixPane.java b/app/src/main/java/view/MatrixPane.java index 85b0a674a23689cb92c5a121fd7a9bdbe718a1e5..185d62d80ae16a6209f7fc0fd50dcea828bf51e9 100644 --- a/app/src/main/java/view/MatrixPane.java +++ b/app/src/main/java/view/MatrixPane.java @@ -24,7 +24,7 @@ public class MatrixPane extends GridPane { public MatrixPane(@NamedArg("tileWidth") Double tileWidth, @NamedArg("tileHeight") Double tileHeight, @NamedArg("numberOfColumns") Integer numberOfColumns, - @NamedArg("numberOfRows") Integer numberOfRows) { + @NamedArg("numberOfRows") Integer numberOfRows) throws Exception { this.tileWidth = tileWidth; this.tileHeight = tileHeight; this.numberOfColumns = numberOfColumns; diff --git a/app/src/test/java/model/ArrayGridTest.java b/app/src/test/java/model/ArrayGridTest.java index 543aeb043d965ed51f9d568e81954eb7bcb6ce66..1d5af42d2e373e66db9f5abfbd23e3fcd346088b 100644 --- a/app/src/test/java/model/ArrayGridTest.java +++ b/app/src/test/java/model/ArrayGridTest.java @@ -15,29 +15,21 @@ class ArrayGridTest { private ArrayGrid arrayGridThreeFour; private final ArrayGrid arrayGridTwoTwo = new ArrayGrid(2,2); + ArrayGridTest() throws Exception { + } + @BeforeEach - void initializeArrayGridThreeFour(){ + void initializeArrayGridThreeFour() throws Exception { arrayGridThreeFour = new ArrayGrid(3,4); } @Test void testGetCellAndGridInitialization() { - assertThat(arrayGridThreeFour.getCell(0,0).getNeighbours()) - .hasSize(2) - .containsExactlyInAnyOrder(arrayGridThreeFour.getCell(1,0), arrayGridThreeFour.getCell(0,1)); - assertThat(arrayGridThreeFour.getCell(1,1).getNeighbours()).hasSize(4) - .containsExactlyInAnyOrder(arrayGridThreeFour.getCell(0,1), - arrayGridThreeFour.getCell(2,1), - arrayGridThreeFour.getCell(1,2), - arrayGridThreeFour.getCell(1,0)); - assertThat(arrayGridThreeFour.getCell(2,3).getNeighbours()).hasSize(2) - .containsExactlyInAnyOrder(arrayGridThreeFour.getCell(1,3), - arrayGridThreeFour.getCell(2,2)); - assertThat(arrayGridThreeFour.getCell(2,2).getNeighbours()).hasSize(3) - .containsExactlyInAnyOrder(arrayGridThreeFour.getCell(1,2), - arrayGridThreeFour.getCell(2,1), - arrayGridThreeFour.getCell(2,3)); + assertThat(arrayGridThreeFour.getCell(0,0).getNeighbours()).hasSize(2).containsExactlyInAnyOrder(arrayGridThreeFour.getCell(1,0), arrayGridThreeFour.getCell(0,1)); + assertThat(arrayGridThreeFour.getCell(1,1).getNeighbours()).hasSize(4).containsExactlyInAnyOrder(arrayGridThreeFour.getCell(0,1), arrayGridThreeFour.getCell(2,1),arrayGridThreeFour.getCell(1,2),arrayGridThreeFour.getCell(1,0)); + assertThat(arrayGridThreeFour.getCell(2,3).getNeighbours()).hasSize(2).containsExactlyInAnyOrder(arrayGridThreeFour.getCell(1,3), arrayGridThreeFour.getCell(2,2)); + assertThat(arrayGridThreeFour.getCell(2,2).getNeighbours()).hasSize(3).containsExactlyInAnyOrder(arrayGridThreeFour.getCell(1,2), arrayGridThreeFour.getCell(2,1), arrayGridThreeFour.getCell(2,3)); } @Test @@ -47,12 +39,12 @@ class ArrayGridTest { } @Test - void testGetNumberOfRows() { + void testGetNumberOfRows() throws Exception { assertThat(new ArrayGrid(100,200).getNumberOfRows()).isEqualTo(100); } @Test - void testGetNumberOfColumns() { + void testGetNumberOfColumns() throws Exception { assertThat(new ArrayGrid(100,200).getNumberOfColumns()).isEqualTo(200); }