-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchill_maze.py
377 lines (337 loc) · 14.8 KB
/
chill_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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# The MIT License (MIT)
#
# Copyright (c) 2014 Sergio Carlos Morales Angeles
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from maze_abstract_base import MazeAbstractBase
from chill_tile import ChillTile
class ChillMaze(MazeAbstractBase):
def __init__(self, width, height, tile_class=ChillTile):
"""Initialization logic for the maze
Params:
width: numeric value for the grid width of the maze
height: numeric value for the gird height of the maze
Raises:
ValueError: If width or height cannot be numerically
interpreted and are not greater than zero
"""
try:
width = int(width)
height = int(height)
if width < 1 or height < 1:
raise ValueError
except (TypeError, ValueError):
raise ValueError("Width and Height must be greater than zero:"
"{},{} provided".format(width, height))
# Initialize data
self._width = width
self._height = height
self._tile_class = tile_class
self._maze = []
self._neighborhood = {}
# Time to actually initialize the tiles
# Lets go through each row
for row_number in range(height):
# Then create all columns
column = []
for column_number in range(width):
tile = self._tile_class()
column.append(tile)
self._maze.append(column)
# Sanity check
if (len(self._maze) != self.get_height() or
len(self._maze[0]) != self.get_width()):
raise AssertionError("Dimensions are not matching up!\n"
"Actual Width: {actual_width}\n"
"Actual Height: {actual_height}\n"
"Intended Width: {intended_width}\n"
"Intended Height: {intended_height}\n"
.format(
actual_width=len(self._maze[0]),
actual_height=len(self._maze),
intended_width=width,
intended_height=height
))
self._set_tile_neighbors_relationship()
def has_been_generated(self):
"""Determine whether or not this maze has been generated and has tiles
Returns: bool
"""
if len(self._maze) > 0 and len(self._maze[0]) > 0:
return True
else:
return False
def get_width(self):
"""Get the width of this maze
Return: Numeric value of the width
"""
return self._width
def get_height(self):
"""Get the height of this maze
Return: Numeric value of the height
"""
return self._height
def get_tiles_in_row(self, index):
"""Return all tiles on a specific row (horizontal dimension)
!!!Remember that tile indexing is 0,0 at the top left corner!!!
Params:
index: integer
Returns:
List of tiles (TileAbstractBase)
Raises:
IndexError: If supplied index is invalid
"""
try:
tiles = self._maze[index]
return tiles
except IndexError:
raise IndexError("Invalid value, {} not a valid index for a row"
.format(index))
def get_tiles_in_column(self, index):
"""Return all tiles from a specific column (vertical dimension)
Params:
index integer
Returns:
List of tiles (TileAbstractBase)
Raises:
IndexError: If supplied index is invalid
"""
try:
tiles = []
for row in self._maze:
tiles.append(row[index])
return tiles
except IndexError:
raise IndexError("Invalid value, {} not a valid index for a column"
.format(index))
def get_tile_at(self, horizontal_index, vertical_index):
"""Return the tile at a specific location (x,y)
Params:
horizontal_index numeric index for the horizontal axis
vertical_index numeric index for the vertical axis
Returns:
A TileAbstractBase
Raises:
Index error if any of the provided indexes is not valid
"""
row = self.get_tiles_in_row(vertical_index)
try:
tile = row[horizontal_index]
except IndexError:
raise IndexError("Invalid position: {horizontal},{vertical}"
.format(horizontal=horizontal_index,
vertical=vertical_index))
return tile
def _set_tile_neighbors_relationship(self):
"""Initialize data to determine tile neighbors
This sets the data so we later can determine the neighbor of a tile
It iterates over every tile and then saves data in an internal dict
If a tile is at the border of a maze, it sets None as its neighbor
"""
for vertical_index in range(self.get_height()):
for horizontal_index in range(self.get_width()):
current_tile = self.get_tile_at(horizontal_index,
vertical_index)
# North neighbor
north_neighbor = None
if vertical_index > 0:
north_neighbor = self.get_tile_at(horizontal_index,
vertical_index-1)
# East neighbor
east_neighbor = None
if horizontal_index < self.get_width()-1:
east_neighbor = self.get_tile_at(horizontal_index+1,
vertical_index)
# South neighbor
south_neighbor = None
if vertical_index < self.get_height()-1:
south_neighbor = self.get_tile_at(horizontal_index,
vertical_index+1)
# West neighbor
west_neighbor = None
if horizontal_index > 0:
west_neighbor = self.get_tile_at(horizontal_index-1,
vertical_index)
self._save_tile_neighbors_relationship(current_tile,
north_neighbor,
east_neighbor,
south_neighbor,
west_neighbor)
def _save_tile_neighbors_relationship(self, reference_tile,
north_neighbor,
east_neighbor,
south_neighbor,
west_neighbor):
"""Save in our internal dictionary a tile neighbors
Each entry in our dictionary is in the form:
{tile: [north_tile, east_tile, south_tile, west_tile]}
Params:
reference_tile: TileAbstractBase for which we are saving the
the actual neighbor data
north_neighbor: TileAbstractBase located at the north
east_neighbor: TileAbstractBase located at the east
south_neighbor: TileAbstractBase located at the south
west_neighbor: TileAbstractBase located at the west
"""
self._neighborhood[reference_tile] = [north_neighbor,
east_neighbor,
south_neighbor,
west_neighbor]
def _get_tile_neighbor(self, tile, neighbor_position):
"""Internal function to retrieve a tile neighbor
Params:
tile: TileAbstractBase for which we want to get the neighbor
neighbor_position: str containing the location of the neighbor
Returns:
TileAbstractBase
Raises:
KeyError: If neighbor position provided is invalid
"""
relationship_index = None
if neighbor_position.capitalize() == "North":
relationship_index = 0
elif neighbor_position.capitalize() == "East":
relationship_index = 1
elif neighbor_position.capitalize() == "South":
relationship_index = 2
elif neighbor_position.capitalize() == "West":
relationship_index = 3
#Check we have a valid position to search for
if relationship_index is None:
raise ValueError("{} is not a valid neighbor position"
.format(neighbor_position))
try:
return self._neighborhood[tile][relationship_index]
except KeyError:
raise ValueError("No neighbor data found for tile")
def get_tile_north_neighbor(self, tile):
"""Return the north neighbor of a tile
Params:
tile: TileAbstractBase
Returns:
TileAbstractBase
"""
return self._get_tile_neighbor(tile, "North")
def get_tile_east_neighbor(self, tile):
"""Return the east neighbor of a tile
Params:
tile: TileAbstractBase
Returns:
TileAbstractBase
"""
return self._get_tile_neighbor(tile, "East")
def get_tile_south_neighbor(self, tile):
"""Return the south neighbor of a tile
Params:
tile: TileAbstractBase
Returns:
TileAbstractBase
"""
return self._get_tile_neighbor(tile, "South")
def get_tile_west_neighbor(self, tile):
"""Return the west neighbor of a tile
Params:
tile: TileAbstractBase
Returns:
TileAbstractBase
"""
return self._get_tile_neighbor(tile, "West")
def get_tile_neighbors(self, tile):
"""Return all neighbors for a tile
This will not always return 4 tiles, a tile may not have a neighbor
and will not be included in the return list
Params:
tile: TileAbstractBase
Returns:
List of TileAbstractBase
"""
neighbors = []
north_neighbor = self.get_tile_north_neighbor(tile)
if north_neighbor is not None:
neighbors.append(north_neighbor)
east_neighbor = self.get_tile_east_neighbor(tile)
if east_neighbor is not None:
neighbors.append(east_neighbor)
south_neighbor = self.get_tile_south_neighbor(tile)
if south_neighbor is not None:
neighbors.append(south_neighbor)
west_neighbor = self.get_tile_west_neighbor(tile)
if west_neighbor is not None:
neighbors.append(west_neighbor)
return neighbors
def connect_tiles(self, tile_1, tile_2):
"""Connect two neighbor tiles (knocks appropriate walls)
Params:
tile_1: TileAbstractBase
tile_2: TileAbstractBase
Raises:
ValueError if tiles are not neighbors
"""
if self.get_tile_north_neighbor(tile_1) == tile_2:
# tile_2 is north of tile_1, knock tile_2 south wall
# and tile_1 north wall
tile_1.knock_wall("North")
tile_2.knock_wall("South")
elif self.get_tile_east_neighbor(tile_1) == tile_2:
# tile_2 is east of tile_1, knock tile_2 west wall
# and tile_1 east wall
tile_1.knock_wall("East")
tile_2.knock_wall("West")
elif self.get_tile_south_neighbor(tile_1) == tile_2:
# tile_2 is south of tile_1, knock tile_2 north wall
# and tile_1 south wall
tile_1.knock_wall("South")
tile_2.knock_wall("North")
elif self.get_tile_west_neighbor(tile_1) == tile_2:
# tile_2 is west of tile_1, knock tile_2 east wall
# and tile_1 west wall
tile_1.knock_wall("West")
tile_2.knock_wall("East")
else:
raise ValueError("Tiles are not neighbors")
def are_tiles_connected(self, tile_1, tile_2):
"""Determine whether two tiles are connected
Params:
tile_1: TileAbstractBase
tile_2: TileAbstractBase
Returns:
Bool
"""
if self.get_tile_north_neighbor(tile_1) == tile_2:
# tile_2 is north of tile_1, check tile_2 south wall
# and tile_1 north wall
return not tile_2.is_wall_up("South") \
and not tile_1.is_wall_up("North")
elif self.get_tile_east_neighbor(tile_1) == tile_2:
# tile_2 is east of tile_1, check tile_2 west wall
# and tile_1 east wall
return not tile_2.is_wall_up("West") \
and not tile_1.is_wall_up("East")
elif self.get_tile_south_neighbor(tile_1) == tile_2:
# tile_2 is south of tile_1, check tile_2 north wall
# and tile_1 south wall
return not tile_2.is_wall_up("North") \
and not tile_1.is_wall_up("South")
elif self.get_tile_west_neighbor(tile_1) == tile_2:
# tile_2 is west of tile_1, check tile_2 east wall
# and tile_1 west wall
return not tile_2.is_wall_up("East") \
and not tile_1.is_wall_up("West")
else:
return False