Advertisement
max2201111

ok 993

May 13th, 2024
762
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.54 KB | Science | 0 0
  1. import chess
  2.  
  3. # Inicializujte globální proměnnou P s výchozí hodnotou, která umožní spuštění minimax funkce
  4. P = None
  5.  
  6. def evaluate_board(board, depth):
  7.     if board.is_checkmate():
  8.         return 1000 - depth if not board.turn else -1000 + depth
  9.     elif board.is_stalemate() or board.is_insufficient_material() or board.can_claim_draw():
  10.         return 0
  11.     return 4  # Default heuristic for non-terminal positions
  12.  
  13. def minimax(board, depth, alpha, beta, maximizing_player, node_count, completed_depths):
  14.     global P  # Deklarujte P jako globální proměnnou
  15.     node_count[0] += 1
  16.     if node_count[0] % 1000000 == 0:
  17.         print(f"Nodes explored: {node_count[0]}")
  18.  
  19.     if board.is_game_over():
  20.         if depth not in completed_depths:
  21.             print(f"Depth {depth} completed for the first time.")
  22.             completed_depths.add(depth)
  23.         return [], evaluate_board(board, depth)
  24.  
  25.     if depth > 9:
  26.         if depth not in completed_depths:
  27.             print(f"Depth {depth} completed for the first time.")
  28.             completed_depths.add(depth)
  29.         return [], evaluate_board(board, depth)
  30.  
  31.     best_eval = float('-inf') if maximizing_player else float('inf')
  32.     best_sequence = []
  33.  
  34.     for move in board.legal_moves:
  35.         board.push(move)
  36.         sequence, eval = minimax(board, depth + 1, alpha, beta, not maximizing_player, node_count, completed_depths)
  37.         board.pop()
  38.  
  39.         if maximizing_player:
  40.             if eval > best_eval:
  41.                 best_eval = eval
  42.                 best_sequence = [board.san(move)] + sequence
  43.             alpha = max(alpha, eval)
  44.             if beta <= alpha:
  45.                 break
  46.         else:
  47.             if eval < best_eval:
  48.                 best_eval = eval
  49.                 best_sequence = [board.san(move)] + sequence
  50.             beta = min(beta, eval)
  51.             if beta <= alpha:
  52.                 break
  53.  
  54.         # Přiřaďte hodnotu evaluace P, kontrola může být tady
  55.         P = eval
  56.  
  57.     if depth not in completed_depths:
  58.         print(f"Depth {depth} completed for the first time.")
  59.         completed_depths.add(depth)
  60.  
  61.     return best_sequence, best_eval
  62.  
  63. # Nastavte P na počáteční hodnotu, která umožňuje první spuštění
  64. P = 5
  65.  
  66. # Testování funkce minimax
  67. board = chess.Board("8/6k1/8/2Q5/4K3/8/8/8 w - - 0 1")
  68. node_count = [0]
  69. completed_depths = set()
  70. sequence, evaluation = minimax(board, 0, float('-inf'), float('inf'), True, node_count, completed_depths)
  71. print(f"Best sequence: {sequence}, Evaluation: {evaluation}")
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement