Advertisement
max2201111

snad OK pro P

Jul 27th, 2024
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 26.74 KB | Science | 0 0
  1. import chess
  2. from typing import Iterator, Optional, Dict, Tuple
  3. from chess import Move, BB_ALL, Bitboard, PieceType, Color
  4. import time
  5. from collections import deque
  6. import threading
  7.  
  8. # Definice nových figur
  9. AMAZON = 7
  10. CYRIL = 8
  11. EVE = 9
  12.  
  13. # Rozšíření seznamu PIECE_SYMBOLS
  14. chess.PIECE_SYMBOLS.append('a')
  15. chess.PIECE_SYMBOLS.append('c')
  16. chess.PIECE_SYMBOLS.append('e')
  17.  
  18. class CustomBoard(chess.Board):
  19.     def __init__(self, fen=None):
  20.         self.amazons_white = chess.BB_EMPTY
  21.         self.amazons_black = chess.BB_EMPTY
  22.         self.cyrils_white = chess.BB_EMPTY
  23.         self.cyrils_black = chess.BB_EMPTY
  24.         self.eves_white = chess.BB_EMPTY
  25.         self.eves_black = chess.BB_EMPTY
  26.         super().__init__(None)
  27.         if fen:
  28.             self.set_custom_fen(fen)
  29.         self.debug_amazons()
  30.         self.debug_cyrils()
  31.         self.debug_eves()
  32.  
  33.     def clear_square(self, square):
  34.         super()._remove_piece_at(square)
  35.         self.amazons_white &= ~chess.BB_SQUARES[square]
  36.         self.amazons_black &= ~chess.BB_SQUARES[square]
  37.         self.cyrils_white &= ~chess.BB_SQUARES[square]
  38.         self.cyrils_black &= ~chess.BB_SQUARES[square]
  39.         self.eves_white &= ~chess.BB_SQUARES[square]
  40.         self.eves_black &= ~chess.BB_SQUARES[square]
  41.  
  42.     def set_custom_fen(self, fen):
  43.         parts = fen.split()
  44.         board_part = parts[0]
  45.  
  46.         self.clear()
  47.         self.amazons_white = chess.BB_EMPTY
  48.         self.amazons_black = chess.BB_EMPTY
  49.         self.cyrils_white = chess.BB_EMPTY
  50.         self.cyrils_black = chess.BB_EMPTY
  51.         self.eves_white = chess.BB_EMPTY
  52.         self.eves_black = chess.BB_EMPTY
  53.  
  54.         square = 56
  55.         for c in board_part:
  56.             if c == '/':
  57.                 square -= 16
  58.             elif c.isdigit():
  59.                 square += int(c)
  60.             else:
  61.                 color = chess.WHITE if c.isupper() else chess.BLACK
  62.                 if c.upper() == 'A':
  63.                     if color == chess.WHITE:
  64.                         self.amazons_white |= chess.BB_SQUARES[square]
  65.                     else:
  66.                         self.amazons_black |= chess.BB_SQUARES[square]
  67.                     piece_type = AMAZON
  68.                 elif c.upper() == 'C':
  69.                     if color == chess.WHITE:
  70.                         self.cyrils_white |= chess.BB_SQUARES[square]
  71.                     else:
  72.                         self.cyrils_black |= chess.BB_SQUARES[square]
  73.                     piece_type = CYRIL
  74.                 elif c.upper() == 'E':
  75.                     if color == chess.WHITE:
  76.                         self.eves_white |= chess.BB_SQUARES[square]
  77.                     else:
  78.                         self.eves_black |= chess.BB_SQUARES[square]
  79.                     piece_type = EVE
  80.                 elif c == 'P' and chess.square_rank(square) == 7:
  81.                     piece_type = chess.QUEEN
  82.                 elif c == 'p' and chess.square_rank(square) == 0:
  83.                     piece_type = chess.QUEEN
  84.                     color = chess.BLACK
  85.                 else:
  86.                     piece_type = chess.PIECE_SYMBOLS.index(c.lower())
  87.                 self._set_piece_at(square, piece_type, color)
  88.                 square += 1
  89.  
  90.         self.turn = chess.WHITE if parts[1] == 'w' else chess.BLACK
  91.         self.castling_rights = chess.BB_EMPTY
  92.         if '-' not in parts[2]:
  93.             if 'K' in parts[2]: self.castling_rights |= chess.BB_H1
  94.             if 'Q' in parts[2]: self.castling_rights |= chess.BB_A1
  95.             if 'k' in parts[2]: self.castling_rights |= chess.BB_H8
  96.             if 'q' in parts[2]: self.castling_rights |= chess.BB_A8
  97.         self.ep_square = chess.parse_square(parts[3]) if parts[3] != '-' else None
  98.  
  99.     def _set_piece_at(self, square: chess.Square, piece_type: PieceType, color: Color) -> None:
  100.         self.clear_square(square)
  101.         super()._set_piece_at(square, piece_type, color)
  102.         if piece_type == AMAZON:
  103.             if color == chess.WHITE:
  104.                 self.amazons_white |= chess.BB_SQUARES[square]
  105.             else:
  106.                 self.amazons_black |= chess.BB_SQUARES[square]
  107.         elif piece_type == CYRIL:
  108.             if color == chess.WHITE:
  109.                 self.cyrils_white |= chess.BB_SQUARES[square]
  110.             else:
  111.                 self.cyrils_black |= chess.BB_SQUARES[square]
  112.         elif piece_type == EVE:
  113.             if color == chess.WHITE:
  114.                 self.eves_white |= chess.BB_SQUARES[square]
  115.             else:
  116.                 self.eves_black |= chess.BB_SQUARES[square]
  117.  
  118.     def piece_at(self, square: chess.Square) -> Optional[chess.Piece]:
  119.         if self.amazons_white & chess.BB_SQUARES[square]:
  120.             return chess.Piece(AMAZON, chess.WHITE)
  121.         elif self.amazons_black & chess.BB_SQUARES[square]:
  122.             return chess.Piece(AMAZON, chess.BLACK)
  123.         elif self.cyrils_white & chess.BB_SQUARES[square]:
  124.             return chess.Piece(CYRIL, chess.WHITE)
  125.         elif self.cyrils_black & chess.BB_SQUARES[square]:
  126.             return chess.Piece(CYRIL, chess.BLACK)
  127.         elif self.eves_white & chess.BB_SQUARES[square]:
  128.             return chess.Piece(EVE, chess.WHITE)
  129.         elif self.eves_black & chess.BB_SQUARES[square]:
  130.             return chess.Piece(EVE, chess.BLACK)
  131.         return super().piece_at(square)
  132.  
  133.     def generate_pseudo_legal_moves(self, from_mask: Bitboard = BB_ALL, to_mask: Bitboard = BB_ALL) -> Iterator[Move]:
  134.         our_pieces = self.occupied_co[self.turn]
  135.         if self.turn == chess.WHITE:
  136.             our_amazons = self.amazons_white
  137.             our_cyrils = self.cyrils_white
  138.             our_eves = self.eves_white
  139.         else:
  140.             our_amazons = self.amazons_black
  141.             our_cyrils = self.cyrils_black
  142.             our_eves = self.eves_black
  143.    
  144.         # Generování tahů pro amazonky
  145.         for from_square in chess.scan_forward(our_amazons & from_mask):
  146.             attacks = self.amazon_attacks(from_square)
  147.             valid_moves = attacks & ~our_pieces & to_mask
  148.             for to_square in chess.scan_forward(valid_moves):
  149.                 yield Move(from_square, to_square)
  150.    
  151.         # Generování tahů pro Cyrily
  152.         for from_square in chess.scan_forward(our_cyrils & from_mask):
  153.             attacks = self.cyril_attacks(from_square)
  154.             valid_moves = attacks & ~our_pieces & to_mask
  155.             for to_square in chess.scan_forward(valid_moves):
  156.                 yield Move(from_square, to_square)
  157.    
  158.         # Generování tahů pro Evy
  159.         for from_square in chess.scan_forward(our_eves & from_mask):
  160.             attacks = self.eve_attacks(from_square)
  161.             valid_moves = attacks & ~our_pieces & to_mask
  162.             for to_square in chess.scan_forward(valid_moves):
  163.                 yield Move(from_square, to_square)
  164.    
  165.         # Generování tahů pro standardní figury
  166.         for move in super().generate_pseudo_legal_moves(from_mask, to_mask):
  167.             piece = self.piece_at(move.from_square)
  168.             if piece and piece.piece_type not in [AMAZON, CYRIL, EVE]:
  169.                 yield move
  170.  
  171.     def queen_attacks(self, square):
  172.         return self.bishop_attacks(square) | self.rook_attacks(square)
  173.  
  174.     def bishop_attacks(self, square):
  175.         return chess.BB_DIAG_ATTACKS[square][self.occupied & chess.BB_DIAG_MASKS[square]]
  176.  
  177.     def rook_attacks(self, square):
  178.         return (chess.BB_RANK_ATTACKS[square][self.occupied & chess.BB_RANK_MASKS[square]] |
  179.                 chess.BB_FILE_ATTACKS[square][self.occupied & chess.BB_FILE_MASKS[square]])
  180.  
  181.     def amazon_attacks(self, square):
  182.         return self.queen_attacks(square) | chess.BB_KNIGHT_ATTACKS[square]
  183.  
  184.     def cyril_attacks(self, square):
  185.         return self.rook_attacks(square) | chess.BB_KNIGHT_ATTACKS(square)
  186.  
  187.     def eve_attacks(self, square):
  188.         return self.bishop_attacks(square) | chess.BB_KNIGHT_ATTACKS(square)
  189.  
  190.     def is_pseudo_legal(self, move):
  191.         from_square = move.from_square
  192.         to_square = move.to_square
  193.         piece = self.piece_at(from_square)
  194.    
  195.         if not piece or piece.color != self.turn:
  196.             return False
  197.    
  198.         if self.occupied_co[self.turn] & chess.BB_SQUARES[to_square]:
  199.             return False
  200.    
  201.         if self.is_castling(move):
  202.             return True
  203.    
  204.         if piece.piece_type == AMAZON:
  205.             return bool(self.amazon_attacks(from_square) & chess.BB_SQUARES[to_square])
  206.         elif piece.piece_type == CYRIL:
  207.             return bool(self.cyril_attacks(from_square) & chess.BB_SQUARES[to_square])
  208.         elif piece.piece_type == EVE:
  209.             return bool(self.eve_attacks(from_square) & chess.BB_SQUARES[to_square])
  210.         else:
  211.             return super().is_pseudo_legal(move)
  212.  
  213.     def is_legal(self, move):
  214.         if not self.is_pseudo_legal(move):
  215.             return False
  216.  
  217.         from_square = move.from_square
  218.         to_square = move.to_square
  219.         piece = self.piece_at(from_square)
  220.         captured_piece = self.piece_at(to_square)
  221.  
  222.         self.clear_square(from_square)
  223.         self.clear_square(to_square)
  224.         self._set_piece_at(to_square, piece.piece_type, piece.color)
  225.  
  226.         king_square = to_square if piece.piece_type == chess.KING else self.king(self.turn)
  227.         is_check = self._is_attacked_by(not self.turn, king_square)
  228.  
  229.         self.clear_square(to_square)
  230.         self._set_piece_at(from_square, piece.piece_type, piece.color)
  231.         if captured_piece:
  232.             self._set_piece_at(to_square, captured_piece.piece_type, captured_piece.color)
  233.  
  234.         return not is_check
  235.  
  236.     def _is_attacked_by(self, color, square):
  237.         attackers = self.attackers(color, square)
  238.         return bool(attackers)
  239.  
  240.     def attackers(self, color, square):
  241.         attackers = chess.BB_EMPTY
  242.        
  243.         # Knights
  244.         knights = self.knights & self.occupied_co[color]
  245.         if chess.BB_KNIGHT_ATTACKS[square] & knights:
  246.             attackers |= knights & chess.BB_KNIGHT_ATTACKS[square]
  247.        
  248.         # King
  249.         king = self.kings & self.occupied_co[color]
  250.         if chess.BB_KING_ATTACKS[square] & king:
  251.             attackers |= king
  252.        
  253.         # Pawns
  254.         pawns = self.pawns & self.occupied_co[color]
  255.         pawn_attacks = chess.BB_PAWN_ATTACKS[not color][square]
  256.         if pawn_attacks & pawns:
  257.             attackers |= pawns & pawn_attacks
  258.        
  259.         # Queens
  260.         queens = self.queens & self.occupied_co[color]
  261.         queen_attacks = (
  262.             chess.BB_DIAG_ATTACKS[square][self.occupied & chess.BB_DIAG_MASKS[square]] |
  263.             chess.BB_RANK_ATTACKS[square][self.occupied & chess.BB_RANK_MASKS[square]] |
  264.             chess.BB_FILE_ATTACKS[square][self.occupied & chess.BB_FILE_MASKS[square]]
  265.         )
  266.         if queen_attacks & queens:
  267.             attackers |= queens & queen_attacks
  268.        
  269.         # Bishops
  270.         bishops = self.bishops & self.occupied_co[color]
  271.         bishop_attacks = chess.BB_DIAG_ATTACKS[square][self.occupied & chess.BB_DIAG_MASKS[square]]
  272.         if bishop_attacks & bishops:
  273.             attackers |= bishops & bishop_attacks
  274.        
  275.         # Rooks
  276.         rooks = self.rooks & self.occupied_co[color]
  277.         rook_attacks = (
  278.             chess.BB_RANK_ATTACKS[square][self.occupied & chess.BB_RANK_MASKS[square]] |
  279.             chess.BB_FILE_ATTACKS[square][self.occupied & chess.BB_FILE_MASKS[square]]
  280.         )
  281.         if rook_attacks & rooks:
  282.             attackers |= rooks & rook_attacks
  283.        
  284.         # Amazons (Queen + Knight)
  285.         amazons = self.amazons_white if color == chess.WHITE else self.amazons_black
  286.         for amazon_square in chess.scan_forward(amazons):
  287.             amazon_attacks = (
  288.                 chess.BB_DIAG_ATTACKS[amazon_square][self.occupied & chess.BB_DIAG_MASKS[amazon_square]] |
  289.                 chess.BB_RANK_ATTACKS[amazon_square][self.occupied & chess.BB_RANK_MASKS[amazon_square]] |
  290.                 chess.BB_FILE_ATTACKS[amazon_square][self.occupied & chess.BB_FILE_MASKS[amazon_square]] |
  291.                 chess.BB_KNIGHT_ATTACKS[amazon_square]
  292.             )
  293.             if amazon_attacks & chess.BB_SQUARES[square]:
  294.                 attackers |= chess.BB_SQUARES[amazon_square]
  295.        
  296.         # Cyrils (Rook + Knight)
  297.         cyrils = self.cyrils_white if color == chess.WHITE else self.cyrils_black
  298.         for cyril_square in chess.scan_forward(cyrils):
  299.             cyril_attacks = (
  300.                 chess.BB_RANK_ATTACKS[cyril_square][self.occupied & chess.BB_RANK_MASKS[cyril_square]] |
  301.                 chess.BB_FILE_ATTACKS[cyril_square][self.occupied & chess.BB_FILE_MASKS[cyril_square]] |
  302.                 chess.BB_KNIGHT_ATTACKS[cyril_square]
  303.             )
  304.             if cyril_attacks & chess.BB_SQUARES[square]:
  305.                 attackers |= chess.BB_SQUARES[cyril_square]
  306.        
  307.         # Eves (Bishop + Knight)
  308. # Eves (Bishop + Knight)
  309.         eves = self.eves_white if color == chess.WHITE else self.eves_black
  310.         for eve_square in chess.scan_forward(eves):
  311.             eve_attacks = (
  312.                 chess.BB_DIAG_ATTACKS[eve_square][self.occupied & chess.BB_DIAG_MASKS[eve_square]] |
  313.                 chess.BB_KNIGHT_ATTACKS[eve_square]
  314.             )
  315.             if eve_attacks & chess.BB_SQUARES[square]:
  316.                 attackers |= chess.BB_SQUARES[eve_square]
  317.        
  318.         return attackers
  319.  
  320.     def push(self, move):
  321.         if not self.is_legal(move):
  322.             raise ValueError(f"Move {move} is not legal in position {self.fen()}")
  323.  
  324.         piece = self.piece_at(move.from_square)
  325.         captured_piece = self.piece_at(move.to_square)
  326.  
  327.         self.clear_square(move.from_square)
  328.         self.clear_square(move.to_square)
  329.         self._set_piece_at(move.to_square, piece.piece_type, piece.color)
  330.  
  331.         self.turn = not self.turn
  332.  
  333.         self.move_stack.append((move, captured_piece))
  334.  
  335.     def pop(self):
  336.         if not self.move_stack:
  337.             return None
  338.  
  339.         move, captured_piece = self.move_stack.pop()
  340.  
  341.         piece = self.piece_at(move.to_square)
  342.        
  343.         self.clear_square(move.from_square)
  344.         self.clear_square(move.to_square)
  345.  
  346.         self._set_piece_at(move.from_square, piece.piece_type, piece.color)
  347.  
  348.         if captured_piece:
  349.             self._set_piece_at(move.to_square, captured_piece.piece_type, captured_piece.color)
  350.  
  351.         self.turn = not self.turn
  352.  
  353.         return move
  354.  
  355.     def is_check(self):
  356.         king_square = self.king(self.turn)
  357.         if king_square is None:
  358.             return False
  359.         is_check = self._is_attacked_by(not self.turn, king_square)
  360.         return is_check
  361.  
  362.     def is_checkmate(self):
  363.         if not self.is_check():
  364.             return False
  365.         legal_moves = list(self.generate_legal_moves())
  366.         return len(legal_moves) == 0
  367.  
  368.     def is_game_over(self):
  369.         return self.is_checkmate() or self.is_stalemate() or self.is_insufficient_material()
  370.  
  371.     def is_stalemate(self):
  372.         if self.is_check():
  373.             return False
  374.         legal_moves = list(self.generate_legal_moves())
  375.         return len(legal_moves) == 0
  376.    
  377.     def is_insufficient_material(self):
  378.         return (self.pawns | self.rooks | self.queens | self.amazons_white | self.amazons_black |
  379.                 self.cyrils_white | self.cyrils_black | self.eves_white | self.eves_black) == 0 and (
  380.             chess.popcount(self.occupied) <= 3
  381.         )
  382.  
  383.     def generate_legal_moves(self, from_mask=chess.BB_ALL, to_mask=chess.BB_ALL):
  384.         for move in self.generate_pseudo_legal_moves(from_mask, to_mask):
  385.             if self.is_legal(move):
  386.                 yield move
  387.  
  388.     def debug_amazons(self):
  389.         pass
  390.  
  391.     def debug_cyrils(self):
  392.         pass
  393.  
  394.     def debug_eves(self):
  395.         pass
  396.  
  397.     def piece_symbol(self, piece):
  398.         if piece is None:
  399.             return '.'
  400.         if piece.piece_type == AMAZON:
  401.             return 'A' if piece.color == chess.WHITE else 'a'
  402.         if piece.piece_type == CYRIL:
  403.             return 'C' if piece.color == chess.WHITE else 'c'
  404.         if piece.piece_type == EVE:
  405.             return 'E' if piece.color == chess.WHITE else 'e'
  406.         return piece.symbol()
  407.  
  408.     def piece_type_at(self, square):
  409.         if (self.amazons_white | self.amazons_black) & chess.BB_SQUARES[square]:
  410.             return AMAZON
  411.         if (self.cyrils_white | self.cyrils_black) & chess.BB_SQUARES[square]:
  412.             return CYRIL
  413.         if (self.eves_white | self.eves_black) & chess.BB_SQUARES[square]:
  414.             return EVE
  415.         return super().piece_type_at(square)
  416.  
  417.     def color_at(self, square):
  418.         if self.amazons_white & chess.BB_SQUARES[square]:
  419.             return chess.WHITE
  420.         if self.amazons_black & chess.BB_SQUARES[square]:
  421.             return chess.BLACK
  422.         if self.cyrils_white & chess.BB_SQUARES[square]:
  423.             return chess.WHITE
  424.         if self.cyrils_black & chess.BB_SQUARES[square]:
  425.             return chess.BLACK
  426.         if self.eves_white & chess.BB_SQUARES[square]:
  427.             return chess.WHITE
  428.         if self.eves_black & chess.BB_SQUARES[square]:
  429.             return chess.BLACK
  430.         return super().color_at(square)
  431.  
  432.     @property
  433.     def legal_moves(self):
  434.         return list(self.generate_legal_moves())
  435.  
  436.     def __str__(self):
  437.         builder = []
  438.         for square in chess.SQUARES_180:
  439.             piece = self.piece_at(square)
  440.             symbol = self.piece_symbol(piece) if piece else '.'
  441.             builder.append(symbol)
  442.             if chess.square_file(square) == 7:
  443.                 if square != chess.H1:
  444.                     builder.append('\n')
  445.         return ''.join(builder)
  446.  
  447. def format_time(seconds):
  448.     hours, remainder = divmod(seconds, 3600)
  449.     minutes, seconds = divmod(remainder, 60)
  450.     return f"{int(hours):02d}h {int(minutes):02d}m {int(seconds):02d}s"
  451.  
  452. def print_elapsed_time(stop_event, start_time):
  453.     while not stop_event.is_set():
  454.         elapsed_time = time.time() - start_time
  455.         print(f"\rUplynulý čas: {format_time(elapsed_time)}", end="", flush=True)
  456.         time.sleep(1)
  457.  
  458. def simplify_fen(fen):
  459.     return ' '.join(fen.split()[:4])
  460.  
  461. def calculate_optimal_moves(start_fen: str) -> Dict[str, Tuple[int, str]]:
  462.     print("Funkce calculate_optimal_moves byla zavolána")
  463.     print(f"Počáteční FEN: {start_fen}")
  464.    
  465.     board = CustomBoard(start_fen)
  466.     POZ = {1: simplify_fen(start_fen)}
  467.     AR = {simplify_fen(start_fen): {'used': 0, 'to_end': None, 'depth': 0, 'type': 'normal'}}
  468.     N = 1
  469.     M = 0
  470.  
  471.     start_time = time.time()
  472.     current_depth = 0
  473.     positions_at_depth = {0: 0}
  474.     depth_start_time = start_time
  475.  
  476.     stop_event = threading.Event()
  477.     timer_thread = threading.Thread(target=print_elapsed_time, args=(stop_event, start_time))
  478.     timer_thread.start()
  479.  
  480.     try:
  481.         print("Začínám generovat pozice...")
  482.         print("Počáteční pozice:")
  483.         print_board(start_fen)
  484.        
  485.         depth_1_positions = []  # Seznam pro ukládání pozic v hloubce 1
  486.  
  487.         # Generate all positions
  488.         while M < N:
  489.             M += 1
  490.             current_fen = POZ[M]
  491.             board.set_custom_fen(current_fen)
  492.             simplified_current_fen = simplify_fen(current_fen)
  493.             current_depth = AR[simplified_current_fen]['depth']
  494.  
  495.             if current_depth not in positions_at_depth:
  496.                 positions_at_depth[current_depth] = 0
  497.                 if current_depth > 0:
  498.                     depth_time = time.time() - depth_start_time
  499.                     total_time = time.time() - start_time
  500.                     print(f"\nHloubka {current_depth - 1}: {positions_at_depth[current_depth - 1]} pozic, "
  501.                           f"Čas hloubky: {format_time(depth_time)} / Celkový čas: {format_time(total_time)}")
  502.                    
  503.                     if current_depth == 1:
  504.                         print("Všechny pozice v hloubce 1:")
  505.                         for pos in depth_1_positions:
  506.                             print_board(pos)
  507.                             print()
  508.                
  509.                 depth_start_time = time.time()
  510.  
  511.             positions_at_depth[current_depth] += 1
  512.  
  513.             if current_depth == 1:
  514.                 depth_1_positions.append(current_fen)
  515.  
  516.             if AR[simplified_current_fen]['used'] == 0:
  517.                 AR[simplified_current_fen]['used'] = 1
  518.                 legal_moves = list(board.legal_moves)
  519.                 for move in legal_moves:
  520.                     board.push(move)
  521.                     POZ2 = board.fen()
  522.                     simplified_POZ2 = simplify_fen(POZ2)
  523.                     if simplified_POZ2 not in AR:
  524.                         N += 1
  525.                         POZ[N] = simplified_POZ2
  526.                         AR[simplified_POZ2] = {'used': 0, 'to_end': None, 'depth': current_depth + 1, 'type': 'normal'}
  527.                     board.pop()
  528.    
  529.         # Print last depth
  530.         depth_time = time.time() - depth_start_time
  531.         total_time = time.time() - start_time
  532.         print(f"\nHloubka {current_depth}: {positions_at_depth[current_depth]} pozic, "
  533.               f"Čas hloubky: {format_time(depth_time)} / Celkový čas: {format_time(total_time)}")
  534.         print(f"Příklad pozice v hloubce {current_depth}:")
  535.         print_board(current_fen)
  536.  
  537.         print(f"Generování pozic dokončeno. Celkový počet pozic: {N}")
  538.  
  539.         # Initial evaluation
  540.         print("\nZačínám počáteční ohodnocení...")
  541.         F = 0
  542.         for i in range(1, N + 1):
  543.             current_fen = POZ[i]
  544.             board.set_custom_fen(current_fen)
  545.             simplified_current_fen = simplify_fen(current_fen)
  546.  
  547.             if board.is_checkmate():
  548.                 AR[simplified_current_fen]['to_end'] = -1000
  549.                 F += 1
  550.             elif board.is_stalemate() or board.is_insufficient_material():
  551.                 AR[simplified_current_fen]['to_end'] = 0
  552.             else:
  553.                 AR[simplified_current_fen]['to_end'] = 0
  554.  
  555.         print(f"Počet pozic v matu je {F}")
  556.  
  557.         # Iterative evaluation
  558.         print("\nZačínám iterativní ohodnocení...")
  559.         uroven = 0
  560.         while F > 0:
  561.             uroven += 1
  562.             level_start_time = time.time()
  563.             print(f"Výpočet v úrovni {uroven}")
  564.            
  565.             F = 0
  566.             current_level_positions = 0
  567.             for i in range(1, N + 1):
  568.                 current_fen = POZ[i]
  569.                 board.set_custom_fen(current_fen)
  570.                 simplified_current_fen = simplify_fen(current_fen)
  571.                 if AR[simplified_current_fen]['to_end'] == 0:
  572.                     hod = -2000
  573.                     for move in board.legal_moves:
  574.                         board.push(move)
  575.                         POZ2 = board.fen()
  576.                         simplified_POZ2 = simplify_fen(POZ2)
  577.                         hod2 = -AR[simplified_POZ2]['to_end']
  578.                         if hod2 > hod:
  579.                             hod = hod2
  580.                         board.pop()
  581.                     if hod == 1001 - uroven:
  582.                         AR[simplified_current_fen]['to_end'] = 1000 - uroven
  583.                         F += 1
  584.                         current_level_positions += 1
  585.                     if hod == -1001 + uroven:
  586.                         AR[simplified_current_fen]['to_end'] = -1000 + uroven
  587.                         F += 1
  588.                         current_level_positions += 1
  589.             level_end_time = time.time()
  590.             total_elapsed_time = level_end_time - start_time
  591.             level_elapsed_time = level_end_time - level_start_time
  592.             print(f"Nalezeno {current_level_positions} pozic v úrovni {uroven}")
  593.             print(f"Čas úrovně: {format_time(level_elapsed_time)} / Celkový čas: {format_time(total_elapsed_time)}")
  594.  
  595.         print(f"Nalezeno {F} pozic celkem")
  596.        
  597.         print("\nVýpočet dokončen.")
  598.         return {fen: (data['to_end'], data['type']) for fen, data in AR.items() if data['to_end'] is not None}
  599.  
  600.     finally:
  601.         stop_event.set()
  602.         timer_thread.join()
  603.        
  604. # Helper function to print the board
  605. def print_board(fen):
  606.     board = CustomBoard(fen)
  607.     print(board)
  608.  
  609. # Najděte nejmenší kladnou hodnotu to_end ve všech FEN záznamech v AR
  610. def find_min_positive_value(AR):
  611.     min_positive_value = float('inf')
  612.     min_fen = None
  613.    
  614.     for fen, (value, type_pozice) in AR.items():
  615.         if value > 0 and value < min_positive_value:
  616.             min_positive_value = value
  617.             min_fen = fen
  618.    
  619.     if min_positive_value == float('inf'):
  620.         print("Žádná kladná hodnota nebyla nalezena.")
  621.     else:
  622.         print(f"Nejmenší kladná hodnota: {min_positive_value}, FEN: {min_fen}")
  623.  
  624. # Main execution
  625. if __name__ == "__main__":
  626.     start_fen = "7K/2P5/8/8/8/1k6/8/8 w - - 0 1"
  627.    
  628.     AR = calculate_optimal_moves(start_fen)
  629.  
  630.     find_min_positive_value(AR)
  631.  
  632.     print("\nVýsledky:")
  633.     for fen, (hodnota, typ_pozice) in AR.items():
  634.         if hodnota == 984 or hodnota == -980:
  635.             print(f"FEN: {fen}")
  636.             print(f"Hodnota: {hodnota}")
  637.             print(f"Typ pozice: {typ_pozice}")
  638.            
  639.             temp_board = CustomBoard(fen)
  640.            
  641.             if temp_board.is_checkmate():
  642.                 print("Stav: Mat")
  643.             elif temp_board.is_stalemate():
  644.                 print("Stav: Pat")
  645.             elif temp_board.is_insufficient_material():
  646.                 print("Stav: Nedostatečný materiál")
  647.             elif temp_board.is_check():
  648.                 print("Stav: Šach")
  649.             else:
  650.                 print("Stav: Normální pozice")
  651.  
  652.             print_board(fen)
  653.            
  654.             print()
  655.  
  656.     # Print optimal moves
  657. # Print optimal moves
  658.     current_fen = start_fen
  659.     simplified_current_fen = simplify_fen(current_fen)
  660.     simplified_current_fen1 = simplify_fen(current_fen)
  661.     optimal_moves = []
  662.  
  663.     while AR[simplified_current_fen][0] > -1000:
  664.         board = CustomBoard(current_fen)
  665.         hod = -2000
  666.         best_fen = None
  667.         for move in board.legal_moves:
  668.             board.push(move)
  669.             POZ2 = board.fen()
  670.             simplified_POZ2 = simplify_fen(POZ2)
  671.             hod2 = -AR[simplified_POZ2][0]
  672.             if hod2 > hod:
  673.                 hod = hod2
  674.                 best_fen = simplified_POZ2
  675.             board.pop()
  676.  
  677.         if best_fen is None:
  678.             break
  679.         optimal_moves.append(best_fen)
  680.         current_fen = best_fen
  681.         simplified_current_fen = simplify_fen(current_fen)
  682.  
  683.     print("\nOptimální tahy:")
  684.     for fen in reversed(optimal_moves):
  685.         print_board(fen)
  686.         print(AR[fen])
  687.         print(fen)
  688.         print("\n")
  689.  
  690.     print_board(simplified_current_fen1)
  691.     print(AR[simplified_current_fen1])
  692.     print(simplified_current_fen1)
  693.     print("\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement