Advertisement
max2201111

konecne petr s Amazonkou

Jul 1st, 2024
698
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.17 KB | Science | 0 0
  1. import chess
  2. import time
  3. from functools import lru_cache
  4.  
  5. # Definice vlastních figur
  6. CUSTOM_PIECES = {'A': 'Q', 'C': 'R', 'E': 'Q'}
  7.  
  8. @lru_cache(maxsize=None)
  9. def simplify_fen_string(fen):
  10.     parts = fen.split(' ')
  11.     simplified_fen = ' '.join(parts[:4])  # Zachováváme pouze informace o pozici
  12.     return simplified_fen
  13.  
  14. def print_board(fen):
  15.     board = chess.Board(fen)
  16.     print(board)
  17.  
  18. def custom_fen_to_standard(fen):
  19.     for custom, standard in CUSTOM_PIECES.items():
  20.         fen = fen.replace(custom, standard).replace(custom.lower(), standard.lower())
  21.     return fen
  22.  
  23. def generate_moves(board, piece_type):
  24.     moves = []
  25.     for move in board.legal_moves:
  26.         moves.append(move)
  27.     return moves
  28.  
  29. # Startovní pozice
  30. start_fen = "8/6A1/8/8/8/k1K5/8/8 w - - 0 1"
  31. simplified_POZ2 = simplify_fen_string(start_fen)
  32. POZ = {1: simplified_POZ2}
  33.  
  34. AR = {simplify_fen_string(start_fen): {'used': 0, 'to_end': None}}
  35. N = 1
  36. M = 0
  37.  
  38. start_time = time.time()
  39.  
  40. def format_elapsed_time(elapsed_time):
  41.     hours, remainder = divmod(elapsed_time, 3600)
  42.     minutes, seconds = divmod(remainder, 60)
  43.     return f"{int(hours)}h {int(minutes)}m {int(seconds)}s"
  44.  
  45. def print_elapsed_time(total_time, level_time):
  46.     print(f"{format_elapsed_time(total_time)} / {format_elapsed_time(level_time)}")
  47.  
  48. while M < N:
  49.     M += 1
  50.     current_fen = POZ[M]
  51.     standard_fen = custom_fen_to_standard(current_fen)
  52.     board = chess.Board(standard_fen)
  53.     simplified_current_fen = current_fen
  54.  
  55.     if AR[simplified_current_fen]['used'] == 0:
  56.         AR[simplified_current_fen]['used'] = 1
  57.         for piece_type in piece_types:
  58.             for move in generate_moves(board, piece_type):
  59.                 board.push(move)
  60.                 POZ2 = board.fen()
  61.                 simplified_POZ2 = simplify_fen_string(POZ2)
  62.  
  63.                 if simplified_POZ2 not in AR:
  64.                     AR[simplified_POZ2] = {'used': None, 'to_end': None}
  65.  
  66.                 if AR[simplified_POZ2]['used'] is None:
  67.                     N += 1
  68.                     POZ[N] = simplified_POZ2
  69.                     AR[simplified_POZ2] = {'used': 0, 'to_end': None}
  70.  
  71.                 board.pop()  # Vrátíme tah zpět
  72.  
  73. print(f"Počet pozic je {N}")
  74.  
  75. # Přidání kontroly pro mat, remízu a výchozí hodnotu
  76. F = 0
  77. for i in range(1, N + 1):
  78.     current_fen = POZ[i]
  79.     standard_fen = custom_fen_to_standard(current_fen)
  80.     board = chess.Board(standard_fen)
  81.     simplified_current_fen = simplify_fen_string(current_fen)
  82.  
  83.     if board.is_checkmate():
  84.         AR[simplified_current_fen]['to_end'] = -1000
  85.         F += 1
  86.     elif board.is_stalemate() or board.is_insufficient_material() or board.is_seventyfive_moves() or board.is_fivefold_repetition():
  87.         AR[simplified_current_fen]['to_end'] = 0
  88.     else:
  89.         AR[simplified_current_fen]['to_end'] = 0
  90.  
  91. print(f"Počet pozic v matu je {F}")
  92.  
  93. uroven = 0
  94. while F > 0:
  95.     uroven += 1
  96.     level_start_time = time.time()
  97.     print(f"Výpočet v úrovni {uroven}")
  98.  
  99.     F = 0
  100.     current_level_positions = 0
  101.     for i in range(1, N + 1):
  102.         current_fen = POZ[i]
  103.         standard_fen = custom_fen_to_standard(current_fen)
  104.         board = chess.Board(standard_fen)
  105.         simplified_current_fen = simplify_fen_string(current_fen)
  106.         if AR[simplified_current_fen]['to_end'] == 0:
  107.             hod = -2000
  108.             for move in board.legal_moves:
  109.                 board.push(move)
  110.                 POZ2 = board.fen()
  111.                 simplified_POZ2 = simplify_fen_string(POZ2)
  112.                 if simplified_POZ2 in AR:
  113.                     hod2 = -AR[simplified_POZ2]['to_end']
  114.                     if hod2 > hod:
  115.                         hod = hod2
  116.                 board.pop()  # Vrátíme tah zpět
  117.             if hod == 1001 - uroven:
  118.                 AR[simplified_current_fen]['to_end'] = 1000 - uroven
  119.                 F += 1
  120.                 current_level_positions += 1
  121.             if hod == -1001 + uroven:
  122.                 AR[simplified_current_fen]['to_end'] = -1000 + uroven
  123.                 F += 1
  124.                 current_level_positions += 1
  125.     level_end_time = time.time()
  126.     total_elapsed_time = level_end_time - start_time
  127.     level_elapsed_time = level_end_time - level_start_time
  128.     print(f"Nalezeno {current_level_positions} pozic v úrovni {uroven}")
  129.     print_elapsed_time(total_elapsed_time, level_elapsed_time)
  130.  
  131. print(f"Nalezeno {F} pozic celkem")
  132.  
  133. # Výpis všech pozic s to_end == 17
  134. print("Pozice s to_end == 17:")
  135. for fen, data in AR.items():
  136.     if data['to_end'] is not None and data['to_end'] < 983 and data['to_end'] > 0:
  137.         print(f"{fen} -> to_end: {data['to_end']}")
  138.  
  139. print("*****")
  140.  
  141. print("Pozice s to_end == -20:")
  142. for fen, data in AR.items():
  143.     if data['to_end'] is not None and data['to_end'] > -981 and data['to_end'] < 0:
  144.         print(f"{fen} -> to_end: {data['to_end']}")
  145.  
  146. current_fen = POZ[1]
  147. board = chess.Board(custom_fen_to_standard(current_fen))
  148. simplified_current_fen = simplify_fen_string(current_fen)
  149. hod = AR[simplified_current_fen]['to_end']
  150. print(f"Hodnocení počáteční pozice je {hod}")
  151.  
  152. # Závěrečný kód pro procházení nejlepších tahů
  153. current_fen = start_fen
  154. simplified_current_fen = simplify_fen_string(current_fen)
  155.  
  156. optimal_moves = []
  157. while AR[simplified_current_fen]['to_end'] is not None and AR[simplified_current_fen]['to_end'] > -1000:
  158.     board = chess.Board(custom_fen_to_standard(current_fen))
  159.     simplified_current_fen = simplify_fen_string(current_fen)
  160.     hod = -2000
  161.     best_fen = None
  162.     for move in board.legal_moves:
  163.         board.push(move)
  164.         POZ2 = board.fen()
  165.         simplified_POZ2 = simplify_fen_string(POZ2)
  166.         if simplified_POZ2 in AR:
  167.             hod2 = -AR[simplified_POZ2]['to_end']
  168.             if hod2 > hod:
  169.                 hod = hod2
  170.                 best_fen = simplified_POZ2
  171.         board.pop()  # Vrátíme tah zpět
  172.  
  173.     if best_fen:
  174.         optimal_moves.append(best_fen)
  175.         current_fen = best_fen
  176.         simplified_current_fen = simplify_fen_string(current_fen)
  177.     else:
  178.         break
  179.  
  180. # Tisk šachovnic v opačném pořadí
  181. for fen in reversed(optimal_moves):
  182.     print_board(fen)
  183.     print("\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement