Advertisement
max2201111

very good fen1 to fen2 number of moves d6e6 Christopher

Apr 29th, 2024
688
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.65 KB | Science | 0 0
  1. import chess
  2. from collections import deque, defaultdict
  3.  
  4. def simplify_fen(fen):
  5.     parts = fen.split(' ')
  6.     return ' '.join(parts[:4])  # Normalize FEN string
  7.  
  8. def generate_moves_and_distances(board):
  9.     initial_fen = simplify_fen(board.fen())
  10.     queue = deque([(initial_fen, 0)])
  11.     distances = defaultdict(lambda: defaultdict(lambda: float('inf')))
  12.     visited = set()
  13.  
  14.     while queue:
  15.         current_fen, current_depth = queue.popleft()
  16.        
  17.         if current_fen in visited:
  18.             continue
  19.         visited.add(current_fen)
  20.         distances[initial_fen][current_fen] = current_depth
  21.  
  22.         current_board = chess.Board(current_fen)
  23.         for move in current_board.legal_moves:
  24.             current_board.push(move)
  25.             next_fen = simplify_fen(current_board.fen())
  26.             if next_fen not in visited or distances[initial_fen][next_fen] > current_depth + 1:
  27.                 distances[initial_fen][next_fen] = current_depth + 1
  28.                 queue.append((next_fen, current_depth + 1))
  29.             current_board.pop()
  30.  
  31.     return distances
  32.  
  33. def minimax(board, depth, maximizing_player):
  34.     if depth == 0 or board.is_game_over():
  35.         evaluation = evaluate_board(board)
  36.         if evaluation is None:
  37.             return 0  # Neutrální hodnota pro nedeterminované pozice
  38.         return evaluation
  39.  
  40.     if maximizing_player:
  41.         max_eval = float('-inf')
  42.         for move in board.legal_moves:
  43.             board.push(move)
  44.             eval = minimax(board, depth - 1, not maximizing_player)
  45.             board.pop()
  46.             if eval is not None:
  47.                 max_eval = max(max_eval, eval)
  48.             else:
  49.                 max_eval = max(max_eval, 0)  # Neutrální hodnota pokud eval je None
  50.         return max_eval
  51.     else:
  52.         min_eval = float('inf')
  53.         for move in board.legal_moves:
  54.             board.push(move)
  55.             eval = minimax(board, depth - 1, not maximizing_player)
  56.             board.pop()
  57.             if eval is not None:
  58.                 min_eval = min(min_eval, eval)
  59.             else:
  60.                 min_eval = min(min_eval, 0)  # Neutrální hodnota pokud eval je None
  61.         return min_eval
  62.  
  63. def find_best_move(board, depth):
  64.     best_move = None
  65.     best_value = float('-inf')
  66.  
  67.     for move in board.legal_moves:
  68.         board.push(move)
  69.         value = minimax(board, depth - 1, False)
  70.         board.pop()
  71.         if value > best_value:
  72.             best_value = value
  73.             best_move = move
  74.  
  75.     return best_move
  76.  
  77.  
  78.  
  79.  
  80. def evaluate_board(board):
  81.     if board.is_checkmate():
  82.         if board.turn:  # Black's turn, white gave checkmate
  83.             return float('-inf')
  84.         else:
  85.             return float('inf')
  86.     if board.is_stalemate() or board.is_insufficient_material():
  87.         return 0
  88.     return None  # No evaluation, return a neutral score
  89.  
  90.  
  91.  
  92. # Main usage
  93. initial_fen = "5k2/8/3K4/6Q1/8/8/8/8 w - - 0 1"
  94.  
  95. initial_fen = "8/8/4k3/8/2K5/5Q2/8/8 w - - 0 1"
  96.  
  97. initial_fen = "8/5k2/8/8/8/8/8/1KQ5 w - - 0 1"
  98.  
  99.  
  100. initial_fen = simplify_fen(initial_fen)
  101.  
  102. board = chess.Board(initial_fen)
  103. AB = generate_moves_and_distances(board)
  104.  
  105. def print_distances_less_than_two(AB, initial_fen):
  106.     for fen2 in AB[initial_fen]:
  107.         if AB[initial_fen][fen2] < 2:
  108.             print(f"From {initial_fen} to {fen2}: {AB[initial_fen][fen2]} moves")
  109.  
  110. print_distances_less_than_two(AB, initial_fen)
  111.  
  112. # Finding best move using minimax
  113. best_move = find_best_move(board, 8)
  114. print("Best move for white:", best_move)
  115.  
  116. fen3 = "8/5k2/8/8/8/8/8/1KQ5 w - -"
  117. fen4 = "8/8/5k2/8/8/4Q3/8/1K6 w - -"
  118.  
  119. print(f"Od {fen3} do {fen4}: {AB[fen3][fen4]} moves")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement