-
Notifications
You must be signed in to change notification settings - Fork 1
/
location.py
500 lines (414 loc) · 13.6 KB
/
location.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
from location.field import Field
from entities.player import Player
from typing import List, Dict, Tuple
class EmptyLocationError(Exception):
"""
Indicates that given List of fields was empty
:param Exception: List of fields cannot be empty
:type Exception: Exception
"""
pass
class DeformedLocationError(Exception):
"""
Indicates that given List of fields wasn't a rectangle
:param Exception: List of fields must be a rectangle
:type Exception: Exception
"""
pass
class StartingPointNotFoundException(Exception):
"""
Indicates that given Location didn't have a starting point
:param Exception: Location must have a starting point
:type Exception: Exception
"""
pass
class InvalidCoordinatesError(Exception):
"""
Indicates that given coordinates are out of range
:param Exception: Invalid coordinates
:type Exception: Exception
"""
pass
class Location:
"""
Location
Contains attributes:
:param location: Matrix of fields
:type location: List[List[Field]]
:param coordinates: Player's coordinates, defaults to None
:type coordinates: Tuple[int, int], optional
:param level: Location level, defaults to 1
:type level: int, optional
"""
def __init__(self,
location: List[List[Field]],
set_boarders: bool = True,
coordinates: Tuple[int, int] = None,
level: int = 1):
"""
Initialize Location
:param location: Matrix of fields
:type location: List[List[Field]]
:param set_boarders: Whether location needs boarders, defaults to True
:type set_boarders: bool, optional
:param coordinates: Player's coordinates, defaults to None
:type coordinates: Tuple[int, int], optional
:param level: Location level, defaults to 1
:type level: int, optional
"""
if set_boarders:
self.set_location(location)
else:
self.set_location_already_with_boarders(location)
self.set_level(level)
if coordinates:
self.set_coordinates(coordinates)
else:
self.set_starting_coordinates(level)
def as_dict(self) -> Dict:
"""
Get Location as a dictionary
:return: Dictionary with location's data
:rtype: Dict
"""
return {
'location': [[field.as_dict() for field in row]
for row in self._location],
'coordinates': self._coordinates,
'level': self._level
}
@staticmethod
def from_dict(dictionary: Dict) -> 'Location':
"""
Get new Location instance loaded from dictionary
:param dictionary: Dictionary with Location's data
:type dictionary: Dict
:return: Loaded Location
:rtype: Location
"""
return Location(
[[Field().from_dict(field) for field in row]
for row in dictionary['location']],
False,
dictionary.get('coordinates', None),
dictionary.get('level', 1))
# Getters and Setters
def location(self) -> List[List[Field]]:
"""
Get location
:return: Matrix of fields
:rtype: List[List[Field]]
"""
return self._location
def set_location(self, location: List[List[Field]]):
"""
Set location and add boarders around it
:param location: Matrix of fields
:type location: List[List[Field]]
"""
self._location = self.create_boarder(location)
def set_location_already_with_boarders(self, location: List[List[Field]]):
"""
Set location without adding boarders
:param location: Matrix of fields
:type location: List[List[Field]]
"""
self._location = location if location else [[]]
def level(self) -> int:
"""
Get location level
:return: Level of the location
:rtype: int
"""
return self._level
def set_level(self, level: int):
"""
Set location level
:param level: Level of the location
:type level: int
"""
self._level = level
def coordinates(self) -> Tuple[int, int]:
"""
Get coordinates
:return: (x, y)
:rtype: Tuple[int, int]
"""
self._coordinates
def set_coordinates(self, coordinates: Tuple[int, int]):
"""
Set coordinates
:param coordinates: (x, y)
:type coordinates: Tuple[int, int]
:raises InvalidCoordinatesError: Indicates that coordinates are
out of range
"""
x, y = coordinates
if not (0 <= x < self.row() and 0 <= y < self.column()):
raise InvalidCoordinatesError()
self._coordinates = (x, y)
def set_starting_coordinates(self, level: int):
"""
Set starting coordinates
:param level: Location level
:type level: int
"""
self.set_coordinates(self.get_starting_coordinates(level))
def get_starting_coordinates(self, level: int) -> Tuple[int, int]:
"""
Get field coordinates with go_to parameter equal to location level
:param level: Location level
:type level: int
:raises StartingPointNotFoundException: Indicates that given Location
didn't have a starting point
:return: Starting coordinates - (x, y)
:rtype: Tuple[int, int]
"""
for y, row in enumerate(self._location):
for x, field in enumerate(row):
if field.go_to() == level:
return x, y
raise StartingPointNotFoundException()
def get_boarder_field(self) -> Field:
"""
Get boarder field
:return: Boarder field
:rtype: Field
"""
return Field(
'Boarder',
'No one is able to go through me!',
enterable=False,
seen=True
)
def boarder_row(self, row_len: int) -> List[Field]:
"""
Get boarder row with given length
:param row_len: Length of the row
:type row_len: int
:return: List of boarder fields
:rtype: List[Field]
"""
row = []
for _ in range(row_len + 2):
row.append(self.get_boarder_field())
return row
def create_boarder(self, location: List[List[Field]]) -> List[List[Field]]:
"""
Generate boarder around location fields
:param location: Matrix of fields without boarder
:type location: List[List[Field]]
:raises EmptyLocationError: Indicates that given List of fields
was empty
:raises DeformedLocationError: Indicates that given List of fields
wasn't a rectangle
:return: [description]
:rtype: List[List[Field]]
"""
column_len = len(location)
if column_len != 0:
row_len = len(location[0])
if column_len == 0 or row_len == 0:
raise EmptyLocationError('Given Location was empty')
for row in location:
if len(row) != row_len:
raise DeformedLocationError("Given Matrix wasn't a rectangle")
new_location = [self.boarder_row(row_len)]
for y, row in enumerate(location):
new_location.append([])
new_location[y+1].append(self.get_boarder_field())
for f in row:
new_location[y+1].append(f)
new_location[y+1].append(self.get_boarder_field())
new_location.append(self.boarder_row(row_len))
return new_location
def field(self, x: int, y: int) -> Field:
"""
Get fields with given coordinates
:param x: Coordinate - x
:type x: int
:param y: Coordinate - y
:type y: int
:return: Field with given coordinates
:rtype: Field
"""
return self._location[y][x]
def current_field(self) -> Field:
"""
Get field player is standing on
:return: Field player is standing on
:rtype: Field
"""
x, y = self._coordinates
return self._location[y][x]
def column(self) -> int:
"""
Get fields column count
:return: Coulum count
:rtype: int
"""
return len(self._location)
def row(self) -> int:
"""
Get fields row count
:return: Row count
:rtype: int
"""
if len(self._location) != 0:
return len(self._location[0])
else:
return 0
def format_map(self) -> str:
"""
Format map based on fields
:return: Formatted map
:rtype: str
"""
p_x, p_y = self._coordinates
output = ''
for y, row in enumerate(self._location):
for x, field in enumerate(row):
if p_x != x or p_y != y:
output += field.field_type()
else:
output += ' P '
output += '\n'
return output
def is_enterable(self, south: int = 0, east: int = 0) -> bool:
"""
Whether location distant from player is enterable
:param south: Distance south, defaults to 0
:type south: int, optional
:param east: Distance east, defaults to 0
:type east: int, optional
:return: Whether field is enterable
:rtype: bool
"""
x, y = self._coordinates
x = x+south
y = y+east
return self.field(x, y).enterable()
def go(self, player: Player, south: int = 0, east: int = 0):
"""
Method that allows player to move
:param player: Player
:type player: Player
:param south: Distance south, defaults to 0
:type south: int, optional
:param east: Distance east, defaults to 0
:type east: int, optional
"""
x, y = self._coordinates
x = x + east
y = y + south
if self.field(x, y).enterable():
self._coordinates = (x, y)
self.current_field().set_seen()
self.description()
self.current_field().entrance(player)
def go_north(self, player: Player):
"""
Moves player north
:param player: Player
:type player: Player
"""
self.go(player, south=-1)
def go_south(self, player: Player):
"""
Moves player south
:param player: Player
:type player: Player
"""
self.go(player, south=1)
def go_east(self, player: Player):
"""
Moves player east
:param player: Player
:type player: Player
"""
self.go(player, east=1)
def go_west(self, player: Player):
"""
Moves player west
:param player: Player
:type player: Player
"""
self.go(player, east=-1)
def wait(self, player: Player):
"""
Calls current field entry function
:param player: Player
:type player: Player
"""
self.current_field().set_seen()
self.current_field().entrance(player)
def description(self, _=None):
"""
Get location description
:param _: None, defaults to None
:type _: Any, optional
"""
self.print_map()
print('')
if self._coordinates:
x, y = self._coordinates
else:
self.set_starting_coordinates(self._level)
x, y = self._coordinates
print(f"NORTH: {self.field(x, y-1).name()}")
print(f"SOUTH: {self.field(x, y+1).name()}")
print(f"EAST: {self.field(x+1, y).name()}")
print(f"WEST: {self.field(x-1, y).name()}")
print('')
field = self.current_field()
print(f"CURRENT: {field.name()}")
print(self.current_field().description())
if field.enemy():
print('')
print(f'ENEMY: {field.enemy().name()}')
print(field.enemy().description())
if field.item():
print('')
print(f'ITEM: {field.item().name()}')
print(field.item().description())
def __str__(self) -> str:
"""
:return: Map of the location
:rtype: str
"""
return self.format_map()
def print_map(self, _=None):
"""
Print map of the location
:param _: None, defaults to None
:type _: Any, optional
"""
print('\n')
print(self)
def __eq__(self, other) -> bool:
"""
:param other: Checked object
:type other: Any
:return: Indicates whether self and other are equal
:rtype: bool
"""
return (isinstance(other, Location) and
self.as_dict() == other.as_dict())
def available_methods(self) -> Dict:
"""
Get location methods available to player during this round
:return: Dictionary with methods
:rtype: Dict
"""
x, y = self._coordinates
dictionary = {'map': self.print_map, 'location info': self.description}
if self.field(x, y - 1).enterable():
dictionary['go north'] = self.go_north
if self.field(x, y + 1).enterable():
dictionary['go south'] = self.go_south
if self.field(x + 1, y).enterable():
dictionary['go east'] = self.go_east
if self.field(x - 1, y).enterable():
dictionary['go west'] = self.go_west
dictionary['wait'] = self.wait
return dictionary