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

New simpler version withour borders

parent 65c97d6e
No related branches found
No related tags found
No related merge requests found
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;
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;
}
}
......@@ -12,7 +12,7 @@ public class Square {
}
public Square(){
this(null, null);
this(null, new Square[4]);
}
......@@ -31,7 +31,7 @@ public class Square {
}
public Optional<SquareTile> getNeighboringSquare(CardinalDirection side) {
if (neighboringSquares == null || neighboringSquares[side.ordinal()] == null)
if (neighboringSquares[side.ordinal()] == null)
return Optional.empty();
return neighboringSquares[side.ordinal()].getSquareTile();
}
......@@ -39,4 +39,7 @@ public class Square {
public void setNeighboringSquares(Square[] neighboringSquares) {
this.neighboringSquares = neighboringSquares;
}
public void setNeighboringSquare(Square neighboringSquare, CardinalDirection direction) {
this.neighboringSquares[direction.ordinal()] = neighboringSquare;
}
}
......@@ -23,28 +23,39 @@ public class SquareGrid implements Iterable<Square> {
public SquareGrid(int numberOfRows, int numberOfColumns) {
this.numberOfRows = numberOfRows;
this.numberOfColumns = numberOfColumns;
this.squares = allocateMatrix();
squares = new Square[getNumberOfRows()][getNumberOfColumns()];
initializeGrid();
}
private Square[][] allocateMatrix() {
Square[][] matrix = new Square[getNumberOfRows()][getNumberOfColumns()];
private void initializeGrid() {
initializeSquares();
initializeNeighborhood();
}
private void initializeNeighborhood() {
for(int row = 0; row < getNumberOfRows(); row++)
for(int column = 0; column < getNumberOfColumns(); column++)
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];
for(int column = 0; column < getNumberOfColumns(); column++) {
for (CardinalDirection direction : CardinalDirection.values()){
int rowNeighbor = row + direction.deltaRow;
int columnNeighbor = column + direction.deltaColumn;
if(containsCoordinates(rowNeighbor, columnNeighbor))
squares[row][column].setNeighboringSquare(squares[rowNeighbor][columnNeighbor], direction);
}
}
matrix[row][column].setNeighboringSquares(neighboringSquares);
}
return matrix;
private void initializeSquares() {
for(int row = 0; row < getNumberOfRows(); row++)
for(int column = 0; column < getNumberOfColumns(); column++)
squares[row][column] = new Square();
}
private boolean containsCoordinates(int row, int column){
return 0 <= row && row < numberOfRows && 0 <= column && column < numberOfColumns;
}
/**
* Returns an iterator over elements of type {@code T}.
* Returns an iterator over Squares.
*
* @return an Iterator.
*/
......@@ -53,14 +64,6 @@ public class SquareGrid implements Iterable<Square> {
return new SquareIterator(this);
}
public Iterator<Square> internalSquareIterator() {
return new InternalSquareIterator(this);
}
public Iterator<Square> borderSquareIterator() {
return new BorderSquareIterator(this);
}
public Optional<SquareTile> getTile(int rowIndex, int columnIndex) {
return getSquare(rowIndex, columnIndex).getSquareTile();
}
......@@ -78,9 +81,8 @@ public class SquareGrid implements Iterable<Square> {
public void fillWithRandomTiles(List<Color> colors, Random randomGenerator){
RandomWangTileGenerator randomTileGenerator = new RandomWangTileGenerator(colors, randomGenerator);
borderSquareIterator().forEachRemaining(square -> square.add(randomTileGenerator.nextUniformTile()));
internalSquareIterator().forEachRemaining(square -> square.add(randomTileGenerator.nextTileWithNeighborhoodConstraint(square)));
for(Square square : this)
square.add(randomTileGenerator.nextTileWithNeighborhoodConstraint(square));
}
public boolean hasTile(int row, int column) {
......
package model;
public class SquareGridCoordinates {
private int rowIndex;
private int columnIndex;
public SquareGridCoordinates(int rowIndex, int columnIndex) {
this.rowIndex = rowIndex;
this.columnIndex = columnIndex;
}
public SquareGridCoordinates(){
this(0, 0);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment