def get_resist_set(self, model, shift): """Returns what blocks the model to be on the given pos. """ pos = v2.sum(model.pos, shift) resist = set() for mark_pos in model.shape: resist.add(self._get_model(v2.sum(pos, mark_pos))) resist.discard(model) resist.discard(None) return resist
def _get_used_cells(self, s): """Returns positions that are needed to decide if the actions are applicable in the state. """ used_cells = set() maze = Maze(s, border=UNKNOWN_MARK) positions = maze.find_all_positions(PLAYER_MARKS) for pos in positions: used_cells.add(pos) for shift in SHIFTS: next_pos = v2.sum(pos, shift) used_cells.add(next_pos) if maze.get(next_pos) in BOX_MARKS: next_next_pos = v2.sum(next_pos, shift) used_cells.add(next_next_pos) return used_cells
def _apply_end_state(s, shift, end_state): next_s = modeling.mutablize(s) for local_y, row in enumerate(end_state): for local_x, mark in enumerate(row): if mark is not UNKNOWN_MARK: x, y = v2.sum(shift, (local_x, local_y)) next_s[y][x] = mark return next_s
def get_actions(self, s): """Returns all possible actions from the given state. """ actions = [] maze = Maze(s) man_positions = maze.find_all_positions(PLAYER_MARKS) for pos in man_positions: for shift in SHIFTS: new_pos = v2.sum(pos, shift) cell = maze.get(new_pos) if cell in EMPTY_MARKS: actions.append(_action_move(maze, pos, new_pos)) elif cell in BOX_MARKS: behind_new_pos = v2.sum(new_pos, shift) if maze.get(behind_new_pos) in EMPTY_MARKS: actions.append( _action_push(maze, pos, new_pos, behind_new_pos)) return actions
def _is_matching(shift, pattern, maze): for y, row in enumerate(pattern): for x, mark in enumerate(row): if mark == UNKNOWN_MARK: continue pos = v2.sum(shift, (x,y)) try: if mark != maze.get(pos): return False except IndexError, e: return False
def test_get_falling(self): steel = parsing.find_model(self.models, "A") for i in range(10): falling = self.landslip.get_falling() self.assertEquals(1, len(falling)) self.assertEquals(steel, falling[0]) steel.pos = v2.sum(steel.pos, (0, 1)) self.field.place_models(self.models) falling = self.landslip.get_falling() self.assertEquals(0, len(falling))
def test_calc_move(self): small = parsing.find_model(self.models, "+") orig_positions = [m.pos for m in self.models] shift = (1, 0) positions, cost = self.rules.calc_move(small, shift) self.assertEquals(orig_positions, [m.pos for m in self.models]) self.assertTrue(positions is not None) self.assertTrue(positions != orig_positions) self.assertEquals(1, cost) index = orig_positions.index(small.pos) self.assertEquals(positions[index], v2.sum(small.pos, shift)) positions[index] = small.pos self.assertEquals(orig_positions, positions) self.assertEquals((None, None), self.rules.calc_move(small, (0, 1))) self.assertEquals((None, None), self.rules.calc_move(small, (-1, 1)))
def predict(self, s, a): shift = a.get_cmd() space_pos = self._find_space(s) new_pos = v2.sum(space_pos, shift) return _exchange(s, space_pos, new_pos)
def _move_models(self, moving_models, shift): for model in moving_models: model.pos = v2.sum(model.pos, shift) self.field.place_models(self.models)
def _get_ground_conds(self, conds): ground = [] for val, shift in conds: pos = v2.sum(self.tile, shift) ground.append((val, pos)) return ground