diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0e049d2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.class +bin/ +lib/ \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..e67e2e5 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + + { + "type": "java", + "name": "MazeMain", + "request": "launch", + "mainClass": "MazeMain", + "console": "internalConsole", + "projectName": "maze_c4be2309" + } + ] +} + +// { +// "type": "java", +// "name": "Current File", +// "request": "launch", +// "mainClass": "${file}" +// }, \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..11a1b37 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,10 @@ +{ + "java.project.sourcePaths": [ + "src/main", + "test" + ], + "java.project.outputPath": "bin", + "java.project.referencedLibraries": [ + "lib/**/*.jar" + ] +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..b10f64c --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +## Maze + +Generates mazes + +Based on Jamis Buck's book *Mazes For Programmers*. \ No newline at end of file diff --git a/src/main/MazeMain.java b/src/main/MazeMain.java new file mode 100644 index 0000000..f87c62b --- /dev/null +++ b/src/main/MazeMain.java @@ -0,0 +1,11 @@ +import net.curtlewis.maze.grid.Cell; + +public class MazeMain { + + public static void main(String[] args) throws Exception { + System.out.println("MazeMain..."); + Cell cell = new Cell(1, 2); + System.out.println(cell); + } + +} diff --git a/src/main/net/curtlewis/maze/grid/Cell.java b/src/main/net/curtlewis/maze/grid/Cell.java new file mode 100644 index 0000000..83b816d --- /dev/null +++ b/src/main/net/curtlewis/maze/grid/Cell.java @@ -0,0 +1,90 @@ +package net.curtlewis.maze.grid; + +import java.util.Map; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class Cell { + + private int row; + private int col; + private Map links; + + private Cell north; + private Cell south; + private Cell east; + private Cell west; + + public Cell(int row, int col) { + this.row = row; + this.col = col; + this.links = new HashMap(); + } + + public int getRow() { return row; } + public int getCol() { return col; } + + public Cell getNorth() { return north; } + public void setNorth(Cell cell) { north = cell; } + + + public Cell getSouth() { return south; } + public void setSouth(Cell cell) { south = cell; } + + public Cell getEast() { return east; } + public void setEast(Cell cell) { east = cell; } + + public Cell getWest() { return west; } + public void setWest(Cell cell) { west = cell; } + + /** + * Link a this cell with its neighbor `cell`. If `bidi` is true + * set `cell` link back to this cell. + * @param cell Neighboring cell of this cell + * @param bidi bidirectional linking boolean + */ + public void link(Cell cell, boolean bidi) { + links.put(cell, true); + if (bidi) { + cell.link(this, false); + } + } + + /** + * Unlink this cell from its neighbor `cell`. If `bidi` is true + * unlink `cell` from this cell. + * @param cell + * @param bidi + */ + public void unlink(Cell cell, boolean bidi) { + if (links.containsKey(cell)) { + links.remove(cell); + } + + if (bidi) { + cell.unlink(this, false); + } + } + + public List getLinks() { + return new ArrayList<>(links.keySet()); + } + + public boolean hasLink(Cell cell) { + return links.containsKey(cell); + } + + public List neighbors() { + ArrayList list = new ArrayList<>(); + if (north != null) { list.add(north); } + if (south != null) { list.add(south); } + if (east != null) { list.add(east); } + if (west != null) { list.add(west); } + return list; + } + + public String toString() { + return "Cell[row: " + row + " col: " + col + "]"; + } +} diff --git a/test/net/curtlewis/maze/grid/CellTest.java b/test/net/curtlewis/maze/grid/CellTest.java new file mode 100644 index 0000000..06df1f7 --- /dev/null +++ b/test/net/curtlewis/maze/grid/CellTest.java @@ -0,0 +1,35 @@ +package net.curtlewis.maze.grid; + +import static org.junit.Assert.*; + +import java.util.List; + +import org.junit.Test; + +public class CellTest { + + @Test + public void testCellConstructor() { + Cell cell = new Cell(1, 2); + assertEquals(1, cell.getRow()); + assertEquals(2, cell.getCol()); + } + + @Test + public void testCellLinks() { + Cell cell1 = new Cell(1, 1); + Cell cell2 = new Cell(0, 1); + Cell cell3 = new Cell(3,3); + cell1.link(cell2, true); + + assertTrue(cell1.hasLink(cell2)); + assertTrue(cell2.hasLink(cell1)); + + assertFalse(cell1.hasLink(cell3)); + assertFalse(cell2.hasLink(cell3)); + + List cell3Links = cell3.getLinks(); + assertEquals(0, cell3Links.size()); + } + +}