Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
Loading items

Target

Select target project
  • travers.c/flooding-template
  • g20031427/flooding-template
  • p20006624/flooding-template
  • b21221851/tp-6
  • n19028187/flooding-template
  • e21221636/tp-6-flood
  • saddem.r/flooding-template
  • r20029383/flooding-template
  • s21226517/flooding-template
  • r20004714/flooding-template
  • e20026270/flooding-template-el-ghaouti-aymane
  • o21205600/flooding-template
12 results
Select Git revision
Loading items
Show changes
Commits on Source (38)
Showing
with 1437 additions and 50 deletions
......@@ -6,5 +6,5 @@ Il s'agit d'implémenter une version du jeu "inondation" (voir par exemple [ici]
## Membre du projet
- NOM, prénom
- EL GHAOUTI , Aymane
- NOM, prénom
This diff is collapsed.
......@@ -7,15 +7,17 @@ import javafx.scene.paint.Color;
import javafx.util.Duration;
import model.*;
import view.MatrixPane;
import model.UniformExceptOneGenerator;
import model.CyclicColorGenerator;
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_FOUR = Color.GREEN;
public static final Color COLOR_ONE = Color.ALICEBLUE;// Aymane
public static final Color COLOR_TWO = Color.DODGERBLUE;// EL GHAOUTI
public static final Color COLOR_THREE = Color.LEMONCHIFFON; // lamine
public static final Color COLOR_FOUR = Color.PALEVIOLETRED; // Zeghar
private static final List<Color> availableColors = List.of(COLOR_ONE, COLOR_TWO, COLOR_THREE, COLOR_FOUR);
public static final double PAUSE_MILLISECONDS = 400;
......@@ -55,40 +57,34 @@ 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));
}
@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(){
......
package model;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class ArrayGrid implements Grid{
protected Cell [][] cells;
private int numberOfRows;
private int numberOfColumns;
public ArrayGrid(int numberOfRows, int numberOfColumns) throws Exception{
if(numberOfRows <= 0 || numberOfColumns <= 0){ throw new IllegalArgumentException("taille nulle ou négative");}
this.numberOfRows = numberOfRows;
this.numberOfColumns = numberOfColumns;
this.cells = new Cell[numberOfRows][numberOfColumns];
for (int i = 0; i < numberOfRows; i++){
for (int j = 0; j < numberOfColumns; j++){
cells[i][j] = new SquareCell();
}
}
for (int i = 0; i < numberOfRows; i++) {
for (int j = 0; j < numberOfColumns; j++) {
calculateNeighbours(i,j);
}
}
}
public void calculateNeighbours( int rowIndex, int columnIndex){
List<Cell> neighbours = new ArrayList<>();
if (rowIndex==0){ neighbours.add(getCell(rowIndex+1,columnIndex));}
else if (rowIndex<this.getNumberOfRows()-1) {
neighbours.add(getCell(rowIndex-1,columnIndex));
neighbours.add(getCell(rowIndex+1,columnIndex));
}
if (rowIndex==this.getNumberOfRows()-1){ neighbours.add(getCell(rowIndex-1,columnIndex)); }
if (columnIndex==0){ neighbours.add(getCell(rowIndex,columnIndex+1));}
else if (columnIndex<this.getNumberOfColumns()-1 ) {
neighbours.add(getCell(rowIndex,columnIndex-1));
neighbours.add(getCell(rowIndex,columnIndex+1));
}
if (columnIndex==this.getNumberOfColumns()-1){ neighbours.add(getCell(rowIndex,columnIndex-1));}
getCell(rowIndex,columnIndex).setNeighbours(neighbours);
}
@Override
public Cell getCell(int row, int column) {
return this.cells[row][column];
}
@Override
public int getNumberOfRows() {
return this.numberOfRows;
}
@Override
public int getNumberOfColumns() {
return this.numberOfColumns;
}
@Override
public void color(ColorGenerator colorGenerator) {
// for(Cell []cell1D:cells){
// for(Cell cell:cell1D){
// cell.setColor(colorGenerator.nextColor(cell));
// }
// }
for(Cell cell:this){
cell.setColor(colorGenerator.nextColor(cell));
}
}
@Override
public Iterator<Cell> iterator() {
return new CellGridIterator(this);
}
}
......@@ -41,6 +41,6 @@ public interface Cell {
* @return this {@link Cell}'s property
*/
Property<Color> getColorProperty();
Iterator<Cell> iterator();
}
package model;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class CellGridIterator implements Iterator<Cell> {
ArrayGrid arrayGrid;
private int rowIndex,columnIndex;
private Cell [][]cells;
public CellGridIterator(ArrayGrid grid) {
arrayGrid=grid;
cells=grid.cells;
rowIndex =0;
columnIndex=0;
}
@Override
public boolean hasNext() {
if(rowIndex+1== cells.length){
return columnIndex<cells[rowIndex].length;
}
return rowIndex< cells.length;
}
@Override
public Cell next() {
if(columnIndex==cells[rowIndex].length)
{
columnIndex=0;
rowIndex++;
}
if(rowIndex== cells.length&& columnIndex==cells[rowIndex].length){
throw new NoSuchElementException();
}
return cells[rowIndex][columnIndex++];
}
}
package model;
import javafx.scene.paint.Color;
import util.SetUtil;
import java.awt.*;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class ColoredCellIterator implements Iterator<Cell> {
private Color color;
private Set<Cell> visitedCells;
private Set<Cell> pendingCells;
private Cell startCell;
public ColoredCellIterator(Cell startCell){
this.startCell=startCell;
visitedCells=new HashSet<>();
this.pendingCells=new HashSet<>();
this.pendingCells.add(startCell);
this.color = startCell.getColor();
}
@Override
public boolean hasNext() {
boolean expression=!(this.pendingCells.isEmpty());
return expression;
}
@Override
public Cell next() {
Cell Cell = SetUtil.anyElement(this.pendingCells);
this.pendingCells.remove(Cell);
this.visitedCells.add(Cell);
for (Cell neighbour : Cell.getNeighbours()){
if (Cell.getColor() == neighbour.getColor() && !(this.visitedCells.contains(neighbour))){
this.pendingCells.add(neighbour);
}
}
return Cell; }
}
package model;
import javafx.scene.paint.Color;
import java.util.List;
public class CyclicColorGenerator implements ColorGenerator{
private List<Color> colors;
private int counter=1;
public CyclicColorGenerator(List<Color> colors){
this.colors = colors;
}
@Override
public Color nextColor(Cell cell) {
if(counter >= this.colors.size()){
counter=0;
}
Color newColor = this.colors.get(counter);
counter++;
return newColor;
}
}
package model;
import javafx.scene.paint.Color;
import java.util.List;
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;
}
@Override
public Color nextColor(Cell cell) {
List<Cell> neighbours = cell.getNeighbours();
for (Color color : this.colors) {
boolean cellNotUsed = true;
for (Cell newCell : neighbours) {
if (color==newCell.getColor())
{ cellNotUsed = false; }
}
if (cellNotUsed == true) {
return color;
}
}
return this.defaultColor;
}
}
package model;
import javafx.scene.paint.Color;
public class Flooder {
static void flood(Cell startCell, Color floodingColor){
ColoredCellIterator myCell = new ColoredCellIterator(startCell);
while (myCell.hasNext()) {
myCell.next().setColor(floodingColor);
}
}
}
......@@ -3,6 +3,7 @@ package model;
import javafx.beans.property.Property;
import javafx.scene.paint.Color;
import java.util.Iterator;
import java.util.List;
public class GrayCell extends AbstractCell{
......@@ -27,7 +28,11 @@ public class GrayCell extends AbstractCell{
*/
@Override
public void setNeighbours(List<Cell> cells) {
}
@Override
public Iterator<Cell> iterator() {
return null;
}
@Override
......
package model;
import java.util.Iterator;
public class GrayGrid implements Grid {
private final int numberOfRows;
......@@ -40,4 +42,15 @@ public class GrayGrid implements Grid{
public int getNumberOfColumns() {
return numnberOfColumns;
}
@Override
public void color(ColorGenerator colorGenerator) {
}
@Override
public Iterator<Cell> iterator() {
return null;
}
}
package model;
public interface Grid {
public interface Grid extends Iterable<Cell>{
/**
* Return the cell located at the given coordinates in the grid.
......@@ -27,5 +27,5 @@ public interface Grid {
* @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}
*/
void color(ColorGenerator colorGenerator);
}
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> colorList;
Random random;
private RandomUtil randomUtil;
public RandomColorGenerator(List<Color> colors , Random randomGenerator) {
colorList = colors;
random = randomGenerator;
}
@Override
public Color nextColor(Cell cell) {
return randomUtil.randomElement(colorList,random);
}
}
......@@ -2,6 +2,7 @@ package model;
import javafx.scene.paint.Color;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
......@@ -10,6 +11,24 @@ public class SquareCell extends AbstractCell{
List<Cell> neighbours;
public SquareCell(){
setColor(AbstractCell.DEFAULT_CELL_COLOR);
this.neighbours= new ArrayList<Cell>();
}
public SquareCell( Color color){
setColor(color);
this.neighbours= new ArrayList<Cell>();
}
public SquareCell(Color color,List<Cell>neighbours){
SquareCell Cell=new SquareCell(color);
setNeighbours(neighbours);
}
/**
* A cell is placed somewhere on a grid. Its neighbours thus depend on the underlying grid.
*
......@@ -17,7 +36,7 @@ public class SquareCell extends AbstractCell{
*/
@Override
public List<Cell> getNeighbours() {
return null;
return neighbours;
}
/**
......@@ -28,8 +47,15 @@ public class SquareCell extends AbstractCell{
*/
@Override
public void setNeighbours(List<Cell> cells) {
this.neighbours=cells;
}
@Override
public Iterator<Cell> iterator() {
return new ColoredCellIterator(SquareCell.this);
}
}
package model;
import javafx.scene.paint.Color;
public class UniformColorGenerator implements ColorGenerator{
private 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{
private Color uniColor;
private Color excepColor;
private int counter=0;
public UniformExceptOneGenerator(Color uniformColor, Color exceptionColor){
this.uniColor=uniformColor;
this.excepColor=exceptionColor;
}
@Override
public Color nextColor(Cell cell) {
counter++;
if(counter==2){
return this.excepColor;
}
return this.uniColor; }
}
package util;
import java.util.List;
import java.util.Random;
public class RandomUtil {
private RandomUtil(){}
public static <T> T randomElement(T[] elements, Random random){
T element= elements[random.nextInt(elements.length)];
return element;
}
public static <T> T randomElement(List<T> elements, Random random){
T element= elements.get(random.nextInt(elements.size()));
return element;
}
}
......@@ -8,6 +8,7 @@ import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.util.Duration;
import model.ArrayGrid;
import model.Cell;
import model.GrayGrid;
import model.Grid;
......@@ -23,13 +24,12 @@ public class MatrixPane extends GridPane {
public MatrixPane(@NamedArg("tileWidth") Double tileWidth,
@NamedArg("tileHeight") Double tileHeight,
@NamedArg("numberOfColumns") Integer numberOfColumns,
@NamedArg("numberOfRows") Integer numberOfRows) {
@NamedArg("numberOfRows") Integer numberOfRows) throws Exception {
this.tileWidth = tileWidth;
this.tileHeight = tileHeight;
this.numberOfColumns = numberOfColumns;
this.numberOfRows = numberOfRows;
// TODO replace by new ArrayGrid(numberOfRows, numberOfColumns)
cellGrid = new GrayGrid(numberOfRows, numberOfColumns);
cellGrid = new ArrayGrid(numberOfRows, numberOfColumns);
initMatrix();
}
......
......@@ -11,35 +11,25 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy;
class ArrayGridTest {
// TODO
// uncomment
/*
private ArrayGrid arrayGridThreeFour;
private final ArrayGrid arrayGridTwoTwo = new ArrayGrid(2,2);
ArrayGridTest() throws Exception {
}
@BeforeEach
void initializeArrayGridThreeFour(){
void initializeArrayGridThreeFour() throws Exception {
arrayGridThreeFour = new ArrayGrid(3,4);
}
@Test
void testGetCellAndGridInitialization() {
assertThat(arrayGridThreeFour.getCell(0,0).getNeighbours())
.hasSize(2)
.containsExactlyInAnyOrder(arrayGridThreeFour.getCell(1,0), arrayGridThreeFour.getCell(0,1));
assertThat(arrayGridThreeFour.getCell(1,1).getNeighbours()).hasSize(4)
.containsExactlyInAnyOrder(arrayGridThreeFour.getCell(0,1),
arrayGridThreeFour.getCell(2,1),
arrayGridThreeFour.getCell(1,2),
arrayGridThreeFour.getCell(1,0));
assertThat(arrayGridThreeFour.getCell(2,3).getNeighbours()).hasSize(2)
.containsExactlyInAnyOrder(arrayGridThreeFour.getCell(1,3),
arrayGridThreeFour.getCell(2,2));
assertThat(arrayGridThreeFour.getCell(2,2).getNeighbours()).hasSize(3)
.containsExactlyInAnyOrder(arrayGridThreeFour.getCell(1,2),
arrayGridThreeFour.getCell(2,1),
arrayGridThreeFour.getCell(2,3));
assertThat(arrayGridThreeFour.getCell(0,0).getNeighbours()).hasSize(2).containsExactlyInAnyOrder(arrayGridThreeFour.getCell(1,0), arrayGridThreeFour.getCell(0,1));
assertThat(arrayGridThreeFour.getCell(1,1).getNeighbours()).hasSize(4).containsExactlyInAnyOrder(arrayGridThreeFour.getCell(0,1), arrayGridThreeFour.getCell(2,1),arrayGridThreeFour.getCell(1,2),arrayGridThreeFour.getCell(1,0));
assertThat(arrayGridThreeFour.getCell(2,3).getNeighbours()).hasSize(2).containsExactlyInAnyOrder(arrayGridThreeFour.getCell(1,3), arrayGridThreeFour.getCell(2,2));
assertThat(arrayGridThreeFour.getCell(2,2).getNeighbours()).hasSize(3).containsExactlyInAnyOrder(arrayGridThreeFour.getCell(1,2), arrayGridThreeFour.getCell(2,1), arrayGridThreeFour.getCell(2,3));
}
@Test
......@@ -49,12 +39,12 @@ class ArrayGridTest {
}
@Test
void testGetNumberOfRows() {
void testGetNumberOfRows() throws Exception {
assertThat(new ArrayGrid(100,200).getNumberOfRows()).isEqualTo(100);
}
@Test
void testGetNumberOfColumns() {
void testGetNumberOfColumns() throws Exception {
assertThat(new ArrayGrid(100,200).getNumberOfColumns()).isEqualTo(200);
}
......@@ -89,5 +79,5 @@ class ArrayGridTest {
assertThat(iterator.hasNext()).isFalse();
}
*/
}
\ No newline at end of file