def get_diagonal_moves(self, piece: Piece, board: List[List[Piece]]) -> List[Move]: """ returns a list of all diagonal moves arguments: piece -- piece to generate moves board -- board to validate moves """ if piece.piece_type != Type.BISHOP and piece.piece_type != Type.QUEEN: return [] directions: List[Tuple[int, int]] = [(1, 1), (-1, 1), (-1, -1), (1, -1)] moves: List[Move] = [] for direction in directions: destination: Coordinate = Coordinate( piece.coordinate.get_rank() + direction[0], piece.coordinate.get_file() + direction[1], ) while self.in_board(destination): moves.append( Move(piece.coordinate, destination, self.piece_at(destination, board))) if self.piece_at(destination, board): break destination = Coordinate( destination.get_rank() + direction[0], destination.get_file() + direction[1], ) return moves
def test_first_input(self): coordinate = Coordinate(1, 2) plateau = Plateau(5, 5) rover = Rover(coordinate, North()) RoverManager(plateau, rover).perform("LMLMLMLMM") self.assertEqual(rover.coordinate, Coordinate(1, 3)) self.assertEqual(rover.direction, North())
def test_second_input(self): coordinate = Coordinate(3, 3) plateau = Plateau(5, 5) rover = Rover(coordinate, East()) RoverManager(plateau, rover).perform("MMRMMRMRRM") self.assertEqual(rover.coordinate, Coordinate(5, 1)) self.assertEqual(rover.direction, East())
def test_snake_has_coordinates(self): snake = self.get_snake() assert snake.coordinates == [ Coordinate(10, 15), Coordinate(11, 15), Coordinate(11, 16), Coordinate(11, 17) ]
def in_board(self, coordinate: Coordinate) -> bool: """ returns whether a coordinate is within the bounds of the board arguments: coordinate -- coordinate to check """ return (coordinate.get_rank() >= 0 and coordinate.get_file() >= 0 and coordinate.get_rank() < self.HEIGHT and coordinate.get_file() < self.WIDTH)
def test_snake_can_tell_if_contains_coordinates(self): snake = self.get_snake() coordinate = Coordinate(9, 15) assert snake.contains_coordinate(coordinate) == False coordinate = Coordinate(10, 14) assert snake.contains_coordinate(coordinate) == False coordinate = Coordinate(10, 15) assert snake.contains_coordinate(coordinate) == True
def test_snake_can_get_all_moves(self): snake = self.get_snake() all_moves = [ Coordinate(9, 15), Coordinate(11, 15), Coordinate(10, 14), Coordinate(10, 16) ] assert snake.get_all_moves() == all_moves
def piece_at(self, coordinate: Coordinate, board: List[List[Piece]]) -> Union[Piece, None]: """ returns a reference to the piece at a specified coordinate, if no piece is present returns None arguments: coordinate -- coordinate to check board -- board to check for piece in """ if not self.in_board(coordinate): return None else: return board[coordinate.get_rank()][coordinate.get_file()]
def test_coordinates_can_be_compared(self): coordinate_a = Coordinate(4, 5) coordinate_b = Coordinate(4, 5) # Coordinate objects are unique. assert coordinate_a is not coordinate_b # Coordinates are comparable based on x and y values. assert coordinate_a == coordinate_b coordinate_c = Coordinate(5, 4) assert coordinate_a != coordinate_c coordinate_d = (coordinate_a.x, coordinate_a.y) # Coordinates can only be compared against other coordinates. assert (coordinate_a == coordinate_d) is not True assert coordinate_a != coordinate_d
def foods(self) -> List[Coordinate]: """Retreive the list of food from the board data.""" if self._foods is None: self._foods = [ Coordinate(food_data) for food_data in self._data['food'] ] return self._foods
def coordinates(self) -> List[Coordinate]: """Get the List of Coordinates which describes the location of this Snake.""" if not self._coordinates: self._coordinates = [ Coordinate(coordinate_tuple) for coordinate_tuple in self._data['body'] ] return self._coordinates
def test_brain_can_avoid_headons(self): two_close_snakes = self.main_snake + self.equal_snake brain = self.get_brain(two_close_snakes, self.foods, self.width) assert brain.get_threatening_snakes_moves() == [ Coordinate(2,1), Coordinate(4,1), Coordinate(3,0), Coordinate(3,2), ] assert brain.get_valid_moves() == ["left"] snake_and_smol_snek = self.main_snake + self.smol_snek unthreatened_brain = self.get_brain(snake_and_smol_snek , self.foods, self.width) assert unthreatened_brain.get_threatening_snakes_moves() == [] assert unthreatened_brain.get_valid_moves() == ["left", "right", "up"]
def test_brain_can_get_moves_to_coord(self): brain = self.get_brain(self.main_snake, self.foods, self.width) assert brain.get_moves_to(Coordinate(9,0)) == ["right", "up"] assert brain.get_moves_to(Coordinate(9,2)) == ["right"] assert brain.get_moves_to(Coordinate(9,9)) == ["right", "down"] assert brain.get_moves_to(Coordinate(2,9)) == ["down"] assert brain.get_moves_to(Coordinate(0,9)) == ["down", "left"] assert brain.get_moves_to(Coordinate(0,2)) == ["left"] assert brain.get_moves_to(Coordinate(0,0)) == ["left", "up"]
def get_nearest_food(self) -> Optional[Coordinate]: """Get the food item which has coordinates closest to this snake's head.""" closest_food = (Coordinate((0, 0)), 9999.0) for food in self.board.foods: distance = self.me.head.get_distance_from(food) if distance < closest_food[1]: closest_food = (food, distance) if closest_food[1] == 9999.0: return None return closest_food[0]
def get_nearest_food(self) -> Optional[Coordinate]: """Get the food item which has coordinates closest to this snake's head.""" closest_food = (Coordinate((0, 0)), 9999.0) for food in self.board.foods: x_diff = self.me.head.x - food.x y_diff = self.me.head.y - food.y distance = math.sqrt(x_diff * x_diff + y_diff * y_diff) if distance < closest_food[1]: closest_food = (food, distance) if closest_food[1] == 9999.0: return None return closest_food[0]
def checkmated(self, color: Color, board: List[List[Piece]]) -> bool: """ checks whether a side is currently in checkmate on a board arguments: color -- color to check if in check board -- board to check on """ if not self.in_check(color, board): return False for r in range(self.HEIGHT): for f in range(self.WIDTH): piece: Union[Piece, None] = self.piece_at(Coordinate(r, f), board) if piece and piece.color == color and len( self.get_moves(piece, board)): return False return True
def get_knight_moves(self, piece: Piece, board: List[List[Piece]]) -> List[Move]: """ returns a list of all knight moves arguments: piece -- piece to generate moves board -- board to validate moves """ if piece.piece_type != Type.KNIGHT: return [] offsets: List[Tuple[int, int]] = [ (2, 1), (1, 2), (-1, 2), (-2, 1), (-2, -1), (-1, -2), (1, -2), (2, -1), ] moves: List[Move] = [] for offset in offsets: destination: Coordinate = Coordinate( piece.coordinate.get_rank() + offset[0], piece.coordinate.get_file() + offset[1], ) if self.in_board(destination): moves.append( Move(piece.coordinate, destination, self.piece_at(destination, board))) return moves
def test_coordinate_is_in_bounds(self): board = self.get_board() coordinate = Coordinate(-1, 0) assert board.is_coordinate_in_bounds(coordinate) == False coordinate = Coordinate(0, -1) assert board.is_coordinate_in_bounds(coordinate) == False coordinate = Coordinate(15, 0) assert board.is_coordinate_in_bounds(coordinate) == False coordinate = Coordinate(0, 15) assert board.is_coordinate_in_bounds(coordinate) == False coordinate = Coordinate(0, 14) assert board.is_coordinate_in_bounds(coordinate) == True coordinate = Coordinate(14, 0) assert board.is_coordinate_in_bounds(coordinate) == True
def test_coordinate_can_provide_left_coordinate(self): coordinate = (self.DEFAULT_X - 1, self.DEFAULT_Y) left_coordinate = Coordinate(coordinate) assert self.get_coordinate().get_left() == left_coordinate
def test_coordinate_can_provide_right_coordinate(self): coordinate = (self.DEFAULT_X + 1, self.DEFAULT_Y) right_coordinate = Coordinate(coordinate) assert self.get_coordinate().get_right() == right_coordinate
def test_coordinate_can_provide_down_coordinate(self): coordinate = (self.DEFAULT_X, self.DEFAULT_Y + 1) down_coordinate = Coordinate(coordinate) assert self.get_coordinate().get_down() == down_coordinate
def test_coordinate_can_provide_up_coordinate(self): coordinate = (self.DEFAULT_X, self.DEFAULT_Y - 1) up_coordinate = Coordinate(coordinate) assert self.get_coordinate().get_up() == up_coordinate
def test_create_coordinate(self): assert type(Coordinate(5, 5)).__name__ == 'Coordinate'
def test_coordinate_Created(self): coordinate = Coordinate(1, 2) self.assertEqual(coordinate, Coordinate(1, 2))
def test_move_when_pointed_east_in_x_direction_forward(self): rover = Rover(self.coordinate, East()) rover.move(self.plateau.is_coordinate_with_bounds) self.assertEqual(rover.coordinate, Coordinate(2, 2))
def get_coordinate(self): coordinate = (self.DEFAULT_X, self.DEFAULT_Y) return Coordinate(coordinate)
def test_board_has_foods(self): assert self.get_board().foods == [Coordinate(10, 5), Coordinate(6, 12)]
def test_move_when_pointed_south_in_y_direction_downwards(self): rover = Rover(self.coordinate, South()) rover.move(self.plateau.is_coordinate_with_bounds) self.assertEqual(rover.coordinate, Coordinate(1, 1))
def test_board_can_advance_snake(self): board = self.get_board() path = [ Coordinate(3, 2), Coordinate(4, 2), Coordinate(5, 2), Coordinate(6, 2), Coordinate(7, 2), Coordinate(8, 2), Coordinate(9, 2), Coordinate(10, 2), Coordinate(10, 3), Coordinate(10, 4), Coordinate(10, 5) ] new_board = board.advance_snake_along_path(board.snakes[0].id, path) new_snake_coords = new_board.snakes[0].coordinates assert new_snake_coords[0] == Coordinate(10, 5) assert new_snake_coords[1] == Coordinate(10, 4) assert new_snake_coords[2] == Coordinate(10, 3) assert new_snake_coords[3] == Coordinate(10, 3) assert len(new_snake_coords) == 4
def test_move_when_pointed_west_in_x_direction_backwards(self): rover = Rover(self.coordinate, West()) rover.move(self.plateau.is_coordinate_with_bounds) self.assertEqual(rover.coordinate, Coordinate(0, 2))