Advertisement
max2201111

the best one minimax

May 8th, 2024
494
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.42 KB | Science | 0 0
  1. import chess
  2. import time
  3.  
  4. def ten_moves_rule(board):
  5.     """Custom rule to evaluate a draw condition based on the last ten moves, considering no captures or pawn moves."""
  6.     history = list(board.move_stack)
  7.     if len(history) < 10:
  8.         return False
  9.  
  10.     for move in history[-10:]:
  11.         if board.is_capture(move):
  12.             return False
  13.         if board.piece_type_at(move.from_square) == chess.PAWN:
  14.             return False
  15.     return True
  16.  
  17. def evaluate_board(board, depth):
  18.     """Evaluate the board state for minimax decision-making."""
  19.     if board.is_checkmate():
  20.         return -1000 + depth if board.turn == chess.WHITE else 1000 - depth
  21.     elif board.is_stalemate():
  22.         return 4
  23.     elif board.is_insufficient_material():
  24.         return 3
  25.     elif ten_moves_rule(board):
  26.         return 2
  27.     return 1  # Default heuristic if none of the above conditions are met
  28.  
  29. def minimax(board, depth, alpha, beta, maximizing_player, depth2, depths, position_count, memo, start_time, last_print_time):
  30.     current_time = time.time()
  31.     if current_time - last_print_time[0] >= 1:
  32.         elapsed_hours, remainder = divmod(current_time - start_time, 3600)
  33.         elapsed_minutes, elapsed_seconds = divmod(remainder, 60)
  34.         print(f"\r{int(elapsed_hours):02d}h {int(elapsed_minutes):02d}m {int(elapsed_seconds):02d}s", end='', flush=True)
  35.         last_print_time[0] = current_time
  36.  
  37.     position_count[0] += 1
  38.     if position_count[0] % 1000000 == 0:
  39.         print(f"\nProzkoumano {position_count[0]} pozic.")
  40.  
  41.     key = (board.fen(), maximizing_player, depth, alpha, beta)
  42.     if key in memo:
  43.         return memo[key]
  44.  
  45.     if depth == 0 or board.is_game_over():
  46.         eval = evaluate_board(board, depth2)
  47.         memo[key] = ([], eval)
  48.         return [], eval
  49.  
  50.     best_eval = float('-inf') if maximizing_player else float('inf')
  51.     best_sequence = []
  52.  
  53.     for move in board.legal_moves:
  54.         move_san = board.san(move)
  55.         board.push(move)
  56.         sequence, eval = minimax(board, depth - 1, alpha, beta, not maximizing_player, depth2 + 1, depths, position_count, memo, start_time, last_print_time)
  57.         board.pop()
  58.        
  59.         if (maximizing_player and eval > best_eval) or (not maximizing_player and eval < best_eval):
  60.             best_eval = eval
  61.             best_sequence = [(move, move_san)] + sequence
  62.  
  63.         if maximizing_player:
  64.             alpha = max(alpha, eval)
  65.         else:
  66.             beta = min(beta, eval)
  67.  
  68.         if beta <= alpha:
  69.             break
  70.  
  71.     memo[key] = (best_sequence, best_eval)
  72.     print(f"\rHloubka {depth} prozkoumána, čas: {time.time() - start_time:.2f}s", end='', flush=True)  # Print when a depth is fully explored
  73.     return best_sequence, best_eval
  74.  
  75. start_time = time.time()
  76. position_count = [0]
  77. memo = {}
  78. last_print_time = [start_time]
  79. depths = []
  80. start_fen = "7k/8/3Q4/5K2/8/8/8/8 w - - 0 1"
  81. board = chess.Board(start_fen)
  82.  
  83. print("Počáteční šachovnice:")
  84. print(board)
  85. print("Počáteční FEN:", board.fen(), "\n")
  86.  
  87. sequence, final_score = minimax(board, 5, float('-inf'), float('inf'), True, 0, depths, position_count, memo, start_time, last_print_time)
  88. print("\n\nOptimal move sequence:")
  89. for move, san in sequence:
  90.     print("Move:", san)
  91.     board.push(move)
  92.     print("Board:\n", board)
  93.     print("FEN:", board.fen())
  94.     print("Evaluation:", evaluate_board(board, 0), "\n")
  95. print("Final evaluation score:", final_score)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement