Skip to content
Snippets Groups Projects
Commit 92481741 authored by EL GAOUAL Zaid's avatar EL GAOUAL Zaid
Browse files

Neighbours init

parent 2f07e688
Branches
No related tags found
No related merge requests found
...@@ -2,7 +2,9 @@ package model; ...@@ -2,7 +2,9 @@ package model;
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List;
public class ArrayGrid implements Grid{ public class ArrayGrid implements Grid{
private Cell[][] cells; private Cell[][] cells;
...@@ -13,11 +15,51 @@ public class ArrayGrid implements Grid{ ...@@ -13,11 +15,51 @@ public class ArrayGrid implements Grid{
this.cells = new Cell[numberOfRows][numberOfColumns]; this.cells = new Cell[numberOfRows][numberOfColumns];
for (int i = 0; i < numberOfRows; i++) { for (int i = 0; i < numberOfRows; i++) {
for (int j = 0; j < numberOfColumns; j++) { for (int j = 0; j < numberOfColumns; j++) {
cells[i][j] = new SquareCell(); 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 @Override
...@@ -48,13 +90,4 @@ public class ArrayGrid implements Grid{ ...@@ -48,13 +90,4 @@ public class ArrayGrid implements Grid{
return new CellGridIterator(this); return new CellGridIterator(this);
} }
@Override
public boolean hasNext() {
return false;
}
@Override
public Cell next() {
return null;
}
} }
...@@ -10,7 +10,7 @@ public class DistinctColorGenerator implements ColorGenerator { ...@@ -10,7 +10,7 @@ public class DistinctColorGenerator implements ColorGenerator {
private List<Color> colors; private List<Color> colors;
private Color defaultColor; private Color defaultColor;
public DistinctColorGenerator(List<Color> colors, Color defaultColor) { public DistinctColorGenerator(Color defaultColor, List<Color> colors) {
this.colors = colors; this.colors = colors;
this.defaultColor = defaultColor; this.defaultColor = defaultColor;
} }
...@@ -18,12 +18,12 @@ public class DistinctColorGenerator implements ColorGenerator { ...@@ -18,12 +18,12 @@ public class DistinctColorGenerator implements ColorGenerator {
@Override @Override
public Color nextColor(Cell cell) { public Color nextColor(Cell cell) {
List<Cell> neighbours = cell.getNeighbours(); List<Cell> neighbours = cell.getNeighbours();
List<Color> neighbourscolor = new ArrayList<>(); List<Color> neighboursColor = new ArrayList<>();
for (Cell neighbour : neighbours) {neighbourscolor.add(neighbour.getColor());} for (Cell neighbour : neighbours) {neighboursColor.add(neighbour.getColor());}
int i = 0, j = 0; int i = 0, j = 0;
while (colors.get(i) == neighbourscolor.get(j)) { while (colors.get(i) == neighboursColor.get(j)) {
if (j == neighbourscolor.size() - 1 && i == colors.size() - 1) {return defaultColor;} if (j == neighboursColor.size() - 1 && i == colors.size() - 1) {return defaultColor;}
if (j == neighbourscolor.size() - 1) { if (j == neighboursColor.size() - 1) {
j=0; j=0;
i+=1; i+=1;
continue;} continue;}
......
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;
...@@ -46,13 +48,9 @@ public class GrayGrid implements Grid{ ...@@ -46,13 +48,9 @@ public class GrayGrid implements Grid{
} }
@Override
public boolean hasNext() {
return false;
}
@Override @Override
public Cell next() { public Iterator<Cell> iterator() {
return null; return null;
} }
} }
...@@ -2,7 +2,7 @@ package model; ...@@ -2,7 +2,7 @@ package model;
import java.util.Iterator; 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. * Return the cell located at the given coordinates in the grid.
......
//package model; package model;
//
//import javafx.scene.paint.Color; import javafx.scene.paint.Color;
//import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
//import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
//
//import java.util.ArrayList; import java.util.ArrayList;
//import java.util.List; import java.util.List;
//
//import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
//
//class ColoredCellIteratorTest { class ColoredCellIteratorTest {
//
//
// /* /*
// * +---+---+---+ * +---+---+---+
// * | R | B | R | * | R | B | R |
// * +---+---+---| * +---+---+---|
// * | R | R | B | * | R | R | B |
// * |---+---+---+ * |---+---+---+
// * | B | B | R | * | B | B | R |
// * +---+---+---+ * +---+---+---+
// */ */
//
// private static ArrayGrid gridThreeThree = new ArrayGrid(3,3); private static ArrayGrid gridThreeThree = new ArrayGrid(3,3);
//
// @BeforeAll @BeforeAll
// private static void initializeColorsGrid(){ private static void initializeColorsGrid(){
// gridThreeThree.getCell(0,0).setColor(Color.RED); gridThreeThree.getCell(0,0).setColor(Color.RED);
// gridThreeThree.getCell(0,1).setColor(Color.BLACK); gridThreeThree.getCell(0,1).setColor(Color.BLACK);
// gridThreeThree.getCell(0,2).setColor(Color.RED); gridThreeThree.getCell(0,2).setColor(Color.RED);
// gridThreeThree.getCell(1,0).setColor(Color.RED); gridThreeThree.getCell(1,0).setColor(Color.RED);
// gridThreeThree.getCell(1,1).setColor(Color.RED); gridThreeThree.getCell(1,1).setColor(Color.RED);
// gridThreeThree.getCell(1,2).setColor(Color.BLACK); gridThreeThree.getCell(1,2).setColor(Color.BLACK);
// gridThreeThree.getCell(2,0).setColor(Color.BLACK); gridThreeThree.getCell(2,0).setColor(Color.BLACK);
// gridThreeThree.getCell(2,1).setColor(Color.BLACK); gridThreeThree.getCell(2,1).setColor(Color.BLACK);
// gridThreeThree.getCell(2,2).setColor(Color.RED); gridThreeThree.getCell(2,2).setColor(Color.RED);
// } }
//
// @Test @Test
// void testIterator() { void testIterator() {
// ColoredCellIterator redCellIterator = new ColoredCellIterator(gridThreeThree.getCell(0,0)); ColoredCellIterator redCellIterator = new ColoredCellIterator(gridThreeThree.getCell(0,0));
// List<Cell> expectedRedCells = List.of(gridThreeThree.getCell(0,0), List<Cell> expectedRedCells = List.of(gridThreeThree.getCell(0,0),
// gridThreeThree.getCell(1,0), gridThreeThree.getCell(1,0),
// gridThreeThree.getCell(1,1)); gridThreeThree.getCell(1,1));
// List<Cell> fromIteratorCells = new ArrayList<>(); List<Cell> fromIteratorCells = new ArrayList<>();
// for(;redCellIterator.hasNext();) fromIteratorCells.add(redCellIterator.next()); for(;redCellIterator.hasNext();) fromIteratorCells.add(redCellIterator.next());
// assertThat(fromIteratorCells).hasSameElementsAs(expectedRedCells).hasSameSizeAs(expectedRedCells); assertThat(fromIteratorCells).hasSameElementsAs(expectedRedCells).hasSameSizeAs(expectedRedCells);
//
// ColoredCellIterator blackCellIterator = new ColoredCellIterator(gridThreeThree.getCell(2,1)); ColoredCellIterator blackCellIterator = new ColoredCellIterator(gridThreeThree.getCell(2,1));
// List<Cell> expectedBlackCells = List.of(gridThreeThree.getCell(2,0), List<Cell> expectedBlackCells = List.of(gridThreeThree.getCell(2,0),
// gridThreeThree.getCell(2,1)); gridThreeThree.getCell(2,1));
// fromIteratorCells = new ArrayList<>(); fromIteratorCells = new ArrayList<>();
// for( ; blackCellIterator.hasNext(); ) fromIteratorCells.add(blackCellIterator.next()); for( ; blackCellIterator.hasNext(); ) fromIteratorCells.add(blackCellIterator.next());
// assertThat(fromIteratorCells).hasSameElementsAs(expectedBlackCells).hasSameSizeAs(expectedBlackCells); assertThat(fromIteratorCells).hasSameElementsAs(expectedBlackCells).hasSameSizeAs(expectedBlackCells);
//
// } }
//
//} }
\ No newline at end of file \ No newline at end of file
//package model; package model;
//
//import javafx.scene.paint.Color; import javafx.scene.paint.Color;
//import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
//import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
//
//import java.util.List; import java.util.List;
//
//import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
//
//class DistinctColorGeneratorTest { class DistinctColorGeneratorTest {
// private final static Color initialColor = Color.GRAY;
// private final Grid grid = new ArrayGrid(2,3);
// @BeforeEach private final static Color initialColor = Color.GRAY;
// void initializeGridColor(){ private final Grid grid = new ArrayGrid(2,3);
// for (Cell cell: grid) @BeforeEach
// cell.setColor(initialColor); void initializeGridColor(){
// } for (Cell cell: grid)
// cell.setColor(initialColor);
// @Test }
// void testNextColorWithTooFewAvailableColor() {
// DistinctColorGenerator colorGeneratorOne = new DistinctColorGenerator(Color.RED, List.of(Color.RED)); @Test
// grid.color(colorGeneratorOne); void testNextColorWithTooFewAvailableColor() {
// assertThat(grid.getCell(0,0).getColor()).isEqualTo(Color.RED); DistinctColorGenerator colorGeneratorOne = new DistinctColorGenerator(Color.RED, List.of(Color.RED));
// assertThat(grid.getCell(1,1).getColor()).isEqualTo(Color.RED); grid.color(colorGeneratorOne);
// assertThat(grid.getCell(1,0).getColor()).isEqualTo(Color.RED); 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; @Test
// DistinctColorGenerator colorGenerator = new DistinctColorGenerator(defaultColor, List.of(Color.RED,Color.BLUE,Color.YELLOW, Color.ORANGE)); void testNextColorWithEnoughAvailableColor(){
// grid.color(colorGenerator); Color defaultColor = Color.BLACK;
// for(Cell cell: grid){ DistinctColorGenerator colorGenerator = new DistinctColorGenerator(defaultColor, List.of(Color.RED,Color.BLUE,Color.YELLOW, Color.ORANGE));
// assertThat(cell.getNeighbours().stream().map(c-> c.getColor())).doesNotContain(cell.getColor()); grid.color(colorGenerator);
// assertThat(cell.getColor()).isNotEqualTo(defaultColor).isNotEqualTo(initialColor); 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 }
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment