Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import chess
- import random
- from itertools import combinations, permutations
- from functools import lru_cache
- from concurrent.futures import ThreadPoolExecutor, as_completed
- 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)):
- perm = permutations(squares)
- for square_perm in perm:
- 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))
- if board.is_valid():
- if random.choice([True, False]):
- board.turn = chess.BLACK # Náhodně nastavíme tah pro černého
- unique_fens.add(board.fen())
- return unique_fens
- def evaluate_board(board, depth):
- if board.is_checkmate():
- return 1000 - depth if not board.turn else -1000 + depth
- elif board.is_stalemate() or board.is_insufficient_material():
- return 0
- return 4 # Basic heuristic for non-terminal positions
- @lru_cache(maxsize=None)
- def minimax(fen, depth, alpha, beta, maximizing_player, max_depth):
- board = chess.Board(fen)
- if depth == max_depth or board.is_game_over():
- return evaluate_board(board, depth), None # Vrátit tuple s hodnocením a None pro tah
- best_eval = float('-inf') if maximizing_player else float('inf')
- best_move = None
- for move in board.legal_moves:
- board.push(move)
- eval, _ = minimax(board.fen(), depth + 1, alpha, beta, not maximizing_player, max_depth)
- board.pop()
- if maximizing_player:
- if eval > best_eval:
- best_eval = eval
- best_move = move
- alpha = max(alpha, eval)
- if beta <= alpha:
- break
- else:
- if eval < best_eval:
- best_eval = eval
- best_move = move
- beta = min(beta, eval)
- if beta <= alpha:
- break
- return best_eval, best_move # Vrátit tuple s hodnocením a nejlepším tahem
- # Hlavní část kódu
- initial_pieces = ['K', 'k', 'Q', 'B']
- unique_positions = generate_chess_positions(initial_pieces)
- evaluations = []
- print("Počet unikátních pozic:", len(unique_positions))
- # Funkce pro paralelní zpracování
- def process_position(fen, max_depth):
- print(".", end='') # Tisk tečky před každým voláním minimax
- board = chess.Board(fen)
- evaluation, best_move = minimax(fen, 0, float('-inf'), float('inf'), board.turn == chess.WHITE, max_depth)
- return (fen, evaluation, best_move)
- # Paralelní zpracování pozic
- max_depth = 8 # Limit depth for demonstration purposes
- with ThreadPoolExecutor(max_workers=8) as executor:
- futures = [executor.submit(process_position, fen, max_depth) for fen in list(unique_positions)[:2000]]
- for future in as_completed(futures):
- fen, evaluation, best_move = future.result()
- evaluations.append((fen, evaluation, best_move))
- # Print results
- for position, eval, move in evaluations:
- if eval != 0 and eval != 4 and eval < 994:
- turn = "White" if chess.Board(position).turn == chess.WHITE else "Black"
- print(f"FEN: {position}, Turn: {turn}, Best Move: {move}, Evaluation: {eval}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement