Advertisement
max2201111

very good best moves with ASCII boards

May 7th, 2024
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.92 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.     """Minimax algorithm with alpha-beta pruning."""
  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]
  45.  
  46.     if depth == 0 or board.is_game_over():
  47.         eval = evaluate_board(board, depth2)
  48.         memo[key] = (None, eval)
  49.         return None, eval
  50.  
  51.     best_move = None
  52.     if maximizing_player:
  53.         max_eval = float('-inf')
  54.         for move in board.legal_moves:
  55.             board.push(move)
  56.             _, eval = minimax(board, depth - 1, alpha, beta, False, depth2 + 1, depths, position_count, memo, start_time, last_print_time)
  57.             board.pop()
  58.             if eval > max_eval:
  59.                 max_eval = eval
  60.                 best_move = move
  61.             alpha = max(alpha, eval)
  62.             if beta <= alpha:
  63.                 break
  64.         memo[key] = (best_move, max_eval)
  65.         return best_move, max_eval
  66.     else:
  67.         min_eval = float('inf')
  68.         for move in board.legal_moves:
  69.             board.push(move)
  70.             _, eval = minimax(board, depth - 1, alpha, beta, True, depth2 + 1, depths, position_count, memo, start_time, last_print_time)
  71.             board.pop()
  72.             if eval < min_eval:
  73.                 min_eval = eval
  74.                 best_move = move
  75.             beta = min(beta, eval)
  76.             if beta <= alpha:
  77.                 break
  78.         memo[key] = (best_move, min_eval)
  79.         return best_move, min_eval
  80.  
  81. # Initialization and main execution logic
  82. start_fen = "7k/8/3Q4/5K2/8/8/8/8 w - - 0 1"
  83.  
  84. start_fen = "7k/8/3Q4/5K2/8/8/8/8 w - - 0 1"
  85.  
  86. board = chess.Board(start_fen)
  87.  
  88. # Tisk počáteční šachovnice a jejího FEN
  89. print("Počáteční šachovnice:")
  90. print(board)
  91. print("Počáteční FEN:", board.fen(), "\n")
  92.  
  93.  
  94.  
  95. sequence = []
  96. depths = []
  97. position_count = [0]
  98. memo = {}
  99. start_time = time.time()
  100. last_print_time = [start_time]  # Initialize last print time
  101.  
  102. move_sequence = []
  103. best_move, best_score = minimax(board, 3, float('-inf'), float('inf'), True, 0, depths, position_count, memo, start_time, last_print_time)
  104. while best_move:
  105.     move_sequence.append(best_move)
  106.     board.push(best_move)
  107.     print(board, "\nFEN:", board.fen(), "\n")  # Printing each board state with FEN
  108.     if board.is_game_over():
  109.         break
  110.     best_move, _ = minimax(board, 5, float('-inf'), float('inf'), board.turn, 0, depths, position_count, memo, start_time, last_print_time)
  111.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement