diff --git a/app/src/main/java/controller/GameController.java b/app/src/main/java/controller/GameController.java index 51031861d328f1fbebc26f9a6d8836bd73f81de1..cc3ab0dfc6044e8441a83fa1ecab5ce6620db5f5 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 0f5904bb4541f97a6e7a5e69f072538461fb67ad..da968c26f993b4140c3366cc1509e25f2f0a71b0 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 6afd3776c1af59626eea4a9461d0f72fbdb07a09..7b81a467e81178538db6673020eade2e2f6f37a4 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 fd7ac9f22d840be221d6e0e05d311bf1f7a530ba..0e0a4dd65195c58aca1b7a67e5fd7de343faf203 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 a9a72ef8e3997eb45a461e023fbfca053821d11d..4c9c488ee068b3b37b6d9eeb370fcd4964badc5e 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 317b3ecfedd6598b1bb3e87c55697601aaade44b..7a93e1c8979147bc7a48c6f680f53a01afe0d178 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 617bb82c197a524850a82d7ea88ad47eba356f3a..2b1e4b5077f4c63cd082cf9205d74dedfc121b17 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 9e2c352ecf5a4c31c711ca4e2469c0d76f343a7c..91d1b9f982349973bec57b8c30e27f1fdc421a7b 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);