Advertisement
max2201111

the best amazon

Jul 7th, 2024
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 11.33 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.         piece.color = self.turn
  150.         if not piece or piece.color != self.turn:
  151.             return False
  152.        
  153.         if self.is_castling(move):
  154.             return True
  155.  
  156.         if piece.piece_type == AMAZON:
  157.             return bool(self.amazon_attacks(move.from_square) & chess.BB_SQUARES[move.to_square])
  158.         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])):
  159.             return bool(self.bishop_attacks(move.from_square) & chess.BB_SQUARES[move.to_square])
  160.         else:
  161.             return super().is_pseudo_legal(move)
  162.  
  163.     def is_legal(self, move):
  164.         print(f"Kontrola legality tahu: {move}")
  165.         if not self.is_pseudo_legal(move):
  166.             print(f"Tah {move} není pseudo-legální")
  167.             return False
  168.         if self.is_into_check(move):
  169.             print(f"Tah {move} staví vlastního krále do šachu")
  170.             return False
  171.         print(f"Tah {move} je legální")
  172.         return True
  173.  
  174.     def piece_symbol(self, piece):
  175.         if piece is None:
  176.             return '.'
  177.         if piece.piece_type == AMAZON:
  178.             return 'A' if piece.color == chess.WHITE else 'a'
  179.         return piece.symbol()
  180.  
  181.     def piece_type_at(self, square):
  182.         if (self.amazons_white | self.amazons_black) & chess.BB_SQUARES[square]:
  183.             return AMAZON
  184.         return super().piece_type_at(square)
  185.  
  186.     def piece_color_at(self, square):
  187.         if self.amazons_white & chess.BB_SQUARES[square]:
  188.             return chess.WHITE
  189.         if self.amazons_black & chess.BB_SQUARES[square]:
  190.             return chess.BLACK
  191.         return super().color_at(square)
  192.  
  193.     def debug_amazons(self):
  194.         print(f"Bitboard bílých amazonek: {format(self.amazons_white, '064b')}")
  195.         print(f"Bitboard černých amazonek: {format(self.amazons_black, '064b')}")
  196.         for square in chess.SQUARES:
  197.             if self.amazons_white & chess.BB_SQUARES[square]:
  198.                 print(f"Bílá amazonka na {chess.SQUARE_NAMES[square]}")
  199.             if self.amazons_black & chess.BB_SQUARES[square]:
  200.                 print(f"Černá amazonka na {chess.SQUARE_NAMES[square]}")
  201.  
  202.     def debug_custom_bishops(self):
  203.         print(f"Bitboard bílých vlastních střelců: {format(self.custom_bishops_white, '064b')}")
  204.         print(f"Bitboard černých vlastních střelců: {format(self.custom_bishops_black, '064b')}")
  205.         for square in chess.SQUARES:
  206.             if self.custom_bishops_white & chess.BB_SQUARES[square]:
  207.                 print(f"Bílý vlastní střelec na {chess.SQUARE_NAMES[square]}")
  208.             if self.custom_bishops_black & chess.BB_SQUARES[square]:
  209.                 print(f"Černý vlastní střelec na {chess.SQUARE_NAMES[square]}")
  210.  
  211.     @property
  212.     def legal_moves(self):
  213.         legal_moves = [move for move in self.generate_pseudo_legal_moves() if self.is_legal(move)]
  214.         for move in legal_moves:
  215.             print(f"Legální tah: {move}")
  216.         return legal_moves
  217.  
  218.     def __str__(self):
  219.         builder = []
  220.         for square in chess.SQUARES_180:
  221.             piece = self.piece_at(square)
  222.             if (self.amazons_white & chess.BB_SQUARES[square]):
  223.                 symbol = 'A'
  224.             elif (self.amazons_black & chess.BB_SQUARES[square]):
  225.                 symbol = 'a'
  226.             else:
  227.                 symbol = self.piece_symbol(piece) if piece else '.'
  228.             builder.append(symbol)
  229.             if chess.square_file(square) == 7:
  230.                 if square != chess.H1:
  231.                     builder.append('\n')
  232.         return ''.join(builder)
  233.  
  234. if __name__ == "__main__":
  235.     start_fen = "1a6/3k4/8/8/1B6/8/A7/6K1 w - - 0 1"
  236.    
  237.     print(f"Vytváření šachovnice s FEN: {start_fen}")
  238.     board = CustomBoard(start_fen)
  239.    
  240.     print("Ladění amazonek...")
  241.     board.debug_amazons()
  242.     board.debug_custom_bishops()
  243.    
  244.     print("Současná pozice:")
  245.     print(board)
  246.    
  247.     print("Generování legálních tahů pro počáteční pozici...")
  248.     legal_moves = list(board.legal_moves)
  249.     print(f"Počet legálních tahů: {len(legal_moves)}")
  250.    
  251.     print("Legální tahy:")
  252.     for move in legal_moves:
  253.         from_square = chess.SQUARE_NAMES[move.from_square]
  254.         to_square = chess.SQUARE_NAMES[move.to_square]
  255.         piece = board.piece_at(move.from_square)
  256.         print(f"{board.piece_symbol(piece)}: {from_square}-{to_square}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement