Exemplo n.º 1
0
    def possible_moves(self):
        x, y = self.square
        # typical one hop moves
        moves = list(
            map(
                lambda square: Move.typical(
                    self.board, piece=self, new_square=square),
                filter(
                    # Either no piece or enemy piece to capture
                    lambda square: square and (not self.board[
                        square] or self.board[square].player != self.player),
                    (Square(x + 1, y), Square(x, y + 1), Square(x + 1, y + 1),
                     Square(x - 1, y + 1), Square(x - 1, y), Square(x, y - 1),
                     Square(x + 1, y - 1), Square(x - 1, y - 1)))))

        # castling moves
        king_side_castle = self.king_side_castle()
        if king_side_castle:
            moves.append(king_side_castle)

        queen_side_castle = self.queen_side_castle()
        if queen_side_castle:
            moves.append(queen_side_castle)

        return moves
Exemplo n.º 2
0
    def possible_moves(self):
        x, y = self.square
        forward_square = Square(
            x, y - 1) if self.player == Player.WHITE else Square(x, y + 1)
        forward_two_square = Square(
            x, y - 2) if self.player == Player.WHITE else Square(x, y + 2)
        right_square = Square(x + 1, y -
                              1) if self.player == Player.WHITE else Square(
                                  x + 1, y + 1)
        left_square = Square(x - 1, y -
                             1) if self.player == Player.WHITE else Square(
                                 x - 1, y + 1)

        moves = []
        if not self.moved:  # if not yet moved
            if not self.board[forward_square] and not self.board[
                    forward_two_square]:
                # if nothing in first two squares, can move two squares
                moves.append(
                    Move.typical(self.board,
                                 piece=self,
                                 new_square=forward_two_square))

        # promotion occurs if pawn reaches last rank
        promotion = (y - 1) == 0 if self.player == Player.WHITE else (y +
                                                                      1) == 7
        if promotion:
            # note: promoted piece is determined by the player.
            if forward_square and not self.board[
                    forward_square]:  # if nothing forward, can move one square forward
                moves.append(
                    Move.pawn_promotion(self.board,
                                        self,
                                        forward_square,
                                        new_piece=None))
            if right_square and self.board[right_square] and self.board[
                    right_square].player != self.player:
                moves.append(
                    Move.pawn_promotion(self.board,
                                        self,
                                        right_square,
                                        new_piece=None))
            if left_square and self.board[left_square] and self.board[
                    left_square].player != self.player:
                moves.append(
                    Move.pawn_promotion(self.board,
                                        self,
                                        left_square,
                                        new_piece=None))

        else:  # if no promotion possible (typical)
            if forward_square and not self.board[
                    forward_square]:  # if nothing forward, can move one square forward
                moves.append(Move.typical(self.board, self, forward_square))
            if right_square and self.board[right_square] and self.board[
                    right_square].player != self.player:
                moves.append(Move.typical(self.board, self, right_square))
            if left_square and self.board[left_square] and self.board[
                    left_square].player != self.player:
                moves.append(Move.typical(self.board, self, left_square))

        en_passant = self.en_passant()
        if en_passant:
            moves.append(en_passant)

        return moves
Exemplo n.º 3
0
 def possible_moves(self):
     return list(
         map(lambda square: Move.typical(self.board, self, square),
             self.attack_squares()))