diff --git a/app/src/main/java/model/Cell.java b/app/src/main/java/model/Cell.java index 4f09b41bc92f4c117ab722f4e7a642c4c691aa54..a280144beb702bd6af4da0fce199a75b2140bcd0 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 0000000000000000000000000000000000000000..a827db1768a67c15545b4edae143c9d4cb1fa891 --- /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 8007887ea0a9ca53b4ec9b5bf8d7a894c9711ac3..7b37e6f3196dd31c59ea79240698f0f27b05d981 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 bddd8bdfc018442d2e72fac2a96c049da865d99e..bcb69329f41e71047221a1e1549b25b7e2b98948 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 3672d51bdc61cbb3b098c8a6f08dd93d07160e75..787c1ccdedcf0f3375fbe1fb11b90d1d59616726 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); } + }