Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import chess
- import time
- from itertools import permutations, combinations
- from functools import lru_cache
- @lru_cache(maxsize=None)
- def simplify_fen_string(fen):
- parts = fen.split(' ')
- simplified_fen = ' '.join(parts[:4]) # Zachováváme pouze informace o pozici
- return simplified_fen
- # Startovní pozice
- start_fen = "6k1/8/5Q2/6K1/8/8/8/8 w - - 0 1"
- simplified_POZ2 = simplify_fen_string(start_fen)
- POZ = {1: simplified_POZ2}
- AR = {simplify_fen_string(start_fen): {'used': 0, 'to_end': None}}
- N = 1
- M = 0
- start_time = time.time()
- def format_elapsed_time(elapsed_time):
- hours, remainder = divmod(elapsed_time, 3600)
- minutes, seconds = divmod(remainder, 60)
- return f"{int(hours)}h {int(minutes)}m {int(seconds)}s"
- def print_elapsed_time(total_time, level_time):
- print(f"{format_elapsed_time(total_time)} / {format_elapsed_time(level_time)}")
- while M < N:
- M += 1
- current_fen = POZ[M]
- board = chess.Board(current_fen)
- simplified_current_fen = current_fen
- if AR[simplified_current_fen]['used'] == 0:
- AR[simplified_current_fen]['used'] = 1
- for move in board.legal_moves:
- board.push(move)
- POZ2 = board.fen()
- simplified_POZ2 = simplify_fen_string(POZ2)
- if simplified_POZ2 not in AR:
- AR[simplified_POZ2] = {'used': None, 'to_end': None}
- if AR[simplified_POZ2]['used'] is None:
- N += 1
- POZ[N] = simplified_POZ2
- AR[simplified_POZ2] = {'used': 0, 'to_end': None}
- board.pop() # Vrátíme tah zpět
- print(f"Počet pozic je {N}")
- # Přidání kontroly pro mat, remízu a výchozí hodnotu
- F = 0
- for i in range(1, N + 1):
- current_fen = POZ[i]
- board = chess.Board(current_fen)
- simplified_current_fen = simplify_fen_string(current_fen)
- if board.is_checkmate():
- AR[simplified_current_fen]['to_end'] = -1000
- F += 1
- elif board.is_stalemate() or board.is_insufficient_material() or board.is_seventyfive_moves() or board.is_fivefold_repetition():
- AR[simplified_current_fen]['to_end'] = 0
- else:
- AR[simplified_current_fen]['to_end'] = 0
- print(f"Počet pozic v matu je {F}")
- uroven = 0
- while F > 0:
- uroven += 1
- level_start_time = time.time()
- print(f"Výpočet v úrovni {uroven}")
- F = 0
- current_level_positions = 0
- for i in range(1, N + 1):
- current_fen = POZ[i]
- board = chess.Board(current_fen)
- simplified_current_fen = simplify_fen_string(current_fen)
- if AR[simplified_current_fen]['to_end'] == 0:
- hod = -2000
- for move in board.legal_moves:
- board.push(move)
- POZ2 = board.fen()
- simplified_POZ2 = simplify_fen_string(POZ2)
- hod2 = -AR[simplified_POZ2]['to_end']
- if hod2 > hod:
- hod = hod2
- board.pop() # Vrátíme tah zpět
- if hod == 1001 - uroven:
- AR[simplified_current_fen]['to_end'] = 1000 - uroven
- F += 1
- current_level_positions += 1
- if hod == -1001 + uroven:
- AR[simplified_current_fen]['to_end'] = -1000 + uroven
- F += 1
- current_level_positions += 1
- level_end_time = time.time()
- total_elapsed_time = level_end_time - start_time
- level_elapsed_time = level_end_time - level_start_time
- print(f"Nalezeno {current_level_positions} pozic v úrovni {uroven}")
- print_elapsed_time(total_elapsed_time, level_elapsed_time)
- print(f"Nalezeno {F} pozic celkem")
- # Výpis všech pozic s to_end == 21
- print("Pozice s to_end == 17:")
- for fen, data in AR.items():
- if data['to_end'] < 983 and data['to_end'] != 0:
- print(f"{fen} -> to_end: {data['to_end']}")
- current_fen = POZ[1]
- board = chess.Board(current_fen)
- simplified_current_fen = simplify_fen_string(current_fen)
- hod = AR[simplified_current_fen]['to_end']
- print(f"Hodnocení počáteční pozice je {hod}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement