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 {
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
......
......@@ -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();
}
}
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]);
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++) {
// 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];
......
......@@ -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);
}
......
......@@ -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;
}
......
......@@ -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;
......
......@@ -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
}
......
......@@ -50,8 +50,6 @@ public class SquareCell extends AbstractCell{
public void setNeighbours(List<Cell> cells) {
this.neighbours=cells;
}
@Override
......
......@@ -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);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment