Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import chess
- # Definujte nový typ figurky Amazon
- AMAZON = 7
- # Rozšiřte existující typy figur o Amazon
- PIECE_TYPES = [chess.PAWN, chess.KNIGHT, chess.BISHOP, chess.ROOK, chess.QUEEN, chess.KING, AMAZON]
- PIECE_SYMBOLS = [None, "p", "n", "b", "r", "q", "k", "a"]
- class CustomPiece(chess.Piece):
- def __init__(self, piece_type, color):
- super().__init__(piece_type, color)
- class Amazon(CustomPiece):
- def __init__(self, color):
- super().__init__(AMAZON, color)
- def symbol(self):
- return 'A' if self.color == chess.WHITE else 'a'
- class CustomBoard(chess.Board):
- def __init__(self, fen=None):
- self.amazons = chess.BB_EMPTY
- self.custom_pieces = {
- 'A': Amazon(chess.WHITE), 'a': Amazon(chess.BLACK)
- }
- super().__init__(fen)
- def set_piece_at(self, square, piece, promoted=False):
- super().set_piece_at(square, piece, promoted)
- if piece.piece_type == AMAZON:
- self.amazons |= chess.BB_SQUARES[square]
- def remove_piece_at(self, square):
- piece = self.piece_at(square)
- if piece and piece.piece_type == AMAZON:
- self.amazons &= ~chess.BB_SQUARES[square]
- super().remove_piece_at(square)
- def set_fen(self, fen):
- parts = fen.split(' ')
- while len(parts) < 6:
- parts.append("0")
- board_part = parts[0]
- turn_part = parts[1]
- castling_part = parts[2]
- en_passant_part = parts[3]
- halfmove_clock = parts[4]
- fullmove_number = parts[5]
- self.set_board_fen(board_part)
- self.turn = chess.WHITE if turn_part == 'w' else chess.BLACK
- self.castling_rights = chess.BB_EMPTY if castling_part == '-' else chess.SquareSet.from_square(chess.parse_square(castling_part))
- self.ep_square = None if en_passant_part == '-' else chess.parse_square(en_passant_part)
- self.halfmove_clock = int(halfmove_clock)
- self.fullmove_number = int(fullmove_number)
- def set_board_fen(self, fen):
- self.clear()
- self.amazons = chess.BB_EMPTY # Clear amazons bitboard
- rows = fen.split('/')
- for rank, row in enumerate(rows):
- file = 0
- for char in row:
- if char.isdigit():
- file += int(char)
- else:
- square = chess.square(file, 7 - rank)
- if char in self.custom_pieces:
- self.set_piece_at(square, self.custom_pieces[char])
- else:
- self.set_piece_at(square, chess.Piece.from_symbol(char))
- file += 1
- def __str__(self):
- builder = []
- for rank in range(7, -1, -1):
- empty = 0
- for file in range(8):
- piece = self.piece_at(chess.square(file, rank))
- if piece:
- if empty > 0:
- builder.append(str(empty))
- empty = 0
- builder.append(piece.symbol())
- else:
- empty += 1
- if empty > 0:
- builder.append(str(empty))
- if rank > 0:
- builder.append("/")
- return "".join(builder)
- def print_custom_board(fen):
- board = CustomBoard()
- board.set_fen(fen)
- for rank in range(7, -1, -1):
- line = ""
- for file in range(8):
- piece = board.piece_at(chess.square(file, rank))
- if piece:
- line += piece.symbol() + " "
- else:
- line += ". "
- print(line)
- # Testovací pozice
- start_fen = "8/6A1/8/8/8/k1K5/8/8 w - - 0 1"
- print_custom_board(start_fen)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement