-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.py
218 lines (181 loc) · 8.21 KB
/
board.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
#! /usr/bin/env python3
from math import sqrt
from element import Element
from point import Point
import re
class Board:
COUNT_LAYERS = 3
INPUT_REGEX = "(.*),\"layers\":\[(.*)\](.*)"
def __init__(self, input):
matcher = re.search(Board.INPUT_REGEX, input)
board_string = matcher.group(2).replace('\n', '').replace(',', '').replace('\"', '') # one line board
self._board = []
_layer_len = int(len(board_string) / Board.COUNT_LAYERS)
for i in range(Board.COUNT_LAYERS):
_layer = []
for j in range(_layer_len):
_layer.append(board_string[j + (i * _layer_len)])
self._board.append(_layer)
self._layer_size = int(sqrt(_layer_len))
def _find_all(self, element):
_points = []
_a_char = element.get_char()
for i in range(len(self._board)):
for j in range(len(self._board[i])):
if self._board[i][j] == _a_char:
_points.append(self._strpos2pt(j))
return _points
def _strpos2pt(self, strpos):
return Point(*self._strpos2xy(strpos))
def _strpos2xy(self, strpos):
return strpos % self._layer_size, strpos // self._layer_size
def get_at(self, x, y):
_strpos = self._xy2strpos(x, y)
_elements = []
for i in range(len(self._board)):
_elements.append(Element(self._board[i][_strpos]))
return _elements
def _xy2strpos(self, x, y):
return self._layer_size * y + x
def is_at(self, x, y, element_object):
return element_object in self.get_at(x, y)
def is_barrier_at(self, x, y):
points = set()
points.update(self.get_floors())
points.update(self.get_starts())
points.update(self.get_exits())
points.update(self.get_golds())
points.update(self.get_holes())
points.update(self.get_lasers())
points.add(self.get_hero())
points.update(self.get_other_heroes())
return Point(x, y) not in list(points)
def get_hero(self):
points = set()
points.update(self._find_all(Element('ROBO_FALLING')))
points.update(self._find_all(Element('ROBO_LASER')))
points.update(self._find_all(Element('ROBO')))
points.update(self._find_all(Element('ROBO_FLYING')))
assert len(points) <= 1, "There should be only one robo"
return list(points)[0]
def is_me_alive(self):
points = set()
points.update(self._find_all(Element('ROBO_FALLING')))
points.update(self._find_all(Element('ROBO_LASER')))
return list(points) == 0
def get_other_heroes(self):
points = set()
points.update(self._find_all(Element('ROBO_OTHER_FALLING')))
points.update(self._find_all(Element('ROBO_OTHER_LASER')))
points.update(self._find_all(Element('ROBO_OTHER')))
points.update(self._find_all(Element('ROBO_OTHER_FLYING')))
return list(points)
def get_empty(self):
return self._find_all(Element('EMPTY'))
def get_zombies(self):
points = set()
points.update(self._find_all(Element('FEMALE_ZOMBIE')))
points.update(self._find_all(Element('MALE_ZOMBIE')))
points.update(self._find_all(Element('ZOMBIE_DIE')))
return list(points)
def get_laser_machines(self):
points = set()
points.update(self._find_all(Element('LASER_MACHINE_CHARGING_LEFT')))
points.update(self._find_all(Element('LASER_MACHINE_CHARGING_RIGHT')))
points.update(self._find_all(Element('LASER_MACHINE_CHARGING_UP')))
points.update(self._find_all(Element('LASER_MACHINE_CHARGING_DOWN')))
points.update(self._find_all(Element('LASER_MACHINE_READY_LEFT')))
points.update(self._find_all(Element('LASER_MACHINE_READY_RIGHT')))
points.update(self._find_all(Element('LASER_MACHINE_READY_UP')))
points.update(self._find_all(Element('LASER_MACHINE_READY_DOWN')))
return list(points)
def get_lasers(self):
points = set()
points.update(self._find_all(Element('LASER_LEFT')))
points.update(self._find_all(Element('LASER_RIGHT')))
points.update(self._find_all(Element('LASER_UP')))
points.update(self._find_all(Element('LASER_DOWN')))
return list(points)
def get_boxes(self):
return self._find_all(Element('BOX'))
def get_floors(self):
return self._find_all(Element('FLOOR'))
def get_holes(self):
points = set()
points.update(self._find_all(Element('HOLE')))
points.update(self._find_all(Element('ROBO_FALLING')))
points.update(self._find_all(Element('ROBO_OTHER_FALLING')))
return list(points)
def get_exits(self):
return self._find_all(Element('EXIT'))
def get_starts(self):
return self._find_all(Element('START'))
def get_golds(self):
return self._find_all(Element('GOLD'))
def get_walls(self):
points = set()
points.update(self._find_all(Element('ANGLE_IN_LEFT')))
points.update(self._find_all(Element('WALL_FRONT')))
points.update(self._find_all(Element('ANGLE_IN_RIGHT')))
points.update(self._find_all(Element('WALL_RIGHT')))
points.update(self._find_all(Element('ANGLE_BACK_RIGHT')))
points.update(self._find_all(Element('WALL_BACK')))
points.update(self._find_all(Element('ANGLE_BACK_LEFT')))
points.update(self._find_all(Element('WALL_LEFT')))
points.update(self._find_all(Element('WALL_BACK_ANGLE_LEFT')))
points.update(self._find_all(Element('WALL_BACK_ANGLE_RIGHT')))
points.update(self._find_all(Element('ANGLE_OUT_RIGHT')))
points.update(self._find_all(Element('ANGLE_OUT_LEFT')))
points.update(self._find_all(Element('SPACE')))
return list(points)
def get_perks(self):
points = set()
points.update(self._find_all(Element('UNSTOPPABLE_LASER_PERK')))
points.update(self._find_all(Element('DEATH_RAY_PERK')))
points.update(self._find_all(Element('UNLIMITED_FIRE_PERK')))
return list(points)
def is_near(self, x, y, elem):
_is_near = False
if not Point(x, y).is_bad(self._layer_size):
_is_near = (self.is_at(x + 1, y, elem) or
self.is_at(x - 1, y, elem) or
self.is_at(x, 1 + y, elem) or
self.is_at(x, 1 - y, elem))
return _is_near
def count_near(self, x, y, elem):
_near_count = 0
if not Point(x, y).is_bad(self._layer_size):
for _x, _y in ((x + 1, y), (x - 1, y), (x, 1 + y), (x, 1 - y)):
if self.is_at(_x, _y, elem):
_near_count += 1
return _near_count
def to_string(self):
return ("Board:\n{brd}\nHero at: {hero}\nOther Heroes "
"at: {others}\nZombies at: {zmb}\nLasers at:"
" {lsr}\nHoles at : {hls}\nGolds at: "
"{gld}\nPerks at: {prk}".format(brd=self._line_by_line(),
hero=self.get_hero(),
others=self.get_other_heroes(),
zmb=self.get_zombies(),
lsr=self.get_lasers(),
hls=self.get_holes(),
gld=self.get_golds(),
prk=self.get_perks())
)
def _line_by_line(self):
_string_board = ' '
for i in range(self._layer_size * Board.COUNT_LAYERS):
_string_board += str(i % 10)
if (i + 1) % self._layer_size == 0:
_string_board += '\t'
_string_board += '\n'
for i in range(self._layer_size):
_string_board += str(i % 10) + ' '
for j in range(Board.COUNT_LAYERS):
for k in range(self._layer_size):
_string_board += self._board[j][k + (i * self._layer_size)]
_string_board += '\t'
_string_board += '\n'
return _string_board
if __name__ == '__main__':
raise RuntimeError("This module is not designed to be ran from CLI")