Advertisement
max2201111

very good minimax depth mimo smycku

May 8th, 2024
798
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.04 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.         # Získání objektu tahu a kontrola, zda se pohyboval pěšcem
  14.         if board.piece_type_at(move.from_square) == chess.PAWN:
  15.             return False
  16.     return True
  17.  
  18. # def evaluate_board(board, depth):
  19. #     if board.is_checkmate():
  20. #         return -1000 + depth if board.turn == chess.WHITE else 1000 - depth
  21. #     elif board.is_stalemate() or board.is_insufficient_material() or ten_moves_rule(board):
  22. #         return 0
  23. #     return 0
  24.  
  25. def evaluate_board(board, depth):
  26.     if board.is_checkmate():
  27.         return -1000 + depth if board.turn == chess.WHITE else 1000 - depth
  28.     elif board.is_stalemate():
  29.         return 2210
  30.     elif board.is_insufficient_material():
  31.         return 3400
  32.     elif ten_moves_rule(board):
  33.         return 9800
  34.     return 7001  # Default return if none of the above conditions are met
  35.  
  36.  
  37. def minimax(board, depth, alpha, beta, maximizing_player, depth2, depths, position_count, memo, start_time, last_print_time):
  38.     current_time = time.time()
  39.     if current_time - last_print_time[0] >= 1:
  40.         elapsed_hours, remainder = divmod(current_time - start_time, 3600)
  41.         elapsed_minutes, elapsed_seconds = divmod(remainder, 60)
  42.         print(f"\r{int(elapsed_hours):02d}h {int(elapsed_minutes):02d}m {int(elapsed_seconds):02d}s", end='', flush=True)
  43.         last_print_time[0] = current_time
  44.  
  45.     position_count[0] += 1
  46.     if position_count[0] % 1000000 == 0:
  47.         print(f"\nProzkoumano {position_count[0]} pozic.")
  48.  
  49.     key = (board.fen(), maximizing_player, depth, alpha, beta)
  50.     if key in memo:
  51.         return memo[key]
  52.  
  53.     if depth == 0 or board.is_game_over():
  54.         eval = evaluate_board(board, depth2)
  55.         memo[key] = (None, eval)
  56.         return None, eval
  57.  
  58.     best_move = None
  59.     if maximizing_player:
  60.         max_eval = float('-inf')
  61.         for move in board.legal_moves:
  62.             board.push(move)
  63.             _, eval = minimax(board, depth - 1, alpha, beta, False, depth2 + 1, depths, position_count, memo, start_time, last_print_time)
  64.             board.pop()
  65.             if eval > max_eval:
  66.                 max_eval = eval
  67.                 best_move = move
  68.             alpha = max(alpha, eval)
  69.             if beta <= alpha:
  70.                 break
  71.         memo[key] = (best_move, max_eval)
  72.         if depth2 not in depths:
  73.             depths.append(depth2)
  74.             print(f"\nHloubka rekurze: {depth2}")
  75.         return best_move, max_eval
  76.     else:
  77.         min_eval = float('inf')
  78.         for move in board.legal_moves:
  79.             board.push(move)
  80.             _, eval = minimax(board, depth - 1, alpha, beta, True, depth2 + 1, depths, position_count, memo, start_time, last_print_time)
  81.             board.pop()
  82.             if eval < min_eval:
  83.                 min_eval = eval
  84.                 best_move = move
  85.             beta = min(beta, eval)
  86.             if beta <= alpha:
  87.                 break
  88.         memo[key] = (best_move, min_eval)
  89.         if depth2 not in depths:
  90.             depths.append(depth2)
  91.             print(f"Hloubka rekurze: {depth2}")
  92.         return best_move, min_eval
  93.  
  94. # Initialization and main execution logic
  95. start_fen = "7K/8/k1P5/7p/8/8/8/8 w - - 0 1"
  96. board = chess.Board(start_fen)
  97. depths = []
  98. position_count = [0]
  99. memo = {}
  100. start_time = time.time()
  101. last_print_time = [start_time]  # Initialize last print time
  102.  
  103. best_move, best_score = minimax(board, 24, float('-inf'), float('inf'), True, 0, depths, position_count, memo, start_time, last_print_time)
  104. if best_move:
  105.     move_san = board.san(best_move)
  106.     print(f"\nThe best move from position {start_fen} is {move_san} with a score of {best_score}.")
  107. else:
  108.     print("\nNo move found, or the game is over. Score: ", best_score)
Advertisement
Comments
  • adscollection
    195 days
    # text 0.39 KB | 0 0
    1. Your code appears to implement a minimax algorithm with alpha-beta pruning for a chess engine. It evaluates board positions using a custom evaluation function and considers factors like checkmate, stalemate, insufficient material, and a custom rule based on the last ten moves. It also prints the depth of recursion and the time taken.
    2.  
    3. website: https://simplified.com/ai-ads-generator/collection-ads
Add Comment
Please, Sign In to add comment
Advertisement