diff --git a/src/main/net/curtlewis/maze/grid/Grid.java b/src/main/net/curtlewis/maze/grid/Grid.java index 4bd2738..779652f 100644 --- a/src/main/net/curtlewis/maze/grid/Grid.java +++ b/src/main/net/curtlewis/maze/grid/Grid.java @@ -30,15 +30,22 @@ for(int row = 0; row < rows; row++) { for(int col = 0; col < columns; col++) { cell = grid[row][col]; - if (row > 0 && row < rows - 1) { + + if (row > 0 && row < rows) { cell.setNorth(grid[row - 1][col]); + } + + if (row < rows - 1) { cell.setSouth(grid[row + 1][col]); } - if (col > 0 && col < columns - 1) { - cell.setWest(grid[row][col - 1]); + if (col < columns - 1) { cell.setEast(grid[row][col + 1]); } + + if (col > 0 && col < columns) { + cell.setWest(grid[row][col - 1]); + } } } } diff --git a/test/net/curtlewis/maze/grid/TestGrid.java b/test/net/curtlewis/maze/grid/TestGrid.java index 5074605..1e2a582 100644 --- a/test/net/curtlewis/maze/grid/TestGrid.java +++ b/test/net/curtlewis/maze/grid/TestGrid.java @@ -12,7 +12,6 @@ int cols = 4; Grid grid = new Grid(rows, cols); - assertEquals(rows * cols, grid.size()); } @Test @@ -53,4 +52,66 @@ } } } + + @Test + public void testNorth() { + int rows = 3; + int cols = 3; + Grid grid = new Grid(rows, cols); + Cell cell01 = grid.getCellAt(0, 1); + Cell cell11 = grid.getCellAt(1, 1); + Cell cell21 = grid.getCellAt(2, 1); + + System.out.println(grid.getCellAt(2, 0)); + + assertNull(cell01.getNorth()); + assertEquals(cell01, cell11.getNorth()); + assertEquals(cell11, cell21.getNorth()); + } + + @Test + public void testSouth() { + int rows = 3; + int cols = 3; + Grid grid = new Grid(rows, cols); + + Cell cell01 = grid.getCellAt(0, 1); + Cell cell11 = grid.getCellAt(1, 1); + Cell cell21 = grid.getCellAt(2, 1); + + assertEquals(cell11, cell01.getSouth()); + assertEquals(cell21, cell11.getSouth()); + assertNull(cell21.getSouth()); + + } + + @Test + public void testEast() { + int rows = 3; + int cols = 3; + Grid grid = new Grid(rows, cols); + + Cell cell00 = grid.getCellAt(0, 0); + Cell cell01 = grid.getCellAt(0, 1); + Cell cell02 = grid.getCellAt(0, 02); + + assertEquals(cell01, cell00.getEast()); + assertEquals(cell02, cell01.getEast()); + assertNull(cell02.getEast()); + } + + @Test + public void testWest() { + int rows = 3; + int cols = 3; + Grid grid = new Grid(rows, cols); + + Cell cell00 = grid.getCellAt(0, 0); + Cell cell01 = grid.getCellAt(0, 1); + Cell cell02 = grid.getCellAt(0, 02); + + assertNull(cell00.getWest()); + assertEquals(cell00, cell01.getWest()); + assertEquals(cell01, cell02.getWest()); + } }