-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrid.py
More file actions
198 lines (149 loc) · 7.63 KB
/
Grid.py
File metadata and controls
198 lines (149 loc) · 7.63 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
from Wall import Wall
from Cell import Cell
from Agent import Agent
from collections import deque
class Grid:
size = 0
rows, cols = (101, 101)
myStack = []
stepSum = 0
def __init__(self):
self.myWall = [[0] * self.cols for i in range(self.rows)] #[[0]*cols]*rows
self.myCell = [[0] * self.cols for i in range(self.rows)] #[[0]*cols]*rows
def setSize(self):
self.size = int(input("Enter maze self.size(min 2, max 50): "))
while ((self.size > 50) or (self.size <= 1)):
print("Min maze self.size is 2, max is 50.")
self.size = int(input("Enter maze self.size(min 2, max 50): "))
def getSize(self):
return self.size
def reinitialize(self):
for i in range(self.size * 2 + 1): # i: horizontal, j: vertical
for j in range(self.size * 2 + 1):
self.myWall[i][j].setWall(True)
if (j % 2 == 1):
if (i % 2 == 1):
self.myWall[i][j].setWall(False)
# initializing Cell
for i in range(self.size):
for j in range(self.size):
self.myCell[i * 2 + 1][j * 2 + 1].resetVisited()
def Initialize(self):
# initializing wall
for i in range(self.size * 2 + 1): # i: horizontal, j: vertical
for j in range(self.size * 2 + 1):
self.myWall[i][j] = Wall(i, j)
if (j % 2 == 1):
if (i % 2 == 1):
self.myWall[i][j].setWall(False)
for i in range(self.size * 2 + 1):
for j in range(self.size * 2 + 1):
# north
if ((j - 1) >= 0):
self.myWall[i][j].setNorthWall(self.myWall[i][(j - 1)])
# south
if ((j + 1) < self.size * 2 + 1):
self.myWall[i][j].setSouthWall(self.myWall[i][(j + 1)])
# east
if ((i + 1) < self.size * 2 + 1):
self.myWall[i][j].setEastWall(self.myWall[(i + 1)][j])
# west
if ((i - 1) >= 0):
self.myWall[i][j].setWestWall(self.myWall[(i - 1)][j])
# initializing Cell
for i in range(self.size):
for j in range(self.size):
self.myCell[i * 2 + 1][j * 2 + 1] = Cell(i * 2 + 1, j * 2 + 1,self.myWall[i * 2 + 1][j * 2], self.myWall[i * 2 + 1][j * 2 + 2],self.myWall[i * 2 + 2][j * 2 + 1], self.myWall[i * 2][j * 2 + 1])
for i in range(self.size):
for j in range(self.size):
# north
if ((j - 1) >= 0):
self.myCell[i * 2 + 1][j * 2 + 1].setNorthCell(self.myCell[i * 2 + 1][(j - 1) * 2 + 1])
# south
if ((j + 1) < self.size):
self.myCell[i * 2 + 1][j * 2 + 1].setSouthCell(self.myCell[i * 2 + 1][(j + 1) * 2 + 1])
# east
if ((i + 1) < self.size):
self.myCell[i * 2 + 1][j * 2 + 1].setEastCell(self.myCell[(i + 1) * 2 + 1][j * 2 + 1])
# west
if ((i - 1) >= 0):
self.myCell[i * 2 + 1][j * 2 + 1].setWestCell(self.myCell[(i - 1) * 2 + 1][j * 2 + 1])
def generateMaze(self):
print("Generating Maze")
currentX = 0
currentY = 0
self.myCell[currentX * 2 + 1][currentY * 2 + 1].setVisited()
while (self.isAllVisited() != True):
if (self.myCell[currentX * 2 + 1][currentY * 2 + 1].isNeighAllVisited() != True):
temp = self.myCell[currentX * 2 + 1][currentY * 2 + 1].getRandomNeigh()
self.myStack.append(self.myCell[currentX * 2 + 1][currentY * 2 + 1])
# north
if (temp == 0):
self.myWall[currentX * 2 + 1][currentY * 2].setWall(False)
self.myCell[currentX * 2 + 1][currentY * 2] = Cell.from_arguments((currentX * 2 + 1, currentY * 2))
currentY = currentY - 1
# south
elif (temp == 1):
self.myWall[currentX * 2 + 1][currentY * 2 + 2].setWall(False)
self.myCell[currentX * 2 + 1][currentY * 2 + 2] = Cell.from_arguments((currentX * 2 + 1, currentY * 2 + 2))
currentY = currentY + 1
#print("--3.0")
# east
elif (temp == 2):
self.myWall[currentX * 2 + 2][currentY * 2 + 1].setWall(False)
self.myCell[currentX * 2 + 2][currentY * 2 + 1] = Cell.from_arguments((currentX * 2 + 2, currentY * 2 + 1))
currentX = currentX + 1
# west
elif (temp == 3):
self.myWall[currentX * 2][currentY * 2 + 1].setWall(False)
self.myCell[currentX * 2][currentY * 2 + 1] = Cell.from_arguments((currentX * 2, currentY * 2 + 1))
currentX = currentX - 1
self.myCell[currentX * 2 + 1][currentY * 2 + 1].setVisited()
#self.printMaze()
elif (self.myStack):
tempCell = self.myStack.pop()
currentX = int((tempCell.getX() - 1) / 2)
currentY = int((tempCell.getY() - 1) / 2)
def isAllVisited(self):
for i in range(self.size):
for j in range(self.size):
if (self.myCell[i * 2 + 1][j * 2 + 1].getVisited() == False):
return False
return True
def printMaze(self):
for i in range(self.size * 2 + 1):
for j in range(self.size * 2 + 1):
if (self.myWall[i][j].getWall() == True):
print("x", end ="")
elif (self.myWall[i][j].getMarked() > 0):
print("o", end ="")
else:
print(" ", end ="")
print("")
print("Wall Follower steps number: ", self.stepSum)
def getStepSum(self):
return self.stepSum
def getSimpleLayout(self):
layout = [[0] * self.cols for i in range(self.rows)]
for i in range(self.size * 2 + 1):
for j in range(self.size * 2 + 1):
if (self.myWall[i][j].getWall() == True):
layout[i][j] = 1
else:
layout[i][j] = 0
return layout
def solveMaze(self):
myAgent = Agent(self.myWall, self.myCell)
myAgent.markCell(myAgent.getX(), myAgent.getY())
if(myAgent.checkFront()==True): myAgent.turnLeft()
while(not(myAgent.getX() == self.size * 2 -1 and myAgent.getY() == self.size * 2-1)):
myAgent.Forward()
self.stepSum += 1
myAgent.markCell(myAgent.getX(), myAgent.getY())
#printMaze()
if(myAgent.checkRight() == False): myAgent.turnRight()
else:
if(myAgent.checkFront()==True):
myAgent.turnLeft()
if(myAgent.checkFront()==True):
myAgent.turnLeft()