- The Maze
- Tutorial Video
- Program Code
The Maze
The Maze
Step 1- The Original maze
Step 2- Add a grid to the maze
Step 3- Add data to each grid cell
Step 4- Convert the grid to a CSV
Tutorial Video
Program Code
Full Program Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv,os,time,sys,random | |
def get_maze(file): | |
f = open(file,'r') | |
reader = csv.reader(f) | |
maze = [] | |
for line in reader: | |
maze.append(line) | |
return maze | |
def display_maze(m, path): | |
m2 = m[:] | |
os.system('clear') # windows use 'cls' | |
for item in path: | |
m2[item[0]][item[1]] = "." | |
m2[path[-1][0]][path[-1][1]] = "M" | |
draw = "" | |
for row in m2: | |
for item in row: | |
item = str(item).replace("1","█") | |
item = str(item).replace("2"," ") | |
item = str(item).replace("0"," ") | |
draw += item | |
draw += "\n" | |
print(draw) | |
def move(path): | |
time.sleep(0.3) | |
cur = path[-1] | |
display_maze(maze,path) | |
possibles = [(cur[0],cur[1] + 1),(cur[0],cur[1]- 1),(cur[0] + 1, cur[1]),(cur[0]-1, cur[1]) ] | |
random.shuffle(possibles) | |
for item in possibles: | |
if item[0] < 0 or item[1] < 0 or item[0] > len(maze) or item[1] > len(maze[0]): | |
continue | |
elif maze[item[0]][item[1]] in ["1","2"] : | |
continue | |
elif item in path: | |
continue | |
elif maze[item[0]][item[1]] == "B": | |
path = path + (item,) | |
display_maze(maze,path) | |
input("Solution found! Press enter to finish") | |
os.system('clear') # windows use 'cls' | |
sys.exit() | |
else: | |
newpath = path + (item,) | |
move(newpath) | |
maze[item[0]][item[1]] = "2" | |
display_maze(maze,path) | |
time.sleep(0.3) | |
maze = get_maze('maze1.csv') | |
move(((1,0),)) |