diff --git a/src/main/java/no/uib/inf101/grid/CellPosIterator.java b/src/main/java/no/uib/inf101/grid/CellPosIterator.java new file mode 100644 index 0000000000000000000000000000000000000000..88c5b095865dc2ac3c7ba2eb9c278eda531d43e5 --- /dev/null +++ b/src/main/java/no/uib/inf101/grid/CellPosIterator.java @@ -0,0 +1,35 @@ +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; + } + +} diff --git a/src/main/java/no/uib/inf101/grid/CellPosition.java b/src/main/java/no/uib/inf101/grid/CellPosition.java new file mode 100644 index 0000000000000000000000000000000000000000..1cab7c3750aaf0edaf628b7bd2c2bd52b8623dd0 --- /dev/null +++ b/src/main/java/no/uib/inf101/grid/CellPosition.java @@ -0,0 +1,5 @@ +package no.uib.inf101.grid; + +public record CellPosition(int row, int col) { + +} diff --git a/src/main/java/no/uib/inf101/grid/Grid.java b/src/main/java/no/uib/inf101/grid/Grid.java new file mode 100644 index 0000000000000000000000000000000000000000..d289df6e96b02c6f7013479222254f29c8da454b --- /dev/null +++ b/src/main/java/no/uib/inf101/grid/Grid.java @@ -0,0 +1,89 @@ +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; + } +} diff --git a/src/main/java/no/uib/inf101/grid/GridCell.java b/src/main/java/no/uib/inf101/grid/GridCell.java new file mode 100644 index 0000000000000000000000000000000000000000..37873f7dd1b466b162d9bf9d7be5110070a26a9d --- /dev/null +++ b/src/main/java/no/uib/inf101/grid/GridCell.java @@ -0,0 +1,5 @@ +package no.uib.inf101.grid; + +public record GridCell(CellPosition pos, Character symbol) { + +} diff --git a/src/main/java/no/uib/inf101/grid/GridCellIterator.java b/src/main/java/no/uib/inf101/grid/GridCellIterator.java new file mode 100644 index 0000000000000000000000000000000000000000..0f9b423e20880f366d1c83bce17e9383d9e34c77 --- /dev/null +++ b/src/main/java/no/uib/inf101/grid/GridCellIterator.java @@ -0,0 +1,26 @@ +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)); + } + +} diff --git a/src/main/java/no/uib/inf101/grid/GridDimension.java b/src/main/java/no/uib/inf101/grid/GridDimension.java new file mode 100644 index 0000000000000000000000000000000000000000..25ab31f73dd532dfa268b05581694b14558f029d --- /dev/null +++ b/src/main/java/no/uib/inf101/grid/GridDimension.java @@ -0,0 +1,11 @@ +package no.uib.inf101.grid; + +public interface GridDimension { + + /** Number of rows in the grid */ + int rows(); + + /** Number of columns in the grid */ + int cols(); + +} diff --git a/src/main/java/no/uib/inf101/grid/IGrid.java b/src/main/java/no/uib/inf101/grid/IGrid.java new file mode 100644 index 0000000000000000000000000000000000000000..8e0f9c885e339c088dd2ef02025f9b7410653334 --- /dev/null +++ b/src/main/java/no/uib/inf101/grid/IGrid.java @@ -0,0 +1,32 @@ +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); +}