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

THN

parent 976240d4
No related branches found
No related tags found
No related merge requests found
Showing with 198 additions and 51 deletions
......@@ -21,25 +21,53 @@ public class ArrayGrid implements Grid{
cells[i][j] = new SquareCell();
}
}
}
for (int i = 0; i < this.numberOfRows; i++) {
for (int j = 0; j < this.numberOfColumns; j++) {
List<Cell> neighbours= new ArrayList<Cell>();
if (i==0)
neighbours.add(getCell(i+1,j));
if (i==numberOfRows-1)
neighbours.add(getCell(i-1,j));
if (j==0)
neighbours.add(getCell(i,j+1));
if (j==numberOfColumns-1)
neighbours.add(getCell(i, j-1));
else {
neighbours.add(getCell(i - 1, j));
neighbours.add(getCell(i, j - 1));
neighbours.add(getCell(i + 1, j));
neighbours.add(getCell(i, j + 1));
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]);
}
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]);
}
}
getCell(i,j).setNeighbours(neighbours);
}
}
}
......
......@@ -10,8 +10,8 @@ import java.util.*;
public class ColoredCellIterator implements Iterator<Cell> {
private final Color color;
Set<Cell> visitedCells;
Set<Cell> pendingCells=new HashSet<Cell>();
public Set<Cell> visitedCells;
public Set<Cell> pendingCells=new HashSet<Cell>();
public ColoredCellIterator(Cell startCell){
......
package model;
import com.sun.prism.paint.Color;
import javafx.scene.paint.Color;
public class ComputerPlayer implements Player{
//PlayStrate sdfdsfdsfm
private String name;
private Cell cellStart;
private PlayStrategy strategi;
public ComputerPlayer(String name, Cell start){
this.name=name;
this.cellStart=start;
}
public ComputerPlayer(Cell start, PlayStrategy strategi){
this.name="player";
this.cellStart=start;
this.strategi=strategi;
}
public ComputerPlayer(String name, Cell start,PlayStrategy strategi){
this.name=name;
this.cellStart=start;
this.strategi= strategi;
}
@Override
public boolean isHuman() {
return false;
}
@Override
public int coloredArea(Cell startCell) {
return 0;
public void setName(String name) {
this.name = name;
}
@Override
public String getName() {
return null;
return this.name;
}
@Override
public Cell getStartCell() {
return null;
return this.cellStart;
}
public Color play(){
return this.strategi.play(this.getStartCell());// Color.RED;
}
public Color setStrategy(PlayStrategy strategy){
this.strategi=strategy;
return this.play();
}
public javafx.scene.paint.Color play(){
return null ;// Color.RED;
public void setStartCell(Cell start){
this.cellStart=start;
}
}
......@@ -6,13 +6,13 @@ import javafx.scene.paint.Color;
import java.util.List;
public class CyclicColorGenerator implements ColorGenerator {
List<Color> colors;
private final List<Color> colors;
private int count;
public CyclicColorGenerator(List<Color> colors){
this.colors=colors;
this.count=0;
}
int count=0;
@Override
......
package model;
import javafx.scene.paint.Color;
import java.util.List;
public class CyclicStrategy implements PlayStrategy{
private final List<Color>colors;
private int count;
public CyclicStrategy(List<Color> colors){
this.colors=colors;
this.count=0;
}
@Override
public Color play(Cell startCell) {
int color_index = count % this.colors.size();
count++;
return this.colors.get(color_index);
}
}
......@@ -50,17 +50,19 @@ public class FloodGame {
public int getPlayerScore(Player player) {
//todo
return this.getPlayerScore(player);
if (player.getStartCell().getColor() == AbstractCell.DEFAULT_CELL_COLOR)
return 0;
return Flooder.coloredArea(player.getStartCell());
}
public boolean hasWon(Player player){
return this.getPlayerScore(player)==this.totalFloodingArea;
return this.getPlayerScore(player)==this.totalFloodingArea&& this.getTurn()<26;
// TODO
}
public boolean hasEnded(){
// TODO
return this.getTurn()>=25;
return (this.getTurn()>=25 || hasWon(player));
}
}
......@@ -2,13 +2,25 @@ package model;
import javafx.scene.paint.Color;
public class Flooder {
import java.util.ArrayList;
import java.util.List;
public class Flooder {
public static void flood(Cell startCell, Color floodingColor){
ColoredCellIterator start= new ColoredCellIterator(startCell);
while(start.hasNext()){
start.next().setColor(floodingColor);
}
}
public static int coloredArea(Cell startCell){
return 0;
ColoredCellIterator start= new ColoredCellIterator(startCell);
int count=0;
while(start.hasNext()) {
start.next();
count++;
}
return count;
}
}
package model;
import javafx.scene.paint.Color;
public class GreedyStrategy implements PlayStrategy{
@Override
public Color play(Cell startCell) {
return null;
}
}
......@@ -14,21 +14,20 @@ public class HumanPlayer implements Player{
}
@Override
public boolean isHuman() {
return false;
return true;
}
@Override
public int coloredArea(Cell startCell) {
return 0;
public void setName(String name) {
this.name=name;
}
@Override
public String getName() {
return null;
return this.name;
}
@Override
public Cell getStartCell() {
return null;
return this.startCell;
}
}
......@@ -2,7 +2,7 @@ package model;
public interface Player {
boolean isHuman();
int coloredArea(Cell startCell);
void setName(String name);
String getName();
Cell getStartCell();
......
package model;
import javafx.scene.paint.Color;
import java.awt.*;
import java.util.Random;
import java.util.List;
import util.RandomUtil;
public class RandomStrategy implements PlayStrategy{
private final List<Color> colors;
private Random randomColor;
public RandomStrategy(List<Color>colors,Random randomColor){
this.colors=colors;
this.randomColor=randomColor;
}
@Override
public Color play(Cell startCell) {
return RandomUtil.randomElement(this.colors, this.randomColor);
}
}
package model;
import javafx.scene.paint.Color;
import util.RandomUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class RandomWalk implements PlayStrategy{
private List<Color> colors;
private Random randomColor;
private Cell cellStart;
public RandomWalk(List<Color> colors, Random randomColor){
this.colors=colors;
this.randomColor=randomColor ;
}
@Override
public Color play(Cell startCell) {
List<Color>colorNeighbor=new ArrayList<Color>();
for(Cell cell:startCell.getNeighbours())
colorNeighbor.add(cell.getColor());
if (colorNeighbor.contains(startCell.getColor())){
colorNeighbor.remove(startCell.getColor());}
return RandomUtil.randomElement(colorNeighbor,this.randomColor);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment