Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import chess
- import random
- from itertools import combinations, permutations
- from concurrent.futures import ThreadPoolExecutor, as_completed
- import time
- import threading
- def simplify_fen_string(fen):
- parts = fen.split(' ')
- simplified_fen = ' '.join(parts[:4]) # Zachováváme pouze informace o pozici
- return simplified_fen
- def generate_chess_positions(pieces):
- all_squares = [chess.SQUARES[i] for i in range(64)]
- unique_fens = set()
- for squares in combinations(all_squares, len(pieces)):
- for square_perm in permutations(squares):
- board = chess.Board(None)
- board.clear_board()
- for piece, square in zip(pieces, square_perm):
- board.set_piece_at(square, chess.Piece.from_symbol(piece))
- # Kontrola platnosti pro tah bílého
- board.turn = chess.WHITE
- if board.is_valid() or board.is_checkmate():
- unique_fens.add(simplify_fen_string(board.fen()))
- # Kontrola platnosti pro tah černého
- board.turn = chess.BLACK
- if board.is_valid() or board.is_checkmate():
- unique_fens.add(simplify_fen_string(board.fen()))
- return unique_fens
- # Funkce pro kontrolu matu a aktualizaci hodnocení
- def evaluate_checkmate(fen):
- board = chess.Board(fen)
- if board.is_checkmate():
- if board.turn == chess.WHITE:
- return {"to_end": -1000, "score": -1} # Bílý je v matu
- else:
- return {"to_end": 1000, "score": 1} # Černý je v matu
- return {"to_end": None, "score": 0}
- # Funkce pro paralelní zpracování
- def process_position(fen, dot_event):
- evaluation = evaluate_checkmate(fen)
- dot_event.set() # Nastavit událost pro tisk tečky
- return fen, evaluation
- # Funkce pro výpis uplynulého času
- def print_elapsed_time(stop_event):
- start_time = time.time()
- while not stop_event.is_set():
- time.sleep(1)
- elapsed_time = time.time() - start_time
- days, remainder = divmod(elapsed_time, 86400)
- hours, remainder = divmod(remainder, 3600)
- minutes, seconds = divmod(remainder, 60)
- print(f"\rUplynulý čas: {int(days)}d {int(hours):02}h {int(minutes):02}m {int(seconds):02}s", end='', flush=True)
- # Funkce pro tisk teček
- def print_dots(dot_event, stop_event):
- while not stop_event.is_set():
- if dot_event.wait(0.1): # Čekat na nastavení události s timeoutem
- print(".", end='', flush=True)
- dot_event.clear()
- # Hlavní část kódu
- initial_pieces = ['K', 'k', 'Q'] # Můžete změnit figury podle potřeby
- unique_positions = generate_chess_positions(initial_pieces)
- evaluations = []
- print("Počet unikátních pozic:", len(unique_positions))
- # Nastavení a spuštění vlákna pro výpis času
- stop_event = threading.Event()
- dot_event = threading.Event()
- time_thread = threading.Thread(target=print_elapsed_time, args=(stop_event,))
- dot_thread = threading.Thread(target=print_dots, args=(dot_event, stop_event))
- time_thread.start()
- dot_thread.start()
- # Paralelní zpracování pozic
- batch_size = 100 # Zpracovat menší dávky paralelně
- with ThreadPoolExecutor(max_workers=16) as executor:
- for i in range(0, len(unique_positions), batch_size):
- batch_positions = list(unique_positions)[i:i + batch_size]
- futures = [executor.submit(process_position, fen, dot_event) for fen in batch_positions]
- for future in as_completed(futures):
- fen, evaluation = future.result()
- evaluations.append({
- "fen_string": fen,
- "to_end": evaluation["to_end"],
- "score": evaluation["score"],
- "parent": None # Přidání parent, může být aktualizováno později, pokud je třeba
- })
- dot_event.set() # Nastavit událost pro tisk tečky po dokončení úlohy
- # Zastavení vlákna pro výpis času a teček
- stop_event.set()
- time_thread.join()
- dot_thread.join()
- # Print results
- print()
- for evaluation in evaluations:
- fen = evaluation["fen_string"]
- score = evaluation["score"]
- to_end = evaluation["to_end"]
- if to_end != None:
- turn = "White" if chess.Board(fen).turn == chess.WHITE else "Black"
- print(f"FEN: {fen}, Turn: {turn}, Score: {score}, To End: {to_end}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement