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
  • main
1 result

Target

Select target project
No results found
Select Git revision
  • main
1 result
Show changes

Commits on Source 25

18 files
+ 361
33
Compare changes
  • Side-by-side
  • Inline

Files

+4 −2
Original line number Original line Diff line number Diff line
@@ -6,5 +6,7 @@ Il s'agit d'implémenter une version du jeu "inondation" (voir par exemple [ici]


## Membre du projet
## Membre du projet


- NOM, prénom
- SAIDI Hatim 2.2
- NOM, prénom 
- AIX MONTERRIN

Original line number Original line Diff line number Diff line
@@ -6,17 +6,22 @@ import javafx.scene.control.Label;
import javafx.scene.paint.Color;
import javafx.scene.paint.Color;
import javafx.util.Duration;
import javafx.util.Duration;
import model.*;
import model.*;
import util.CyclicColorGenerator;
import util.DistinctColorGenerator;
import util.RandomColorGenerator;
import util.UniformExceptOneGenerator;
import view.MatrixPane;
import view.MatrixPane;


import java.util.List;
import java.util.List;
import java.util.Random;
import java.util.Random;


public class GameController {
public class GameController {
    public static final Color COLOR_ONE   = Color.RED;
    public static final Color COLOR_FIRST_NAME_ONE   = Color.GRAY;
    public static final Color COLOR_TWO   = Color.BLUE;
    public static final Color COLOR_LAST_NAME_ONE  = Color.SADDLEBROWN;
    public static final Color COLOR_THREE = Color.YELLOW;
    public static final Color  COLOR_FIRST_NAME_TWO = Color.ALICEBLUE;
    public static final Color COLOR_FOUR  = Color.GREEN;
    public static final Color  COLOR_LAST_NAME_TWO  = Color.ANTIQUEWHITE;
    private static final List<Color> availableColors = List.of(COLOR_ONE, COLOR_TWO, COLOR_THREE, COLOR_FOUR);
    private static final List<Color> availableColors = List.of(COLOR_FIRST_NAME_ONE, COLOR_LAST_NAME_ONE
            ,  COLOR_FIRST_NAME_TWO,  COLOR_LAST_NAME_TWO);


    public static final double PAUSE_MILLISECONDS = 400;
    public static final double PAUSE_MILLISECONDS = 400;
    private final PauseTransition pause = new PauseTransition(Duration.millis(PAUSE_MILLISECONDS));
    private final PauseTransition pause = new PauseTransition(Duration.millis(PAUSE_MILLISECONDS));
@@ -56,39 +61,39 @@ public class GameController {


    private void colorGrid(ColorGenerator colorGenerator){
    private void colorGrid(ColorGenerator colorGenerator){
        // TODO
        // TODO
        //  matrixPane.getGrid().color(colorGenerator);
         matrixPane.getGrid().color(colorGenerator);
    }
    }




    @FXML
    @FXML
    public void fillGridUniform() {
    public void fillGridUniform() {
        // TODO uncomment:
        // TODO uncomment:
        //  colorGrid(new UniformColorGenerator(COLOR_ONE));
         colorGrid(new UniformColorGenerator(COLOR_FIRST_NAME_ONE));
    }
    }


    @FXML
    @FXML
    public void fillGridRandom() {
    public void fillGridRandom() {
        // TODO uncomment
        // TODO uncomment
        //  colorGrid(new RandomColorGenerator(availableColors,random));
        colorGrid(new RandomColorGenerator(availableColors,random));
    }
    }


    @FXML
    @FXML
    public void fillGridDistinct() {
    public void fillGridDistinct() {
        // TODO uncomment
        // TODO uncomment
        //  fillGridUniform();
         fillGridUniform();
        //  colorGrid(new DistinctColorGenerator(COLOR_ONE,List.of(COLOR_THREE, COLOR_FOUR)));
        colorGrid(new DistinctColorGenerator(COLOR_FIRST_NAME_ONE ,List.of(COLOR_FIRST_NAME_TWO, COLOR_LAST_NAME_TWO)));
    }
    }


    @FXML
    @FXML
    public void fillGridCycle() {
    public void fillGridCycle() {
        // TODO uncomment
        // TODO uncomment
        //  colorGrid(new CyclingColorGenerator(availableColors));
        colorGrid(new CyclicColorGenerator((availableColors)));
    }
    }


    @FXML
    @FXML
    public void fillGridUniformExceptOne() {
    public void fillGridUniformExceptOne() {
        // TODO uncomment
        // TODO uncomment
        //  colorGrid(new UniformExceptOneColorGenerator(COLOR_ONE,COLOR_TWO));
        colorGrid(new UniformExceptOneGenerator(COLOR_FIRST_NAME_ONE,COLOR_LAST_NAME_ONE));
    }
    }


    private void playComputerTurn(){
    private void playComputerTurn(){
+48 −0
Original line number Original line Diff line number Diff line
package model;

import java.util.Iterator;

public class ArrayGrid implements Grid{

    private Cell[][] cells;
    private final  int numberOfRows ;
    private final int numberOfColumns ;


    public ArrayGrid(int numberOfRows,int numberOfColumns) {
        this.numberOfRows = numberOfRows;
        this.numberOfColumns = numberOfColumns;
        if (numberOfColumns <= 0 || numberOfRows <= 0) {
            throw new IllegalArgumentException();
                }
        else {
            for (int j = 0; j < this.numberOfColumns; j++) {
                for (int i = 0; i < this.numberOfRows; i++) {
                    cells[j][i] = new SquareCell();
                }
            }
        }
    }

    public Cell getCell(int row, int column) {
        return this.cells[row][column];
    }

    public int getNumberOfRows(){
        return this.numberOfRows;
    }
    public int getNumberOfColumns(){
        return  this.numberOfColumns;
    }

    public void color(ColorGenerator colorGenerator){
        for(Cell cell : this)
               cell.setColor(colorGenerator.nextColor(cell));
    }

    public Iterator<Cell> iterator(){
        return  new CellGridIterator(new ArrayGrid(numberOfColumns,numberOfRows));
    }


}
Original line number Original line Diff line number Diff line
@@ -42,5 +42,7 @@ public interface Cell {
     */
     */
    Property<Color> getColorProperty();
    Property<Color> getColorProperty();


    Iterator<Cell> iterator();



}
}
Original line number Original line Diff line number Diff line
package model;

import javafx.scene.paint.Color;

import java.util.Iterator;

public class CellGridIterator implements Iterator<Cell> {
    private  ArrayGrid grid;
    private int x ;
    private int y ;



   public CellGridIterator(ArrayGrid grid){
        this.grid = grid;
   }

    @Override
    public boolean hasNext() {
        if(x< grid.getNumberOfColumns()-1 ||y< grid.getNumberOfRows()-1){
            return true;
        }
        else {
            return false;
        }
    }

    public Cell next(){
       Cell cell = grid.getCell(y, x);
       if (x< grid.getNumberOfRows()-1){
           x = x + 1;
       }
       else {
            y = y + 1;
            x = 0;
       }
       return cell;
   }
}
Original line number Original line Diff line number Diff line
package model;

import javafx.scene.paint.Color;

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 ;

    public ColoredCellIterator(Cell startCell){
        this.color = startCell.getColor();
        this.visitedCells = new HashSet<>();
        this.pendingCells = new HashSet<>();
        pendingCells.add(startCell);
    }

    @Override
    public Cell next() {
        for(Cell cell : pendingCells){
            if(cell.getColor() == pendingCells.stream().findFirst().get().getColor()&& hasNext()&& !visitedCells.contains(cell)  ){
                for (int i=0;i<cell.getNeighbours().size()-1;i++){
                    if(visitedCells.contains(cell.getNeighbours().get(i)) ||
                                cell.getNeighbours().get(i).getColor()!= pendingCells.stream().findFirst().get().getColor()){
                        visitedCells.add(cell);
                        return cell;
                    }
                    else {
                        pendingCells.add(cell.getNeighbours().get(i));
                    }
                visitedCells.add(cell);
                return cell;
                }
            visitedCells.add(cell);
            return cell;
            }
        }
        return new SquareCell();
    }

    @Override
    public boolean hasNext() {
        if (pendingCells.isEmpty()){
            return false;
        }
        else {
            return true;
        }
    }
}
Original line number Original line Diff line number Diff line
@@ -3,6 +3,7 @@ package model;
import javafx.beans.property.Property;
import javafx.beans.property.Property;
import javafx.scene.paint.Color;
import javafx.scene.paint.Color;


import java.util.Iterator;
import java.util.List;
import java.util.List;


public class GrayCell extends AbstractCell{
public class GrayCell extends AbstractCell{
@@ -34,4 +35,7 @@ public class GrayCell extends AbstractCell{
    public void setColor(Color color){
    public void setColor(Color color){


    }
    }
    public Iterator<Cell> iterator(){
        return null;
    }
}
}
Original line number Original line Diff line number Diff line
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,11 @@ public class GrayGrid implements Grid{
    public int getNumberOfColumns() {
    public int getNumberOfColumns() {
        return numnberOfColumns;
        return numnberOfColumns;
    }
    }

    public void color(ColorGenerator colorGenerator){

    }
    public Iterator<Cell> iterator(){
        return null;
    }
}
}
Original line number Original line Diff line number Diff line
package model;
package model;


public interface  Grid  {
public interface  Grid  extends Iterable<Cell>{


    /**
    /**
     * Return the cell located at the given coordinates in the grid.
     * Return the cell located at the given coordinates in the grid.
@@ -28,4 +28,6 @@ public interface Grid {
     *                       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);
}
}
Original line number Original line Diff line number Diff line
@@ -2,6 +2,8 @@ package model;


import javafx.scene.paint.Color;
import javafx.scene.paint.Color;


import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Iterator;
import java.util.List;
import java.util.List;


@@ -9,27 +11,35 @@ public class SquareCell extends AbstractCell{


    List<Cell> neighbours;
    List<Cell> neighbours;


    public SquareCell() {
        Color cell = DEFAULT_CELL_COLOR;
        this.neighbours = new ArrayList<>();
    }

    public SquareCell(Color color){
       Color cell = color;
       this.neighbours = new ArrayList<>();
    }

    public SquareCell(Color color,List<Cell>neighbours){
        Color cell = color;
        this.neighbours = neighbours;
    }


    /**
     * A cell is placed somewhere on a grid. Its neighbours thus depend on the underlying grid.
     *
     * @return the list of cell that are neighbours of this{@code Cell}.
     */
    @Override
    @Override
    public List<Cell> getNeighbours() {
    public List<Cell> getNeighbours() {
        return null;
        return this.neighbours;
    }
    }


    /**

     * Update the list of neighbours of this {@code Cell}.
     *
     * @param cells a list of cells that are the neighbours of this {@code cell}
     *              int the underlying grid.
     */
    @Override
    @Override
    public void setNeighbours(List<Cell> cells) {
    public void setNeighbours(List<Cell> cells) {

        for (int i = 0; i < neighbours.size()  ; i++) {
            this.neighbours.set(i,cells.get(i));
        }
    }
    }



    public Iterator<Cell> iterator(){
        return new ColoredCellIterator(new SquareCell());
    }
}
}
 No newline at end of file
Original line number Original line Diff line number Diff line
package model;

import javafx.scene.paint.Color;

public class UniformColorGenerator implements ColorGenerator{
    private Color color;

    public UniformColorGenerator(Color color){
        this.color = color;
    }

    public Color nextColor(Cell cell){
        return color;
    }

}
Original line number Original line Diff line number Diff line
package util;

import model.Cell;
import model.ColorGenerator;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import javafx.scene.paint.Color;

public class CyclicColorGenerator implements ColorGenerator {
    private List<Color> colors ;
    private int appels;



    public CyclicColorGenerator(List<Color> colors){
        this.colors = colors;
        this.appels = appels+1;
    }

    public Color nextColor(Cell cell){
        CyclicColorGenerator cyclicColorGenerator = new CyclicColorGenerator(new ArrayList<>());
        while(cyclicColorGenerator.appels <=this.colors.size()){
            return colors.get(cyclicColorGenerator.appels-1);
        }
        cyclicColorGenerator.appels = 0;
        return colors.get(cyclicColorGenerator.appels);

    }
}
Original line number Original line Diff line number Diff line
package util;

import javafx.scene.paint.Color;
import model.Cell;
import model.ColorGenerator;

import java.util.List;

public class DistinctColorGenerator implements ColorGenerator {
    private final List<Color> colors;
    private final Color defaultcolor;

    public  DistinctColorGenerator(Color defaultcolor,List<Color>colors){
            this.colors = colors;
            this.defaultcolor = defaultcolor;
    }

    @Override
    public Color nextColor(Cell cell) {
        for(int i = 0;i< colors.size()-1;i++){
           for (int j= 0;j<cell.getNeighbours().size()-1;j++){
               if(colors.get(i)!= cell.getNeighbours().get(j).getColor()){
                   return colors.get(i);
               }
           }
        }

        return defaultcolor;
    }
}
Original line number Original line Diff line number Diff line
package util;

import model.Cell;
import model.ColorGenerator;
import  javafx.scene.paint.Color;
import java.util.List;
import java.util.Random;

public class RandomColorGenerator implements ColorGenerator {
    private List<Color> colors;
    private Random randomGenerator;


    public RandomColorGenerator(List<Color> colors, Random randomGenerator){
        this.colors = colors;
        this.randomGenerator =randomGenerator;
    }

    @Override
    public Color nextColor(Cell cell) {
        return colors.get(randomGenerator.nextInt(colors.size()));
    }

}
+21 −0
Original line number Original line Diff line number Diff line
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()));
    }
}
Original line number Original line Diff line number Diff line
package util;
package util;


import model.Cell;

import java.util.HashSet;
import java.util.NoSuchElementException;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.Set;


@@ -7,4 +10,5 @@ public class SetUtil {
    public static <T> T anyElement(Set<T> elements){
    public static <T> T anyElement(Set<T> elements){
        return elements.stream().findAny().orElseThrow(NoSuchElementException::new);
        return elements.stream().findAny().orElseThrow(NoSuchElementException::new);
    }
    }

}
}
Original line number Original line Diff line number Diff line
package util;

import javafx.scene.paint.Color;
import model.Cell;
import model.ColorGenerator;

public class UniformExceptOneGenerator implements ColorGenerator {

    private Color uniformcolor;
    private Color exeptioncolor;
    private  int appels ;

    public UniformExceptOneGenerator(Color uniformColor, Color exceptionColor){
        this.uniformcolor =uniformColor;
        this.exeptioncolor = exceptionColor;
        this.appels += appels +1;
    }


    @Override
    public Color nextColor(Cell cell) {
        UniformExceptOneGenerator appel =new UniformExceptOneGenerator(uniformcolor,exeptioncolor);
        if (appel.appels==1){
          return this.uniformcolor;
        }
        else {
            return this.exeptioncolor;}
    }
}
Original line number Original line Diff line number Diff line
@@ -11,8 +11,6 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy;


class ArrayGridTest {
class ArrayGridTest {


    // TODO
    //  uncomment
/*
/*
    private  ArrayGrid arrayGridThreeFour;
    private  ArrayGrid arrayGridThreeFour;
    private final ArrayGrid arrayGridTwoTwo = new ArrayGrid(2,2);
    private final ArrayGrid arrayGridTwoTwo = new ArrayGrid(2,2);