├── README.md └── main.py /README.md: -------------------------------------------------------------------------------- 1 | # Maze Solver | Path Finder with Python 2 | 3 | This is a simple maze solver written in Python using the curses library for terminal graphics. 4 | 5 | ## Installation 6 | 7 | To run this program, you will need to have Python 3.x installed on your computer. You can download Python from the official website. 8 | 9 | Additionally, you will need to install the curses library. If you are using a Unix-based system (e.g. Linux, macOS), this library should already be included with your system. If you are using Windows, you may need to install it separately. 10 | 11 | ## Usage 12 | 13 | To use the program, simply run the `main.py` script from the command line: 14 | 15 | ```bash 16 | python main.py 17 | ``` 18 | 19 | The program will display the maze and highlight the shortest path from the starting point (marked with an 'O') to the end point (marked with an 'X'). 20 | 21 | You can modify the maze by editing the `maze` variable at the top of the script. 22 | 23 | ## License 24 | 25 | This program is licensed under the MIT [License](https://opensource.org/license/mit/). See the LICENSE file for more information. 26 | 27 | By programmer [Andrew Tsegaye](https://github.com/Andrew-Tsegaye) 28 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | import curses 2 | from curses import wrapper 3 | import queue 4 | import time 5 | 6 | maze = [ 7 | ["#", "O", "#", "#", "#", "#", "#", "#", "#"], 8 | ["#", " ", " ", " ", " ", " ", " ", " ", "#"], 9 | ["#", " ", "#", "#", " ", "#", "#", " ", "#"], 10 | ["#", " ", "#", " ", " ", " ", "#", " ", "#"], 11 | ["#", " ", "#", " ", "#", " ", "#", " ", "#"], 12 | ["#", " ", "#", " ", "#", " ", "#", " ", "#"], 13 | ["#", " ", "#", " ", "#", " ", "#", "#", "#"], 14 | ["#", "#", " ", " ", " ", " ", " ", " ", "#"], 15 | ["#", "#", "#", "#", "#", "#", "#", "X", "#"] 16 | ] 17 | 18 | 19 | def print_maze(maze, stdscr, path=[]): 20 | BLUE = curses.color_pair(1) 21 | RED = curses.color_pair(2) 22 | 23 | for i, row in enumerate(maze): 24 | for j, value in enumerate(row): 25 | if (i, j) in path: 26 | stdscr.addstr(i, j*2, "X", RED) 27 | else: 28 | stdscr.addstr(i, j*2, value, BLUE) 29 | 30 | 31 | def find_start(maze, start): 32 | for i, row in enumerate(maze): 33 | for j, value in enumerate(row): 34 | if value == start: 35 | return i, j 36 | 37 | return None 38 | 39 | 40 | def find_path(maze, stdscr): 41 | start = "O" 42 | end = "X" 43 | start_pos = find_start(maze, start) 44 | 45 | q = queue.Queue() 46 | q.put((start_pos, [start_pos])) 47 | 48 | visited = set() 49 | 50 | while not q.empty(): 51 | current_pos, path = q.get() 52 | row, col = current_pos 53 | 54 | stdscr.clear() 55 | print_maze(maze, stdscr, path) 56 | time.sleep(0.2) 57 | stdscr.refresh() 58 | 59 | if maze[row][col] == end: 60 | return path 61 | 62 | neighbors = find_neighbors(maze, row, col) 63 | for neighbor in neighbors: 64 | if neighbor in visited: 65 | continue 66 | 67 | r, c = neighbor 68 | if maze[r][c] == "#": 69 | continue 70 | 71 | new_path = path + [neighbor] 72 | q.put((neighbor, new_path)) 73 | visited.add(neighbor) 74 | 75 | 76 | def find_neighbors(maze, row, col): 77 | neighbors = [] 78 | 79 | if row > 0: # UP 80 | neighbors.append((row - 1, col)) 81 | if row + 1 < len(maze): # DOWN 82 | neighbors.append((row + 1, col)) 83 | if col > 0: # LEFT 84 | neighbors.append((row, col - 1)) 85 | if col + 1 < len(maze[0]): # RIGHT 86 | neighbors.append((row, col + 1)) 87 | 88 | return neighbors 89 | 90 | 91 | def main(stdscr): 92 | curses.init_pair(1, curses.COLOR_BLUE, curses.COLOR_BLACK) 93 | curses.init_pair(2, curses.COLOR_RED, curses.COLOR_BLACK) 94 | 95 | find_path(maze, stdscr) 96 | stdscr.getch() 97 | 98 | 99 | wrapper(main) --------------------------------------------------------------------------------