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

Neighbours init

parent 2f07e688
No related branches found
No related tags found
No related merge requests found
......@@ -2,7 +2,9 @@ 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;
......@@ -13,11 +15,51 @@ public class ArrayGrid implements Grid{
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();
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;
}
}
......@@ -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;}
......
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;
}
}
......@@ -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.
......
//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
//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);
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment