Advertisement
max2201111

hloubka rekurze minimax OK spolu s casem

May 5th, 2024
752
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.45 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."""
  6.     history = list(board.move_stack)
  7.     if len(history) < 10:
  8.         return False
  9.     for move in history[-10:]:
  10.         if board.is_capture(move):
  11.             return False
  12.     return True
  13.  
  14. def evaluate_board(board, depth):
  15.     if board.is_checkmate():
  16.         return -1000 + depth if board.turn == chess.WHITE else 1000 - depth
  17.     elif board.is_stalemate() or board.is_insufficient_material() or ten_moves_rule(board):
  18.         return 0
  19.     return 0
  20.  
  21. def minimax(board, depth, alpha, beta, maximizing_player, depth2, depths, position_count, memo, start_time, last_print_time):
  22.     current_time = time.time()
  23.     if current_time - last_print_time[0] >= 1:
  24.         elapsed_hours, remainder = divmod(current_time - start_time, 3600)
  25.         elapsed_minutes, elapsed_seconds = divmod(remainder, 60)
  26.         print(f"\r{int(elapsed_hours):02d}h {int(elapsed_minutes):02d}m {int(elapsed_seconds):02d}s", end='', flush=True)
  27.         last_print_time[0] = current_time
  28.  
  29.     position_count[0] += 1
  30.     if position_count[0] % 1000000 == 0:
  31.         print(f"\nProzkoumano {position_count[0]} pozic.")
  32.  
  33.     key = (board.fen(), maximizing_player, depth, alpha, beta)
  34.     if key in memo:
  35.         return memo[key]
  36.  
  37.     if depth == 0 or board.is_game_over():
  38.         eval = evaluate_board(board, depth2)
  39.         memo[key] = (None, eval)
  40.         return None, eval
  41.  
  42.     best_move = None
  43.     if maximizing_player:
  44.         max_eval = float('-inf')
  45.         for move in board.legal_moves:
  46.             board.push(move)
  47.             _, eval = minimax(board, depth - 1, alpha, beta, False, depth2 + 1, depths, position_count, memo, start_time, last_print_time)
  48.             board.pop()
  49.             if eval > max_eval:
  50.                 max_eval = eval
  51.                 best_move = move
  52.             alpha = max(alpha, eval)
  53.             if beta <= alpha:
  54.                 break
  55.         memo[key] = (best_move, max_eval)
  56.         if depth2 not in depths:
  57.             depths.append(depth2)
  58.             print(f"\nHloubka rekurze: {depth2}")
  59.         return best_move, max_eval
  60.     else:
  61.         min_eval = float('inf')
  62.         for move in board.legal_moves:
  63.             board.push(move)
  64.             _, eval = minimax(board, depth - 1, alpha, beta, True, depth2 + 1, depths, position_count, memo, start_time, last_print_time)
  65.             board.pop()
  66.             if eval < min_eval:
  67.                 min_eval = eval
  68.                 best_move = move
  69.             beta = min(beta, eval)
  70.             if beta <= alpha:
  71.                 break
  72.         memo[key] = (best_move, min_eval)
  73.         if depth2 not in depths:
  74.             depths.append(depth2)
  75.             print(f"Hloubka rekurze: {depth2}")
  76.         return best_move, min_eval
  77.  
  78. # Initialization and main execution logic
  79. start_fen = "7K/8/k1P5/7p/8/8/8/8 w - - 0 1"
  80. board = chess.Board(start_fen)
  81. depths = []
  82. position_count = [0]
  83. memo = {}
  84. start_time = time.time()
  85. last_print_time = [start_time]  # Initialize last print time
  86.  
  87. best_move, best_score = minimax(board, 22, float('-inf'), float('inf'), True, 0, depths, position_count, memo, start_time, last_print_time)
  88. if best_move:
  89.     move_san = board.san(best_move)
  90.     print(f"\nThe best move from position {start_fen} is {move_san} with a score of {best_score}.")
  91. else:
  92.     print("\nNo move found, or the game is over. Score: ", best_score)
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement