From 09169c50b1b80ac824c0ef4a3f3e4ebdc3d89d58 Mon Sep 17 00:00:00 2001
From: dragapsy <elghaoutiayman20@gmail.com>
Date: Fri, 18 Nov 2022 01:31:30 +0100
Subject: [PATCH] =?UTF-8?q?Tache=202=20rectification=20du=20constructeur?=
 =?UTF-8?q?=20et=20ajout=20de=20la=20methode=20calculateNeighbours.=20(tes?=
 =?UTF-8?q?t=20passe=20avec=20succ=C3=A8s)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 app/src/main/java/model/ArrayGrid.java     | 62 +++++++++++++++-------
 app/src/main/java/model/SquareCell.java    | 19 +++----
 app/src/main/java/view/MatrixPane.java     |  2 +-
 app/src/test/java/model/ArrayGridTest.java | 28 ++++------
 4 files changed, 59 insertions(+), 52 deletions(-)

diff --git a/app/src/main/java/model/ArrayGrid.java b/app/src/main/java/model/ArrayGrid.java
index 3fd9a6b..826cf47 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 87656a1..7869fe7 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 85b0a67..185d62d 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 543aeb0..1d5af42 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);
     }
 
-- 
GitLab