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

Changed code to generate a grid of tiles with borders containing uniform tile.

parent 6f00ba27
No related branches found
No related tags found
No related merge requests found
...@@ -6,7 +6,7 @@ import view.GridTileCanvas; ...@@ -6,7 +6,7 @@ import view.GridTileCanvas;
public class MainPaneController { public class MainPaneController {
@FXML @FXML
public GridTileCanvas GridTileCanvas; public GridTileCanvas gridTileCanvas;
private MainApp mainApp; private MainApp mainApp;
...@@ -15,8 +15,8 @@ public class MainPaneController { ...@@ -15,8 +15,8 @@ public class MainPaneController {
* *
* @param mainApp the main app * @param mainApp the main app
*/ */
public void setMainApp(MainApp mainApp) { public void initializeGrid() {
this.mainApp = mainApp; gridTileCanvas.initializeGrid();
} }
} }
...@@ -8,6 +8,7 @@ import javafx.scene.Scene; ...@@ -8,6 +8,7 @@ import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane; import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage; import javafx.stage.Stage;
import model.TileGame; import model.TileGame;
import view.GridTileCanvas;
import java.io.IOException; import java.io.IOException;
...@@ -42,7 +43,7 @@ public class MainApp extends Application { ...@@ -42,7 +43,7 @@ public class MainApp extends Application {
// Give the controller access to the main app. // Give the controller access to the main app.
MainPaneController controller = loader.getController(); MainPaneController controller = loader.getController();
controller.setMainApp(this); controller.initializeGrid();
primaryStage.show(); primaryStage.show();
} catch (IOException e) { } catch (IOException e) {
......
package model;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class BorderSquareIterator implements Iterator<Square> {
private int rowIndex;
private int columnIndex;
private final SquareGrid grid;
public BorderSquareIterator(SquareGrid grid) {
this.rowIndex = 0;
this.columnIndex = 1;
this.grid = grid;
}
/**
* Returns {@code true} if the iteration has more elements.
* (In other words, returns {@code true} if {@link #next} would
* return an element rather than throwing an exception.)
*
* @return {@code true} if the iteration has more elements
*/
@Override
public boolean hasNext() {
return rowIndex < grid.getNumberOfRows();
}
/**
* Returns the next element in the iteration.
*
* @return the next element in the iteration
* @throws NoSuchElementException if the iteration has no more elements
*/
@Override
public Square next() {
Square next = grid.getSquare(rowIndex, columnIndex);
if(rowIndex == 0){
if(columnIndex == grid.getNumberOfColumns()-2){
columnIndex = 0;
rowIndex = 1;
}
else{
columnIndex++;
}
}
else if(columnIndex == 0){
if(rowIndex == grid.getNumberOfRows()-2){
columnIndex = grid.getNumberOfColumns() - 1;
rowIndex = 1;
}
else{
rowIndex++;
}
}
else if(columnIndex == grid.getNumberOfColumns() - 1){
if(rowIndex == grid.getNumberOfRows()-2){
rowIndex = grid.getNumberOfRows() - 1;
columnIndex = 1;
}
else{
rowIndex++;
}
}
else {
if(columnIndex == grid.getNumberOfColumns()-2){
rowIndex = grid.getNumberOfRows();
}
else{
columnIndex++;
}
}
return next;
}
}
package model; package model;
public enum CardinalDirection { public enum CardinalDirection {
NORTH, NORTH(-1, 0),
EAST, EAST(0, 1),
SOUTH, SOUTH(1, 0),
WEST WEST(0, -1);
public final int deltaX;
public final int deltaY;
CardinalDirection(int deltaX, int deltaY) {
this.deltaX = deltaX;
this.deltaY = deltaY;
}
public CardinalDirection oppositeDirection(){
return switch (this){
case EAST -> WEST;
case WEST -> EAST;
case NORTH -> SOUTH;
case SOUTH -> NORTH;
};
}
} }
package model;
import java.util.Iterator;
public class InternalSquareIterator implements Iterator<Square> {
private int rowIndex;
private int columnIndex;
private final SquareGrid grid;
InternalSquareIterator(SquareGrid grid) {
this.rowIndex = 1;
this.columnIndex = 1;
this.grid = grid;
}
@Override
public boolean hasNext() {
return rowIndex < grid.getNumberOfRows() - 1;
}
@Override
public Square next() {
final Square result = grid.getSquare(rowIndex, columnIndex);
if(columnIndex == grid.getNumberOfColumns() - 2){
rowIndex++;
columnIndex = 1;
}
else{
columnIndex++;
}
return result;
}
}
...@@ -25,4 +25,12 @@ public class RandomTileGenerator { ...@@ -25,4 +25,12 @@ public class RandomTileGenerator {
return new Tile(colorsOfTile); return new Tile(colorsOfTile);
} }
public Tile nextUniformTile(){
Color[] colorsOfTile = new Color[4];
Color randomColor = randomColor();
for(int index = 0; index < 4; index++)
colorsOfTile[index] = randomColor;
return new Tile(colorsOfTile);
}
} }
...@@ -4,27 +4,33 @@ import java.util.NoSuchElementException; ...@@ -4,27 +4,33 @@ import java.util.NoSuchElementException;
import java.util.Optional; import java.util.Optional;
public class Square { public class Square {
private Optional<Tile> optionalTile; private Tile tile;
private Square[] neighboringSquares;
private Square(Optional<Tile> tile) { private Square(Tile tile, Square[] neighboringSquares) {
this.optionalTile = tile; this.tile = tile;
this.neighboringSquares = neighboringSquares;
}
private Square(Square[] neighboringSquares) {
this(null, neighboringSquares);
} }
public Square(){ public Square(){
this(Optional.empty()); this(null, null);
} }
public Square(Tile tile){
this(Optional.of(tile));
}
public Tile getTile(){ public Tile getTile(){
if(!optionalTile.isPresent()) if(tile == null)
throw new NoSuchElementException(); throw new NoSuchElementException();
return optionalTile.get(); return tile;
} }
public void add(Tile tile) { public void add(Tile tile) {
optionalTile = Optional.of(tile); this.tile = tile;
}
public boolean hasTile() {
return tile != null;
} }
} }
...@@ -29,10 +29,17 @@ public class SquareGrid implements Iterable<Square> { ...@@ -29,10 +29,17 @@ public class SquareGrid implements Iterable<Square> {
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 < getNumberOfRows(); column++)
if(!squareIsInternal(row, column))
matrix[row][column] = new Square(); matrix[row][column] = new Square();
else {
matrix[row][column] = new Square();
}
return matrix; return matrix;
} }
private boolean squareIsInternal(int row, int column){
return 1 <= row && row < getNumberOfRows()-1 && 1 <= column && column < getNumberOfColumns()-1;
}
/** /**
* Returns an iterator over elements of type {@code T}. * Returns an iterator over elements of type {@code T}.
...@@ -41,7 +48,15 @@ public class SquareGrid implements Iterable<Square> { ...@@ -41,7 +48,15 @@ public class SquareGrid implements Iterable<Square> {
*/ */
@Override @Override
public Iterator<Square> iterator() { public Iterator<Square> iterator() {
return new GridIterator(this); return new SquareIterator(this);
}
public Iterator<Square> internalSquareIterator() {
return new InternalSquareIterator(this);
}
public Iterator<Square> borderSquareIterator() {
return new BorderSquareIterator(this);
} }
public Tile getTile(int rowIndex, int columnIndex) { public Tile getTile(int rowIndex, int columnIndex) {
...@@ -61,7 +76,11 @@ public class SquareGrid implements Iterable<Square> { ...@@ -61,7 +76,11 @@ 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);
for(Square square : this) internalSquareIterator().forEachRemaining(square -> square.add(randomTileGenerator.nextTile()));
square.add(randomTileGenerator.nextTile()); borderSquareIterator().forEachRemaining(square -> square.add(randomTileGenerator.nextUniformTile()));
}
public boolean hasTile(int row, int column) {
return getSquare(row, column).hasTile();
} }
} }
...@@ -2,12 +2,12 @@ package model; ...@@ -2,12 +2,12 @@ package model;
import java.util.Iterator; import java.util.Iterator;
public class GridIterator implements Iterator<Square> { public class SquareIterator implements Iterator<Square> {
private int rowIndex; private int rowIndex;
private int columnIndex; private int columnIndex;
private final SquareGrid grid; private final SquareGrid grid;
GridIterator(SquareGrid grid) { SquareIterator(SquareGrid grid) {
this.rowIndex = 0; this.rowIndex = 0;
this.columnIndex = 0; this.columnIndex = 0;
this.grid = grid; this.grid = grid;
...@@ -15,7 +15,7 @@ public class GridIterator implements Iterator<Square> { ...@@ -15,7 +15,7 @@ public class GridIterator implements Iterator<Square> {
@Override @Override
public boolean hasNext() { public boolean hasNext() {
return columnIndex < grid.getNumberOfColumns() && rowIndex < grid.getNumberOfRows(); return rowIndex < grid.getNumberOfRows();
} }
@Override @Override
......
...@@ -5,7 +5,6 @@ import javafx.scene.canvas.Canvas; ...@@ -5,7 +5,6 @@ import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext; import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
import model.CardinalDirection; import model.CardinalDirection;
import model.Square;
import model.SquareGrid; import model.SquareGrid;
import model.Tile; import model.Tile;
...@@ -16,8 +15,7 @@ public class GridTileCanvas extends Canvas { ...@@ -16,8 +15,7 @@ public class GridTileCanvas extends Canvas {
public final double tileWidth; public final double tileWidth;
private final double tileHeight; private final double tileHeight;
private final Integer numberOfColumns; private final SquareGrid tileGrid;
private final Integer numberOfRows;
public GridTileCanvas(@NamedArg("tileWidth") Double tileWidth, public GridTileCanvas(@NamedArg("tileWidth") Double tileWidth,
@NamedArg("tileHeight") Double tileHeight, @NamedArg("tileHeight") Double tileHeight,
...@@ -25,11 +23,12 @@ public class GridTileCanvas extends Canvas { ...@@ -25,11 +23,12 @@ public class GridTileCanvas extends Canvas {
@NamedArg("numberOfRows") Integer numberOfRows) { @NamedArg("numberOfRows") Integer numberOfRows) {
this.tileWidth = tileWidth; this.tileWidth = tileWidth;
this.tileHeight = tileHeight; this.tileHeight = tileHeight;
this.numberOfColumns = numberOfColumns;
this.numberOfRows = numberOfRows;
this.setWidth(tileWidth * numberOfColumns); this.setWidth(tileWidth * numberOfColumns);
this.setHeight(tileHeight * numberOfRows); this.setHeight(tileHeight * numberOfRows);
SquareGrid tileGrid = new SquareGrid(numberOfRows, numberOfColumns); tileGrid = new SquareGrid(numberOfRows, numberOfColumns);
}
public void initializeGrid(){
tileGrid.fillWithRandomTiles(List.of(Color.BLUE, Color.GREEN, Color.BLACK, Color.RED), new Random()); tileGrid.fillWithRandomTiles(List.of(Color.BLUE, Color.GREEN, Color.BLACK, Color.RED), new Random());
drawGridTile(tileGrid); drawGridTile(tileGrid);
} }
...@@ -37,6 +36,7 @@ public class GridTileCanvas extends Canvas { ...@@ -37,6 +36,7 @@ public class GridTileCanvas extends Canvas {
public void drawGridTile(SquareGrid tileGrid){ public void drawGridTile(SquareGrid tileGrid){
for(int row = 0; row < tileGrid.getNumberOfRows(); row++) for(int row = 0; row < tileGrid.getNumberOfRows(); row++)
for(int column = 0; column < tileGrid.getNumberOfColumns(); column++){ for(int column = 0; column < tileGrid.getNumberOfColumns(); column++){
if(tileGrid.hasTile(row, column))
drawTile(tileGrid.getTile(row, column), column, row); drawTile(tileGrid.getTile(row, column), column, row);
} }
} }
......
...@@ -12,13 +12,13 @@ ...@@ -12,13 +12,13 @@
fx:controller="controller.MainPaneController"> fx:controller="controller.MainPaneController">
<GridTileCanvas xmlns="http://javafx.com/javafx" <GridTileCanvas xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml" xmlns:fx="http://javafx.com/fxml"
fx:id="GridTileCanvas"> fx:id="gridTileCanvas">
<tileHeight>
<Double fx:value="10"/>
</tileHeight>
<tileWidth> <tileWidth>
<Double fx:value="10"/> <Double fx:value="10"/>
</tileWidth> </tileWidth>
<tileHeight>
<Double fx:value="10"/>
</tileHeight>
<numberOfColumns> <numberOfColumns>
<Integer fx:value="100"/> <Integer fx:value="100"/>
</numberOfColumns> </numberOfColumns>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment