├── 3232maze.png ├── EPYCLINES.png ├── LICENSE ├── New Piskel(2).png ├── README.md ├── epic.png ├── loopyloop.png ├── maze_solver.py ├── requirements.txt ├── stares.png ├── superskrible.png └── whitespace.png /3232maze.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JonPizza/recursive-maze-solver/7a68ba7ed00c0e5f74430b9cf520b979a241e0f8/3232maze.png -------------------------------------------------------------------------------- /EPYCLINES.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JonPizza/recursive-maze-solver/7a68ba7ed00c0e5f74430b9cf520b979a241e0f8/EPYCLINES.png -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 JonPizza 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /New Piskel(2).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JonPizza/recursive-maze-solver/7a68ba7ed00c0e5f74430b9cf520b979a241e0f8/New Piskel(2).png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Recursive Maze Solver! 2 | 3 | ![recursive-maze-solver gif](https://i.imgur.com/kLDZJq8.gif) 4 | 5 | [![scriptlytics flag](https://jon.network/scriptlytics/34/image/)](https://jon.network/scriptlytics/34/) 6 | 7 | Contributers: Welcome! 8 | 9 | An algorithm that solves mazes recursivly. It displays to the console using ansi escape codes! Thanks to @drasis for the neat-o gif! 10 | 11 | ## ToDos 12 | 13 | Display the maze with pygame or ncurses for Windows users to enjoy! 14 | 15 | ## Links 16 | 17 | Blog Post: https://jon.network/programming/62/Recursive-Maze-Solver/ 18 | 19 | Chatroom: https://jon.network/chat/ 20 | 21 | My Other Blog Posts: https://jon.network/programming-articles/ 22 | -------------------------------------------------------------------------------- /epic.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JonPizza/recursive-maze-solver/7a68ba7ed00c0e5f74430b9cf520b979a241e0f8/epic.png -------------------------------------------------------------------------------- /loopyloop.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JonPizza/recursive-maze-solver/7a68ba7ed00c0e5f74430b9cf520b979a241e0f8/loopyloop.png -------------------------------------------------------------------------------- /maze_solver.py: -------------------------------------------------------------------------------- 1 | from PIL import Image 2 | from time import sleep 3 | import sys 4 | 5 | 6 | def open_img(img, dim): 7 | img = Image.open(img) 8 | px_map = [] 9 | for x in range(dim): 10 | px_row = [] 11 | for y in range(dim): 12 | px_row.append('b' if img.load()[y, x] == (0, 0, 0, 255) else 'w') 13 | px_map.append(px_row) 14 | return px_map 15 | 16 | 17 | def show_image(img_list): 18 | print('\n\n') 19 | for i in img_list: 20 | p = '' 21 | for x in i: 22 | if x == 'r': 23 | p += '\033[41m \033[0m' 24 | elif x == 'b': 25 | p += '\033[40m \033[0m' 26 | elif x == 'w': 27 | p += '\033[47m \033[0m' 28 | print(p) 29 | 30 | 31 | def solve(maze): 32 | path = [(0, maze[0].index('w'))] 33 | win = [len(maze), maze[-1].index('w')] 34 | try: 35 | recurse_solve(maze, win, path) 36 | except: 37 | return 'Done!' 38 | 39 | def recurse_solve(maze, win, path): 40 | if win in path: 41 | return 'SOLVED!!!' 42 | if maze[path[-1][0]][path[-1][1]] == 'b' or maze[path[-1][0]][path[-1][1]] == 'r' or path[-1] in path[:-1]: 43 | return [] 44 | maze[path[-1][0]][path[-1][1]] = 'r' 45 | show_image(maze) 46 | sleep(0.03) 47 | 48 | r1 = recurse_solve(maze, win, path+[(path[-1][0]+1, path[-1][1])]) 49 | r2 = recurse_solve(maze, win, path+[(path[-1][0], path[-1][1]+1)]) 50 | r3 = recurse_solve(maze, win, path+[(path[-1][0]-1, path[-1][1])]) 51 | r4 = recurse_solve(maze, win, path+[(path[-1][0], path[-1][1]-1)]) 52 | arrs = [len(r1), len(r2), len(r3), len(r4)] 53 | if arrs.index(max(arrs)) == 0: 54 | return r1 55 | elif arrs.index(max(arrs)) == 1: 56 | return r2 57 | elif arrs.index(max(arrs)) == 2: 58 | return r3 59 | else: 60 | return r4 61 | 62 | 63 | maze = open_img(input('da maze 2 solve: '), 32) 64 | solve(maze) 65 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | certifi==2019.9.11 2 | chardet==3.0.4 3 | idna==2.8 4 | Pillow==6.2.1 5 | requests==2.22.0 6 | urllib3==1.25.7 7 | -------------------------------------------------------------------------------- /stares.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JonPizza/recursive-maze-solver/7a68ba7ed00c0e5f74430b9cf520b979a241e0f8/stares.png -------------------------------------------------------------------------------- /superskrible.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JonPizza/recursive-maze-solver/7a68ba7ed00c0e5f74430b9cf520b979a241e0f8/superskrible.png -------------------------------------------------------------------------------- /whitespace.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JonPizza/recursive-maze-solver/7a68ba7ed00c0e5f74430b9cf520b979a241e0f8/whitespace.png --------------------------------------------------------------------------------