From a829f37c2d81dd6445e17e488916e14943248737 Mon Sep 17 00:00:00 2001 From: Martin Vatshelle <mva021@ii0233228.klient.uib.no> Date: Thu, 20 Mar 2025 12:27:01 +0100 Subject: [PATCH] Implemented iterator for Grid --- .../no/uib/inf101/grid/CellPosIterator.java | 35 ++++++++ .../java/no/uib/inf101/grid/CellPosition.java | 5 ++ src/main/java/no/uib/inf101/grid/Grid.java | 89 +++++++++++++++++++ .../java/no/uib/inf101/grid/GridCell.java | 5 ++ .../no/uib/inf101/grid/GridCellIterator.java | 26 ++++++ .../no/uib/inf101/grid/GridDimension.java | 11 +++ src/main/java/no/uib/inf101/grid/IGrid.java | 32 +++++++ 7 files changed, 203 insertions(+) create mode 100644 src/main/java/no/uib/inf101/grid/CellPosIterator.java create mode 100644 src/main/java/no/uib/inf101/grid/CellPosition.java create mode 100644 src/main/java/no/uib/inf101/grid/Grid.java create mode 100644 src/main/java/no/uib/inf101/grid/GridCell.java create mode 100644 src/main/java/no/uib/inf101/grid/GridCellIterator.java create mode 100644 src/main/java/no/uib/inf101/grid/GridDimension.java create mode 100644 src/main/java/no/uib/inf101/grid/IGrid.java 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 0000000..88c5b09 --- /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 0000000..1cab7c3 --- /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 0000000..d289df6 --- /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 0000000..37873f7 --- /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 0000000..0f9b423 --- /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 0000000..25ab31f --- /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 0000000..8e0f9c8 --- /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); +} -- GitLab