Advertisement
max2201111

dynamicka hloubka the very best

May 8th, 2024
517
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.15 KB | Science | 0 0
  1. import chess
  2. import time
  3.  
  4. def ten_moves_rule(board):
  5.     """Pravidlo pro vyhodnocení remízového stavu na základě posledních deseti tahů bez záběru nebo pohybu pěšce."""
  6.     history = list(board.move_stack)
  7.     if len(history) < 10:
  8.         return False
  9.     for move in history[-10:]:
  10.         if board.is_capture(move) or board.piece_type_at(move.from_square) == chess.PAWN:
  11.             return False
  12.     return True
  13.  
  14. def evaluate_board(board, depth):
  15.     """Vyhodnoťte stav šachovnice pro rozhodování minimaxem."""
  16.     if board.is_checkmate():
  17.         return -1000 + depth if board.turn == chess.WHITE else 1000 - depth
  18.     elif board.is_stalemate():
  19.         return 4
  20.     elif board.is_insufficient_material():
  21.         return 3
  22.     elif ten_moves_rule(board):
  23.         return 2
  24.     return 1  # Výchozí heuristika, pokud žádná z výše uvedených podmínek není splněna
  25.  
  26. def minimax(board, depth, alpha, beta, maximizing_player, position_count, memo, start_time, last_print_time, depth_limit):
  27.     current_time = time.time()
  28.     if current_time - start_time > 5:  # Limit výpočtu na 5 sekund
  29.         return [], 0  # Vrátí neutrální hodnotu, pokud vyprší čas
  30.  
  31.     position_count[0] += 1
  32.     key = (board.fen(), maximizing_player, depth, alpha, beta)
  33.     if key in memo:
  34.         return memo[key]
  35.  
  36.     if board.is_game_over() or depth >= depth_limit:
  37.         eval = evaluate_board(board, depth)
  38.         memo[key] = ([], eval)
  39.         return [], eval
  40.  
  41.     best_eval = float('-inf') if maximizing_player else float('inf')
  42.     best_sequence = []
  43.  
  44.     for move in board.legal_moves:
  45.         move_san = board.san(move)
  46.         board.push(move)
  47.         current_depth_limit = depth_limit if not board.is_check() else depth_limit + 1  # Zvyš hloubku, pokud je král v šachu
  48.         sequence, eval = minimax(board, depth + 1, alpha, beta, not maximizing_player, position_count, memo, start_time, last_print_time, current_depth_limit)
  49.         board.pop()
  50.        
  51.         if (maximizing_player and eval > best_eval) or (not maximizing_player and eval < best_eval):
  52.             best_eval = eval
  53.             best_sequence = [(move, move_san)] + sequence
  54.  
  55.         if maximizing_player:
  56.             alpha = max(alpha, eval)
  57.         else:
  58.             beta = min(beta, eval)
  59.  
  60.         if beta <= alpha:
  61.             break
  62.  
  63.     memo[key] = (best_sequence, best_eval)
  64.     return best_sequence, best_eval
  65.  
  66. start_time = time.time()
  67. position_count = [0]
  68. memo = {}
  69. last_print_time = [start_time]
  70. depth_limit = 3  # Nastav počáteční omezení hloubky
  71. start_fen = "7k/8/3Q4/5K2/8/8/8/8 w - - 0 1"
  72. board = chess.Board(start_fen)
  73.  
  74. print("Počáteční šachovnice:")
  75. print(board)
  76. print("Počáteční FEN:", board.fen(), "\n")
  77.  
  78. sequence, final_score = minimax(board, 0, float('-inf'), float('inf'), True, position_count, memo, start_time, last_print_time, depth_limit)
  79. print("\n\nOptimal move sequence:")
  80. for move, san in sequence:
  81.     print("Move:", san)
  82.     board.push(move)
  83.     print("Board:\n", board)
  84.     print("FEN:", board.fen())
  85.     print("Evaluation:", evaluate_board(board, 0), "\n")
  86. print("Final evaluation score:", final_score)
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement