Skip to content
Snippets Groups Projects
Commit dd702ad4 authored by NGUYEN Thi hang's avatar NGUYEN Thi hang
Browse files

THN

parent f1908b4d
No related branches found
No related tags found
No related merge requests found
...@@ -152,13 +152,15 @@ public class GameController { ...@@ -152,13 +152,15 @@ public class GameController {
public void setPlayerRandom() { public void setPlayerRandom() {
// TODO // TODO
//Player player = ... instantiate a ComputerPlayer named "random" that follows the random strategy //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 @FXML
public void setPlayerRobin() { public void setPlayerRobin() {
// TODO // TODO
// Player player = ... instantiate a ComputerPlayer named "cyclic" that follows the cyclic strategy // Player player = ... instantiate a ComputerPlayer named "cyclic" that follows the cyclic strategy
// game.setPlayer(player); // game.setPlayer(player);
Player player=new RobinStrategy()
} }
@FXML @FXML
......
...@@ -9,6 +9,7 @@ public class ArrayGrid implements Grid{ ...@@ -9,6 +9,7 @@ public class ArrayGrid implements Grid{
private Cell[][] cells; private Cell[][] cells;
private int numberOfRows; private int numberOfRows;
private int numberOfColumns; private int numberOfColumns;
public ArrayGrid(int numberOfRows, int numberOfColumns){ public ArrayGrid(int numberOfRows, int numberOfColumns){
if (numberOfRows <= 0 || numberOfColumns <= 0){ if (numberOfRows <= 0 || numberOfColumns <= 0){
throw new IllegalArgumentException();} throw new IllegalArgumentException();}
...@@ -19,58 +20,28 @@ public class ArrayGrid implements Grid{ ...@@ -19,58 +20,28 @@ public class ArrayGrid implements Grid{
for (int i = 0; i < numberOfRows; i++) { for (int i = 0; i < numberOfRows; i++) {
for (int j = 0; j < numberOfColumns; j++) { for (int j = 0; j < numberOfColumns; j++) {
cells[i][j] = new SquareCell(); cells[i][j] = new SquareCell();
}
}
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) { int mx[] = {0, 0, -1, 1};
this.getCell(i,i).getNeighbours().add(this.cells[i - 1][j]); int my[] = {-1, 1, 0, 0};
this.getCell(i,i).getNeighbours().add(this.cells[i][j + 1]); for (int rowIndex= 0; rowIndex < numberOfRows ; rowIndex++) {
} for (int columnIndex = 0; columnIndex < numberOfColumns; columnIndex++) {
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 @Override
public Cell getCell(int row, int column) { public Cell getCell(int row, int column) {
return this.cells[row][column]; return this.cells[row][column];
......
...@@ -32,7 +32,7 @@ public class ColoredCellIterator implements Iterator<Cell> { ...@@ -32,7 +32,7 @@ public class ColoredCellIterator implements Iterator<Cell> {
Cell pendingElement=SetUtil.anyElement(pendingCells); Cell pendingElement=SetUtil.anyElement(pendingCells);
for(Cell cell: pendingElement.getNeighbours()){ for(Cell cell: pendingElement.getNeighbours()){
if( cell.getColor()==this.color&& !(visitedCells.contains(pendingElement))) if( cell.getColor()==this.color&& !(visitedCells.contains(cell)))
pendingCells.add(cell); pendingCells.add(cell);
} }
......
...@@ -44,10 +44,10 @@ public class ComputerPlayer implements Player{ ...@@ -44,10 +44,10 @@ public class ComputerPlayer implements Player{
public Color play(){ public Color play(){
return this.strategi.play(this.getStartCell());// Color.RED; return this.strategi.play(this.getStartCell());// Color.RED;
} }
public Color setStrategy(PlayStrategy strategy){ // public Color setStrategy(PlayStrategy strategy){
this.strategi=strategy; // this.strategi=strategy;
return this.play(); // return this.play();
} // }
public void setStartCell(Cell start){ public void setStartCell(Cell start){
this.cellStart=start; this.cellStart=start;
} }
......
...@@ -15,7 +15,7 @@ public class DistinctColorGenerator implements ColorGenerator { ...@@ -15,7 +15,7 @@ public class DistinctColorGenerator implements ColorGenerator {
public Color nextColor(Cell cell ) { public Color nextColor(Cell cell ) {
List<Cell> neighbors = new ArrayList<>(); List<Cell> neighbors = cell.getNeighbours();
//System.out.println(neighbors.size()); //System.out.println(neighbors.size());
...@@ -25,13 +25,21 @@ public class DistinctColorGenerator implements ColorGenerator { ...@@ -25,13 +25,21 @@ public class DistinctColorGenerator implements ColorGenerator {
for (Cell c : neighbors) { for (Cell c : neighbors) {
neighbor_colors.add(c.getColor()); neighbor_colors.add(c.getColor());
} }
for (int i = 0; i < colors.size(); i++) { int i = 0;
for (int j = 0; j < neighbor_colors.size(); j++) { for (; i < colors.size(); i++) {
if (colors.get(i)==neighbor_colors.get(j)) if (neighbor_colors.contains(colors.get(i)) == false){
break;//if (!(neighbor_colors.contains(colors.get(i)))){ 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; return defaultColor;
......
...@@ -56,7 +56,7 @@ public class FloodGame { ...@@ -56,7 +56,7 @@ public class FloodGame {
} }
public boolean hasWon(Player player){ public boolean hasWon(Player player){
return this.getPlayerScore(player)==this.totalFloodingArea&& this.getTurn()<26; return this.getPlayerScore(player)==this.totalFloodingArea;
// TODO // TODO
} }
......
...@@ -50,8 +50,6 @@ public class SquareCell extends AbstractCell{ ...@@ -50,8 +50,6 @@ public class SquareCell extends AbstractCell{
public void setNeighbours(List<Cell> cells) { public void setNeighbours(List<Cell> cells) {
this.neighbours=cells; this.neighbours=cells;
} }
@Override @Override
......
...@@ -66,7 +66,7 @@ class ArrayGridTest { ...@@ -66,7 +66,7 @@ class ArrayGridTest {
@Test @Test
void testColor() { void testColor() {
setArrayGridThreeFourRed(); setArrayGridThreeFourRed();
arrayGridThreeFour.color(cell -> Color.GREEN); arrayGridThreeFour.color(cell -> Color.BLACK);
for (int rowIndex = 0; rowIndex < 3; rowIndex++) { for (int rowIndex = 0; rowIndex < 3; rowIndex++) {
for (int columnIndex = 0; columnIndex < 4; columnIndex++) { for (int columnIndex = 0; columnIndex < 4; columnIndex++) {
assertThat(arrayGridThreeFour.getCell(rowIndex,columnIndex).getColor()).isEqualTo(Color.BLACK); assertThat(arrayGridThreeFour.getCell(rowIndex,columnIndex).getColor()).isEqualTo(Color.BLACK);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment