diff --git a/src/main/net/curtlewis/maze/grid/Grid.java b/src/main/net/curtlewis/maze/grid/Grid.java new file mode 100644 index 0000000..4bd2738 --- /dev/null +++ b/src/main/net/curtlewis/maze/grid/Grid.java @@ -0,0 +1,77 @@ +package net.curtlewis.maze.grid; + +public class Grid { + + private int rows; + private int columns; + private Cell[][] grid; + + public Grid (int rows, int columns) { + this.rows = rows; + this.columns = columns; + + grid = new Cell[rows][columns]; + + prepareGrid(); + configureCells(); + } + + private void prepareGrid() { + + for(int row = 0; row < rows; row++) { + for(int col = 0; col < columns; col++) { + grid[row][col] = new Cell(row, col); + } + } + } + + private void configureCells() { + Cell cell = null; + for(int row = 0; row < rows; row++) { + for(int col = 0; col < columns; col++) { + cell = grid[row][col]; + if (row > 0 && row < rows - 1) { + cell.setNorth(grid[row - 1][col]); + cell.setSouth(grid[row + 1][col]); + } + + if (col > 0 && col < columns - 1) { + cell.setWest(grid[row][col - 1]); + cell.setEast(grid[row][col + 1]); + } + } + } + } + + public Cell getCellAt(int row, int col) { + if (row >= 0 && row < rows && col >= 0 && col < columns) { + return grid[row][col]; + } else { + return null; + } + } + + public Cell[] getRowAt(int row) { + if (row >= 0 && row < rows) { + return grid[row]; + } else { + return null; + } + } + + + public Cell getRandomCell() { + int randomRow = (int)(Math.random()) * (rows - 1); + int randomCol = (int)(Math.random()) * (columns - 1); + return grid[randomRow][randomCol]; + } + + public int size() { + return rows * columns; + } + + public String toString() { + return "Grid(rows: " + rows + " , cols: " + columns + "); size = " + size(); + } + +} diff --git a/test/net/curtlewis/maze/grid/TestGrid.java b/test/net/curtlewis/maze/grid/TestGrid.java new file mode 100644 index 0000000..5074605 --- /dev/null +++ b/test/net/curtlewis/maze/grid/TestGrid.java @@ -0,0 +1,56 @@ +package net.curtlewis.maze.grid; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class TestGrid { + + @Test + public void testGridSize() { + int rows = 4; + int cols = 4; + Grid grid = new Grid(rows, cols); + + assertEquals(rows * cols, grid.size()); + } + + @Test + public void testGridCellLocation() { + int rows = 4; + int cols = 4; + Grid grid = new Grid(rows, cols); + + for(int r = 0; r < rows; r++) { + for(int c = 0; c < cols; c++) { + Cell cell = grid.getCellAt(r,c); + assertEquals(r, cell.getRow()); + assertEquals(c, cell.getCol()); + } + } + } + + @Test + public void testCellIsNull() { + Grid grid = new Grid(4, 4); + assertNull(grid.getCellAt(-1, 3)); + assertNull(grid.getCellAt(1, -1)); + assertNull(grid.getCellAt(0, 4)); + assertNull(grid.getCellAt(4, 0)); + assertNull(grid.getCellAt(0, 5)); + assertNull(grid.getCellAt(5, 0)); + } + + @Test + public void testGetIndividualRow() { + int rows = 4; + int cols = 4; + Grid grid = new Grid(rows, cols); + for(int r = 0; r < rows; r++) { + Cell[] cellsInRow = grid.getRowAt(r); + for(int c = 0; c < cols; c++) { + assertEquals(grid.getCellAt(r, c), cellsInRow[c]); + } + } + } +}