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

THN

parent 40e52b8e
Branches
No related tags found
No related merge requests found
Showing with 122 additions and 20 deletions
package model; package model;
import java.util.Iterator;
public class ArrayGrid implements Grid{ public class ArrayGrid implements Grid{
private Cell[][] cells; private Cell[][] cells;
...@@ -33,6 +35,6 @@ public class ArrayGrid implements Grid{ ...@@ -33,6 +35,6 @@ public class ArrayGrid implements Grid{
} }
} }
public Iterator<Cell> iterator(){ public Iterator<Cell> iterator(){
return new CellGridIterator(this); return (Iterator<Cell>) new CellGridIterator(this);
} }
} }
package model; package model;
public class CellGridIterator implements Iterator<Cell>{ public class CellGridIterator {
ArrayGrid grid; ArrayGrid grid;
int indixRow; int indixRow;
int indixColum; int indixColum;
......
package model;
import java.awt.*;
import javafx.scene.paint.Color;
import java.util.List;
public class CyclicColorGenerator implements ColorGenerator {
List<Color> colors;
public CyclicColorGenerator(List<Color> colors){
this.colors=colors;
}
int count=0;
@Override
public Color nextColor(Cell cell) {
count++;
return this.colors.get(count-1);
}
}
package model; package model;
import java.util.Iterator;
public class GrayGrid implements Grid{ public class GrayGrid implements Grid{
private final int numberOfRows; private final int numberOfRows;
...@@ -40,4 +42,14 @@ public class GrayGrid implements Grid{ ...@@ -40,4 +42,14 @@ public class GrayGrid implements Grid{
public int getNumberOfColumns() { public int getNumberOfColumns() {
return numnberOfColumns; return numnberOfColumns;
} }
@Override
public void color(ColorGenerator colorGenerator) {
}
@Override
public Iterator<Cell> iterator() {
return null;
}
} }
...@@ -27,5 +27,6 @@ public interface Grid extends Iterable<Cell> { ...@@ -27,5 +27,6 @@ public interface Grid extends Iterable<Cell> {
* @param colorGenerator the generator used to determine the color of each cell. * @param colorGenerator the generator used to determine the color of each cell.
* The new color of {@code cell} is obtained by calling the method {@code nextColor} * The new color of {@code cell} is obtained by calling the method {@code nextColor}
*/ */
void color(ColorGenerator colorGenerator);
} }
package model;
public interface Iterator<Cell> {
}
package model;
import javafx.scene.paint.Color;
import util.RandomUtil;
import java.util.List;
import java.util.Random;
public class RandomColorGenerator implements ColorGenerator{
List<Color> colors;
Random radomGenerator;
public RandomColorGenerator(List<Color>colors,Random radomGenerator){
this.colors=colors;
this.radomGenerator=radomGenerator;
}
@Override
public Color nextColor(Cell cell) {
return RandomUtil.randomElement(this.colors, this.radomGenerator);
}
}
...@@ -9,17 +9,6 @@ public class SquareCell extends AbstractCell{ ...@@ -9,17 +9,6 @@ public class SquareCell extends AbstractCell{
private List<Cell> neighbours; private List<Cell> neighbours;
public SquareCell (){
Cell DEFAULT_CELL_COLOR;
}
public SquareCell(Color color){
this.setColor(color);
}
public SquareCell(Color color,List<Cell> neighbours ){
this.setColor(color);
this.neighbours= neighbours;
}
public SquareCell (){ public SquareCell (){
Cell DEFAULT_CELL_COLOR; Cell DEFAULT_CELL_COLOR;
......
package model;
import javafx.scene.paint.Color;
public class UniformColorGenerator implements ColorGenerator{
Color color;
public UniformColorGenerator(Color color){
this.color=color;
}
@Override
public Color nextColor(Cell cell) {
return this.color;
}
}
package model;
import javafx.scene.paint.Color;
public class UniformExceptOneGenerator implements ColorGenerator{
Color uniformColor;
Color exceptionColor;
public UniformExceptOneGenerator(Color uniformColor, Color exceptionColor){
this.uniformColor=uniformColor;
this.exceptionColor=exceptionColor;
}
int count=0;
public Color nextColor(Cell cell) {
count++;
if(count==2){
return this.exceptionColor;
}
return this.uniformColor;
}
}
package util;
import java.util.List;
import java.util.Random;
public class RandomUtil {
private RandomUtil(){};
public static <T> T randomElement(T[] elements, Random random){
return elements[random.nextInt(elements.length)];
}
public static <T> T randomElement(List<T> elements, Random random){
return elements.get(random.nextInt(elements.size()));
}
}
...@@ -8,6 +8,7 @@ import javafx.scene.layout.GridPane; ...@@ -8,6 +8,7 @@ import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle; import javafx.scene.shape.Rectangle;
import javafx.util.Duration; import javafx.util.Duration;
import model.ArrayGrid;
import model.Cell; import model.Cell;
import model.GrayGrid; import model.GrayGrid;
import model.Grid; import model.Grid;
...@@ -29,7 +30,7 @@ public class MatrixPane extends GridPane { ...@@ -29,7 +30,7 @@ public class MatrixPane extends GridPane {
this.numberOfColumns = numberOfColumns; this.numberOfColumns = numberOfColumns;
this.numberOfRows = numberOfRows; this.numberOfRows = numberOfRows;
// TODO replace by new ArrayGrid(numberOfRows, numberOfColumns) // TODO replace by new ArrayGrid(numberOfRows, numberOfColumns)
cellGrid = new GrayGrid(numberOfRows, numberOfColumns); cellGrid = new ArrayGrid(numberOfRows, numberOfColumns);
initMatrix(); initMatrix();
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment