Advertisement
max2201111

amazonky lepsi but not cool

Jul 9th, 2024
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 13.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_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):
  68.         super()._set_piece_at(square, piece_type, color)
  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_custom_pieces = (self.custom_bishops_white | self.amazons_white) if self.turn == chess.WHITE else (self.custom_bishops_black | self.amazons_black)
  92.         our_custom_pieces &= from_mask
  93.         for from_square in chess.scan_forward(our_custom_pieces):
  94.             piece_type = self.piece_type_at(from_square)
  95.             if piece_type == AMAZON:
  96.                 attacks = self.amazon_attacks(from_square)
  97.             else:
  98.                 attacks = self.bishop_attacks(from_square)
  99.            
  100.             valid_moves = attacks & ~self.occupied & to_mask
  101.             for to_square in chess.scan_forward(valid_moves):
  102.                 yield Move(from_square, to_square)
  103.  
  104.     def amazon_attacks(self, square):
  105.         return self.queen_attacks(square) | self.knight_attacks(square)
  106.  
  107.     def queen_attacks(self, square):
  108.         return self.bishop_attacks(square) | self.rook_attacks(square)
  109.  
  110.     def bishop_attacks(self, square):
  111.         return chess.BB_DIAG_ATTACKS[square][self.occupied & chess.BB_DIAG_MASKS[square]]
  112.  
  113.     def rook_attacks(self, square):
  114.         return (chess.BB_RANK_ATTACKS[square][self.occupied & chess.BB_RANK_MASKS[square]] |
  115.                 chess.BB_FILE_ATTACKS[square][self.occupied & chess.BB_FILE_MASKS[square]])
  116.  
  117.     def knight_attacks(self, square):
  118.         return chess.BB_KNIGHT_ATTACKS[square]
  119.  
  120.     def is_pseudo_legal(self, move):
  121.         piece = self.piece_at(move.from_square)
  122.         if not piece or piece.color != self.turn:
  123.             return False
  124.        
  125.         if self.is_castling(move):
  126.             return True
  127.  
  128.         if piece.piece_type == AMAZON:
  129.             return bool(self.amazon_attacks(move.from_square) & chess.BB_SQUARES[move.to_square])
  130.         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])):
  131.             return bool(self.bishop_attacks(move.from_square) & chess.BB_SQUARES[move.to_square])
  132.         else:
  133.             return super().is_pseudo_legal(move)
  134.  
  135.     def is_legal(self, move):
  136.         return self.is_pseudo_legal(move) and not self.is_into_check(move)
  137.  
  138.     def push(self, move):
  139.         if not self.is_pseudo_legal(move):
  140.             raise ValueError(f"Move {move} is not pseudo-legal in position {self.fen()}")
  141.        
  142.         piece = self.piece_at(move.from_square)
  143.         captured_piece = self.piece_at(move.to_square)
  144.        
  145.         # Odstranění figury z původní pozice
  146.         self._remove_piece_at(move.from_square)
  147.        
  148.         # Přesunutí figury na novou pozici
  149.         self._set_piece_at(move.to_square, piece.piece_type, piece.color)
  150.        
  151.         # Aktualizace bitboardů pro vlastní figury
  152.         if piece.piece_type == AMAZON:
  153.             if piece.color == chess.WHITE:
  154.                 self.amazons_white &= ~chess.BB_SQUARES[move.from_square]
  155.                 self.amazons_white |= chess.BB_SQUARES[move.to_square]
  156.             else:
  157.                 self.amazons_black &= ~chess.BB_SQUARES[move.from_square]
  158.                 self.amazons_black |= chess.BB_SQUARES[move.to_square]
  159.         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])):
  160.             if piece.color == chess.WHITE:
  161.                 self.custom_bishops_white &= ~chess.BB_SQUARES[move.from_square]
  162.                 self.custom_bishops_white |= chess.BB_SQUARES[move.to_square]
  163.             else:
  164.                 self.custom_bishops_black &= ~chess.BB_SQUARES[move.from_square]
  165.                 self.custom_bishops_black |= chess.BB_SQUARES[move.to_square]
  166.        
  167.         # Aktualizace turn, fullmove_number a halfmove_clock
  168.         self.turn = not self.turn
  169.         self.fullmove_number += 1 if self.turn == chess.WHITE else 0
  170.         self.halfmove_clock += 1
  171.        
  172.         if captured_piece or piece.piece_type == chess.PAWN:
  173.             self.halfmove_clock = 0
  174.        
  175.         # Uložení tahu do historie
  176.         self.move_stack.append(move)
  177.  
  178.     def pop(self):
  179.         if not self.move_stack:
  180.             return None
  181.        
  182.         move = self.move_stack.pop()
  183.        
  184.         # Vrácení figury na původní pozici
  185.         piece = self.piece_at(move.to_square)
  186.         self._remove_piece_at(move.to_square)
  187.         self._set_piece_at(move.from_square, piece.piece_type, piece.color)
  188.        
  189.         # Aktualizace bitboardů pro vlastní figury
  190.         if piece.piece_type == AMAZON:
  191.             if piece.color == chess.WHITE:
  192.                 self.amazons_white &= ~chess.BB_SQUARES[move.to_square]
  193.                 self.amazons_white |= chess.BB_SQUARES[move.from_square]
  194.             else:
  195.                 self.amazons_black &= ~chess.BB_SQUARES[move.to_square]
  196.                 self.amazons_black |= chess.BB_SQUARES[move.from_square]
  197.         elif piece.piece_type == chess.BISHOP and ((self.custom_bishops_white & chess.BB_SQUARES[move.to_square]) or (self.custom_bishops_black & chess.BB_SQUARES[move.to_square])):
  198.             if piece.color == chess.WHITE:
  199.                 self.custom_bishops_white &= ~chess.BB_SQUARES[move.to_square]
  200.                 self.custom_bishops_white |= chess.BB_SQUARES[move.from_square]
  201.             else:
  202.                 self.custom_bishops_black &= ~chess.BB_SQUARES[move.to_square]
  203.                 self.custom_bishops_black |= chess.BB_SQUARES[move.from_square]
  204.        
  205.         # Obnovení zabrané figury, pokud existovala
  206.         if move.drop:
  207.             self._set_piece_at(move.to_square, move.drop, not piece.color)
  208.        
  209.         # Aktualizace turn, fullmove_number a halfmove_clock
  210.         self.turn = not self.turn
  211.         self.fullmove_number -= 1 if self.turn == chess.BLACK else 0
  212.        
  213.         return move
  214.  
  215.     def is_check(self):
  216.         king_square = self.king(self.turn)
  217.         if king_square is None:
  218.             return False
  219.  
  220.         if super().is_check():
  221.             return True
  222.  
  223.         opponent_amazons = self.amazons_black if self.turn == chess.WHITE else self.amazons_white
  224.         for amazon_square in chess.scan_forward(opponent_amazons):
  225.             if self.amazon_attacks(amazon_square) & chess.BB_SQUARES[king_square]:
  226.                 return True
  227.  
  228.         return False
  229.  
  230.     def is_into_check(self, move):
  231.         self.push(move)
  232.         is_check = self.is_check()
  233.         self.pop()
  234.         return is_check
  235.  
  236.     def debug_amazons(self):
  237.         print(f"Bitboard bílých amazonek: {format(self.amazons_white, '064b')}")
  238.         print(f"Bitboard černých amazonek: {format(self.amazons_black, '064b')}")
  239.         for square in chess.SQUARES:
  240.             if self.amazons_white & chess.BB_SQUARES[square]:
  241.                 print(f"Bílá amazonka na {chess.SQUARE_NAMES[square]}")
  242.             if self.amazons_black & chess.BB_SQUARES[square]:
  243.                 print(f"Černá amazonka na {chess.SQUARE_NAMES[square]}")
  244.  
  245.     def debug_custom_bishops(self):
  246.         print(f"Bitboard bílých vlastních střelců: {format(self.custom_bishops_white, '064b')}")
  247.         print(f"Bitboard černých vlastních střelců: {format(self.custom_bishops_black, '064b')}")
  248.         for square in chess.SQUARES:
  249.             if self.custom_bishops_white & chess.BB_SQUARES[square]:
  250.                 print(f"Bílý vlastní střelec na {chess.SQUARE_NAMES[square]}")
  251.             if self.custom_bishops_black & chess.BB_SQUARES[square]:
  252.                 print(f"Černý vlastní střelec na {chess.SQUARE_NAMES[square]}")
  253.  
  254.  
  255.     def piece_symbol(self, piece):
  256.         if piece is None:
  257.             return '.'
  258.         if piece.piece_type == AMAZON:
  259.             return 'A' if piece.color == chess.WHITE else 'a'
  260.         return piece.symbol()
  261.  
  262.     def piece_type_at(self, square):
  263.         if (self.amazons_white | self.amazons_black) & chess.BB_SQUARES[square]:
  264.             return AMAZON
  265.         return super().piece_type_at(square)
  266.  
  267.     def color_at(self, square):
  268.         if self.amazons_white & chess.BB_SQUARES[square]:
  269.             return chess.WHITE
  270.         if self.amazons_black & chess.BB_SQUARES[square]:
  271.             return chess.BLACK
  272.         return super().color_at(square)
  273.  
  274.     @property
  275.     def legal_moves(self):
  276.         return [move for move in self.generate_pseudo_legal_moves() if self.is_legal(move)]
  277.  
  278.     def __str__(self):
  279.         builder = []
  280.         for square in chess.SQUARES_180:
  281.             piece = self.piece_at(square)
  282.             if (self.amazons_white & chess.BB_SQUARES[square]):
  283.                 symbol = 'A'
  284.             elif (self.amazons_black & chess.BB_SQUARES[square]):
  285.                 symbol = 'a'
  286.             else:
  287.                 symbol = self.piece_symbol(piece) if piece else '.'
  288.             builder.append(symbol)
  289.             if chess.square_file(square) == 7:
  290.                 if square != chess.H1:
  291.                     builder.append('\n')
  292.         return ''.join(builder)
  293.  
  294. if __name__ == "__main__":
  295.     start_fen = "a7/3k4/8/8/1B6/8/A7/6K1 w - - 0 1"
  296.    
  297.     print(f"Vytváření šachovnice s FEN: {start_fen}")
  298.     board = CustomBoard(start_fen)
  299.    
  300.     print("Současná pozice:")
  301.     print(board)
  302.    
  303.     print("Generování legálních tahů pro počáteční pozici...")
  304.     legal_moves = list(board.legal_moves)
  305.     print(f"Počet legálních tahů: {len(legal_moves)}")
  306.    
  307.     print("Legální tahy:")
  308.     for move in legal_moves:
  309.         from_square = chess.SQUARE_NAMES[move.from_square]
  310.         to_square = chess.SQUARE_NAMES[move.to_square]
  311.         piece = board.piece_at(move.from_square)
  312.         print(f"{board.piece_symbol(piece)}: {from_square}-{to_square}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement