diff --git a/app/src/main/java/model/ArrayGrid.java b/app/src/main/java/model/ArrayGrid.java index d99f9db6034fd756104c7f6bc913bc8b4c203f79..caa8edd5b69d51c829c99f8255e885eab03f5a8b 100644 --- a/app/src/main/java/model/ArrayGrid.java +++ b/app/src/main/java/model/ArrayGrid.java @@ -2,22 +2,64 @@ package model; import javafx.scene.paint.Color; +import java.util.ArrayList; import java.util.Iterator; +import java.util.List; public class ArrayGrid implements Grid{ private Cell[][] cells; public ArrayGrid(int numberOfRows, int numberOfColumns) { - if(numberOfRows <= 0 || numberOfColumns <= 0) + if (numberOfRows <= 0 || numberOfColumns <= 0) throw new IllegalArgumentException("Le nombre de lignes ou de colonnes ne peut pas être nul ou négatif."); 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++) { + Cell origin = cells[i][j] = new SquareCell(); + tryAddNeighbour2(origin,tryGetCell(i-1,j)); + tryAddNeighbour2(origin,tryGetCell(i,j-1)); } } + // initNeighbours(numberOfRows,numberOfColumns); } + /* private void initNeighbours(int numberOfRows, int numberOfColumns) { + for (int i = 0; i < numberOfRows; i++) { + for (int j = 0; j < numberOfColumns; j++) { + List<Cell> neighbours = getCell(i,j).getNeighbours(); + // tryAddNeighbour(i, j - 1, neighbours); + // tryAddNeighbour(i, j + 1, neighbours); + tryAddNeighbour(i - 1, j, neighbours); + tryAddNeighbour(i + 1, j, neighbours); + // getCell(i, j).setNeighbours(neighbours); + } + } + }*/ + + private Cell tryGetCell(int row , int column){ + try { + return this.getCell(row,column); + + } catch (Exception e) { + + } + return null; + } + /* private void tryAddNeighbour(int row , int column,List<Cell> cells){ + Cell neighbour = tryGetCell(row,column); + if (neighbour != null){ + cells.add(neighbour); + } + } */ + private void tryAddNeighbour2(Cell origin , Cell neighbour){ + if (neighbour != null){ + addNeighbourTo(origin,neighbour); + } + } + private void addNeighbourTo(Cell neighbour1,Cell neighbour2){ + neighbour1.getNeighbours().add(neighbour2); + neighbour2.getNeighbours().add(neighbour1); + } @Override @@ -48,13 +90,4 @@ public class ArrayGrid implements Grid{ return new CellGridIterator(this); } - @Override - public boolean hasNext() { - return false; - } - - @Override - public Cell next() { - return null; - } } diff --git a/app/src/main/java/model/DistinctColorGenerator.java b/app/src/main/java/model/DistinctColorGenerator.java index 3d9bd1ee25c8437039a8ab659777d9f71d5788c9..07b89f46c18171beefb295e1381764f59adb7e15 100644 --- a/app/src/main/java/model/DistinctColorGenerator.java +++ b/app/src/main/java/model/DistinctColorGenerator.java @@ -10,7 +10,7 @@ public class DistinctColorGenerator implements ColorGenerator { private List<Color> colors; private Color defaultColor; - public DistinctColorGenerator(List<Color> colors, Color defaultColor) { + public DistinctColorGenerator(Color defaultColor, List<Color> colors) { this.colors = colors; this.defaultColor = defaultColor; } @@ -18,12 +18,12 @@ public class DistinctColorGenerator implements ColorGenerator { @Override public Color nextColor(Cell cell) { List<Cell> neighbours = cell.getNeighbours(); - List<Color> neighbourscolor = new ArrayList<>(); - for (Cell neighbour : neighbours) {neighbourscolor.add(neighbour.getColor());} + List<Color> neighboursColor = new ArrayList<>(); + for (Cell neighbour : neighbours) {neighboursColor.add(neighbour.getColor());} int i = 0, j = 0; - while (colors.get(i) == neighbourscolor.get(j)) { - if (j == neighbourscolor.size() - 1 && i == colors.size() - 1) {return defaultColor;} - if (j == neighbourscolor.size() - 1) { + while (colors.get(i) == neighboursColor.get(j)) { + if (j == neighboursColor.size() - 1 && i == colors.size() - 1) {return defaultColor;} + if (j == neighboursColor.size() - 1) { j=0; i+=1; continue;} diff --git a/app/src/main/java/model/GrayGrid.java b/app/src/main/java/model/GrayGrid.java index b7b13916c2f43f8aec94b5e3d102d25abc6f1d7f..15592faf0899565df0d7227723e5a25b627d1af8 100644 --- a/app/src/main/java/model/GrayGrid.java +++ b/app/src/main/java/model/GrayGrid.java @@ -1,5 +1,7 @@ package model; +import java.util.Iterator; + public class GrayGrid implements Grid{ private final int numberOfRows; @@ -46,13 +48,9 @@ public class GrayGrid implements Grid{ } - @Override - public boolean hasNext() { - return false; - } @Override - public Cell next() { + public Iterator<Cell> iterator() { return null; } } diff --git a/app/src/main/java/model/Grid.java b/app/src/main/java/model/Grid.java index 004f293ff3adc1211439e47de5321a27819e822b..4487a8a2dc7c51132a146d98eca9617166f1e6e3 100644 --- a/app/src/main/java/model/Grid.java +++ b/app/src/main/java/model/Grid.java @@ -2,7 +2,7 @@ package model; import java.util.Iterator; -public interface Grid extends Iterator<Cell> { +public interface Grid extends Iterable<Cell> { /** * Return the cell located at the given coordinates in the grid. diff --git a/app/src/test/java/model/ColoredCellIteratorTest.java b/app/src/test/java/model/ColoredCellIteratorTest.java index bd0bb10c50bfdb804b22080d2897b050f68b3987..c6d41c2dffc089791af9241f0401d9591397fb8b 100644 --- a/app/src/test/java/model/ColoredCellIteratorTest.java +++ b/app/src/test/java/model/ColoredCellIteratorTest.java @@ -1,59 +1,59 @@ -//package model; -// -//import javafx.scene.paint.Color; -//import org.junit.jupiter.api.BeforeAll; -//import org.junit.jupiter.api.Test; -// -//import java.util.ArrayList; -//import java.util.List; -// -//import static org.assertj.core.api.Assertions.assertThat; -// -//class ColoredCellIteratorTest { -// -// -// /* -// * +---+---+---+ -// * | R | B | R | -// * +---+---+---| -// * | R | R | B | -// * |---+---+---+ -// * | B | B | R | -// * +---+---+---+ -// */ -// -// private static ArrayGrid gridThreeThree = new ArrayGrid(3,3); -// -// @BeforeAll -// private static void initializeColorsGrid(){ -// gridThreeThree.getCell(0,0).setColor(Color.RED); -// gridThreeThree.getCell(0,1).setColor(Color.BLACK); -// gridThreeThree.getCell(0,2).setColor(Color.RED); -// gridThreeThree.getCell(1,0).setColor(Color.RED); -// gridThreeThree.getCell(1,1).setColor(Color.RED); -// gridThreeThree.getCell(1,2).setColor(Color.BLACK); -// gridThreeThree.getCell(2,0).setColor(Color.BLACK); -// gridThreeThree.getCell(2,1).setColor(Color.BLACK); -// gridThreeThree.getCell(2,2).setColor(Color.RED); -// } -// -// @Test -// void testIterator() { -// ColoredCellIterator redCellIterator = new ColoredCellIterator(gridThreeThree.getCell(0,0)); -// List<Cell> expectedRedCells = List.of(gridThreeThree.getCell(0,0), -// gridThreeThree.getCell(1,0), -// gridThreeThree.getCell(1,1)); -// List<Cell> fromIteratorCells = new ArrayList<>(); -// for(;redCellIterator.hasNext();) fromIteratorCells.add(redCellIterator.next()); -// assertThat(fromIteratorCells).hasSameElementsAs(expectedRedCells).hasSameSizeAs(expectedRedCells); -// -// ColoredCellIterator blackCellIterator = new ColoredCellIterator(gridThreeThree.getCell(2,1)); -// List<Cell> expectedBlackCells = List.of(gridThreeThree.getCell(2,0), -// gridThreeThree.getCell(2,1)); -// fromIteratorCells = new ArrayList<>(); -// for( ; blackCellIterator.hasNext(); ) fromIteratorCells.add(blackCellIterator.next()); -// assertThat(fromIteratorCells).hasSameElementsAs(expectedBlackCells).hasSameSizeAs(expectedBlackCells); -// -// } -// -//} \ No newline at end of file +package model; + +import javafx.scene.paint.Color; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +class ColoredCellIteratorTest { + + + /* + * +---+---+---+ + * | R | B | R | + * +---+---+---| + * | R | R | B | + * |---+---+---+ + * | B | B | R | + * +---+---+---+ + */ + + private static ArrayGrid gridThreeThree = new ArrayGrid(3,3); + + @BeforeAll + private static void initializeColorsGrid(){ + gridThreeThree.getCell(0,0).setColor(Color.RED); + gridThreeThree.getCell(0,1).setColor(Color.BLACK); + gridThreeThree.getCell(0,2).setColor(Color.RED); + gridThreeThree.getCell(1,0).setColor(Color.RED); + gridThreeThree.getCell(1,1).setColor(Color.RED); + gridThreeThree.getCell(1,2).setColor(Color.BLACK); + gridThreeThree.getCell(2,0).setColor(Color.BLACK); + gridThreeThree.getCell(2,1).setColor(Color.BLACK); + gridThreeThree.getCell(2,2).setColor(Color.RED); + } + + @Test + void testIterator() { + ColoredCellIterator redCellIterator = new ColoredCellIterator(gridThreeThree.getCell(0,0)); + List<Cell> expectedRedCells = List.of(gridThreeThree.getCell(0,0), + gridThreeThree.getCell(1,0), + gridThreeThree.getCell(1,1)); + List<Cell> fromIteratorCells = new ArrayList<>(); + for(;redCellIterator.hasNext();) fromIteratorCells.add(redCellIterator.next()); + assertThat(fromIteratorCells).hasSameElementsAs(expectedRedCells).hasSameSizeAs(expectedRedCells); + + ColoredCellIterator blackCellIterator = new ColoredCellIterator(gridThreeThree.getCell(2,1)); + List<Cell> expectedBlackCells = List.of(gridThreeThree.getCell(2,0), + gridThreeThree.getCell(2,1)); + fromIteratorCells = new ArrayList<>(); + for( ; blackCellIterator.hasNext(); ) fromIteratorCells.add(blackCellIterator.next()); + assertThat(fromIteratorCells).hasSameElementsAs(expectedBlackCells).hasSameSizeAs(expectedBlackCells); + + } + +} \ No newline at end of file diff --git a/app/src/test/java/model/DistinctColorGeneratorTest.java b/app/src/test/java/model/DistinctColorGeneratorTest.java index 541d425c417210c5fdecbf849d37eee7607e36ab..c0158184f9c29978822c064b7288778d46fca3fd 100644 --- a/app/src/test/java/model/DistinctColorGeneratorTest.java +++ b/app/src/test/java/model/DistinctColorGeneratorTest.java @@ -1,39 +1,43 @@ -//package model; -// -//import javafx.scene.paint.Color; -//import org.junit.jupiter.api.BeforeEach; -//import org.junit.jupiter.api.Test; -// -//import java.util.List; -// -//import static org.assertj.core.api.Assertions.assertThat; -// -//class DistinctColorGeneratorTest { -// private final static Color initialColor = Color.GRAY; -// private final Grid grid = new ArrayGrid(2,3); -// @BeforeEach -// void initializeGridColor(){ -// for (Cell cell: grid) -// cell.setColor(initialColor); -// } -// -// @Test -// void testNextColorWithTooFewAvailableColor() { -// DistinctColorGenerator colorGeneratorOne = new DistinctColorGenerator(Color.RED, List.of(Color.RED)); -// grid.color(colorGeneratorOne); -// assertThat(grid.getCell(0,0).getColor()).isEqualTo(Color.RED); -// assertThat(grid.getCell(1,1).getColor()).isEqualTo(Color.RED); -// assertThat(grid.getCell(1,0).getColor()).isEqualTo(Color.RED); -// } -// -// @Test -// void testNextColorWithEnoughAvailableColor(){ -// Color defaultColor = Color.BLACK; -// DistinctColorGenerator colorGenerator = new DistinctColorGenerator(defaultColor, List.of(Color.RED,Color.BLUE,Color.YELLOW, Color.ORANGE)); -// grid.color(colorGenerator); -// for(Cell cell: grid){ -// assertThat(cell.getNeighbours().stream().map(c-> c.getColor())).doesNotContain(cell.getColor()); -// assertThat(cell.getColor()).isNotEqualTo(defaultColor).isNotEqualTo(initialColor); -// } -// } -//} \ No newline at end of file +package model; + +import javafx.scene.paint.Color; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +class DistinctColorGeneratorTest { + + + private final static Color initialColor = Color.GRAY; + private final Grid grid = new ArrayGrid(2,3); + @BeforeEach + void initializeGridColor(){ + for (Cell cell: grid) + cell.setColor(initialColor); + } + + @Test + void testNextColorWithTooFewAvailableColor() { + DistinctColorGenerator colorGeneratorOne = new DistinctColorGenerator(Color.RED, List.of(Color.RED)); + grid.color(colorGeneratorOne); + assertThat(grid.getCell(0,0).getColor()).isEqualTo(Color.RED); + assertThat(grid.getCell(1,1).getColor()).isEqualTo(Color.RED); + assertThat(grid.getCell(1,0).getColor()).isEqualTo(Color.RED); + } + + @Test + void testNextColorWithEnoughAvailableColor(){ + Color defaultColor = Color.BLACK; + DistinctColorGenerator colorGenerator = new DistinctColorGenerator(defaultColor, List.of(Color.RED,Color.BLUE,Color.YELLOW, Color.ORANGE)); + grid.color(colorGenerator); + for(Cell cell: grid){ + assertThat(cell.getNeighbours().stream().map(c-> c.getColor())).doesNotContain(cell.getColor()); + assertThat(cell.getColor()).isNotEqualTo(defaultColor).isNotEqualTo(initialColor); + } + } + +} +