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

THN

parent e5997ace
Branches
No related tags found
No related merge requests found
......@@ -8,19 +8,22 @@ import javafx.util.Duration;
import model.*;
import view.MatrixPane;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class GameController {
public static final Color COLOR_ONE = Color.RED;
public static final Color COLOR_TWO = Color.BLUE;
public static final Color COLOR_THREE = Color.YELLOW;
public static final Color COLOR_ONE = Color.TAN;
public static final Color COLOR_TWO = Color.NAVAJOWHITE;
public static final Color COLOR_THREE = Color.GRAY;
public static final Color COLOR_FOUR = Color.GREEN;
private static final List<Color> availableColors = List.of(COLOR_ONE, COLOR_TWO, COLOR_THREE, COLOR_FOUR);
public static final double PAUSE_MILLISECONDS = 400;
private final PauseTransition pause = new PauseTransition(Duration.millis(PAUSE_MILLISECONDS));
private final Random random = new Random();
@FXML
......@@ -36,8 +39,7 @@ public class GameController {
public void initialize(){
this.game = new FloodGame(
matrixPane.getGrid().getNumberOfColumns() * matrixPane.getGrid().getNumberOfRows());
this.game = new FloodGame(matrixPane.getGrid().getNumberOfColumns() * matrixPane.getGrid().getNumberOfRows());
setPlayerHuman();
matrixPane.setGameController(this);
setScoreLabelTextProperty();
......@@ -56,39 +58,37 @@ public class GameController {
private void colorGrid(ColorGenerator colorGenerator){
// TODO
// matrixPane.getGrid().color(colorGenerator);
matrixPane.getGrid().color(colorGenerator);
}
@FXML
public void fillGridUniform() {
// TODO uncomment:
// colorGrid(new UniformColorGenerator(COLOR_ONE));
colorGrid(new UniformColorGenerator(COLOR_ONE));
}
@FXML
public void fillGridRandom() {
// TODO uncomment
// colorGrid(new RandomColorGenerator(availableColors,random));
colorGrid(new RandomColorGenerator(availableColors,random));
}
@FXML
public void fillGridDistinct() {
// TODO uncomment
// fillGridUniform();
// colorGrid(new DistinctColorGenerator(COLOR_ONE,List.of(COLOR_THREE, COLOR_FOUR)));
fillGridUniform();
colorGrid( new DistinctColorGenerator(List.of(COLOR_THREE, COLOR_FOUR), COLOR_ONE));//List.of(COLOR_THREE, COLOR_FOUR)));
}
@FXML
public void fillGridCycle() {
// TODO uncomment
// colorGrid(new CyclingColorGenerator(availableColors));
colorGrid(new CyclicColorGenerator(availableColors));
}
@FXML
public void fillGridUniformExceptOne() {
// TODO uncomment
// colorGrid(new UniformExceptOneColorGenerator(COLOR_ONE,COLOR_TWO));
colorGrid(new UniformExceptOneGenerator(COLOR_ONE,COLOR_TWO));
}
private void playComputerTurn(){
......
......@@ -8,9 +8,16 @@ public class ArrayGrid implements Grid{
private int numberOfRows;
private int numberOfColumns;
public ArrayGrid(int numberOfRows, int numberOfColumns){
this.cells=new SquareCell[numberOfRows][numberOfColumns];
if (numberOfRows <= 0 || numberOfColumns <= 0)
throw new IllegalArgumentException();
this.cells=new SquareCell[numberOfRows][numberOfColumns];
for (int i=0; i< numberOfRows;i++){
for (int j=0; j < numberOfColumns; j++){
cells[i][j] = new SquareCell();
}
}
}
@Override
public Cell getCell(int row, int column) {
......
package model;
public class CellGridIterator {
import java.util.Iterator;
public class CellGridIterator implements Iterator<Cell> {
ArrayGrid grid;
int indixRow;
int indixColum;
......@@ -12,9 +14,12 @@ public class CellGridIterator {
}
public Cell next() {
final Cell result = grid.getCell(indixRow, indixColum);
indixColum = (indixColum +1) % grid.getNumberOfColumns();
if(indixColum == 0){
indixRow++;
this.indixColum = this.indixColum +1;
if (this.indixColum == this.grid.getNumberOfColumns()) {
this.indixRow = this.indixRow + 1;
this.indixColum = 0;
}
return result;
......@@ -23,4 +28,6 @@ public class CellGridIterator {
public boolean hasNext(){
return this.indixColum < this.grid.getNumberOfColumns() && indixRow < this.grid.getNumberOfRows();
}
}
......@@ -17,9 +17,10 @@ public class CyclicColorGenerator implements ColorGenerator {
@Override
public Color nextColor(Cell cell) {
int color_index = count % this.colors.size();
count++;
return this.colors.get(count-1);
return this.colors.get(color_index);}
}
}
package model;
import java.awt.*;
import java.util.ArrayList;
import javafx.scene.paint.Color;
import java.util.List;
public class DistinctColorGenerator {
List<Color> colors;
Color defaultColor;
public class DistinctColorGenerator implements ColorGenerator {
private List<Color> colors;
private Color defaultColor;
public DistinctColorGenerator(List<Color> colors, Color defaultColor){
this.colors=colors;
this.defaultColor=defaultColor;
......@@ -14,11 +14,20 @@ public class DistinctColorGenerator {
}
public Color nextColor(Cell cell ){
List<Cell> neighbors = cell.getNeighbours();
List<Color> neighbor_colors = new ArrayList<Color>();
for(Cell c : neighbors){
neighbor_colors.add(c.getColor());
}
for(int i=0; i < colors.size(); i++){
if (colors.get(i)==cell){
if (!neighbor_colors.contains(colors.get(i))){
return colors.get(i);
}
}
return defaultColor;
}
}
......@@ -2,6 +2,7 @@ package model;
import javafx.scene.paint.Color;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
......@@ -11,15 +12,19 @@ public class SquareCell extends AbstractCell{
public SquareCell (){
Cell DEFAULT_CELL_COLOR;
this.setColor( DEFAULT_CELL_COLOR);
this.neighbours=new ArrayList<Cell>();
}
public SquareCell(Color color){
this.setColor(color);
this.neighbours=new ArrayList<Cell>();
}
public SquareCell(Color color,List<Cell> neighbours ){
this.setColor(color);
this.neighbours= neighbours;
}
......
......@@ -66,7 +66,7 @@ class ArrayGridTest {
@Test
void testColor() {
setArrayGridThreeFourRed();
arrayGridThreeFour.color(cell -> Color.BLACK);
arrayGridThreeFour.color(cell -> Color.GREEN);
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