From 57b02fba64a93fbf8e58892747a565e78a189bda Mon Sep 17 00:00:00 2001
From: EL GAOUAL Zaid <zaid.el-gaoual@etu.univ-amu.fr>
Date: Fri, 18 Nov 2022 13:07:09 +0100
Subject: [PATCH] Human player

---
 .../main/java/controller/GameController.java  |  24 +--
 app/src/main/java/model/Cell.java             |   2 +-
 app/src/main/java/model/Flooder.java          |  25 +++
 app/src/main/java/model/GrayCell.java         |   6 +
 app/src/main/java/model/HumanPlayer.java      |   4 +-
 app/src/main/java/model/SquareCell.java       |   4 +
 .../test/java/model/ComputerPlayerTest.java   |  50 ++---
 app/src/test/java/model/FloodGameTest.java    | 186 +++++++++---------
 8 files changed, 168 insertions(+), 133 deletions(-)

diff --git a/app/src/main/java/controller/GameController.java b/app/src/main/java/controller/GameController.java
index b193dac..7463c99 100644
--- a/app/src/main/java/controller/GameController.java
+++ b/app/src/main/java/controller/GameController.java
@@ -94,22 +94,22 @@ public class GameController {
     private void playComputerTurn(){
         // TODO
         //  uncomment
-        // if (!game.hasEnded() && !game.isHumanTurn()){
-        //    ComputerPlayer player = ((ComputerPlayer) game.getPlayer());
-        //    Flooder.flood(player.getStartCell(), player.play());
-        //    setScoreLabelTextProperty();
-        //    pause.play();
-        // }
+         if (!game.hasEnded() && !game.isHumanTurn()){
+            ComputerPlayer player = ((ComputerPlayer) game.getPlayer());
+            Flooder.flood(player.getStartCell(), player.play());
+            setScoreLabelTextProperty();
+            pause.play();
+         }
     }
 
     public void playHumanTurn(Color color){
         // TODO
         //  uncomment
-        // if(!game.hasEnded() && game.isHumanTurn()){
-        //     Flooder.flood(game.getPlayer().getStartCell(), color);
-        //    setScoreLabelTextProperty();
-        //    game.incrementTurn();
-        //}
+         if(!game.hasEnded() && game.isHumanTurn()){
+             Flooder.flood(game.getPlayer().getStartCell(), color);
+            setScoreLabelTextProperty();
+            game.incrementTurn();
+        }
     }
 
     private void setPauseAnimation(){
@@ -145,7 +145,7 @@ public class GameController {
     @FXML
     public void setPlayerHuman(){
         // TODO
-        //  game.setPlayer(new HumanPlayer("human", getGridStartCell()));
+          game.setPlayer(new HumanPlayer("human", getGridStartCell()));
     }
 
     @FXML
diff --git a/app/src/main/java/model/Cell.java b/app/src/main/java/model/Cell.java
index 4f09b41..d7d0b60 100644
--- a/app/src/main/java/model/Cell.java
+++ b/app/src/main/java/model/Cell.java
@@ -41,6 +41,6 @@ public interface Cell {
      * @return this {@link Cell}'s property
      */
     Property<Color> getColorProperty();
-
+    Iterator<Cell> iterator();
 
 }
diff --git a/app/src/main/java/model/Flooder.java b/app/src/main/java/model/Flooder.java
index e69de29..831e391 100644
--- a/app/src/main/java/model/Flooder.java
+++ b/app/src/main/java/model/Flooder.java
@@ -0,0 +1,25 @@
+package model;
+
+import javafx.scene.paint.Color;
+
+import java.util.Iterator;
+
+public class Flooder {
+
+    public static void flood(Cell startCell, Color floodingColor){
+        Iterator<Cell> i = startCell.iterator();
+        while (i.hasNext()){
+            i.next().setColor(floodingColor);
+        }
+    }
+
+    public static int coloredArea(Cell startCell){
+        int colored=0;
+        Iterator<Cell> i = startCell.iterator();
+        while (i.hasNext()){
+            i.next();
+            colored++;
+        }
+        return colored;
+    }
+}
\ No newline at end of file
diff --git a/app/src/main/java/model/GrayCell.java b/app/src/main/java/model/GrayCell.java
index 8007887..afd2c1a 100644
--- a/app/src/main/java/model/GrayCell.java
+++ b/app/src/main/java/model/GrayCell.java
@@ -3,6 +3,7 @@ package model;
 import javafx.beans.property.Property;
 import javafx.scene.paint.Color;
 
+import java.util.Iterator;
 import java.util.List;
 
 public class GrayCell extends AbstractCell{
@@ -30,6 +31,11 @@ public class GrayCell extends AbstractCell{
 
     }
 
+    @Override
+    public Iterator<Cell> iterator() {
+        return null;
+    }
+
     @Override
     public void setColor(Color color){
 
diff --git a/app/src/main/java/model/HumanPlayer.java b/app/src/main/java/model/HumanPlayer.java
index 2621aff..d2447e3 100644
--- a/app/src/main/java/model/HumanPlayer.java
+++ b/app/src/main/java/model/HumanPlayer.java
@@ -5,11 +5,11 @@ public class HumanPlayer implements Player {
     private  String name;
     private  Cell startCell;
 
-     HumanPlayer(String name, Cell startCell) {
+     public HumanPlayer(String name, Cell startCell) {
         this.name = name;
         this.startCell = startCell;
     }
-     HumanPlayer(Cell startCell1) {
+     public HumanPlayer(Cell startCell1) {
 
         this.startCell = startCell1;
     }
diff --git a/app/src/main/java/model/SquareCell.java b/app/src/main/java/model/SquareCell.java
index 2519ce0..673a9cc 100644
--- a/app/src/main/java/model/SquareCell.java
+++ b/app/src/main/java/model/SquareCell.java
@@ -44,6 +44,10 @@ public class SquareCell extends AbstractCell{
     public void setNeighbours(List<Cell> cells) {
         this.neighbours=cells;
     }
+    public Iterator<Cell> iterator(){
+        return new ColoredCellIterator(this);
+
+    }
 
 
 }
diff --git a/app/src/test/java/model/ComputerPlayerTest.java b/app/src/test/java/model/ComputerPlayerTest.java
index 0beb370..d37b9b3 100644
--- a/app/src/test/java/model/ComputerPlayerTest.java
+++ b/app/src/test/java/model/ComputerPlayerTest.java
@@ -1,25 +1,25 @@
-//package model;
-//
-//import javafx.scene.paint.Color;
-//import org.junit.jupiter.api.Test;
-//
-//import static org.assertj.core.api.Assertions.assertThat;
-//
-//class ComputerPlayerTest {
-//
-//
-//
-//    @Test
-//    void testSetStrategyAndPlay() {
-//        ComputerPlayer player = new ComputerPlayer("computer",null);
-//        player.setStrategy(startCell -> Color.INDIGO);
-//        player.setStartCell(new SquareCell());
-//        assertThat(player.play()).isEqualTo(Color.INDIGO);
-//    }
-//
-//
-//    @Test
-//    void testIsHuman() {
-//        assertThat(new ComputerPlayer("computer", null).isHuman()).isFalse();
-//    }
-//}
\ No newline at end of file
+package model;
+
+import javafx.scene.paint.Color;
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+class ComputerPlayerTest {
+
+
+
+    @Test
+    void testSetStrategyAndPlay() {
+        ComputerPlayer player = new ComputerPlayer("computer",null);
+        player.setStrategy(startCell -> Color.INDIGO);
+        player.setStartCell(new SquareCell());
+        assertThat(player.play()).isEqualTo(Color.INDIGO);
+    }
+
+
+    @Test
+    void testIsHuman() {
+        assertThat(new ComputerPlayer("computer", null).isHuman()).isFalse();
+    }
+}
\ No newline at end of file
diff --git a/app/src/test/java/model/FloodGameTest.java b/app/src/test/java/model/FloodGameTest.java
index 3bdaad5..f8587ad 100644
--- a/app/src/test/java/model/FloodGameTest.java
+++ b/app/src/test/java/model/FloodGameTest.java
@@ -1,93 +1,93 @@
-//package model;
-//
-//import javafx.scene.paint.Color;
-//import org.junit.jupiter.api.BeforeEach;
-//import org.junit.jupiter.api.Test;
-//
-//import static javafx.scene.paint.Color.*;
-//import static org.assertj.core.api.Assertions.assertThat;
-//
-//class FloodGameTest {
-//
-//    private final int totalNumberOfCells = 6;
-//    private final Grid gridTwoThree = new ArrayGrid(2,3);
-//    private final Color colorONE = RED;
-//    private final Color colorTWO = BLUE;
-//    private FloodGame game;
-//    private final Player playerONE = new ComputerPlayer("player1", gridTwoThree.getCell(0, 0), startCell -> colorONE);
-//    private final Player playerTWO = new ComputerPlayer("player2", gridTwoThree.getCell(1, 2), startCell -> colorTWO);
-//
-//    @BeforeEach
-//    private void initGame(){
-//        game = new FloodGame(totalNumberOfCells);
-//    }
-//
-//
-//    @Test
-//    void testSetTurnAndGetTurn() {
-//        assertThat(game.getTurn()).isEqualTo(0);
-//        game.setTurn(100);
-//        assertThat(game.getTurn()).isEqualTo(100);
-//    }
-//
-//
-//
-//
-//    @Test
-//    void testResetTurn() {
-//        game.setTurn(100);
-//        game.resetTurn();
-//        assertThat(game.getTurn()).isEqualTo(0);
-//    }
-//
-//
-//    @Test
-//    void testIncrementTurn() {
-//        game.incrementTurn();
-//        game.incrementTurn();
-//        assertThat(game.getTurn()).isEqualTo(2);
-//    }
-//
-//    @Test
-//    void testGetPlayer() {
-//        game.setPlayer(playerONE);
-//        game.setPlayer(playerTWO);
-//        assertThat(game.getPlayer()).isEqualTo(playerTWO);
-//        game.incrementTurn();
-//        assertThat(game.getPlayer()).isEqualTo(playerTWO);
-//    }
-//
-//    @Test
-//    void testIsHumanTurn() {
-//        game.setPlayer(new HumanPlayer("human",gridTwoThree.getCell(1,2)));
-//        assertThat(game.isHumanTurn()).isTrue();
-//        game.setPlayer(playerONE);
-//        assertThat(game.isHumanTurn()).isFalse();
-//    }
-//
-//    private void fillGridYellow(Grid grid){
-//        for(Cell cell: grid)
-//            cell.setColor(YELLOW);
-//    }
-//
-//    @Test
-//    void testHasWon() {
-//        game.setPlayer(playerONE);
-//        gridTwoThree.getCell(0,0).setColor(RED);
-//        gridTwoThree.getCell(1,1).setColor(BLUE);
-//        assertThat(game.hasWon(game.getPlayer())).isFalse();
-//        fillGridYellow(gridTwoThree);
-//        assertThat(game.hasWon(game.getPlayer())).isTrue();
-//    }
-//
-//    @Test
-//    void testHasEnded() {
-//        game.setPlayer(playerONE);
-//        gridTwoThree.getCell(1,2).setColor(RED);
-//        gridTwoThree.getCell(0,1).setColor(YELLOW);
-//        assertThat(game.hasEnded()).isFalse();
-//        fillGridYellow(gridTwoThree);
-//        assertThat(game.hasEnded()).isTrue();
-//    }
-//
-//}
\ 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 static javafx.scene.paint.Color.*;
+import static org.assertj.core.api.Assertions.assertThat;
+
+class FloodGameTest {
+
+    private final int totalNumberOfCells = 6;
+    private final Grid gridTwoThree = new ArrayGrid(2,3);
+    private final Color colorONE = RED;
+    private final Color colorTWO = BLUE;
+    private FloodGame game;
+    private final Player playerONE = new ComputerPlayer("player1", gridTwoThree.getCell(0, 0), startCell -> colorONE);
+    private final Player playerTWO = new ComputerPlayer("player2", gridTwoThree.getCell(1, 2), startCell -> colorTWO);
+
+    @BeforeEach
+    private void initGame(){
+        game = new FloodGame(totalNumberOfCells);
+    }
+
+
+    @Test
+    void testSetTurnAndGetTurn() {
+        assertThat(game.getTurn()).isEqualTo(0);
+        game.setTurn(100);
+        assertThat(game.getTurn()).isEqualTo(100);
+    }
+
+
+
+
+    @Test
+    void testResetTurn() {
+        game.setTurn(100);
+        game.resetTurn();
+        assertThat(game.getTurn()).isEqualTo(0);
+    }
+
+
+    @Test
+    void testIncrementTurn() {
+        game.incrementTurn();
+        game.incrementTurn();
+        assertThat(game.getTurn()).isEqualTo(2);
+    }
+
+    @Test
+    void testGetPlayer() {
+        game.setPlayer(playerONE);
+        game.setPlayer(playerTWO);
+        assertThat(game.getPlayer()).isEqualTo(playerTWO);
+        game.incrementTurn();
+        assertThat(game.getPlayer()).isEqualTo(playerTWO);
+    }
+
+    @Test
+    void testIsHumanTurn() {
+        game.setPlayer(new HumanPlayer("human",gridTwoThree.getCell(1,2)));
+        assertThat(game.isHumanTurn()).isTrue();
+        game.setPlayer(playerONE);
+        assertThat(game.isHumanTurn()).isFalse();
+    }
+
+    private void fillGridYellow(Grid grid){
+        for(Cell cell: grid)
+            cell.setColor(YELLOW);
+    }
+
+    @Test
+    void testHasWon() {
+        game.setPlayer(playerONE);
+        gridTwoThree.getCell(0,0).setColor(RED);
+        gridTwoThree.getCell(1,1).setColor(BLUE);
+        assertThat(game.hasWon(game.getPlayer())).isFalse();
+        fillGridYellow(gridTwoThree);
+        assertThat(game.hasWon(game.getPlayer())).isTrue();
+    }
+
+    @Test
+    void testHasEnded() {
+        game.setPlayer(playerONE);
+        gridTwoThree.getCell(1,2).setColor(RED);
+        gridTwoThree.getCell(0,1).setColor(YELLOW);
+        assertThat(game.hasEnded()).isFalse();
+        fillGridYellow(gridTwoThree);
+        assertThat(game.hasEnded()).isTrue();
+    }
+
+}
\ No newline at end of file
-- 
GitLab