def return_moves(self, game): """Return all possible beetle moves""" available_moves = [] if self.under: for c in range(NUM_OF_CONNECTIONS): position = count_new_position(self.position, c) available_moves.append(AvailMove(self, position)) else: available_positions = self.basic_move(1, game) for position in available_positions: available_moves.append(AvailMove(self, position)) for c in range(NUM_OF_CONNECTIONS): if self.connections[c] != PLACEHOLDER: position = count_new_position(self.position, c) available_moves.append(AvailMove(self, position)) return available_moves
def test_correct_position(self): assert count_new_position((10, 10), 0) == (9, 9) assert count_new_position((10, 10), 1) == (9, 11) assert count_new_position((10, 10), 2) == (10, 12) assert count_new_position((10, 10), 3) == (11, 11) assert count_new_position((10, 10), 4) == (11, 9) assert count_new_position((10, 10), 5) == (10, 8) assert count_new_position((10, 10), 6) == (9, 9)
def return_moves(self, _): """Return all possible grasshopper moves""" available_moves = [] for c in range(NUM_OF_CONNECTIONS): if self.connections[c] != PLACEHOLDER: possible_position = self while possible_position.connections[c] != PLACEHOLDER: possible_position = possible_position.connections[c] position = count_new_position(possible_position.position, c) available_moves.append(AvailMove(self, position)) return available_moves
def test_incorrect_position(self): assert count_new_position((10, 10), 0) != (9, 10) assert count_new_position((10, 10), 1) != (9, 10) assert count_new_position((10, 10), 2) != (10, 10) assert count_new_position((10, 10), 3) != (11, 10) assert count_new_position((10, 10), 4) != (11, 10) assert count_new_position((10, 10), 5) != (10, 10)
def spider_move(self, game, current_position, distance=3, previous_position=None): """Basic move for spider (sliding 3 steps around hive) :param distance: distance how :param game: game with board :param current_position: position from where will be looking for next position :param previous_position: previous position where stone can't return :return: positions where the spider can move """ available_positions = [] next_positions = [] for c in range(NUM_OF_CONNECTIONS): condition1 = check_position(game, current_position, c) == PLACEHOLDER condition2 = check_position(game, current_position, c - 1) == PLACEHOLDER condition3 = check_position(game, current_position, c + 1) == PLACEHOLDER condition4 = count_new_position(current_position, c) != previous_position condition2_or_condition3 = not (condition2 and condition3) and ( condition2 or condition3) if condition1 and condition2_or_condition3 and condition4: next_position = count_new_position(current_position, c) next_positions.append(next_position) for position in next_positions: if distance == 0: available_positions.append(position) else: available_positions += self.spider_move( game, current_position=position, distance=distance - 1, previous_position=current_position, ) return available_positions
def return_placing_positions(): def check_placable(position): if game.board[position[0]][position[1]] == PLACEHOLDER: for index in range(NUM_OF_CONNECTIONS): condition1 = check_position(game, position, index) == PLACEHOLDER condition2 = check_position(game, position, index) in player.placed_stones if not condition1 and not condition2: return [] return position return [] placing_positions = set() for stone in player.placed_stones: for index in range(NUM_OF_CONNECTIONS): possible_position = count_new_position(stone.position, index) if check_placable(possible_position): placing_positions.add(possible_position) return placing_positions
def basic_move(self, distance, game): """Return all possible basic positions(where stone could move by sliding around hive) in chosen distance :param distance: integer stating how far can stone move :param game: game :return: all position where stone could move by sliding around hive """ game.board[self.position[0]][self.position[1]] = PLACEHOLDER track_positions = [self.position] available_positions = [] closed_positions = [] depth = 0 while track_positions: if depth == distance: break for c in range(NUM_OF_CONNECTIONS): condition1 = check_position(game, track_positions[0], c) == PLACEHOLDER condition2 = check_position(game, track_positions[0], c - 1) == PLACEHOLDER condition3 = check_position(game, track_positions[0], c + 1) == PLACEHOLDER if condition1 and not (condition2 and condition3) and ( condition2 or condition3): next_position = count_new_position(track_positions[0], c) condition1 = next_position not in closed_positions condition2 = next_position not in available_positions if condition1 and condition2: track_positions.append(next_position) available_positions.append(next_position) closed_positions.append(track_positions.pop(0)) depth += 1 game.board[self.position[0]][self.position[1]] = self return available_positions