Advertisement
max2201111

12 tisk reverse optimal ascii boards very good OK

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