From 92481741f717358f60823dc8dfccc92b58e1dd34 Mon Sep 17 00:00:00 2001
From: EL GAOUAL Zaid <zaid.el-gaoual@etu.univ-amu.fr>
Date: Fri, 18 Nov 2022 11:00:47 +0100
Subject: [PATCH] Neighbours init

---
 app/src/main/java/model/ArrayGrid.java        |  59 +++++++--
 .../java/model/DistinctColorGenerator.java    |  12 +-
 app/src/main/java/model/GrayGrid.java         |   8 +-
 app/src/main/java/model/Grid.java             |   2 +-
 .../java/model/ColoredCellIteratorTest.java   | 118 +++++++++---------
 .../model/DistinctColorGeneratorTest.java     |  82 ++++++------
 6 files changed, 158 insertions(+), 123 deletions(-)

diff --git a/app/src/main/java/model/ArrayGrid.java b/app/src/main/java/model/ArrayGrid.java
index d99f9db..caa8edd 100644
--- a/app/src/main/java/model/ArrayGrid.java
+++ b/app/src/main/java/model/ArrayGrid.java
@@ -2,22 +2,64 @@ package model;
 
 import javafx.scene.paint.Color;
 
+import java.util.ArrayList;
 import java.util.Iterator;
+import java.util.List;
 
 public class ArrayGrid implements Grid{
     private Cell[][] cells;
 
     public ArrayGrid(int numberOfRows, int numberOfColumns) {
-        if(numberOfRows <= 0 || numberOfColumns <= 0)
+        if (numberOfRows <= 0 || numberOfColumns <= 0)
             throw new IllegalArgumentException("Le nombre de lignes ou de colonnes ne peut pas être nul ou négatif.");
         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++) {
+                Cell origin = cells[i][j] = new SquareCell();
+                tryAddNeighbour2(origin,tryGetCell(i-1,j));
+                tryAddNeighbour2(origin,tryGetCell(i,j-1));
             }
         }
+       // initNeighbours(numberOfRows,numberOfColumns);
     }
+   /*     private void initNeighbours(int numberOfRows, int numberOfColumns) {
+            for (int i = 0; i < numberOfRows; i++) {
+                for (int j = 0; j < numberOfColumns; j++) {
+                    List<Cell> neighbours = getCell(i,j).getNeighbours();
+                  //  tryAddNeighbour(i, j - 1, neighbours);
+                  //  tryAddNeighbour(i, j + 1, neighbours);
+                    tryAddNeighbour(i - 1, j, neighbours);
+                    tryAddNeighbour(i + 1, j, neighbours);
 
+                   // getCell(i, j).setNeighbours(neighbours);
+                }
+            }
+        }*/
+
+        private Cell tryGetCell(int row , int column){
+             try {
+                return this.getCell(row,column);
+
+          } catch (Exception e) {
+
+              }
+            return null;
+        }
+      /*  private void tryAddNeighbour(int row , int column,List<Cell> cells){
+            Cell neighbour = tryGetCell(row,column);
+            if (neighbour != null){
+                cells.add(neighbour);
+         }
+     } */
+      private void tryAddNeighbour2(Cell origin , Cell neighbour){
+        if (neighbour != null){
+               addNeighbourTo(origin,neighbour);
+        }
+    }
+     private void addNeighbourTo(Cell neighbour1,Cell neighbour2){
+        neighbour1.getNeighbours().add(neighbour2);
+        neighbour2.getNeighbours().add(neighbour1);
+     }
 
 
     @Override
@@ -48,13 +90,4 @@ public class ArrayGrid implements Grid{
         return new CellGridIterator(this);
     }
 
-    @Override
-    public boolean hasNext() {
-        return false;
-    }
-
-    @Override
-    public Cell next() {
-        return null;
-    }
 }
diff --git a/app/src/main/java/model/DistinctColorGenerator.java b/app/src/main/java/model/DistinctColorGenerator.java
index 3d9bd1e..07b89f4 100644
--- a/app/src/main/java/model/DistinctColorGenerator.java
+++ b/app/src/main/java/model/DistinctColorGenerator.java
@@ -10,7 +10,7 @@ public class DistinctColorGenerator implements ColorGenerator {
     private List<Color> colors;
     private Color defaultColor;
 
-    public DistinctColorGenerator(List<Color> colors, Color defaultColor) {
+    public DistinctColorGenerator(Color defaultColor, List<Color> colors) {
         this.colors = colors;
         this.defaultColor = defaultColor;
     }
@@ -18,12 +18,12 @@ public class DistinctColorGenerator implements ColorGenerator {
     @Override
     public Color nextColor(Cell cell) {
         List<Cell> neighbours = cell.getNeighbours();
-        List<Color> neighbourscolor = new ArrayList<>();
-        for (Cell neighbour : neighbours) {neighbourscolor.add(neighbour.getColor());}
+        List<Color> neighboursColor = new ArrayList<>();
+        for (Cell neighbour : neighbours) {neighboursColor.add(neighbour.getColor());}
         int i = 0, j = 0;
-        while (colors.get(i) == neighbourscolor.get(j)) {
-            if (j == neighbourscolor.size() - 1 && i == colors.size() - 1) {return defaultColor;}
-            if (j == neighbourscolor.size() - 1) {
+        while (colors.get(i) == neighboursColor.get(j)) {
+            if (j == neighboursColor.size() - 1 && i == colors.size() - 1) {return defaultColor;}
+            if (j == neighboursColor.size() - 1) {
                 j=0;
                 i+=1;
                 continue;}
diff --git a/app/src/main/java/model/GrayGrid.java b/app/src/main/java/model/GrayGrid.java
index b7b1391..15592fa 100644
--- a/app/src/main/java/model/GrayGrid.java
+++ b/app/src/main/java/model/GrayGrid.java
@@ -1,5 +1,7 @@
 package model;
 
+import java.util.Iterator;
+
 public class GrayGrid implements Grid{
 
     private final int numberOfRows;
@@ -46,13 +48,9 @@ public class GrayGrid implements Grid{
 
     }
 
-    @Override
-    public boolean hasNext() {
-        return false;
-    }
 
     @Override
-    public Cell next() {
+    public Iterator<Cell> iterator() {
         return null;
     }
 }
diff --git a/app/src/main/java/model/Grid.java b/app/src/main/java/model/Grid.java
index 004f293..4487a8a 100644
--- a/app/src/main/java/model/Grid.java
+++ b/app/src/main/java/model/Grid.java
@@ -2,7 +2,7 @@ package model;
 
 import java.util.Iterator;
 
-public interface  Grid extends Iterator<Cell> {
+public interface  Grid extends Iterable<Cell> {
 
     /**
      * Return the cell located at the given coordinates in the grid.
diff --git a/app/src/test/java/model/ColoredCellIteratorTest.java b/app/src/test/java/model/ColoredCellIteratorTest.java
index bd0bb10..c6d41c2 100644
--- a/app/src/test/java/model/ColoredCellIteratorTest.java
+++ b/app/src/test/java/model/ColoredCellIteratorTest.java
@@ -1,59 +1,59 @@
-//package model;
-//
-//import javafx.scene.paint.Color;
-//import org.junit.jupiter.api.BeforeAll;
-//import org.junit.jupiter.api.Test;
-//
-//import java.util.ArrayList;
-//import java.util.List;
-//
-//import static org.assertj.core.api.Assertions.assertThat;
-//
-//class ColoredCellIteratorTest {
-//
-//
-//    /*
-//     * +---+---+---+
-//     * | R | B | R |
-//     * +---+---+---|
-//     * | R | R | B |
-//     * |---+---+---+
-//     * | B | B | R |
-//     * +---+---+---+
-//     */
-//
-//    private static ArrayGrid gridThreeThree = new ArrayGrid(3,3);
-//
-//    @BeforeAll
-//    private static void initializeColorsGrid(){
-//        gridThreeThree.getCell(0,0).setColor(Color.RED);
-//        gridThreeThree.getCell(0,1).setColor(Color.BLACK);
-//        gridThreeThree.getCell(0,2).setColor(Color.RED);
-//        gridThreeThree.getCell(1,0).setColor(Color.RED);
-//        gridThreeThree.getCell(1,1).setColor(Color.RED);
-//        gridThreeThree.getCell(1,2).setColor(Color.BLACK);
-//        gridThreeThree.getCell(2,0).setColor(Color.BLACK);
-//        gridThreeThree.getCell(2,1).setColor(Color.BLACK);
-//        gridThreeThree.getCell(2,2).setColor(Color.RED);
-//    }
-//
-//    @Test
-//    void testIterator() {
-//        ColoredCellIterator redCellIterator = new ColoredCellIterator(gridThreeThree.getCell(0,0));
-//        List<Cell> expectedRedCells = List.of(gridThreeThree.getCell(0,0),
-//                gridThreeThree.getCell(1,0),
-//                gridThreeThree.getCell(1,1));
-//        List<Cell> fromIteratorCells = new ArrayList<>();
-//        for(;redCellIterator.hasNext();) fromIteratorCells.add(redCellIterator.next());
-//        assertThat(fromIteratorCells).hasSameElementsAs(expectedRedCells).hasSameSizeAs(expectedRedCells);
-//
-//        ColoredCellIterator blackCellIterator = new ColoredCellIterator(gridThreeThree.getCell(2,1));
-//        List<Cell> expectedBlackCells = List.of(gridThreeThree.getCell(2,0),
-//                gridThreeThree.getCell(2,1));
-//        fromIteratorCells = new ArrayList<>();
-//        for( ; blackCellIterator.hasNext(); ) fromIteratorCells.add(blackCellIterator.next());
-//        assertThat(fromIteratorCells).hasSameElementsAs(expectedBlackCells).hasSameSizeAs(expectedBlackCells);
-//
-//    }
-//
-//}
\ No newline at end of file
+package model;
+
+import javafx.scene.paint.Color;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+class ColoredCellIteratorTest {
+
+
+    /*
+     * +---+---+---+
+     * | R | B | R |
+     * +---+---+---|
+     * | R | R | B |
+     * |---+---+---+
+     * | B | B | R |
+     * +---+---+---+
+     */
+
+    private static ArrayGrid gridThreeThree = new ArrayGrid(3,3);
+
+    @BeforeAll
+    private static void initializeColorsGrid(){
+        gridThreeThree.getCell(0,0).setColor(Color.RED);
+        gridThreeThree.getCell(0,1).setColor(Color.BLACK);
+        gridThreeThree.getCell(0,2).setColor(Color.RED);
+        gridThreeThree.getCell(1,0).setColor(Color.RED);
+        gridThreeThree.getCell(1,1).setColor(Color.RED);
+        gridThreeThree.getCell(1,2).setColor(Color.BLACK);
+        gridThreeThree.getCell(2,0).setColor(Color.BLACK);
+        gridThreeThree.getCell(2,1).setColor(Color.BLACK);
+        gridThreeThree.getCell(2,2).setColor(Color.RED);
+    }
+
+    @Test
+    void testIterator() {
+        ColoredCellIterator redCellIterator = new ColoredCellIterator(gridThreeThree.getCell(0,0));
+        List<Cell> expectedRedCells = List.of(gridThreeThree.getCell(0,0),
+                gridThreeThree.getCell(1,0),
+                gridThreeThree.getCell(1,1));
+        List<Cell> fromIteratorCells = new ArrayList<>();
+        for(;redCellIterator.hasNext();) fromIteratorCells.add(redCellIterator.next());
+        assertThat(fromIteratorCells).hasSameElementsAs(expectedRedCells).hasSameSizeAs(expectedRedCells);
+
+        ColoredCellIterator blackCellIterator = new ColoredCellIterator(gridThreeThree.getCell(2,1));
+        List<Cell> expectedBlackCells = List.of(gridThreeThree.getCell(2,0),
+                gridThreeThree.getCell(2,1));
+        fromIteratorCells = new ArrayList<>();
+        for( ; blackCellIterator.hasNext(); ) fromIteratorCells.add(blackCellIterator.next());
+        assertThat(fromIteratorCells).hasSameElementsAs(expectedBlackCells).hasSameSizeAs(expectedBlackCells);
+
+    }
+
+}
\ No newline at end of file
diff --git a/app/src/test/java/model/DistinctColorGeneratorTest.java b/app/src/test/java/model/DistinctColorGeneratorTest.java
index 541d425..c015818 100644
--- a/app/src/test/java/model/DistinctColorGeneratorTest.java
+++ b/app/src/test/java/model/DistinctColorGeneratorTest.java
@@ -1,39 +1,43 @@
-//package model;
-//
-//import javafx.scene.paint.Color;
-//import org.junit.jupiter.api.BeforeEach;
-//import org.junit.jupiter.api.Test;
-//
-//import java.util.List;
-//
-//import static org.assertj.core.api.Assertions.assertThat;
-//
-//class DistinctColorGeneratorTest {
-//    private final static Color initialColor = Color.GRAY;
-//    private final Grid grid = new ArrayGrid(2,3);
-//    @BeforeEach
-//    void initializeGridColor(){
-//        for (Cell cell: grid)
-//            cell.setColor(initialColor);
-//    }
-//
-//    @Test
-//    void testNextColorWithTooFewAvailableColor() {
-//        DistinctColorGenerator colorGeneratorOne = new DistinctColorGenerator(Color.RED, List.of(Color.RED));
-//        grid.color(colorGeneratorOne);
-//        assertThat(grid.getCell(0,0).getColor()).isEqualTo(Color.RED);
-//        assertThat(grid.getCell(1,1).getColor()).isEqualTo(Color.RED);
-//        assertThat(grid.getCell(1,0).getColor()).isEqualTo(Color.RED);
-//    }
-//
-//    @Test
-//    void testNextColorWithEnoughAvailableColor(){
-//        Color defaultColor = Color.BLACK;
-//        DistinctColorGenerator colorGenerator = new DistinctColorGenerator(defaultColor, List.of(Color.RED,Color.BLUE,Color.YELLOW, Color.ORANGE));
-//        grid.color(colorGenerator);
-//        for(Cell cell: grid){
-//            assertThat(cell.getNeighbours().stream().map(c-> c.getColor())).doesNotContain(cell.getColor());
-//            assertThat(cell.getColor()).isNotEqualTo(defaultColor).isNotEqualTo(initialColor);
-//        }
-//    }
-//}
\ No newline at end of file
+package model;
+
+import javafx.scene.paint.Color;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.util.List;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+class DistinctColorGeneratorTest {
+
+
+    private final static Color initialColor = Color.GRAY;
+    private final Grid grid = new ArrayGrid(2,3);
+    @BeforeEach
+    void initializeGridColor(){
+        for (Cell cell: grid)
+            cell.setColor(initialColor);
+    }
+
+    @Test
+    void testNextColorWithTooFewAvailableColor() {
+        DistinctColorGenerator colorGeneratorOne = new DistinctColorGenerator(Color.RED, List.of(Color.RED));
+        grid.color(colorGeneratorOne);
+        assertThat(grid.getCell(0,0).getColor()).isEqualTo(Color.RED);
+        assertThat(grid.getCell(1,1).getColor()).isEqualTo(Color.RED);
+        assertThat(grid.getCell(1,0).getColor()).isEqualTo(Color.RED);
+    }
+
+    @Test
+    void testNextColorWithEnoughAvailableColor(){
+        Color defaultColor = Color.BLACK;
+        DistinctColorGenerator colorGenerator = new DistinctColorGenerator(defaultColor, List.of(Color.RED,Color.BLUE,Color.YELLOW, Color.ORANGE));
+        grid.color(colorGenerator);
+        for(Cell cell: grid){
+            assertThat(cell.getNeighbours().stream().map(c-> c.getColor())).doesNotContain(cell.getColor());
+            assertThat(cell.getColor()).isNotEqualTo(defaultColor).isNotEqualTo(initialColor);
+        }
+    }
+
+}
+
-- 
GitLab