From dd702ad45a62a067c0502671320d6136cd2fe420 Mon Sep 17 00:00:00 2001
From: Hang <thi-hang.NGUYEN@etu.univ-amu.fr>
Date: Fri, 18 Nov 2022 19:34:48 +0100
Subject: [PATCH] THN

---
 .../main/java/controller/GameController.java  |  4 +-
 app/src/main/java/model/ArrayGrid.java        | 63 +++++--------------
 .../main/java/model/ColoredCellIterator.java  |  2 +-
 app/src/main/java/model/ComputerPlayer.java   |  8 +--
 .../java/model/DistinctColorGenerator.java    | 20 ++++--
 app/src/main/java/model/FloodGame.java        |  2 +-
 app/src/main/java/model/SquareCell.java       |  2 -
 app/src/test/java/model/ArrayGridTest.java    |  2 +-
 8 files changed, 41 insertions(+), 62 deletions(-)

diff --git a/app/src/main/java/controller/GameController.java b/app/src/main/java/controller/GameController.java
index 5103186..cc3ab0d 100644
--- a/app/src/main/java/controller/GameController.java
+++ b/app/src/main/java/controller/GameController.java
@@ -152,13 +152,15 @@ public class GameController {
     public void setPlayerRandom() {
         // TODO
           //Player player = ... instantiate  a ComputerPlayer named "random" that follows the random strategy
-          //game.setPlayer(player);
+          Player player = new ComputerPlayer("random", getGridStartCell(), new RandomStrategy(availableColors, random));
+          game.setPlayer(player);
     }
     @FXML
     public void setPlayerRobin() {
         // TODO
         //  Player player = ... instantiate a ComputerPlayer named "cyclic" that follows the cyclic strategy
         //  game.setPlayer(player);
+        Player player=new RobinStrategy()
     }
 
     @FXML
diff --git a/app/src/main/java/model/ArrayGrid.java b/app/src/main/java/model/ArrayGrid.java
index 0f5904b..da968c2 100644
--- a/app/src/main/java/model/ArrayGrid.java
+++ b/app/src/main/java/model/ArrayGrid.java
@@ -9,6 +9,7 @@ public class ArrayGrid implements Grid{
     private Cell[][] cells;
     private int numberOfRows;
     private int numberOfColumns;
+
     public ArrayGrid(int numberOfRows, int numberOfColumns){
         if (numberOfRows <= 0 || numberOfColumns <= 0){
             throw new IllegalArgumentException();}
@@ -19,58 +20,28 @@ public class ArrayGrid implements Grid{
             for (int i = 0; i < numberOfRows; i++) {
                 for (int j = 0; j < numberOfColumns; j++) {
                     cells[i][j] = new SquareCell();
+
                 }
             }
+            int mx[] = {0, 0, -1, 1};
+            int my[] = {-1, 1, 0, 0};
+            for (int rowIndex= 0; rowIndex < numberOfRows ; rowIndex++) {
+                for (int columnIndex = 0; columnIndex < numberOfColumns; columnIndex++) {
 
-            for (int i = 0; i < this.numberOfRows; i++) {
-                for (int j = 0; j < this.numberOfColumns; j++) {
-                    if ((0 < i && i < this.numberOfRows - 1) && (0 < j && j < this.numberOfColumns - 1)) {
-                        this.getCell(i,i).getNeighbours().add(this.cells[i - 1][j]);
-                        this.getCell(i,i).getNeighbours().add(getCell(i, j - 1));
-                        this.getCell(i,i).getNeighbours().add(getCell(i + 1, j));
-                        this.getCell(i,i).getNeighbours().add(getCell(i, j + 1));
-                    }
-                    if (i == 0 && j == 0) {
-                        this.getCell(i,i).getNeighbours().add(this.cells[i + 1][j]);
-                        this.getCell(i,i).getNeighbours().add(this.cells[i][j + 1]);
-                    }
-                    if (i == numberOfRows - 1 && j == this.numberOfColumns - 1) {
-                        this.getCell(i,i).getNeighbours().add(this.cells[i - 1][j]);
-                        this.getCell(i,i).getNeighbours().add(this.cells[i][j - 1]);
-                    }
-                    if (i == this.numberOfRows - 1 && j == 0) {
-                        this.getCell(i,i).getNeighbours().add(this.cells[i - 1][j]);
-                        this.getCell(i,i).getNeighbours().add(this.cells[i][j + 1]);
-                    }
-                    if (i == 0 && j == numberOfColumns - 1) {
-                        this.getCell(i,i).getNeighbours().add(this.cells[i + 1][j]);
-                        this.getCell(i,i).getNeighbours().add(this.cells[i][j - 1]);
-                    }
-                    if (i == 0 & 0 < j && j < this.numberOfColumns - 1) {
-                        this.getCell(i,i).getNeighbours().add(this.cells[i][j - 1]);
-                        this.getCell(i,i).getNeighbours().add(this.cells[i + 1][j]);
-                        this.getCell(i,i).getNeighbours().add(this.cells[i][j + 1]);
+                    // Adding neighbor list
+                    List<Cell> neighbors = new ArrayList<Cell>();
+                    for(int k = 0; k < 4; k++){
+                        int c_x = rowIndex + mx[k];
+                        int c_y = columnIndex + my[k];
+                        if (c_x >= 0 && c_x < numberOfRows & c_y >= 0 && c_y < numberOfColumns) {
+                            neighbors.add(this.cells[c_x][c_y]);
+                        }
 
                     }
-                    if (i == this.numberOfRows - 1 & 0 < j && j < this.numberOfColumns - 1) {
-                        this.getCell(i,i).getNeighbours().add(this.cells[i][j - 1]);
-                        this.getCell(i,i).getNeighbours().add(this.cells[i][j + 1]);
-                        this.getCell(i,i).getNeighbours().add(this.cells[i - 1][j]);
-                    }
-                    if ((0 < i && i < this.numberOfRows - 1) && j == 0) {
-                        this.getCell(i,i).getNeighbours().add(this.cells[i - 1][j]);
-                        this.getCell(i,i).getNeighbours().add(this.cells[i + 1][j]);
-                        this.getCell(i,i).getNeighbours().add(this.cells[i][j + 1]);
-                    }
-                    if ((0 < i && i < this.numberOfRows - 1) && j == this.numberOfColumns - 1) {
-                        this.getCell(i,i).getNeighbours().add(this.cells[i - 1][j]);
-                        this.getCell(i,i).getNeighbours().add(this.cells[i + 1][j]);
-                        this.getCell(i,i).getNeighbours().add(this.cells[i][j - 1]);
-                    }
-                }
-            }
+                    this.cells[rowIndex][columnIndex].setNeighbours(neighbors);}
+
         }
-    }
+    }}
     @Override
     public Cell getCell(int row, int column) {
         return this.cells[row][column];
diff --git a/app/src/main/java/model/ColoredCellIterator.java b/app/src/main/java/model/ColoredCellIterator.java
index 6afd377..7b81a46 100644
--- a/app/src/main/java/model/ColoredCellIterator.java
+++ b/app/src/main/java/model/ColoredCellIterator.java
@@ -32,7 +32,7 @@ public class ColoredCellIterator implements Iterator<Cell> {
         Cell pendingElement=SetUtil.anyElement(pendingCells);
 
         for(Cell cell: pendingElement.getNeighbours()){
-            if( cell.getColor()==this.color&& !(visitedCells.contains(pendingElement)))
+            if( cell.getColor()==this.color&& !(visitedCells.contains(cell)))
                 pendingCells.add(cell);
         }
 
diff --git a/app/src/main/java/model/ComputerPlayer.java b/app/src/main/java/model/ComputerPlayer.java
index fd7ac9f..0e0a4dd 100644
--- a/app/src/main/java/model/ComputerPlayer.java
+++ b/app/src/main/java/model/ComputerPlayer.java
@@ -44,10 +44,10 @@ public class ComputerPlayer implements Player{
     public Color play(){
         return this.strategi.play(this.getStartCell());//  Color.RED;
     }
-    public Color setStrategy(PlayStrategy strategy){
-        this.strategi=strategy;
-        return this.play();
-    }
+//    public Color setStrategy(PlayStrategy strategy){
+//        this.strategi=strategy;
+//        return this.play();
+//    }
     public void setStartCell(Cell start){
         this.cellStart=start;
     }
diff --git a/app/src/main/java/model/DistinctColorGenerator.java b/app/src/main/java/model/DistinctColorGenerator.java
index a9a72ef..4c9c488 100644
--- a/app/src/main/java/model/DistinctColorGenerator.java
+++ b/app/src/main/java/model/DistinctColorGenerator.java
@@ -15,7 +15,7 @@ public class DistinctColorGenerator implements ColorGenerator {
 
     public Color nextColor(Cell cell ) {
 
-        List<Cell> neighbors = new ArrayList<>();
+        List<Cell> neighbors = cell.getNeighbours();
 
         //System.out.println(neighbors.size());
 
@@ -25,13 +25,21 @@ public class DistinctColorGenerator implements ColorGenerator {
         for (Cell c : neighbors) {
             neighbor_colors.add(c.getColor());
         }
-        for (int i = 0; i < colors.size(); i++) {
-            for (int j = 0; j < neighbor_colors.size(); j++) {
-                if (colors.get(i)==neighbor_colors.get(j))
-                    break;//if (!(neighbor_colors.contains(colors.get(i)))){
+        int i = 0;
+        for (; i < colors.size(); i++) {
+            if (neighbor_colors.contains(colors.get(i)) == false){
+                return colors.get(i);
             }
+//            int j = 0;
+//            for (; j < neighbor_colors.size(); j++) {
+//                if (colors.get(i).equals(neighbor_colors.get(j)) == true)
+//                    break;//if (!(neighbor_colors.contains(colors.get(i)))){
+//            }
+//
+//            if(j == neighbor_colors.size()){
+//                return colors.get(i);
+//            }
 
-            return this.colors.get(i);
         }
         return defaultColor;
 
diff --git a/app/src/main/java/model/FloodGame.java b/app/src/main/java/model/FloodGame.java
index 317b3ec..7a93e1c 100644
--- a/app/src/main/java/model/FloodGame.java
+++ b/app/src/main/java/model/FloodGame.java
@@ -56,7 +56,7 @@ public class FloodGame {
     }
 
     public boolean hasWon(Player player){
-        return this.getPlayerScore(player)==this.totalFloodingArea&& this.getTurn()<26;
+        return this.getPlayerScore(player)==this.totalFloodingArea;
         // TODO
     }
 
diff --git a/app/src/main/java/model/SquareCell.java b/app/src/main/java/model/SquareCell.java
index 617bb82..2b1e4b5 100644
--- a/app/src/main/java/model/SquareCell.java
+++ b/app/src/main/java/model/SquareCell.java
@@ -50,8 +50,6 @@ public class SquareCell extends AbstractCell{
     public void setNeighbours(List<Cell> cells) {
         this.neighbours=cells;
 
-
-
     }
 
     @Override
diff --git a/app/src/test/java/model/ArrayGridTest.java b/app/src/test/java/model/ArrayGridTest.java
index 9e2c352..91d1b9f 100644
--- a/app/src/test/java/model/ArrayGridTest.java
+++ b/app/src/test/java/model/ArrayGridTest.java
@@ -66,7 +66,7 @@ class ArrayGridTest {
     @Test
     void testColor() {
         setArrayGridThreeFourRed();
-        arrayGridThreeFour.color(cell -> Color.GREEN);
+        arrayGridThreeFour.color(cell -> Color.BLACK);
         for (int rowIndex = 0; rowIndex < 3; rowIndex++) {
             for (int columnIndex = 0; columnIndex < 4; columnIndex++) {
                 assertThat(arrayGridThreeFour.getCell(rowIndex,columnIndex).getColor()).isEqualTo(Color.BLACK);
-- 
GitLab