Esempio n. 1
0
 def load_directions(self):
     return [
         Vector2f(1, 1),
         Vector2f(-1, 1),
         Vector2f(-1, -1),
         Vector2f(1, -1)
     ]
Esempio n. 2
0
 def moves_available(self, board) -> List[movedata.MoveData]:
     moves = []
     # to divide and move 1 case at once
     divisor = 8
     for dir_angle in self.directions:
         # The next case on which the piece will be
         next_destination = Vector2f(self.position.x + dir_angle.x,
                                     self.position.y + dir_angle.y)
         index = 0
         while index != divisor:
             # if the piece goes out of the board or if it is on another piece
             # stop the loop
             piece = board.piece_at_location(next_destination)
             if piece is not None and gamepiece.Piece.location_on_board(
                     next_destination):
                 if piece.color != self.color:
                     moves.append(
                         self.to_simple_move_data(next_destination))
                 break
             elif piece is None and gamepiece.Piece.location_on_board(
                     next_destination):
                 moves.append(self.to_simple_move_data(next_destination))
             next_destination = Vector2f(next_destination.x + dir_angle.x,
                                         next_destination.y + dir_angle.y)
             index += 1
     return moves
Esempio n. 3
0
 def add_regular_moves(self, board, moves):
     direction = Vector2f(
         0, 1) if self.color.color_name == 'white' else Vector2f(0, -1)
     can_go_forward = self.add_if_empty(board, moves,
                                        self.position + direction)
     # When it is the first time the pawn moves, it can moves 2 cases
     if can_go_forward and not self.already_moved:
         self.add_if_empty(board, moves,
                           self.position + direction.scalar_mult(2))
Esempio n. 4
0
    def add_taking_moves(self, board, moves):
        """
        Verify if there is an adverse piece that can be taken.
        """
        can_go = []

        factor = -1 if self.color.color_name == "black" else 1
        diagonal_left = Vector2f(self.position.x - 1, self.position.y + factor)
        diagonal_right = Vector2f(self.position.x + 1,
                                  self.position.y + factor)

        can_go.append(board.piece_at_location(diagonal_left))
        can_go.append(board.piece_at_location(diagonal_right))
        for piece in can_go:
            if piece is not None:
                if piece.color != self.color:
                    moves.append(piece.position)
Esempio n. 5
0
def initial_row(row, color):
    """
    Used to retrieve a list of renderable pieces according to their initial row.
    Row 1 or row 6 correpond to a range of 8 pawns
    Row 0 or row 7 correpond to the defense line, containing the king
    """
    if row == 1 or row == 6:
        return [
            pieces.PawnRenderable(Vector2f(x, row), color) for x in range(8)
        ]
    return [
        pieces.RookRenderable(Vector2f(0, row), color),
        pieces.KnightRenderable(Vector2f(1, row), color),
        pieces.BishopRenderable(Vector2f(2, row), color),
        pieces.QueenRenderable(Vector2f(3, row), color),
        pieces.KingRenderable(Vector2f(4, row), color),
        pieces.BishopRenderable(Vector2f(5, row), color),
        pieces.KnightRenderable(Vector2f(6, row), color),
        pieces.RookRenderable(Vector2f(7, row), color)
    ]
Esempio n. 6
0
    def moves_available(self, board):
        """
        How the piece moves.
        """
        moves = []
        # Every move possible
        for i in [-1, -2, 1, 2]:
            for j in [-1, -2, 1, 2]:
                next_position = Vector2f(self.position.x + i,
                                         self.position.y + j)
                if abs(i) != abs(j) and board.can_move_at_location(
                        next_position, self.color):
                    if gamepiece.Piece.location_on_board(next_position):
                        moves.append(self.to_simple_move_data(next_position))

        return moves
Esempio n. 7
0
    def moves_available(self, board):
        """
        The moves executable by the king
        """
        moves = []
        for i in [-1, 0, 1]:
            for j in [-1, 0, 1]:
                next_position = Vector2f(self.position.x + i,
                                         self.position.y + j)
                # verify if the next location is on the board and if it can goes there
                if gamepiece.Piece.location_on_board(
                        next_position) and board.can_move_at_location(
                            next_position, self.color):
                    moves.append(self.to_simple_move_data(next_position))

        # To check if the king can castle with the rooks
        for rook in board.get_by_name("rook", self.color):
            self.add_castling_move(rook, board, moves)
        return moves