Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import chess
- import time
- from functools import lru_cache
- # Definice vlastních figur
- CUSTOM_PIECES = {'A': 'Q', 'C': 'R', 'E': 'Q'}
- @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
- def print_board(fen):
- board = chess.Board(fen)
- print(board)
- def custom_fen_to_standard(fen):
- for custom, standard in CUSTOM_PIECES.items():
- fen = fen.replace(custom, standard).replace(custom.lower(), standard.lower())
- return fen
- def generate_moves(board, piece_type):
- moves = []
- for move in board.legal_moves:
- moves.append(move)
- return moves
- # Startovní pozice
- start_fen = "8/6A1/8/8/8/k1K5/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]
- standard_fen = custom_fen_to_standard(current_fen)
- board = chess.Board(standard_fen)
- simplified_current_fen = current_fen
- if AR[simplified_current_fen]['used'] == 0:
- AR[simplified_current_fen]['used'] = 1
- for piece_type in piece_types:
- for move in generate_moves(board, piece_type):
- 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]
- standard_fen = custom_fen_to_standard(current_fen)
- board = chess.Board(standard_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]
- standard_fen = custom_fen_to_standard(current_fen)
- board = chess.Board(standard_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)
- if simplified_POZ2 in AR:
- 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 == 17
- print("Pozice s to_end == 17:")
- for fen, data in AR.items():
- if data['to_end'] is not None and data['to_end'] < 983 and data['to_end'] > 0:
- print(f"{fen} -> to_end: {data['to_end']}")
- print("*****")
- print("Pozice s to_end == -20:")
- for fen, data in AR.items():
- if data['to_end'] is not None and data['to_end'] > -981 and data['to_end'] < 0:
- print(f"{fen} -> to_end: {data['to_end']}")
- current_fen = POZ[1]
- board = chess.Board(custom_fen_to_standard(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}")
- # Závěrečný kód pro procházení nejlepších tahů
- current_fen = start_fen
- simplified_current_fen = simplify_fen_string(current_fen)
- optimal_moves = []
- while AR[simplified_current_fen]['to_end'] is not None and AR[simplified_current_fen]['to_end'] > -1000:
- board = chess.Board(custom_fen_to_standard(current_fen))
- simplified_current_fen = simplify_fen_string(current_fen)
- hod = -2000
- best_fen = None
- for move in board.legal_moves:
- board.push(move)
- POZ2 = board.fen()
- simplified_POZ2 = simplify_fen_string(POZ2)
- if simplified_POZ2 in AR:
- hod2 = -AR[simplified_POZ2]['to_end']
- if hod2 > hod:
- hod = hod2
- best_fen = simplified_POZ2
- board.pop() # Vrátíme tah zpět
- if best_fen:
- optimal_moves.append(best_fen)
- current_fen = best_fen
- simplified_current_fen = simplify_fen_string(current_fen)
- else:
- break
- # Tisk šachovnic v opačném pořadí
- for fen in reversed(optimal_moves):
- print_board(fen)
- print("\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement