Advertisement
max2201111

dobre i s casem 10 moves rule

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