Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import chess
- def simplify_fen_string(fen):
- """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."""
- parts = fen.split(' ')
- simplified_fen = ' '.join(parts[:4]) # Zachováváme informace o pozici, hráči na tahu, rošádách a en passant
- return simplified_fen
- def bfs_shortest_path(start_fen, goal_fen):
- """Vyhledává nejkratší cestu mezi dvěma FEN pozicemi pomocí BFS algoritmu."""
- start_fen = simplify_fen_string(start_fen) # Zjednodušení vstupního FEN
- goal_fen = simplify_fen_string(goal_fen) # Zjednodušení cílového FEN
- board = chess.Board(start_fen)
- queue = [(board, 0)] # fronta pro BFS, obsahuje dvojice (šachovnice, hloubka)
- visited = set()
- visited.add(start_fen)
- while queue:
- current_board, depth = queue.pop(0)
- current_fen = simplify_fen_string(current_board.fen())
- if current_fen == goal_fen:
- return depth
- for move in current_board.legal_moves:
- current_board.push(move)
- fen = simplify_fen_string(current_board.fen())
- if fen not in visited:
- visited.add(fen)
- queue.append((current_board.copy(), depth + 1))
- current_board.pop()
- return None # cesta nebyla nalezena
- # Zadání FEN pozic
- start_fen = "8/5k2/8/8/8/8/8/1KQ5 w - - 0 1"
- goal_fen = "8/8/5k2/8/8/4Q3/8/1K6 w - - 0 1"
- # Spuštění BFS vyhledávání
- path_length = bfs_shortest_path(start_fen, goal_fen)
- print(f"Nejkratší cesta z {start_fen} do {goal_fen} je {path_length} tahů.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement