Advertisement
max2201111

correct number of plies between fen1 a fen2

May 1st, 2024
432
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.64 KB | Science | 0 0
  1. import chess
  2.  
  3. def simplify_fen_string(fen):
  4.     """Odstraní z FEN řetězce části týkající se počtu tahů, zachovává pozici, hráče na tahu, rošády a en passant."""
  5.     parts = fen.split(' ')
  6.     simplified_fen = ' '.join(parts[:4])  # Zachováváme informace o pozici, hráči na tahu, rošádách a en passant
  7.     return simplified_fen
  8.  
  9. def bfs_shortest_path(start_fen, goal_fen):
  10.     """Vyhledává nejkratší cestu mezi dvěma FEN pozicemi pomocí BFS algoritmu."""
  11.     start_fen = simplify_fen_string(start_fen)  # Zjednodušení vstupního FEN
  12.     goal_fen = simplify_fen_string(goal_fen)  # Zjednodušení cílového FEN
  13.     board = chess.Board(start_fen)
  14.     queue = [(board, 0)]  # fronta pro BFS, obsahuje dvojice (šachovnice, hloubka)
  15.     visited = set()
  16.     visited.add(start_fen)
  17.    
  18.     while queue:
  19.         current_board, depth = queue.pop(0)
  20.         current_fen = simplify_fen_string(current_board.fen())
  21.         if current_fen == goal_fen:
  22.             return depth
  23.        
  24.         for move in current_board.legal_moves:
  25.             current_board.push(move)
  26.             fen = simplify_fen_string(current_board.fen())
  27.             if fen not in visited:
  28.                 visited.add(fen)
  29.                 queue.append((current_board.copy(), depth + 1))
  30.             current_board.pop()
  31.    
  32.     return None  # cesta nebyla nalezena
  33.  
  34. # Zadání FEN pozic
  35. start_fen = "8/5k2/8/8/8/8/8/1KQ5 w - - 0 1"
  36. goal_fen = "8/8/5k2/8/8/4Q3/8/1K6 w - - 0 1"
  37.  
  38. # Spuštění BFS vyhledávání
  39. path_length = bfs_shortest_path(start_fen, goal_fen)
  40. print(f"Nejkratší cesta z {start_fen} do {goal_fen} je {path_length} tahů.")
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement