Advertisement
max2201111

opraveny kod dost dobre AKkBb BEZ KONTROLY BARVY v pseudo_legal!

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