Advertisement
max2201111

very good attacks_mask NOPar fixed AKakBb

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