Advertisement
max2201111

push a pop implementovany dost dobre ale s malo vystupy KB bez A

Jul 9th, 2024
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 13.73 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_type = AMAZON
  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_type = chess.BISHOP
  51.                 else:
  52.                     piece_type = chess.PIECE_SYMBOLS.index(c.lower())
  53.                 self._set_piece_at(square, piece_type, 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.     def _set_piece_at(self, square, piece_type, color, promoted=None): # Add promoted argument
  68.             super()._set_piece_at(square, piece_type, color, promoted)
  69.             if piece_type is not None:
  70.                 if piece_type == AMAZON:
  71.                     if color == chess.WHITE:
  72.                         self.amazons_white |= chess.BB_SQUARES[square]
  73.                     else:
  74.                         self.amazons_black |= chess.BB_SQUARES[square]
  75.                 elif piece_type == chess.BISHOP:
  76.                     if color == chess.WHITE:
  77.                         self.custom_bishops_white |= chess.BB_SQUARES[square]
  78.                     else:
  79.                         self.custom_bishops_black |= chess.BB_SQUARES[square]
  80.             else:
  81.                 self.amazons_white &= ~chess.BB_SQUARES[square]
  82.                 self.amazons_black &= ~chess.BB_SQUARES[square]
  83.                 self.custom_bishops_white &= ~chess.BB_SQUARES[square]
  84.                 self.custom_bishops_black &= ~chess.BB_SQUARES[square]
  85.  
  86.     def generate_pseudo_legal_moves(self, from_mask: Bitboard = BB_ALL, to_mask: Bitboard = BB_ALL) -> Iterator[Move]:
  87.         for move in super().generate_pseudo_legal_moves(from_mask, to_mask):
  88.             if self.piece_type_at(move.from_square) not in [AMAZON, chess.BISHOP]:
  89.                 yield move
  90.  
  91.         our_bishops = self.custom_bishops_white if self.turn == chess.WHITE else self.custom_bishops_black
  92.         our_bishops &= from_mask
  93.         for from_square in chess.scan_forward(our_bishops):
  94.             attacks = self.bishop_attacks(from_square)
  95.             valid_moves = attacks & ~self.occupied & to_mask
  96.             for to_square in chess.scan_forward(valid_moves):
  97.                 yield Move(from_square, to_square)
  98.  
  99.         our_amazons = self.amazons_white if self.turn == chess.WHITE else self.amazons_black
  100.         our_amazons &= from_mask
  101.         for from_square in chess.scan_forward(our_amazons):
  102.             attacks = self.amazon_attacks(from_square)
  103.             valid_moves = attacks & ~self.occupied & to_mask
  104.             for to_square in chess.scan_forward(valid_moves):
  105.                 yield Move(from_square, to_square)
  106.  
  107.     def amazon_attacks(self, square):
  108.         return self.queen_attacks(square) | self.knight_attacks(square)
  109.  
  110.     def queen_attacks(self, square):
  111.         return self.bishop_attacks(square) | self.rook_attacks(square)
  112.  
  113.     def bishop_attacks(self, square):
  114.         return chess.BB_DIAG_ATTACKS[square][self.occupied & chess.BB_DIAG_MASKS[square]]
  115.  
  116.     def rook_attacks(self, square):
  117.         return (chess.BB_RANK_ATTACKS[square][self.occupied & chess.BB_RANK_MASKS[square]] |
  118.                 chess.BB_FILE_ATTACKS[square][self.occupied & chess.BB_FILE_MASKS[square]])
  119.  
  120.     def knight_attacks(self, square):
  121.         return chess.BB_KNIGHT_ATTACKS[square]
  122.  
  123.     def is_pseudo_legal(self, move):
  124.         piece = self.piece_at(move.from_square)
  125.         if not piece or piece.color != self.turn:
  126.             return False
  127.        
  128.         if self.is_castling(move):
  129.             return True
  130.  
  131.         if piece.piece_type == AMAZON:
  132.             return bool(self.amazon_attacks(move.from_square) & chess.BB_SQUARES[move.to_square])
  133.         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])):
  134.             return bool(self.bishop_attacks(move.from_square) & chess.BB_SQUARES[move.to_square])
  135.         else:
  136.             return super().is_pseudo_legal(move)
  137.  
  138.     def attackers_mask(self, color: chess.Color, square: chess.Square) -> Bitboard:
  139.         attackers = super().attackers_mask(color, square)
  140.         our_amazons = self.amazons_white if color == chess.WHITE else self.amazons_black
  141.         for amazon_square in chess.scan_forward(our_amazons):
  142.             if self.amazon_attacks(amazon_square) & chess.BB_SQUARES[square]:
  143.                 attackers |= chess.BB_SQUARES[amazon_square]
  144.         return attackers
  145.  
  146.     def is_check(self):
  147.         return bool(self.attackers_mask(not self.turn, self.king(self.turn)))
  148.  
  149.     def is_into_check(self, move):
  150.         self.push(move)
  151.         is_check = self.is_check()
  152.         self.pop()
  153.         return is_check
  154.  
  155.     def is_legal(self, move):
  156.         return self.is_pseudo_legal(move) and not self.is_into_check(move)
  157.  
  158.     def piece_symbol(self, piece):
  159.         if piece is None:
  160.             return '.'
  161.         if piece.piece_type == AMAZON:
  162.             return 'A' if piece.color == chess.WHITE else 'a'
  163.         return piece.symbol()
  164.  
  165.     def piece_type_at(self, square):
  166.         if (self.amazons_white | self.amazons_black) & chess.BB_SQUARES[square]:
  167.             return AMAZON
  168.         return super().piece_type_at(square)
  169.  
  170.     def color_at(self, square):
  171.         if self.amazons_white & chess.BB_SQUARES[square]:
  172.             return chess.WHITE
  173.         if self.amazons_black & chess.BB_SQUARES[square]:
  174.             return chess.BLACK
  175.         return super().color_at(square)
  176.  
  177.     def push(self, move):
  178.         piece = self.piece_at(move.from_square)
  179.         captured_piece = self.piece_at(move.to_square)
  180.        
  181.         super().push(move)
  182.        
  183.         if piece.piece_type == AMAZON:
  184.             if piece.color == chess.WHITE:
  185.                 self.amazons_white &= ~chess.BB_SQUARES[move.from_square]
  186.                 self.amazons_white |= chess.BB_SQUARES[move.to_square]
  187.             else:
  188.                 self.amazons_black &= ~chess.BB_SQUARES[move.from_square]
  189.                 self.amazons_black |= chess.BB_SQUARES[move.to_square]
  190.         elif piece.piece_type == chess.BISHOP:
  191.             if piece.color == chess.WHITE:
  192.                 self.custom_bishops_white &= ~chess.BB_SQUARES[move.from_square]
  193.                 self.custom_bishops_white |= chess.BB_SQUARES[move.to_square]
  194.             else:
  195.                 self.custom_bishops_black &= ~chess.BB_SQUARES[move.from_square]
  196.                 self.custom_bishops_black |= chess.BB_SQUARES[move.to_square]
  197.        
  198.         if captured_piece:
  199.             if captured_piece.piece_type == AMAZON:
  200.                 if captured_piece.color == chess.WHITE:
  201.                     self.amazons_white &= ~chess.BB_SQUARES[move.to_square]
  202.                 else:
  203.                     self.amazons_black &= ~chess.BB_SQUARES[move.to_square]
  204.             elif captured_piece.piece_type == chess.BISHOP:
  205.                 if captured_piece.color == chess.WHITE:
  206.                     self.custom_bishops_white &= ~chess.BB_SQUARES[move.to_square]
  207.                 else:
  208.                     self.custom_bishops_black &= ~chess.BB_SQUARES[move.to_square]
  209.  
  210.  
  211.     def pop(self):
  212.             move = self.move_stack[-1]
  213.             piece = self.piece_at(move.to_square)
  214.            
  215.             last_move = super().pop()
  216.            
  217.             if piece.piece_type == AMAZON:
  218.                 if piece.color == chess.WHITE:
  219.                     self.amazons_white &= ~chess.BB_SQUARES[move.to_square]
  220.                     self.amazons_white |= chess.BB_SQUARES[move.from_square]
  221.                 else:
  222.                     self.amazons_black &= ~chess.BB_SQUARES[move.to_square]
  223.                     self.amazons_black |= chess.BB_SQUARES[move.from_square]
  224.             elif piece.piece_type == chess.BISHOP:
  225.                 if piece.color == chess.WHITE:
  226.                     self.custom_bishops_white &= ~chess.BB_SQUARES[move.to_square]
  227.                     self.custom_bishops_white |= chess.BB_SQUARES[move.from_square]
  228.                 else:
  229.                     self.custom_bishops_black &= ~chess.BB_SQUARES[move.to_square]
  230.                     self.custom_bishops_black |= chess.BB_SQUARES[move.from_square]
  231.            
  232.             # Check if a piece was captured using the piece_at method
  233.             if self.piece_at(move.to_square) is None and move.from_square != move.to_square:
  234.                 captured_square = move.to_square
  235.                 captured_color = not piece.color
  236.                 if self.piece_type_at(captured_square) == AMAZON: # Determine the captured piece type
  237.                     if captured_color == chess.WHITE:
  238.                         self.amazons_white |= chess.BB_SQUARES[captured_square]
  239.                     else:
  240.                         self.amazons_black |= chess.BB_SQUARES[captured_square]
  241.                 elif self.piece_type_at(captured_square) == chess.BISHOP:
  242.                     if captured_color == chess.WHITE:
  243.                         self.custom_bishops_white |= chess.BB_SQUARES[captured_square]
  244.                     else:
  245.                         self.custom_bishops_black |= chess.BB_SQUARES[captured_square]
  246.            
  247.             return last_move
  248.  
  249. # ... (rest of your code remains the same)
  250.  
  251.     def debug_amazons(self):
  252.         print(f"Bitboard bílých amazonek: {format(self.amazons_white, '064b')}")
  253.         print(f"Bitboard černých amazonek: {format(self.amazons_black, '064b')}")
  254.         for square in chess.SQUARES:
  255.             if self.amazons_white & chess.BB_SQUARES[square]:
  256.                 print(f"Bílá amazonka na {chess.SQUARE_NAMES[square]}")
  257.             if self.amazons_black & chess.BB_SQUARES[square]:
  258.                 print(f"Černá amazonka na {chess.SQUARE_NAMES[square]}")
  259.  
  260.     def debug_custom_bishops(self):
  261.         print(f"Bitboard bílých vlastních střelců: {format(self.custom_bishops_white, '064b')}")
  262.         print(f"Bitboard černých vlastních střelců: {format(self.custom_bishops_black, '064b')}")
  263.         for square in chess.SQUARES:
  264.             if self.custom_bishops_white & chess.BB_SQUARES[square]:
  265.                 print(f"Bílý vlastní střelec na {chess.SQUARE_NAMES[square]}")
  266.             if self.custom_bishops_black & chess.BB_SQUARES[square]:
  267.                 print(f"Černý vlastní střelec na {chess.SQUARE_NAMES[square]}")
  268.  
  269.     @property
  270.     def legal_moves(self):
  271.         return [move for move in self.generate_pseudo_legal_moves() if self.is_legal(move)]
  272.  
  273.     def __str__(self):
  274.         builder = []
  275.         for square in chess.SQUARES_180:
  276.             piece = self.piece_at(square)
  277.             if (self.amazons_white & chess.BB_SQUARES[square]):
  278.                 symbol = 'A'
  279.             elif (self.amazons_black & chess.BB_SQUARES[square]):
  280.                 symbol = 'a'
  281.             else:
  282.                 symbol = self.piece_symbol(piece) if piece else '.'
  283.             builder.append(symbol)
  284.             if chess.square_file(square) == 7:
  285.                 if square != chess.H1:
  286.                     builder.append('\n')
  287.         return ''.join(builder)
  288.  
  289. if __name__ == "__main__":
  290.     start_fen = "1a6/3k4/8/8/1B6/8/A7/6K1 w - - 0 1"
  291.    
  292.     print(f"Vytváření šachovnice s FEN: {start_fen}")
  293.     board = CustomBoard(start_fen)
  294.    
  295.     print("Současná pozice:")
  296.     print(board)
  297.    
  298.     print("Generování legálních tahů pro počáteční pozici...")
  299.     legal_moves = list(board.legal_moves)
  300.     print(f"Počet legálních tahů: {len(legal_moves)}")
  301.    
  302.     print("Legální tahy:")
  303.     for move in legal_moves:
  304.         from_square = chess.SQUARE_NAMES[move.from_square]
  305.         to_square = chess.SQUARE_NAMES[move.to_square]
  306.         piece = board.piece_at(move.from_square)
  307.         print(f"{board.piece_symbol(piece)}: {from_square}-{to_square}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement