|
| 1 | +# Maze_solver |
| 2 | +This program is a console-based maze solving in Java with BFS, DFS, A*. |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +Maze structure |
| 8 | +====== |
| 9 | +In this implementation, Mazes consists in a matrix of Squares. |
| 10 | + |
| 11 | +Here is the orthogonal reprensentation of a Maze: |
| 12 | +``` |
| 13 | +o---> X [Lines] |
| 14 | +| |
| 15 | +v |
| 16 | +Y [Columns] |
| 17 | +``` |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | +Solve mazes |
| 23 | +====== |
| 24 | + |
| 25 | +Firstly, see how to [load a maze](#load-a-maze) from a .txt file or [create one](#create-a-maze) directly from code. |
| 26 | + |
| 27 | +Next, refer to "[Use a solver](#use-a-solver)" to begin solving when your Maze is all set. |
| 28 | + |
| 29 | +Like any solver, you can also [set your own cardinal solving order](#changing-the-solving-cardinal-order) as the default one is North-East-South-West. |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | +Load a maze |
| 35 | +------ |
| 36 | +You can load a maze from a .txt as an argument in Maze class constructor, like this: |
| 37 | + |
| 38 | +```Java |
| 39 | +Maze myMaze = new Maze("./path/to/my/maze/myMaze.txt"); |
| 40 | +``` |
| 41 | +[Example in Main.java line 25][1] |
| 42 | + |
| 43 | +The file must be written within this form : |
| 44 | + |
| 45 | +``` |
| 46 | +-----------------e- |
| 47 | +--xxxxxxxxxxxx----- |
| 48 | +-------------x----- |
| 49 | +----s--------x----- |
| 50 | +-------------x----- |
| 51 | +------------------- |
| 52 | +------------------- |
| 53 | +``` |
| 54 | +The number of characters in the first line will be the maze number of columns and the number of lines... the number of lines. |
| 55 | + |
| 56 | +Every character is a square of the maze and is read like this: |
| 57 | +* `s` : maze starting point |
| 58 | +* `e` : maze end point |
| 59 | +* `x` : wall |
| 60 | +* Other ones (whatever they are) are empty squares. |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | +Create a maze |
| 67 | +------ |
| 68 | +It is also possible to create a maze directly from code by following those steps, but this is the most laborious method. |
| 69 | + |
| 70 | +__1. In Main.java, create a starting and a ending square:__ |
| 71 | +```Java |
| 72 | +Square start = new Square(1, 0, "S"); |
| 73 | +Square end = new Square(3, 4, "E"); |
| 74 | +``` |
| 75 | +Square constructor arguments: `Line (int), Column (int), Identifier (String)` |
| 76 | + |
| 77 | +`S` stands for starting point and `E` for ending point. |
| 78 | + |
| 79 | +__2. Create a new Maze:__ |
| 80 | +```Java |
| 81 | +Maze myMaze = new Maze(6, 5, start, end); |
| 82 | +``` |
| 83 | +Maze constructor arguments : `Number of lines (int), Number of columns (int), Starting point (Square), Ending point (Square)` |
| 84 | + |
| 85 | +__3. Create walls:__ |
| 86 | + |
| 87 | +(If you don't want walls, you can skip this step) |
| 88 | + |
| 89 | +Use the `setMazeWall()` method: |
| 90 | +```Java |
| 91 | +myMaze.setMazeWall(2, 0); |
| 92 | +myMaze.setMazeWall(3, 0); |
| 93 | +myMaze.setMazeWall(1, 2); |
| 94 | +myMaze.setMazeWall(2, 2); |
| 95 | +... |
| 96 | +``` |
| 97 | +Arguments: `Wall line pos (int), Wall column pos (int)` |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | +Use a solver |
| 104 | +------ |
| 105 | +You have 3 solvers available, each one corresponding to one algorithm: |
| 106 | +* AStarSolver |
| 107 | +* BFS_Solver |
| 108 | +* DFS_Solver |
| 109 | + |
| 110 | +Simply use |
| 111 | +```Java |
| 112 | +BFS_Solver bfsSolver = new BFS_Solver(myMaze); |
| 113 | +DFS_Solver dfsSolver = new DFS_Solver(myMaze); |
| 114 | +AStarSolver aStarSolver = new AStarSolver(myMaze, true); |
| 115 | + |
| 116 | +System.out.println(bfsSolver.solve()); |
| 117 | +System.out.println(dfsSolver.solve()); |
| 118 | +System.out.println(aStarSolver.solve()); |
| 119 | +``` |
| 120 | + |
| 121 | +Note that `AStarSolver` takes one other boolean argument to specifies if you want your maze to be solve by A* with Manhattan or Euclidean heuristic. |
| 122 | + |
| 123 | +Set to `true` to use Manhattan heuristic. Euclidean heuristic otherwhise. |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | + |
| 128 | +Changing the solving cardinal order |
| 129 | +------ |
| 130 | +To change the order the solvers will use to test squares, use |
| 131 | +```Java |
| 132 | +char[] order = {'W', 'E', 'N', 'S'}; |
| 133 | +myMaze.setOrder(order); |
| 134 | +``` |
| 135 | +As the exemple shows it, the order is now West-East-North-South |
| 136 | + |
| 137 | + |
| 138 | + |
| 139 | + |
| 140 | +About the author |
| 141 | +====== |
| 142 | +My name is Gabriel, I'm a french student in Video Games Development in Canada |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | +[1]:https://github.com/Gaderr/Maze_solver/blob/a6ac782c316adee15d8029a12531a7fcd5f659ef/src/Main.java#L25 |
0 commit comments