Remove the wall between the current cell and the chosen cell. Choose randomly one of the unvisited neighbours. If the current cell has any neighbors which have not been visited. Make the initial cell the current cell and mark it as visited. There are lots of algorithms to do that (see here,) but since it is easier to implement and understand than others, I have used recursiveīacktracking method (or if you look in a general perspective DFS -Depth First Search- algorithm.) Recursive Backtracking Algorithm We want to generate a maze which has only one entrance, one exit and there must be only one path How It Does That? The ProblemĪctually problem is very simple. Java maze generator algorithm code#
My goal to put this code on github is to give some examples about using simple GUI components (buttons, panels, slide bars etc.) andĬreating basic XML output in Java.
If don’t like it, you can generate a new one. If you want to watch generation process click on Generate & Simulate button and control the speed of the process via slide bar. In that way, we get a maze that has only one solution and one exit. The application generates maze with using recursive backtracking method. After my graduation, I found it one more time and this time I added XML output function, also decided to writeĪ blog post about it. I found that console application on my disk drive and added UI (Yes, just for fun.) And I forgotĪbout it again. Then I forgot about it but, one year later when I was on holiday, I wrote a basic console application just for fun. I needed a simple maze generator for some homeworks of my AI course, while I was studying at university.