From 7171edb047456ed43fc918beb86a75b705a7073a Mon Sep 17 00:00:00 2001 From: Hatim Saidi <hatim.saidi@etu.univ-amu.fr> Date: Sun, 27 Nov 2022 20:48:32 +0100 Subject: [PATCH] test --- app/src/main/java/model/Cell.java | 2 + .../main/java/model/ColoredCellIterator.java | 53 +++++++++++++++++++ app/src/main/java/model/GrayCell.java | 4 ++ app/src/main/java/model/SquareCell.java | 4 ++ app/src/main/java/util/SetUtil.java | 4 ++ 5 files changed, 67 insertions(+) create mode 100644 app/src/main/java/model/ColoredCellIterator.java diff --git a/app/src/main/java/model/Cell.java b/app/src/main/java/model/Cell.java index 4f09b41..a280144 100644 --- a/app/src/main/java/model/Cell.java +++ b/app/src/main/java/model/Cell.java @@ -42,5 +42,7 @@ public interface Cell { */ Property<Color> getColorProperty(); + Iterator<Cell> iterator(); + } diff --git a/app/src/main/java/model/ColoredCellIterator.java b/app/src/main/java/model/ColoredCellIterator.java new file mode 100644 index 0000000..a827db1 --- /dev/null +++ b/app/src/main/java/model/ColoredCellIterator.java @@ -0,0 +1,53 @@ +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; + } + } +} diff --git a/app/src/main/java/model/GrayCell.java b/app/src/main/java/model/GrayCell.java index 8007887..7b37e6f 100644 --- a/app/src/main/java/model/GrayCell.java +++ b/app/src/main/java/model/GrayCell.java @@ -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{ @@ -34,4 +35,7 @@ public class GrayCell extends AbstractCell{ public void setColor(Color color){ } + public Iterator<Cell> iterator(){ + return null; + } } diff --git a/app/src/main/java/model/SquareCell.java b/app/src/main/java/model/SquareCell.java index bddd8bd..bcb6932 100644 --- a/app/src/main/java/model/SquareCell.java +++ b/app/src/main/java/model/SquareCell.java @@ -38,4 +38,8 @@ public class SquareCell extends AbstractCell { this.neighbours.set(i,cells.get(i)); } } + + public Iterator<Cell> iterator(){ + return new ColoredCellIterator(new SquareCell()); + } } \ No newline at end of file diff --git a/app/src/main/java/util/SetUtil.java b/app/src/main/java/util/SetUtil.java index 3672d51..787c1cc 100644 --- a/app/src/main/java/util/SetUtil.java +++ b/app/src/main/java/util/SetUtil.java @@ -1,5 +1,8 @@ package util; +import model.Cell; + +import java.util.HashSet; import java.util.NoSuchElementException; import java.util.Set; @@ -7,4 +10,5 @@ public class SetUtil { public static <T> T anyElement(Set<T> elements){ return elements.stream().findAny().orElseThrow(NoSuchElementException::new); } + } -- GitLab