Advertisement
max2201111

10 moves rule Petre

May 4th, 2024
780
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.14 KB | Science | 0 0
  1. import chess
  2. import ast
  3.  
  4. def simplify_fen_string(fen):
  5.     parts = fen.split(' ')
  6.     simplified_fen = ' '.join(parts[:4])  # Retaining position, turn, castling rights, and en passant
  7.     return simplified_fen
  8.  
  9. def ten_moves_rule(board):
  10.     """Custom rule to evaluate a draw condition based on the last ten moves."""
  11.     history = list(board.move_stack)
  12.     if len(history) < 10:
  13.         return False
  14.     for move in history[-10:]:
  15.         if board.is_capture(move):
  16.             return False
  17.    
  18.     print("H")    
  19.     return True
  20.  
  21. def evaluate_board(board, depth):
  22.     if board.is_checkmate():
  23.         return -1000 + depth if board.turn == chess.WHITE else 1000 - depth
  24.     elif board.is_stalemate() or board.is_insufficient_material() or ten_moves_rule(board):
  25.         return 0
  26.     return 0
  27.  
  28. def minimax(board, depth, alpha, beta, maximizing_player, depth2, depths, position_count, memo):
  29.     position_count[0] += 1
  30.     if position_count[0] % 1000000 == 0:
  31.         print(f"Explored {position_count[0]} positions.")
  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)
  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.  
  57.         if depth2 not in depths:
  58.             depths.append(depth2)
  59.             print(f"Depth of recursion: {depth2}")
  60.  
  61.         return best_move, max_eval
  62.     else:
  63.         min_eval = float('inf')
  64.         for move in board.legal_moves:
  65.             board.push(move)
  66.             _, eval = minimax(board, depth - 1, alpha, beta, True, depth2 + 1, depths, position_count, memo)
  67.             board.pop()
  68.             if eval < min_eval:
  69.                 min_eval = eval
  70.                 best_move = move
  71.             beta = min(beta, eval)
  72.             if beta <= alpha:
  73.                 break
  74.         memo[key] = (best_move, min_eval)
  75.  
  76.         if depth2 not in depths:
  77.             depths.append(depth2)
  78.             print(f"Depth of recursion: {depth2}")
  79.  
  80.         return best_move, min_eval
  81.  
  82. # Initialization and main execution logic
  83. start_fen = "8/4Q1K1/8/8/3k4/8/2q5/8 w - - 0 1"
  84.  
  85. start_fen = "8/4Q1K1/8/8/3k4/8/2q5/8 w - - 0 1"
  86.  
  87.  
  88. board = chess.Board(start_fen)
  89. depths = []
  90. position_count = [0]
  91. memo = {}
  92.  
  93. best_move, best_score = minimax(board, 52, float('-inf'), float('inf'), True, 0, depths, position_count, memo)
  94. if best_move:
  95.     move_san = board.san(best_move)
  96.     print(f"The best move from position {start_fen} is {move_san} with a score of {best_score}.")
  97. else:
  98.     print("No move found, or the game is over. Score: ", best_score)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement