-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaze.py
executable file
·78 lines (68 loc) · 2.35 KB
/
Maze.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from Node import Node
import random
import pickle
from tkinter import *
class Maze:
def __init__(self):
pass
def readfiftymazes(self):
f = open("SavedMazes", "rb")
mazeLibrary = pickle.load(f)
allActualMazes = []
i = 0
for m in mazeLibrary:
i+=1
maze = self.generate_blank_maze(101)
for x in range(0, 101):
for y in range(0, 101):
if m[x][y] == 1:
maze[x][y].cost = float("inf")
allActualMazes.append(maze)
return allActualMazes
def generate_actual_maze(self, size):
maze = Maze().generate_blank_maze(size)
start = maze[random.randint(0, size - 1)][random.randint(0, size - 1)]
open = []
open.append(start)
closed = set()
while (open != []):
current = open.pop()
if current in closed:
continue
closed.add(current)
index = [0, 1, 2, 3]
random.shuffle(index)
for j in range(0, 4):
i = index[j]
child = current.traverse_children(i)
if (child is None): continue
if (child not in closed) & (child not in open):
isBlocked = random.randint(1, 101)
if isBlocked > 70: child.cost = float("inf")
open.append(child)
return maze
def generate_blank_maze(self, size):
maze = []
for i in range(0, size):
row = [None for x in range(size)]
for k in range(0, size):
cell = Node(i, k)
row[k] = cell
maze.append(row)
for p in range(0, size):
for j in range(0, size):
if j != 0:
maze[p][j].left_child = maze[p][j - 1]
if j != size - 1:
maze[p][j].right_child = maze[p][j + 1]
if p != 0:
maze[p][j].top_child = maze[p - 1][j]
if p != size - 1:
maze[p][j].down_child = maze[p + 1][j]
return maze
def generate_fifty_gridworlds(self, size):
libOfMazes = []
for x in range(0, 50):
maze = Maze().generate_actual_maze(size)
libOfMazes.append(maze)
return libOfMazes