How to use the chess.__init__.Piece function in chess

To help you get started, we’ve selected a few chess examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github niklasf / python-chess / chess / __init__.py View on Github external
def piece_at(self, square):
        mask = BB_SQUARES[square]
        color = int(bool(self.occupied_co[BLACK] & mask))

        if mask & self.pawns:
            return Piece(PAWN, color)
        elif mask & self.knights:
            return Piece(KNIGHT, color)
        elif mask & self.bishops:
            return Piece(BISHOP, color)
        elif mask & self.rooks:
            return Piece(ROOK, color)
        elif mask & self.queens:
            return Piece(QUEEN, color)
        elif mask & self.kings:
            return Piece(KING, color)
github niklasf / python-chess / chess / __init__.py View on Github external
else:
                    raise ValueError("invalid character in position part of fen: {!r}".format(fen))

            if field_sum != 8:
                raise ValueError("expected 8 columns per row in position part of fen: {!r}".format(fen))

        # Clear the board.
        self._clear_board()

        # Put pieces on the board.
        square_index = 0
        for c in fen:
            if c in ["1", "2", "3", "4", "5", "6", "7", "8"]:
                square_index += int(c)
            elif c.lower() in PIECE_SYMBOLS:
                piece = Piece.from_symbol(c)
                self._set_piece_at(SQUARES_180[square_index], piece.piece_type, piece.color)
                square_index += 1
            elif c == "~":
                self.promoted |= BB_SQUARES[SQUARES_180[square_index - 1]]
github niklasf / python-chess / chess / __init__.py View on Github external
if piece_type == KING:
            if move.from_square == E1 and move.to_square == G1:
                self.set_piece_at(F1, Piece(ROOK, WHITE))
                self.remove_piece_at(H1)
            elif move.from_square == E1 and move.to_square == C1:
                self.set_piece_at(D1, Piece(ROOK, WHITE))
                self.remove_piece_at(A1)
            elif move.from_square == E8 and move.to_square == G8:
                self.set_piece_at(F8, Piece(ROOK, BLACK))
                self.remove_piece_at(H8)
            elif move.from_square == E8 and move.to_square == C8:
                self.set_piece_at(D8, Piece(ROOK, BLACK))
                self.remove_piece_at(A8)

        # Put piece on target square.
        self.set_piece_at(move.to_square, Piece(piece_type, self.turn))

        # Swap turn.
        self.turn ^= 1
github niklasf / python-chess / chess / __init__.py View on Github external
elif move.from_square == H8 or move.to_square == H8:
            self.castling_rights &= ~CASTLING_BLACK_KINGSIDE

        # Castling.
        if piece_type == KING:
            if move.from_square == E1 and move.to_square == G1:
                self.set_piece_at(F1, Piece(ROOK, WHITE))
                self.remove_piece_at(H1)
            elif move.from_square == E1 and move.to_square == C1:
                self.set_piece_at(D1, Piece(ROOK, WHITE))
                self.remove_piece_at(A1)
            elif move.from_square == E8 and move.to_square == G8:
                self.set_piece_at(F8, Piece(ROOK, BLACK))
                self.remove_piece_at(H8)
            elif move.from_square == E8 and move.to_square == C8:
                self.set_piece_at(D8, Piece(ROOK, BLACK))
                self.remove_piece_at(A8)

        # Put piece on target square.
        self.set_piece_at(move.to_square, Piece(piece_type, self.turn))

        # Swap turn.
        self.turn ^= 1

        # Update transposition table.
        self.transpositions.update((self.zobrist_hash(), ))
github niklasf / python-chess / chess / __init__.py View on Github external
if self.turn == WHITE:
                    self.set_piece_at(move.to_square + 8, Piece(PAWN, WHITE))
                else:
                    self.set_piece_at(move.to_square - 8, Piece(PAWN, BLACK))

        # Restore rook position after castling.
        if piece == KING:
            if move.from_square == E1 and move.to_square == G1:
                self.remove_piece_at(F1)
                self.set_piece_at(H1, Piece(ROOK, WHITE))
            elif move.from_square == E1 and move.to_square == C1:
                self.remove_piece_at(D1)
                self.set_piece_at(A1, Piece(ROOK, WHITE))
            elif move.from_square == E8 and move.to_square == G8:
                self.remove_piece_at(F8)
                self.set_piece_at(H8, Piece(ROOK, BLACK))
            elif move.from_square == E8 and move.to_square == C8:
                self.remove_piece_at(D8)
                self.set_piece_at(A8, Piece(ROOK, BLACK))

        # Swap turn.
        self.turn ^= 1

        return move
github niklasf / python-chess / chess / __init__.py View on Github external
return move

        # Restore the source square.
        piece = PAWN if move.promotion else self.piece_type_at(move.to_square)
        self.set_piece_at(move.from_square, Piece(piece, self.turn ^ 1))

        # Restore target square.
        if captured_piece:
            self.set_piece_at(move.to_square, Piece(captured_piece, captured_piece_color))
        else:
            self.remove_piece_at(move.to_square)

            # Restore captured pawn after en-passant.
            if piece == PAWN and abs(move.from_square - move.to_square) in (7, 9):
                if self.turn == WHITE:
                    self.set_piece_at(move.to_square + 8, Piece(PAWN, WHITE))
                else:
                    self.set_piece_at(move.to_square - 8, Piece(PAWN, BLACK))

        # Restore rook position after castling.
        if piece == KING:
            if move.from_square == E1 and move.to_square == G1:
                self.remove_piece_at(F1)
                self.set_piece_at(H1, Piece(ROOK, WHITE))
            elif move.from_square == E1 and move.to_square == C1:
                self.remove_piece_at(D1)
                self.set_piece_at(A1, Piece(ROOK, WHITE))
            elif move.from_square == E8 and move.to_square == G8:
                self.remove_piece_at(F8)
                self.set_piece_at(H8, Piece(ROOK, BLACK))
            elif move.from_square == E8 and move.to_square == C8:
                self.remove_piece_at(D8)
github niklasf / python-chess / chess / __init__.py View on Github external
self.ply -= 1

        # Restore state.
        self.half_moves = self.half_move_stack.pop()
        self.castling_rights = self.castling_right_stack.pop()
        self.ep_square = self.ep_square_stack.pop()
        captured_piece = self.captured_piece_stack.pop()
        captured_piece_color = self.turn

        # Restore the source square.
        piece = PAWN if move.promotion else self.piece_type_at(move.to_square)
        self.set_piece_at(move.from_square, Piece(piece, self.turn ^ 1))

        # Restore target square.
        if captured_piece:
            self.set_piece_at(move.to_square, Piece(captured_piece, captured_piece_color))
        else:
            self.remove_piece_at(move.to_square)

            # Restore captured pawn after en passant.
            if piece == PAWN and abs(move.from_square - move.to_square) in (7, 9):
                if self.turn == WHITE:
                    self.set_piece_at(move.to_square + 8, Piece(PAWN, WHITE))
                else:
                    self.set_piece_at(move.to_square - 8, Piece(PAWN, BLACK))

        # Restore rook position after castling.
        if piece == KING:
            if move.from_square == E1 and move.to_square == G1:
                self.remove_piece_at(F1)
                self.set_piece_at(H1, Piece(ROOK, WHITE))
            elif move.from_square == E1 and move.to_square == C1:
github niklasf / python-chess / chess / __init__.py View on Github external
self.set_piece_at(move.to_square - 8, Piece(PAWN, BLACK))

        # Restore rook position after castling.
        if piece == KING:
            if move.from_square == E1 and move.to_square == G1:
                self.remove_piece_at(F1)
                self.set_piece_at(H1, Piece(ROOK, WHITE))
            elif move.from_square == E1 and move.to_square == C1:
                self.remove_piece_at(D1)
                self.set_piece_at(A1, Piece(ROOK, WHITE))
            elif move.from_square == E8 and move.to_square == G8:
                self.remove_piece_at(F8)
                self.set_piece_at(H8, Piece(ROOK, BLACK))
            elif move.from_square == E8 and move.to_square == C8:
                self.remove_piece_at(D8)
                self.set_piece_at(A8, Piece(ROOK, BLACK))

        # Swap turn.
        self.turn ^= 1

        return move
github niklasf / python-chess / chess / __init__.py View on Github external
def remove_piece_at(self, square):
        """
        Removes the piece from the given square. Returns the
        :class:`~chess.Piece` or ``None`` if the square was already empty.
        """
        color = bool(self.occupied_co[WHITE] & BB_SQUARES[square])
        piece_type = self._remove_piece_at(square)
        if piece_type:
            return Piece(piece_type, color)
github niklasf / python-chess / chess / __init__.py View on Github external
def piece_at(self, square):
        mask = BB_SQUARES[square]
        color = int(bool(self.occupied_co[BLACK] & mask))

        if mask & self.pawns:
            return Piece(PAWN, color)
        elif mask & self.knights:
            return Piece(KNIGHT, color)
        elif mask & self.bishops:
            return Piece(BISHOP, color)
        elif mask & self.rooks:
            return Piece(ROOK, color)
        elif mask & self.queens:
            return Piece(QUEEN, color)
        elif mask & self.kings:
            return Piece(KING, color)

chess

A chess library with move generation and validation, Polyglot opening book probing, PGN reading and writing, Gaviota tablebase probing, Syzygy tablebase probing, and XBoard/UCI engine communication.

GPL-3.0
Latest version published 1 month ago

Package Health Score

82 / 100
Full package analysis