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

Added square to have a grid of squares that can contain tiles.

parent 094538f2
No related branches found
No related tags found
No related merge requests found
...@@ -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<Tile> { public class GridIterator implements Iterator<Square> {
private int rowIndex; private int rowIndex;
private int columnIndex; private int columnIndex;
private final TileGrid grid; private final SquareGrid grid;
GridIterator(TileGrid grid) { GridIterator(SquareGrid grid) {
this.rowIndex = 0; this.rowIndex = 0;
this.columnIndex = 0; this.columnIndex = 0;
this.grid = grid; this.grid = grid;
...@@ -19,8 +19,8 @@ public class GridIterator implements Iterator<Tile> { ...@@ -19,8 +19,8 @@ public class GridIterator implements Iterator<Tile> {
} }
@Override @Override
public Tile next() { public Square next() {
final Tile result = grid.getTile(rowIndex, columnIndex); final Square result = grid.getSquare(rowIndex, columnIndex);
columnIndex = (columnIndex +1) % grid.getNumberOfColumns(); columnIndex = (columnIndex +1) % grid.getNumberOfColumns();
if(columnIndex == 0){ if(columnIndex == 0){
rowIndex++; rowIndex++;
......
package model;
import java.util.NoSuchElementException;
import java.util.Optional;
public class Square {
private Optional<Tile> optionalTile;
private Square(Optional<Tile> tile) {
this.optionalTile = tile;
}
public Square(){
this(Optional.empty());
}
public Square(Tile tile){
this(Optional.of(tile));
}
public Tile getTile(){
if(!optionalTile.isPresent())
throw new NoSuchElementException();
return optionalTile.get();
}
public void add(Tile tile) {
optionalTile = Optional.of(tile);
}
}
...@@ -6,10 +6,10 @@ import java.util.Iterator; ...@@ -6,10 +6,10 @@ import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
public class TileGrid implements Iterable<Tile> { public class SquareGrid implements Iterable<Square> {
private final int numberOfRows; private final int numberOfRows;
private final int numberOfColumns; private final int numberOfColumns;
private final Tile[][] tiles; private final Square[][] squares;
/** /**
* Creates a new {@code Grid} instance given the number of rows and columns. * Creates a new {@code Grid} instance given the number of rows and columns.
...@@ -19,14 +19,18 @@ public class TileGrid implements Iterable<Tile> { ...@@ -19,14 +19,18 @@ public class TileGrid implements Iterable<Tile> {
* @throws IllegalArgumentException if {@code numberOfRows} or {@code numberOfColumns} are * @throws IllegalArgumentException if {@code numberOfRows} or {@code numberOfColumns} are
* less than or equal to 0 * less than or equal to 0
*/ */
public TileGrid(int numberOfRows, int numberOfColumns) { public SquareGrid(int numberOfRows, int numberOfColumns) {
this.numberOfRows = numberOfRows; this.numberOfRows = numberOfRows;
this.numberOfColumns = numberOfColumns; this.numberOfColumns = numberOfColumns;
this.tiles = allocateMatrix(); this.squares = allocateMatrix();
} }
private Tile[][] allocateMatrix() { private Square[][] allocateMatrix() {
return new Tile[getNumberOfRows()][getNumberOfColumns()]; Square[][] matrix = new Square[getNumberOfRows()][getNumberOfColumns()];
for(int row = 0; row < getNumberOfRows(); row++)
for(int column = 0; column < getNumberOfRows(); column++)
matrix[row][column] = new Square();
return matrix;
} }
...@@ -36,14 +40,17 @@ public class TileGrid implements Iterable<Tile> { ...@@ -36,14 +40,17 @@ public class TileGrid implements Iterable<Tile> {
* @return an Iterator. * @return an Iterator.
*/ */
@Override @Override
public Iterator<Tile> iterator() { public Iterator<Square> iterator() {
return new GridIterator(this); return new GridIterator(this);
} }
public Tile getTile(int rowIndex, int columnIndex) { public Tile getTile(int rowIndex, int columnIndex) {
return tiles[rowIndex][columnIndex]; return getSquare(rowIndex,columnIndex).getTile();
} }
public Square getSquare(int rowIndex, int columnIndex) {
return squares[rowIndex][columnIndex];
}
public int getNumberOfRows() { public int getNumberOfRows() {
return numberOfRows; return numberOfRows;
} }
...@@ -54,9 +61,7 @@ public class TileGrid implements Iterable<Tile> { ...@@ -54,9 +61,7 @@ public class TileGrid implements Iterable<Tile> {
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(int row = 0; row < numberOfRows; row++) for(Square square : this)
for(int column = 0; column < numberOfColumns; column++){ square.add(randomTileGenerator.nextTile());
tiles[row][column] = randomTileGenerator.nextTile();
}
} }
} }
...@@ -6,17 +6,17 @@ import java.util.List; ...@@ -6,17 +6,17 @@ import java.util.List;
import java.util.Random; import java.util.Random;
public class TileGame { public class TileGame {
private final TileGrid tileGrid; private final SquareGrid tileGrid;
public TileGame(TileGrid tileGrid) { public TileGame(SquareGrid tileGrid) {
this.tileGrid = tileGrid; this.tileGrid = tileGrid;
tileGrid.fillWithRandomTiles(List.of(Color.BLUE, Color.WHITE, Color.BLACK, Color.RED), new Random()); tileGrid.fillWithRandomTiles(List.of(Color.BLUE, Color.WHITE, Color.BLACK, Color.RED), new Random());
} }
public TileGame() { public TileGame() {
this(new TileGrid(10, 10)); this(new SquareGrid(10, 10));
} }
public TileGrid getTileGrid() { public SquareGrid getTileGrid() {
return tileGrid; return tileGrid;
} }
} }
...@@ -5,8 +5,10 @@ import javafx.scene.canvas.Canvas; ...@@ -5,8 +5,10 @@ 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.Tile; import model.Tile;
import model.TileGrid;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
...@@ -27,12 +29,12 @@ public class GridTileCanvas extends Canvas { ...@@ -27,12 +29,12 @@ public class GridTileCanvas extends Canvas {
this.numberOfRows = numberOfRows; this.numberOfRows = numberOfRows;
this.setWidth(tileWidth * numberOfColumns); this.setWidth(tileWidth * numberOfColumns);
this.setHeight(tileHeight * numberOfRows); this.setHeight(tileHeight * numberOfRows);
TileGrid tileGrid = new TileGrid(numberOfRows, numberOfColumns); SquareGrid tileGrid = new SquareGrid(numberOfRows, numberOfColumns);
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);
} }
public void drawGridTile(TileGrid 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++){
drawTile(tileGrid.getTile(row, column), column, row); drawTile(tileGrid.getTile(row, column), column, row);
......
...@@ -14,10 +14,10 @@ ...@@ -14,10 +14,10 @@
xmlns:fx="http://javafx.com/fxml" xmlns:fx="http://javafx.com/fxml"
fx:id="GridTileCanvas"> fx:id="GridTileCanvas">
<tileHeight> <tileHeight>
<Double fx:value="5"/> <Double fx:value="10"/>
</tileHeight> </tileHeight>
<tileWidth> <tileWidth>
<Double fx:value="5"/> <Double fx:value="10"/>
</tileWidth> </tileWidth>
<numberOfColumns> <numberOfColumns>
<Integer fx:value="100"/> <Integer fx:value="100"/>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment