Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import chess
- # Inicializujte globální proměnnou P s výchozí hodnotou, která umožní spuštění minimax funkce
- P = None
- 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() or board.can_claim_draw():
- return 0
- return 4 # Default heuristic for non-terminal positions
- def minimax(board, depth, alpha, beta, maximizing_player, node_count, completed_depths):
- global P # Deklarujte P jako globální proměnnou
- node_count[0] += 1
- if node_count[0] % 1000000 == 0:
- print(f"Nodes explored: {node_count[0]}")
- if board.is_game_over():
- if depth not in completed_depths:
- print(f"Depth {depth} completed for the first time.")
- completed_depths.add(depth)
- return [], evaluate_board(board, depth)
- if depth > 9:
- if depth not in completed_depths:
- print(f"Depth {depth} completed for the first time.")
- completed_depths.add(depth)
- return [], evaluate_board(board, depth)
- best_eval = float('-inf') if maximizing_player else float('inf')
- best_sequence = []
- for move in board.legal_moves:
- board.push(move)
- sequence, eval = minimax(board, depth + 1, alpha, beta, not maximizing_player, node_count, completed_depths)
- board.pop()
- if maximizing_player:
- if eval > best_eval:
- best_eval = eval
- best_sequence = [board.san(move)] + sequence
- alpha = max(alpha, eval)
- if beta <= alpha:
- break
- else:
- if eval < best_eval:
- best_eval = eval
- best_sequence = [board.san(move)] + sequence
- beta = min(beta, eval)
- if beta <= alpha:
- break
- # Přiřaďte hodnotu evaluace P, kontrola může být tady
- P = eval
- if depth not in completed_depths:
- print(f"Depth {depth} completed for the first time.")
- completed_depths.add(depth)
- return best_sequence, best_eval
- # Nastavte P na počáteční hodnotu, která umožňuje první spuštění
- P = 5
- # Testování funkce minimax
- board = chess.Board("8/6k1/8/2Q5/4K3/8/8/8 w - - 0 1")
- node_count = [0]
- completed_depths = set()
- sequence, evaluation = minimax(board, 0, float('-inf'), float('inf'), True, node_count, completed_depths)
- print(f"Best sequence: {sequence}, Evaluation: {evaluation}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement