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
Branches
No related tags found
No related merge requests found
......@@ -2,12 +2,12 @@ package model;
import java.util.Iterator;
public class GridIterator implements Iterator<Tile> {
public class GridIterator implements Iterator<Square> {
private int rowIndex;
private int columnIndex;
private final TileGrid grid;
private final SquareGrid grid;
GridIterator(TileGrid grid) {
GridIterator(SquareGrid grid) {
this.rowIndex = 0;
this.columnIndex = 0;
this.grid = grid;
......@@ -19,8 +19,8 @@ public class GridIterator implements Iterator<Tile> {
}
@Override
public Tile next() {
final Tile result = grid.getTile(rowIndex, columnIndex);
public Square next() {
final Square result = grid.getSquare(rowIndex, columnIndex);
columnIndex = (columnIndex +1) % grid.getNumberOfColumns();
if(columnIndex == 0){
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;
import java.util.List;
import java.util.Random;
public class TileGrid implements Iterable<Tile> {
public class SquareGrid implements Iterable<Square> {
private final int numberOfRows;
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.
......@@ -19,14 +19,18 @@ public class TileGrid implements Iterable<Tile> {
* @throws IllegalArgumentException if {@code numberOfRows} or {@code numberOfColumns} are
* less than or equal to 0
*/
public TileGrid(int numberOfRows, int numberOfColumns) {
public SquareGrid(int numberOfRows, int numberOfColumns) {
this.numberOfRows = numberOfRows;
this.numberOfColumns = numberOfColumns;
this.tiles = allocateMatrix();
this.squares = allocateMatrix();
}
private Tile[][] allocateMatrix() {
return new Tile[getNumberOfRows()][getNumberOfColumns()];
private Square[][] allocateMatrix() {
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> {
* @return an Iterator.
*/
@Override
public Iterator<Tile> iterator() {
public Iterator<Square> iterator() {
return new GridIterator(this);
}
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() {
return numberOfRows;
}
......@@ -54,9 +61,7 @@ public class TileGrid implements Iterable<Tile> {
public void fillWithRandomTiles(List<Color> colors, Random randomGenerator){
RandomTileGenerator randomTileGenerator = new RandomTileGenerator(colors, randomGenerator);
for(int row = 0; row < numberOfRows; row++)
for(int column = 0; column < numberOfColumns; column++){
tiles[row][column] = randomTileGenerator.nextTile();
}
for(Square square : this)
square.add(randomTileGenerator.nextTile());
}
}
......@@ -6,17 +6,17 @@ import java.util.List;
import java.util.Random;
public class TileGame {
private final TileGrid tileGrid;
private final SquareGrid tileGrid;
public TileGame(TileGrid tileGrid) {
public TileGame(SquareGrid tileGrid) {
this.tileGrid = tileGrid;
tileGrid.fillWithRandomTiles(List.of(Color.BLUE, Color.WHITE, Color.BLACK, Color.RED), new Random());
}
public TileGame() {
this(new TileGrid(10, 10));
this(new SquareGrid(10, 10));
}
public TileGrid getTileGrid() {
public SquareGrid getTileGrid() {
return tileGrid;
}
}
......@@ -5,8 +5,10 @@ import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import model.CardinalDirection;
import model.Square;
import model.SquareGrid;
import model.Tile;
import model.TileGrid;
import java.util.List;
import java.util.Random;
......@@ -27,12 +29,12 @@ public class GridTileCanvas extends Canvas {
this.numberOfRows = numberOfRows;
this.setWidth(tileWidth * numberOfColumns);
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());
drawGridTile(tileGrid);
}
public void drawGridTile(TileGrid tileGrid){
public void drawGridTile(SquareGrid tileGrid){
for(int row = 0; row < tileGrid.getNumberOfRows(); row++)
for(int column = 0; column < tileGrid.getNumberOfColumns(); column++){
drawTile(tileGrid.getTile(row, column), column, row);
......
......@@ -14,10 +14,10 @@
xmlns:fx="http://javafx.com/fxml"
fx:id="GridTileCanvas">
<tileHeight>
<Double fx:value="5"/>
<Double fx:value="10"/>
</tileHeight>
<tileWidth>
<Double fx:value="5"/>
<Double fx:value="10"/>
</tileWidth>
<numberOfColumns>
<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