Advertisement
max2201111

hloubka 20 OK very good

Jun 22nd, 2024
486
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.07 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. # Startovní pozice
  13. start_fen = "6k1/8/5Q2/6K1/8/8/8/8 w - - 0 1"
  14. simplified_POZ2 = simplify_fen_string(start_fen)
  15. POZ = {1: simplified_POZ2}
  16.  
  17. AR = {simplify_fen_string(start_fen): {'used': 0, 'to_end': None}}
  18. N = 1
  19. M = 0
  20.  
  21. start_time = time.time()
  22.  
  23. def format_elapsed_time(elapsed_time):
  24.     hours, remainder = divmod(elapsed_time, 3600)
  25.     minutes, seconds = divmod(remainder, 60)
  26.     return f"{int(hours)}h {int(minutes)}m {int(seconds)}s"
  27.  
  28. def print_elapsed_time(total_time, level_time):
  29.     print(f"{format_elapsed_time(total_time)} / {format_elapsed_time(level_time)}")
  30.  
  31. while M < N:
  32.     M += 1
  33.     current_fen = POZ[M]
  34.     board = chess.Board(current_fen)
  35.     simplified_current_fen = current_fen
  36.  
  37.     if AR[simplified_current_fen]['used'] == 0:
  38.         AR[simplified_current_fen]['used'] = 1
  39.         for move in board.legal_moves:
  40.             board.push(move)
  41.             POZ2 = board.fen()
  42.             simplified_POZ2 = simplify_fen_string(POZ2)
  43.  
  44.             if simplified_POZ2 not in AR:
  45.                 AR[simplified_POZ2] = {'used': None, 'to_end': None}
  46.  
  47.             if AR[simplified_POZ2]['used'] is None:
  48.                 N += 1
  49.                 POZ[N] = simplified_POZ2
  50.                 AR[simplified_POZ2] = {'used': 0, 'to_end': None}
  51.  
  52.             board.pop()  # Vrátíme tah zpět
  53.  
  54. print(f"Počet pozic je {N}")
  55.  
  56. # Přidání kontroly pro mat, remízu a výchozí hodnotu
  57. F = 0
  58. for i in range(1, N + 1):
  59.     current_fen = POZ[i]
  60.     board = chess.Board(current_fen)
  61.     simplified_current_fen = simplify_fen_string(current_fen)
  62.  
  63.     if board.is_checkmate():
  64.         AR[simplified_current_fen]['to_end'] = -1000
  65.         F += 1
  66.     elif board.is_stalemate() or board.is_insufficient_material() or board.is_seventyfive_moves() or board.is_fivefold_repetition():
  67.         AR[simplified_current_fen]['to_end'] = 0
  68.     else:
  69.         AR[simplified_current_fen]['to_end'] = 0
  70.  
  71. print(f"Počet pozic v matu je {F}")
  72.  
  73. uroven = 0
  74. while F > 0:
  75.     uroven += 1
  76.     level_start_time = time.time()
  77.     print(f"Výpočet v úrovni {uroven}")
  78.    
  79.     F = 0
  80.     current_level_positions = 0
  81.     for i in range(1, N + 1):
  82.         current_fen = POZ[i]
  83.         board = chess.Board(current_fen)
  84.         simplified_current_fen = simplify_fen_string(current_fen)
  85.         if AR[simplified_current_fen]['to_end'] == 0:
  86.             hod = -2000
  87.             for move in board.legal_moves:
  88.                 board.push(move)
  89.                 POZ2 = board.fen()
  90.                 simplified_POZ2 = simplify_fen_string(POZ2)
  91.                 hod2 = -AR[simplified_POZ2]['to_end']
  92.                 if hod2 > hod:
  93.                     hod = hod2
  94.                 board.pop()  # Vrátíme tah zpět
  95.             if hod == 1001 - uroven:
  96.                 AR[simplified_current_fen]['to_end'] = 1000 - uroven
  97.                 F += 1
  98.                 current_level_positions += 1
  99.             if hod == -1001 + uroven:
  100.                 AR[simplified_current_fen]['to_end'] = -1000 + uroven
  101.                 F += 1
  102.                 current_level_positions += 1
  103.     level_end_time = time.time()
  104.     total_elapsed_time = level_end_time - start_time
  105.     level_elapsed_time = level_end_time - level_start_time
  106.     print(f"Nalezeno {current_level_positions} pozic v úrovni {uroven}")
  107.     print_elapsed_time(total_elapsed_time, level_elapsed_time)
  108.  
  109. print(f"Nalezeno {F} pozic celkem")
  110.  
  111. # Výpis všech pozic s to_end == 21
  112. print("Pozice s to_end == 17:")
  113. for fen, data in AR.items():
  114.     if data['to_end'] < 983 and data['to_end'] != 0:
  115.         print(f"{fen} -> to_end: {data['to_end']}")
  116.  
  117. current_fen = POZ[1]
  118. board = chess.Board(current_fen)
  119. simplified_current_fen = simplify_fen_string(current_fen)
  120. hod = AR[simplified_current_fen]['to_end']
  121. print(f"Hodnocení počáteční pozice je {hod}")
  122.  
  123.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement