Advertisement
max2201111

petr correct 3 plies

May 8th, 2024
738
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.97 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 1
  23.     elif board.is_insufficient_material():
  24.         return 2
  25.     elif ten_moves_rule(board):
  26.         return 3
  27.     return 4  # Default heuristic if none of the above conditions are met
  28.  
  29.  
  30. def minimax(board, depth, alpha, beta, maximizing_player, depth2, printed_depths, position_count, memo, start_time, last_print_time, T):
  31.     current_time = time.time()
  32.     if current_time - last_print_time[0] >= 1:
  33.         elapsed_hours, remainder = divmod(current_time - start_time, 3600)
  34.         elapsed_minutes, elapsed_seconds = divmod(remainder, 60)
  35.         print(f"\r{int(elapsed_hours):02d}h {int(elapsed_minutes):02d}m {int(elapsed_seconds):02d}s", end='', flush=True)
  36.         last_print_time[0] = current_time
  37.  
  38.     position_count[0] += 1
  39.     if position_count[0] % 1000000 == 0:
  40.         print(f"\nProzkoumano {position_count[0]} pozic.")
  41.  
  42.     key = (board.fen(), maximizing_player, depth, alpha, beta)
  43.     if key in memo:
  44.         return memo[key][0], memo[key][1], T
  45.  
  46.     if depth == 0 or board.is_game_over():
  47.         eval = evaluate_board(board, depth2)
  48.         memo[key] = ([], eval)
  49.         T = T or eval != 4
  50.         return [], eval, T
  51.  
  52.     best_eval = float('-inf') if maximizing_player else float('inf')
  53.     best_sequence = []
  54.     all_true = True
  55.  
  56.     for move in board.legal_moves:
  57.         board.push(move)
  58.         sequence, eval, returned_T = minimax(board, depth - 1, alpha, beta, not maximizing_player, depth2 + 1, printed_depths, position_count, memo, start_time, last_print_time, T)
  59.         board.pop()
  60.  
  61.         if maximizing_player:
  62.             if eval > best_eval:
  63.                 best_eval = eval
  64.                 best_sequence = [(move, board.san(move))] + sequence
  65.             alpha = max(alpha, eval)
  66.             if beta <= alpha:
  67.                 break
  68.         else:
  69.             all_true = all_true and returned_T
  70.             if eval < best_eval:
  71.                 best_eval = eval
  72.                 best_sequence = [(move, board.san(move))] + sequence
  73.             beta = min(beta, eval)
  74.             if beta <= alpha:
  75.                 break
  76.  
  77.     if not maximizing_player and all_true:
  78.         return best_sequence, best_eval, True
  79.    
  80.     if maximizing_player and returned_T:
  81.         return best_sequence, best_eval, True
  82.  
  83.     memo[key] = (best_sequence, best_eval)
  84.     if depth2 not in printed_depths:
  85.         printed_depths.add(depth2)
  86.         print(f"\nHloubka {depth2} prozkoumána, čas: {time.time() - start_time:.2f}s")
  87.     return best_sequence, best_eval, T
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94. start_time = time.time()
  95. position_count = [0]
  96. memo = {}
  97. last_print_time = [start_time]
  98. printed_depths = set()
  99. start_fen = "7k/8/3Q4/5K2/8/8/8/8 w - - 0 1"
  100.  
  101. #start_fen = "7K/8/k1P5/7p/8/8/8/8 w - - 0 1"
  102.  
  103.  
  104. board = chess.Board(start_fen)
  105.  
  106. print("Počáteční šachovnice:")
  107. print(board)
  108. print("Počáteční FEN:", board.fen(), "\n")
  109.  
  110. sequence, final_score, T = minimax(board, 9, float('-inf'), float('inf'), True, 0, printed_depths, position_count, memo, start_time, last_print_time,T=False)
  111. print("\n\nOptimal move sequence:")
  112. for move, san in sequence:
  113.     print("Move:", san)
  114.     board.push(move)
  115.     print("Board:\n", board)
  116.     print("FEN:", board.fen())
  117.     print("Evaluation:", evaluate_board(board, 0), "\n")
  118. print("Final evaluation score:", final_score, T)
  119.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement