Advertisement
max2201111

no A printed

Jun 29th, 2024
601
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.74 KB | Science | 0 0
  1. import chess
  2.  
  3. # Definujte nový typ figurky Amazon
  4. AMAZON = 7
  5.  
  6. # Rozšiřte existující typy figur o Amazon
  7. PIECE_TYPES = [chess.PAWN, chess.KNIGHT, chess.BISHOP, chess.ROOK, chess.QUEEN, chess.KING, AMAZON]
  8. PIECE_SYMBOLS = [None, "p", "n", "b", "r", "q", "k", "a"]
  9.  
  10. class CustomPiece(chess.Piece):
  11.     def __init__(self, piece_type, color):
  12.         super().__init__(piece_type, color)
  13.  
  14. class Amazon(CustomPiece):
  15.     def __init__(self, color):
  16.         super().__init__(AMAZON, color)
  17.  
  18.     def symbol(self):
  19.         return 'A' if self.color == chess.WHITE else 'a'
  20.  
  21. class CustomBoard(chess.Board):
  22.     def __init__(self, fen=None):
  23.         self.amazons = chess.BB_EMPTY
  24.         self.custom_pieces = {
  25.             'A': Amazon(chess.WHITE), 'a': Amazon(chess.BLACK)
  26.         }
  27.         super().__init__(fen)
  28.  
  29.     def set_piece_at(self, square, piece, promoted=False):
  30.         super().set_piece_at(square, piece, promoted)
  31.         if piece.piece_type == AMAZON:
  32.             self.amazons |= chess.BB_SQUARES[square]
  33.  
  34.     def remove_piece_at(self, square):
  35.         piece = self.piece_at(square)
  36.         if piece and piece.piece_type == AMAZON:
  37.             self.amazons &= ~chess.BB_SQUARES[square]
  38.         super().remove_piece_at(square)
  39.  
  40.     def set_fen(self, fen):
  41.         parts = fen.split(' ')
  42.         while len(parts) < 6:
  43.             parts.append("0")
  44.         board_part = parts[0]
  45.         turn_part = parts[1]
  46.         castling_part = parts[2]
  47.         en_passant_part = parts[3]
  48.         halfmove_clock = parts[4]
  49.         fullmove_number = parts[5]
  50.  
  51.         self.set_board_fen(board_part)
  52.         self.turn = chess.WHITE if turn_part == 'w' else chess.BLACK
  53.         self.castling_rights = chess.BB_EMPTY if castling_part == '-' else chess.SquareSet.from_square(chess.parse_square(castling_part))
  54.         self.ep_square = None if en_passant_part == '-' else chess.parse_square(en_passant_part)
  55.         self.halfmove_clock = int(halfmove_clock)
  56.         self.fullmove_number = int(fullmove_number)
  57.  
  58.     def set_board_fen(self, fen):
  59.         self.clear()
  60.         self.amazons = chess.BB_EMPTY  # Clear amazons bitboard
  61.         rows = fen.split('/')
  62.         for rank, row in enumerate(rows):
  63.             file = 0
  64.             for char in row:
  65.                 if char.isdigit():
  66.                     file += int(char)
  67.                 else:
  68.                     square = chess.square(file, 7 - rank)
  69.                     if char in self.custom_pieces:
  70.                         self.set_piece_at(square, self.custom_pieces[char])
  71.                     else:
  72.                         self.set_piece_at(square, chess.Piece.from_symbol(char))
  73.                     file += 1
  74.  
  75.     def __str__(self):
  76.         builder = []
  77.         for rank in range(7, -1, -1):
  78.             empty = 0
  79.             for file in range(8):
  80.                 piece = self.piece_at(chess.square(file, rank))
  81.                 if piece:
  82.                     if empty > 0:
  83.                         builder.append(str(empty))
  84.                         empty = 0
  85.                     builder.append(piece.symbol())
  86.                 else:
  87.                     empty += 1
  88.             if empty > 0:
  89.                 builder.append(str(empty))
  90.             if rank > 0:
  91.                 builder.append("/")
  92.         return "".join(builder)
  93.  
  94. def print_custom_board(fen):
  95.     board = CustomBoard()
  96.     board.set_fen(fen)
  97.     for rank in range(7, -1, -1):
  98.         line = ""
  99.         for file in range(8):
  100.             piece = board.piece_at(chess.square(file, rank))
  101.             if piece:
  102.                 line += piece.symbol() + " "
  103.             else:
  104.                 line += ". "
  105.         print(line)
  106.  
  107. # Testovací pozice
  108. start_fen = "8/6A1/8/8/8/k1K5/8/8 w - - 0 1"
  109. print_custom_board(start_fen)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement