Skip to content
Snippets Groups Projects
Commit 92ed70fa authored by LABOUREL Arnaud's avatar LABOUREL Arnaud
Browse files

Changed code to generate a grid of tiles with constrained colors.

parent 2d8d1842
No related branches found
No related tags found
No related merge requests found
...@@ -6,12 +6,12 @@ public enum CardinalDirection { ...@@ -6,12 +6,12 @@ public enum CardinalDirection {
SOUTH(1, 0), SOUTH(1, 0),
WEST(0, -1); WEST(0, -1);
public final int deltaX; public final int deltaRow;
public final int deltaY; public final int deltaColumn;
CardinalDirection(int deltaX, int deltaY) { CardinalDirection(int deltaRow, int deltaColumn) {
this.deltaX = deltaX; this.deltaRow = deltaRow;
this.deltaY = deltaY; this.deltaColumn = deltaColumn;
} }
public CardinalDirection oppositeDirection(){ public CardinalDirection oppositeDirection(){
......
...@@ -33,4 +33,12 @@ public class RandomTileGenerator { ...@@ -33,4 +33,12 @@ public class RandomTileGenerator {
return new Tile(colorsOfTile); return new Tile(colorsOfTile);
} }
public Tile nextTileWithColorConstraint(Square square){
Color[] colorsOfTile = new Color[4];
for(CardinalDirection side : CardinalDirection.values()){
colorsOfTile[side.ordinal()] = square.getColorConstraint(side).orElse(randomColor());
}
return new Tile(colorsOfTile);
}
} }
package model; package model;
import javafx.scene.paint.Color;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Optional; import java.util.Optional;
...@@ -11,7 +13,7 @@ public class Square { ...@@ -11,7 +13,7 @@ public class Square {
this.tile = tile; this.tile = tile;
this.neighboringSquares = neighboringSquares; this.neighboringSquares = neighboringSquares;
} }
private Square(Square[] neighboringSquares) { public Square(Square[] neighboringSquares) {
this(null, neighboringSquares); this(null, neighboringSquares);
} }
...@@ -33,4 +35,17 @@ public class Square { ...@@ -33,4 +35,17 @@ public class Square {
public boolean hasTile() { public boolean hasTile() {
return tile != null; return tile != null;
} }
public Optional<Color> getColorConstraint(CardinalDirection side) {
if (neighboringSquares == null)
throw new NoSuchElementException();
Square neighboringSquare = neighboringSquares[side.ordinal()];
if(!neighboringSquare.hasTile())
return Optional.empty();
return Optional.of(neighboringSquare.getTile().getColor(side.oppositeDirection()));
}
public void setNeighboringSquares(Square[] neighboringSquares) {
this.neighboringSquares = neighboringSquares;
}
} }
...@@ -28,17 +28,18 @@ public class SquareGrid implements Iterable<Square> { ...@@ -28,17 +28,18 @@ public class SquareGrid implements Iterable<Square> {
private Square[][] allocateMatrix() { private Square[][] allocateMatrix() {
Square[][] matrix = new Square[getNumberOfRows()][getNumberOfColumns()]; Square[][] matrix = new Square[getNumberOfRows()][getNumberOfColumns()];
for(int row = 0; row < getNumberOfRows(); row++) for(int row = 0; row < getNumberOfRows(); row++)
for(int column = 0; column < getNumberOfRows(); column++) for(int column = 0; column < getNumberOfColumns(); column++)
if(!squareIsInternal(row, column))
matrix[row][column] = new Square();
else {
matrix[row][column] = new Square(); matrix[row][column] = new Square();
for(int row = 1; row < getNumberOfRows() - 1; row++)
for(int column = 1; column < getNumberOfColumns() - 1; column++) {
Square[] neighboringSquares = new Square[CardinalDirection.values().length];
for (int index = 0; index < neighboringSquares.length; index++){
CardinalDirection direction = CardinalDirection.values()[index];
neighboringSquares[index] = matrix[row + direction.deltaRow][column + direction.deltaColumn];
} }
return matrix; matrix[row][column].setNeighboringSquares(neighboringSquares);
} }
return matrix;
private boolean squareIsInternal(int row, int column){
return 1 <= row && row < getNumberOfRows()-1 && 1 <= column && column < getNumberOfColumns()-1;
} }
/** /**
...@@ -76,8 +77,9 @@ public class SquareGrid implements Iterable<Square> { ...@@ -76,8 +77,9 @@ public class SquareGrid implements Iterable<Square> {
public void fillWithRandomTiles(List<Color> colors, Random randomGenerator){ public void fillWithRandomTiles(List<Color> colors, Random randomGenerator){
RandomTileGenerator randomTileGenerator = new RandomTileGenerator(colors, randomGenerator); RandomTileGenerator randomTileGenerator = new RandomTileGenerator(colors, randomGenerator);
internalSquareIterator().forEachRemaining(square -> square.add(randomTileGenerator.nextTile()));
borderSquareIterator().forEachRemaining(square -> square.add(randomTileGenerator.nextUniformTile())); borderSquareIterator().forEachRemaining(square -> square.add(randomTileGenerator.nextUniformTile()));
internalSquareIterator().forEachRemaining(square -> square.add(randomTileGenerator.nextTileWithColorConstraint(square)));
} }
public boolean hasTile(int row, int column) { public boolean hasTile(int row, int column) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment