-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.py
174 lines (138 loc) · 5.55 KB
/
map.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
"""Definition of the maze that the player will be in."""
import os
from coordinates import Coordinates
from direction import Direction
class Map:
"""Map definition."""
MAP_DIR = "./maps"
WIDTH = 32
HEIGHT = 32
TILE_OUTSIDE_MAP = chr(9618)
@staticmethod
def __createEmptyMap():
"""Create an empty map."""
return [[None for x in range(Map.WIDTH)] for y in range(Map.HEIGHT)]
def __init__(self, name):
"""Initialize the map."""
self.__startLocation = None
self.__exitLocation = None
map = Map.__createEmptyMap()
y = 0
with open(
os.path.join(self.MAP_DIR, f"{name}.map"),
"r",
encoding="utf8"
) as f:
for line in f:
x = 0
for ch in line.rstrip(os.linesep):
map[y][x] = ch
if ch == "S":
self.__startLocation = Coordinates(x, y)
elif ch == "E":
self.__exitLocation = Coordinates(x, y)
x += 1
y += 1
self.__map = map
f.close()
@property
def startLocation(self):
"""Get the starting location of the map."""
return self.__startLocation
@property
def exitLocation(self):
"""Get the exit location of the map."""
return self.__exitLocation
def __getTileAtCoordinates(self, coordinates):
"""Get the map tile specified by the coordinates."""
x = coordinates.x
y = coordinates.y
# Check if the coordinates are out of the bounds of the map
if x < 0 or x > Map.WIDTH or y < 0 or y > Map.HEIGHT:
return None
return self.__map[y][x]
def getDestinationInDirectionFromLocation(
self,
originLocation,
direction
):
"""Get the location that is adjacent to the direction's location."""
originTile = self.__getTileAtCoordinates(originLocation)
if originTile == Map.TILE_OUTSIDE_MAP:
return None
x = originLocation.x
y = originLocation.y
if direction == Direction.NORTH:
y -= 1
elif direction == Direction.EAST:
x += 1
elif direction == Direction.SOUTH:
y += 1
elif direction == Direction.WEST:
x -= 1
destinationLocation = Coordinates(x, y)
destinationTile = self.__getTileAtCoordinates(coordinates=destinationLocation)
return None if destinationTile == Map.TILE_OUTSIDE_MAP else destinationLocation
def getViewableArea(self, originLocation, direction):
"""Get the viewable area from the origin location facing the direction."""
viewableDepth = 4
viewableWidth = 3
# Initialize the viewable area to be all outside the map
viewableArea = [[Map.TILE_OUTSIDE_MAP for x in range(viewableWidth)] for y in range(viewableDepth)]
originX = originLocation.x
originY = originLocation.y
writeX = None
writeY = None
if direction == Direction.NORTH:
writeX = 0
writeY = viewableDepth - 1
for readY in reversed(range(originY - viewableDepth + 1, originY + 1)):
if readY < 0:
break
for readX in range(originX - 1, originX + 2):
if readX in range(0, Map.WIDTH):
# print(f"readX={readX},readY={readY},writeX={writeX},writeY={writeY}")
viewableArea[writeY][writeX] = self.__map[readY][readX]
writeX += 1
writeY -= 1
writeX = 0
elif direction == Direction.EAST:
writeX = 0
writeY = viewableDepth - 1
for readX in range(originX, originX + viewableDepth):
if readX >= Map.WIDTH:
break
for readY in range(originY - 1, originY + 2):
if readY in range(0, Map.HEIGHT):
# print(f"readX={readX},readY={readY},writeX={writeX},writeY={writeY}")
viewableArea[writeY][writeX] = self.__map[readY][readX]
writeX += 1
writeY -= 1
writeX = 0
elif direction == Direction.SOUTH:
writeX = 0
writeY = viewableDepth - 1
for readY in range(originY, originY + viewableDepth):
if readY >= Map.HEIGHT:
break
for readX in reversed(range(originX - 1, originX + 2)):
if readX in range(0, Map.WIDTH):
# print(f"readX={readX},readY={readY},writeX={writeX},writeY={writeY}")
viewableArea[writeY][writeX] = self.__map[readY][readX]
writeX += 1
writeY -= 1
writeX = 0
elif direction == Direction.WEST:
writeX = 0
writeY = viewableDepth - 1
for readX in reversed(range(originX - viewableDepth + 1, originX + 1)):
if readX < 0:
break
for readY in reversed(range(originY - 1, originY + 2)):
if readY in range(0, Map.HEIGHT):
# print(f"readX={readX},readY={readY},writeX={writeX},writeY={writeY}")
viewableArea[writeY][writeX] = self.__map[readY][readX]
writeX += 1
writeY -= 1
writeX = 0
return viewableArea