Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import chess
- from itertools import permutations
- from functools import lru_cache
- def generate_chess_positions(pieces):
- all_squares = [chess.SQUARES[i] for i in range(64)]
- unique_fens = set()
- for squares in permutations(all_squares, len(pieces)):
- board = chess.Board(None)
- board.clear_board()
- for piece, square in zip(pieces, squares):
- board.set_piece_at(square, chess.Piece.from_symbol(piece))
- if board.is_valid():
- 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)
- best_eval = float('-inf') if maximizing_player else float('inf')
- 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:
- best_eval = max(best_eval, eval)
- alpha = max(alpha, eval)
- if beta <= alpha:
- break
- else:
- best_eval = min(best_eval, eval)
- beta = min(beta, eval)
- if beta <= alpha:
- break
- return best_eval
- # Hlavní část kódu
- initial_pieces = ['K', 'k', 'Q']
- unique_positions = generate_chess_positions(initial_pieces)
- evaluations = []
- print("Počet unikátních pozic:", len(unique_positions))
- # Omezení na prvních 10000 pozic
- for fen in list(unique_positions)[:10000]:
- print(".", end='') # Tisk teček pro sledování průběhu
- board = chess.Board(fen)
- max_depth = 8 # Limit depth for demonstration purposes
- evaluation = minimax(fen, 0, float('-inf'), float('inf'), board.turn == chess.WHITE, max_depth)
- evaluations.append((fen, evaluation))
- # Print results
- for position, eval in evaluations:
- if eval != 0 and eval != 4:
- print(f"FEN: {position}, Evaluation: {eval}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement