Advertisement
max2201111

posledni amazonky clekem OK az na a namisto A

Jul 7th, 2024
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.91 KB | Science | 0 0
  1. import chess
  2. from typing import Iterator
  3. from chess import Move, BB_ALL, Bitboard, scan_reversed
  4.  
  5. # Definice nových figur
  6. AMAZON = 7
  7.  
  8. class CustomBoard(chess.Board):
  9.     def __init__(self, fen=None):
  10.         self.amazons = chess.BB_EMPTY
  11.         self.custom_bishops_white = chess.BB_EMPTY
  12.         self.custom_bishops_black = chess.BB_EMPTY
  13.         super().__init__(None)
  14.         if fen:
  15.             self.set_custom_fen(fen)
  16.         print("Šachovnice inicializována")
  17.         self.debug_amazons()
  18.         self.debug_custom_bishops()
  19.  
  20.     def set_custom_fen(self, fen):
  21.         parts = fen.split()
  22.         board_part = parts[0]
  23.        
  24.         self.clear()
  25.         self.amazons = chess.BB_EMPTY
  26.         self.custom_bishops_white = chess.BB_EMPTY
  27.         self.custom_bishops_black = chess.BB_EMPTY
  28.        
  29.         square = 56
  30.         for c in board_part:
  31.             if c == '/':
  32.                 square -= 16
  33.             elif c.isdigit():
  34.                 square += int(c)
  35.             else:
  36.                 color = chess.WHITE if c.isupper() else chess.BLACK
  37.                 if c.upper() == 'A':
  38.                     self.amazons |= chess.BB_SQUARES[square]
  39.                     piece = chess.Piece(AMAZON, color)
  40.                 elif c.upper() == 'B':
  41.                     if color == chess.WHITE:
  42.                         self.custom_bishops_white |= chess.BB_SQUARES[square]
  43.                     else:
  44.                         self.custom_bishops_black |= chess.BB_SQUARES[square]
  45.                     piece = chess.Piece(chess.BISHOP, color)
  46.                 else:
  47.                     piece = chess.Piece.from_symbol(c)
  48.                 self._set_piece_at(square, piece.piece_type, piece.color)
  49.                 square += 1
  50.  
  51.         self.turn = chess.WHITE if parts[1] == 'w' else chess.BLACK
  52.         self.castling_rights = chess.BB_EMPTY
  53.         if '-' not in parts[2]:
  54.             if 'K' in parts[2]: self.castling_rights |= chess.BB_H1
  55.             if 'Q' in parts[2]: self.castling_rights |= chess.BB_A1
  56.             if 'k' in parts[2]: self.castling_rights |= chess.BB_H8
  57.             if 'q' in parts[2]: self.castling_rights |= chess.BB_A8
  58.         self.ep_square = chess.parse_square(parts[3]) if parts[3] != '-' else None
  59.         self.halfmove_clock = int(parts[4])
  60.         self.fullmove_number = int(parts[5])
  61.  
  62.         print(f"Po nastavení FEN, bitboard amazonek: {self.amazons:064b}")
  63.         print(f"Po nastavení FEN, bitboard bílých vlastních střelců: {self.custom_bishops_white:064b}")
  64.         print(f"Po nastavení FEN, bitboard černých vlastních střelců: {self.custom_bishops_black:064b}")
  65.  
  66.     def _set_piece_at(self, square, piece_type, color):
  67.         super()._set_piece_at(square, piece_type, color)
  68.         if piece_type is not None:
  69.             if piece_type == AMAZON:
  70.                 self.amazons |= chess.BB_SQUARES[square]
  71.                 print(f"Nastavena amazonka na {chess.SQUARE_NAMES[square]}")
  72.             elif piece_type == chess.BISHOP:
  73.                 if color == chess.WHITE:
  74.                     self.custom_bishops_white |= chess.BB_SQUARES[square]
  75.                 else:
  76.                     self.custom_bishops_black |= chess.BB_SQUARES[square]
  77.         else:
  78.             self.amazons &= ~chess.BB_SQUARES[square]
  79.             self.custom_bishops_white &= ~chess.BB_SQUARES[square]
  80.             self.custom_bishops_black &= ~chess.BB_SQUARES[square]
  81.  
  82.     def generate_pseudo_legal_moves(self, from_mask: Bitboard = BB_ALL, to_mask: Bitboard = BB_ALL) -> Iterator[Move]:
  83.         print("Generování pseudo-legálních tahů...")
  84.        
  85.         # Generování standardních tahů
  86.         for move in super().generate_pseudo_legal_moves(from_mask, to_mask):
  87.             if self.piece_type_at(move.from_square) not in [AMAZON, chess.BISHOP]:
  88.                 print(f"Standardní pseudo-legální tah: {move}")
  89.                 yield move
  90.  
  91.         # Generování tahů vlastních střelců
  92.         our_bishops = self.custom_bishops_white if self.turn == chess.WHITE else self.custom_bishops_black
  93.         our_bishops &= from_mask
  94.         print(f"Naši střelci: {our_bishops:064b}")
  95.         for from_square in chess.scan_forward(our_bishops):
  96.             print(f"Generování tahů pro střelce na {chess.SQUARE_NAMES[from_square]}")
  97.             attacks = self.bishop_attacks(from_square)
  98.             print(f"Útoky střelce: {attacks:064b}")
  99.             valid_moves = attacks & ~self.occupied & to_mask
  100.             print(f"Platné cílové pole pro střelce: {valid_moves:064b}")
  101.             for to_square in chess.scan_forward(valid_moves):
  102.                 move = Move(from_square, to_square)
  103.                 print(f"Pseudo-legální tah vlastního střelce: {move}")
  104.                 yield move
  105.  
  106.         # Generování tahů amazonek
  107.         our_amazons = self.amazons & self.occupied_co[self.turn] & from_mask
  108.         print(f"Naše amazonky: {our_amazons:064b}")
  109.         for from_square in chess.scan_forward(our_amazons):
  110.             print(f"Generování tahů pro amazonku na {chess.SQUARE_NAMES[from_square]}")
  111.             attacks = self.amazon_attacks(from_square)
  112.             print(f"Útoky amazonky: {attacks:064b}")
  113.             valid_moves = attacks & ~self.occupied & to_mask
  114.             print(f"Platné cílové pole pro amazonku: {valid_moves:064b}")
  115.             for to_square in chess.scan_forward(valid_moves):
  116.                 move = Move(from_square, to_square)
  117.                 print(f"Pseudo-legální tah amazonky: {move}")
  118.                 yield move
  119.  
  120.     def amazon_attacks(self, square):
  121.         return self.queen_attacks(square) | self.knight_attacks(square)
  122.  
  123.     def queen_attacks(self, square):
  124.         return self.bishop_attacks(square) | self.rook_attacks(square)
  125.  
  126.     def bishop_attacks(self, square):
  127.         return chess.BB_DIAG_ATTACKS[square][self.occupied & chess.BB_DIAG_MASKS[square]]
  128.  
  129.     def rook_attacks(self, square):
  130.         return (chess.BB_RANK_ATTACKS[square][self.occupied & chess.BB_RANK_MASKS[square]] |
  131.                 chess.BB_FILE_ATTACKS[square][self.occupied & chess.BB_FILE_MASKS[square]])
  132.  
  133.     def knight_attacks(self, square):
  134.         return chess.BB_KNIGHT_ATTACKS[square]
  135.  
  136.     def is_pseudo_legal(self, move):
  137.         piece = self.piece_at(move.from_square)
  138.         if not piece or piece.color != self.turn:
  139.             return False
  140.        
  141.         if self.is_castling(move):
  142.             return True
  143.  
  144.         if piece.piece_type == AMAZON:
  145.             return bool(self.amazon_attacks(move.from_square) & chess.BB_SQUARES[move.to_square])
  146.         elif piece.piece_type == chess.BISHOP and (self.custom_bishops_white & chess.BB_SQUARES[move.from_square] or self.custom_bishops_black & chess.BB_SQUARES[move.from_square]):
  147.             return bool(self.bishop_attacks(move.from_square) & chess.BB_SQUARES[move.to_square])
  148.         else:
  149.             return super().is_pseudo_legal(move)
  150.  
  151.     def is_legal(self, move):
  152.         print(f"Kontrola legality tahu: {move}")
  153.         if not self.is_pseudo_legal(move):
  154.             print(f"Tah {move} není pseudo-legální")
  155.             return False
  156.         if self.is_into_check(move):
  157.             print(f"Tah {move} staví vlastního krále do šachu")
  158.             return False
  159.         print(f"Tah {move} je legální")
  160.         return True
  161.  
  162.     def piece_symbol(self, piece):
  163.         if piece is None:
  164.             return '.'
  165.         if piece.piece_type == AMAZON:
  166.             return 'A' if piece.color == chess.WHITE else 'a'
  167.         return piece.symbol()
  168.  
  169.     def piece_type_at(self, square):
  170.         if self.amazons & chess.BB_SQUARES[square]:
  171.             return AMAZON
  172.         return super().piece_type_at(square)
  173.  
  174.     def debug_amazons(self):
  175.         print(f"Bitboard amazonek: {format(self.amazons, '064b')}")
  176.         for square in chess.SQUARES:
  177.             if self.amazons & chess.BB_SQUARES[square]:
  178.                 print(f"Amazonka na {chess.SQUARE_NAMES[square]}")
  179.  
  180.     def debug_custom_bishops(self):
  181.         print(f"Bitboard bílých vlastních střelců: {format(self.custom_bishops_white, '064b')}")
  182.         print(f"Bitboard černých vlastních střelců: {format(self.custom_bishops_black, '064b')}")
  183.         for square in chess.SQUARES:
  184.             if self.custom_bishops_white & chess.BB_SQUARES[square]:
  185.                 print(f"Bílý vlastní střelec na {chess.SQUARE_NAMES[square]}")
  186.             if self.custom_bishops_black & chess.BB_SQUARES[square]:
  187.                 print(f"Černý vlastní střelec na {chess.SQUARE_NAMES[square]}")
  188.  
  189.     @property
  190.     def legal_moves(self):
  191.         legal_moves = [move for move in self.generate_pseudo_legal_moves() if self.is_legal(move)]
  192.         for move in legal_moves:
  193.             print(f"Legální tah: {move}")
  194.         return legal_moves
  195.  
  196.     def __str__(self):
  197.         builder = []
  198.         for square in chess.SQUARES_180:
  199.             piece = self.piece_at(square)
  200.             symbol = self.piece_symbol(piece) if piece else '.'
  201.             builder.append(symbol)
  202.             if chess.square_file(square) == 7:
  203.                 if square != chess.H1:
  204.                     builder.append('\n')
  205.         return ''.join(builder)
  206.  
  207. if __name__ == "__main__":
  208.     start_fen = "1A6/3k4/8/8/1B6/8/A7/6K1 w - - 0 1"
  209.    
  210.     print(f"Vytváření šachovnice s FEN: {start_fen}")
  211.     board = CustomBoard(start_fen)
  212.    
  213.     print("Ladění amazonek...")
  214.     board.debug_amazons()
  215.     board.debug_custom_bishops()
  216.    
  217.     print("Současná pozice:")
  218.     print(board)
  219.    
  220.     print("Generování legálních tahů pro počáteční pozici...")
  221.     legal_moves = list(board.legal_moves)
  222.     print(f"Počet legálních tahů: {len(legal_moves)}")
  223.    
  224.     print("Legální tahy:")
  225.     for move in legal_moves:
  226.         from_square = chess.SQUARE_NAMES[move.from_square]
  227.         to_square = chess.SQUARE_NAMES[move.to_square]
  228.         piece = board.piece_at(move.from_square)
  229.         print(f"{board.piece_symbol(piece)}: {from_square}-{to_square}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement