Skip to content
Snippets Groups Projects
Commit a829f37c authored by Martin Vatshelle's avatar Martin Vatshelle
Browse files

Implemented iterator for Grid

parent d8774d5e
No related branches found
No related tags found
No related merge requests found
package no.uib.inf101.grid;
import java.util.Iterator;
public class CellPosIterator implements Iterator<CellPosition>{
int rows;
int cols;
int curRow;
int curCol;
public CellPosIterator(int rows, int cols) {
this.rows=rows;
this.cols = cols;
curRow=0;
curCol=0;
}
@Override
public boolean hasNext() {
return curRow<rows && curCol<cols;
}
@Override
public CellPosition next() {
CellPosition pos = new CellPosition(curRow, curCol);
curRow++;
if(curRow>=rows) {
curRow=0;
curCol++;
}
return pos;
}
}
package no.uib.inf101.grid;
public record CellPosition(int row, int col) {
}
package no.uib.inf101.grid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
public class Grid implements IGrid {
@Override
public Iterator<GridCell> iterator() {
ArrayList<GridCell> cells = new ArrayList<>();
for(int row=0; row<rows(); row++) {
for(int col=0; col<cols; col++) {
CellPosition pos = new CellPosition(row, col);
cells.add(new GridCell(pos, get(pos)));
}
}
return cells.iterator();
}
private int rows;
private int cols;
HashMap<CellPosition,Character> map;
public Grid(int rows, int cols) {
this.rows = rows;
this.cols = cols;
map = new HashMap<>();
}
public Grid(int rows, int cols, Character defaultValue) {
this(rows,cols);
fill(defaultValue);
}
private void fill(Character defaultValue) {
CellPosIterator posIter = new CellPosIterator(rows, cols);
while(posIter.hasNext()) {
set(posIter.next(),defaultValue);
}
}
@Override
public int rows() {
return rows;
}
@Override
public int cols() {
return cols;
}
@Override
public void set(CellPosition pos, Character symbol) {
map.put(pos, symbol);
}
@Override
public Character get(CellPosition pos) {
if(!positionIsOnGrid(pos))
throw new IndexOutOfBoundsException("Position must be on the grid");
return map.get(pos);
}
@Override
public boolean positionIsOnGrid(CellPosition pos) {
if(pos==null)
return false;
if(pos.row()<0 || pos.row()>=rows)
return false;
if(pos.col()<0 || pos.col()>=cols)
return false;
return true;
}
}
package no.uib.inf101.grid;
public record GridCell(CellPosition pos, Character symbol) {
}
package no.uib.inf101.grid;
import java.util.Iterator;
public class GridCellIterator implements Iterator<GridCell> {
IGrid grid;
CellPosIterator positionsIter;
public GridCellIterator(Grid grid) {
this.grid = grid;
positionsIter = new CellPosIterator(grid.rows(),grid.cols());
}
@Override
public boolean hasNext() {
return positionsIter.hasNext();
}
@Override
public GridCell next() {
CellPosition pos = positionsIter.next();
return new GridCell(pos, grid.get(pos));
}
}
package no.uib.inf101.grid;
public interface GridDimension {
/** Number of rows in the grid */
int rows();
/** Number of columns in the grid */
int cols();
}
package no.uib.inf101.grid;
public interface IGrid extends GridDimension, Iterable<GridCell> {
/**
* Sets the value of a position in the grid. A subsequent call to {@link #get}
* with an equal position as argument will return the value which was set. The
* method will overwrite any previous value that was stored at the location.
*
* @param pos the position in which to store the value
* @param symbol the new value
* @throws IndexOutOfBoundsException if the position does not exist in the grid
*/
void set(CellPosition pos, Character symbol);
/**
* Gets the current value at the given coordinate.
*
* @param pos the position to get
* @return the value stored at the position
* @throws IndexOutOfBoundsException if the position does not exist in the grid
*/
Character get(CellPosition pos);
/**
* Reports whether the position is within bounds for this grid
*
* @param pos position to check
* @return true if the coordinate is within bounds, false otherwise
*/
boolean positionIsOnGrid(CellPosition pos);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment