def get_vision(self, direction: Snake.Direction, pos: Coord, cherry: Coord): vision = list() r_pos = Coord(pos.x, pos.y) adj = [(-1, 0), (0, -1), (1, 0)] for i in range(direction.value): adj = [(-y, x) for x, y in adj] r_pos.x, r_pos.y = -r_pos.y, r_pos.x x, y = (cherry - r_pos).get_unit_vector().get() if x == 0: vision += [0, 0, 0] if y == -1 else [1, 0, 0] elif x == 1: if y == 0: vision += [0, 1, 0] else: vision += [0, 0, 1] if y == -1 else [0, 1, 1] elif x == -1: if y == 0: vision += [1, 1, 0] else: vision += [1, 1, 1] if y == -1 else [1, 0, 1] for vector in adj: last = pos + vector if last not in self.cells or self.cells[last].is_wall(): vision.append(1) elif self.cells[last].has_body: vision.append(1) elif self.cells[last].has_cherry: vision.append(0) else: vision.append(0) return vision
def from_atlas(cls, atlas, chart_id): # Bounding box variables of the chart low = Coord(atlas.rows - 1, atlas.cols - 1) high = Coord(0, 0) # Search the atlas for the chart and find the bounding for i in range(atlas.rows): for j in range(atlas.cols): tmp_id = atlas[i, j] if tmp_id == chart_id: low.x = min(low.x, i) low.y = min(low.y, j) high.x = max(high.x, i) high.y = max(high.y, j) # Calculate dimensions height = high.x - low.x + 1 width = high.y - low.y + 1 # Create array for the chart chart_arr = np.ndarray(shape=(height, width), dtype=Chart.DATA_TYPE) chart_arr.fill(Chart.EMPTY) # Copy the chart from the atlas to the array pixels = 0 for i in range(height): for j in range(width): tmp_id = atlas[low.x + i, low.y + j] if tmp_id == chart_id: chart_arr[i, j] = Chart.OCCUPIED pixels += 1 # Create a new Chart object and return it index = low chart = Chart.create(chart_arr, height, width, chart_id=chart_id, index=index, pixels=pixels) return chart