Exemple #1
0
    def get_piece_value(self, board, config=None, *, color=None):
        """
        Returns value of pieces remaining on board (difference of your pieces and opponent's pieces)

        Accepts Board object and config dictionary

        Uses standard piece values (and 8 for king)
        """
        # print("Getting piece value!")
        assert config or color
        board = board.board
        color = color if color else config['color']
        values = {'p': 1, 'n': 3, 'b': 3, 'q': 9, 'k': 8, 'r': 5}
        black_value = sum(
            sum(values[piece] for piece in row
                if piece and piece_color(piece) == "black") for row in board)
        white_value = sum(
            sum(values[piece.lower()] for piece in row
                if piece and piece_color(piece) == "white") for row in board)
        return white_value - black_value if color == 'white' else black_value - white_value
Exemple #2
0
 def check_pos(new_pos, diag):
     if valid_pos(new_pos):
         y2, x2 = new_pos
         piece2 = self.board[y2][x2]
         if (not diag
                 and not piece2) or (diag and piece2
                                     and color != piece_color(piece2)):
             if y2 in [0, 7]:
                 # Promote to queen by default
                 moves.append(
                     (y2, x2, "Q" if color == 'white' else "q"))
             else:
                 moves.append((y2, x2))
Exemple #3
0
 def check_for_attacks(self, color):
     # Find king
     found = False
     target = 'K' if color == 'white' else 'k'
     for (y, x) in all_squares():
         if self.board[y][x] == target:
             king = (y, x)
             found = True
             break
     if not found:
         # print(f"Looking for {target}, {color}, but not found!")
         return False  # To allow a dead side to make hypothetical postmortem moves
     # Check for attacking pieces
     for (y2, x2) in all_squares():
         piece2 = self.board[y2][x2]
         color2 = piece_color(piece2)
         if color != color2 and king in self.get_moves(
             (y2, x2), color2, check_for_check=False):
             return True
     return False
Exemple #4
0
    def get_moves(self, pos, color, *, check_for_check=True):
        y, x = pos
        piece = self.board[y][x]
        if not piece:
            return []
        if piece:
            p_color = piece_color(piece)

            moves = []

            # Check that we are not moving the opponent's pieces
            if p_color == color:
                lower = piece.lower()
                # Rook or Queen
                if lower == 'r' or lower == 'q':
                    self.__get_rook_moves__(pos, color, moves)

                # Knight
                if lower == 'n':
                    self.__get_knight_moves__(pos, color, moves)

                # Bishop or Queen
                if lower == 'b' or lower == 'q':
                    self.__get_bishop_moves__(pos, color, moves)

                # King
                if lower == 'k':
                    self.__get_king_moves__(pos, color, moves)

                # Pawn
                if lower == 'p':
                    self.__get_pawn_moves__(pos, color, moves)

                # if pos == (2, 7): print("PRECHECK", moves)
                if check_for_check:
                    moves = list(
                        filter(
                            lambda move: not self.puts_into_check(
                                pos, move, color), moves))
                # if pos == (2, 7): print("\tPOSTCHECK", moves)
            return moves
Exemple #5
0
    def __get_rook_moves__(self, pos, color, moves):
        y, x = pos

        # Increase y
        y2 = y + 1
        while y2 < 8:
            piece2 = self.board[y2][x]
            # Piece collision
            if piece2:
                color2 = piece_color(piece2)
                # Capture
                if color != color2:
                    moves.append((y2, x))
                break
            else:
                moves.append((y2, x))
            y2 += 1

        # Decrease y
        y2 = y - 1
        while y2 >= 0:
            piece2 = self.board[y2][x]
            # Piece collision
            if piece2:
                color2 = piece_color(piece2)
                # Capture
                if color != color2:
                    moves.append((y2, x))
                break
            else:
                moves.append((y2, x))
            y2 -= 1

        # Increase x
        x2 = x + 1
        while x2 < 8:
            piece2 = self.board[y][x2]
            # Piece collision
            if piece2:
                color2 = piece_color(piece2)
                # Capture
                if color != color2:
                    moves.append((y, x2))
                break
            else:
                moves.append((y, x2))
            x2 += 1

        # Decrease x
        x2 = x - 1
        while x2 >= 0:
            piece2 = self.board[y][x2]
            # Piece collision
            if piece2:
                color2 = piece_color(piece2)
                # Capture
                if color != color2:
                    moves.append((y, x2))
                break
            else:
                moves.append((y, x2))
            x2 -= 1
Exemple #6
0
 def check_pos(new_pos):
     if valid_pos(new_pos):
         y2, x2 = new_pos
         piece2 = self.board[y2][x2]
         if not piece2 or color != piece_color(piece2):
             moves.append((y2, x2))
Exemple #7
0
    def __get_bishop_moves__(self, pos, color, moves):
        y, x = pos

        # Increase y and x
        y2, x2 = y + 1, x + 1
        while y2 < 8 and x2 < 8:
            piece2 = self.board[y2][x2]
            # Piece collision
            if piece2:
                color2 = piece_color(piece2)
                # Capture
                if color != color2:
                    moves.append((y2, x2))
                break
            else:
                moves.append((y2, x2))
            y2 += 1
            x2 += 1

        # Decrease y and x
        y2, x2 = y - 1, x - 1
        while y2 >= 0 and x2 >= 0:
            piece2 = self.board[y2][x2]
            # Piece collision
            if piece2:
                color2 = piece_color(piece2)
                # Capture
                if color != color2:
                    moves.append((y2, x2))
                break
            else:
                moves.append((y2, x2))
            y2 -= 1
            x2 -= 1

        # Increase y, decrease x
        y2, x2 = y + 1, x - 1
        while y2 < 8 and x2 >= 0:
            piece2 = self.board[y2][x2]
            # Piece collision
            if piece2:
                color2 = piece_color(piece2)
                # Capture
                if color != color2:
                    moves.append((y2, x2))
                break
            else:
                moves.append((y2, x2))
            y2 += 1
            x2 -= 1

        # Decrease y, increase x
        y2, x2 = y - 1, x + 1
        while y2 >= 0 and x2 < 8:
            piece2 = self.board[y2][x2]
            # Piece collision
            if piece2:
                color2 = piece_color(piece2)
                # Capture
                if color != color2:
                    moves.append((y2, x2))
                break
            else:
                moves.append((y2, x2))
            y2 -= 1
            x2 += 1