def detect_maze(vertex, width, height): """Try to detect maze with given maze size, starting at given vertex.""" maze = Maze(width, height) dualmaze = Maze(width, height) last_vertex = vertex stack = [vertex] while len(stack) > 0: vertex = stack.pop() if is_done(vertex, maze, dualmaze): continue if vertex != last_vertex: run(maze.bfs(last_vertex, vertex), maze) last_vertex = detect_walls(vertex, maze, dualmaze) # Fill stack; avoid unnecessary paths stack.extend([v for v in maze.get_reachables(*vertex) if v != last_vertex and not is_done(v, maze, dualmaze)]) if vertex != last_vertex: stack.append(last_vertex) return maze
height = int(raw_input('Height: ')) # Find nearest vertex to begin with start = estimate_current_vertex(width, height) m = detect_maze(start, width, height) print 'Maze detected!' name = raw_input('File name: ') with open(name, 'w') as f: f.write(repr(m)) print 'String representation saved.' elif answer.upper() == 'B': name = raw_input('File name: ') with open(name, 'r') as f: s = f.read() m.parse(s) start = raw_input('Starting point (input format is "x,y"): ') finish = raw_input('Finish (input format is "x,y"): ') s = start.partition(',') f = finish.partition(',') a = (int(s[0]), int(s[2])) b = (int(f[0]), int(f[2])) path = m.bfs(a, b) run(path, m)
def main(): state = "WALL" running = True LEFT = 1 RIGHT = 3 width = 900 height = 900 node_size = 30 pygame.init() screen = pygame.display.set_mode((width,height), 0, 32) screen.fill((0,0,0)) maze= Maze(screen, node_size) maze.draw_boundaries(width, height) maze.draw_grid(width,height) while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False if event.type == pygame.MOUSEBUTTONDOWN and event.button == LEFT: x, y = event.pos[0]//node_size, event.pos[1]//node_size maze.draw_block(state,x,y) if event.type == pygame.MOUSEBUTTONDOWN and event.button == RIGHT: x, y = event.pos[0]//node_size, event.pos[1]//node_size maze.delete_block(x,y) if event.type == pygame.KEYDOWN: if event.key == pygame.K_q: state = "START" if event.key == pygame.K_w: state = "WALL" if event.key == pygame.K_e: state = "END" if event.key == pygame.K_b: maze.bfs() maze.draw_path() #print(maze.get_path()) #print(maze.get_visited()) if event.key == pygame.K_d: maze.dfs(maze.starting_node) #print(maze.get_path()) #print(maze.get_visited()) maze.draw_path() if event.key == pygame.K_r: maze.restart() if event.key == pygame.K_l: maze.createMaze("maze.csv") if event.key == pygame.K_s: maze.save_maze() pygame.display.update() pygame.quit() sys.exit()