Advertisement
max2201111

good AR pomale

Apr 25th, 2024
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.21 KB | Science | 0 0
  1. import chess
  2.  
  3. def simplify_fen_string(fen):
  4.     parts = fen.split(' ')
  5.     simplified_fen = ' '.join(parts[:4])  # Zachováváme pouze informace o pozici
  6.     return simplified_fen
  7.  
  8. def generate_positions(initial_fen):
  9.     board = chess.Board(initial_fen)
  10.     queue = [(board, None, 0)]  # Přidáváme hloubku jako třetí prvek v tuple
  11.     AR = {}
  12.     depths_reached = []  # Seznam dosažených hloubek
  13.     positions_visited = 0  # Počet projitých pozic
  14.  
  15.     target_position_fen = "8/8/4k3/8/8/2K5/8/7Q"  # Cílová pozice
  16.     target_position_reached = False
  17.  
  18.     while queue:
  19.         current_board, parent_fen, depth = queue.pop(0)
  20.         current_simplified_fen = simplify_fen_string(current_board.fen())
  21.         positions_visited += 1
  22.  
  23.         if target_position_fen in current_simplified_fen and not target_position_reached:
  24.             print("Cílová pozice dosažena!")
  25.             target_position_reached = False
  26.  
  27.         if positions_visited % 100000 == 0:  # Vypisujeme po každých 100 000 projitých pozicích
  28.             print(f"Počet projitých pozic: {positions_visited}")
  29.  
  30.         if current_simplified_fen not in AR:
  31.             AR[current_simplified_fen] = {
  32.                 'fen': current_simplified_fen,
  33.                 'parent': parent_fen,
  34.                 'color': chess.WHITE if 'w' in current_board.fen() else chess.BLACK,
  35.                 'children': [],
  36.                 'result': None,
  37.                 'sequence': [],
  38.                 'to_end': None,
  39.                 'depth_first_reached': depth  # Přidáváme hloubku, ve které byla pozice poprvé dosažena
  40.             }
  41.             # Pokud jsme dosáhli nové hloubky, vytiskneme to
  42.             if depth not in depths_reached:
  43.                 depths_reached.append(depth)
  44.                 print(f"Hloubka {depth} poprvé dosažena")
  45.  
  46.             # Kontrola matu nebo remízy
  47.             if current_board.is_checkmate():
  48.                 AR[current_simplified_fen]['result'] = 1000 if current_board.turn == chess.WHITE else -1000
  49.             elif current_board.is_stalemate() or current_board.is_insufficient_material():
  50.                 AR[current_simplified_fen]['result'] = 0
  51.  
  52.             # Generujeme tahy pouze pokud nejsme v koncové pozici
  53.             if AR[current_simplified_fen]['result'] is None:
  54.                 unique_moves = set()  # Uchováváme unikátní tahy
  55.                 for move in current_board.legal_moves:
  56.                     if move not in unique_moves:  # Pokud tah ještě nebyl proveden
  57.                         unique_moves.add(move)  # Přidáme ho do seznamu unikátních tahů
  58.                         current_board.push(move)
  59.                         queue.append((current_board.copy(), current_simplified_fen, depth + 1))  # Zvyšujeme hloubku pro potomky
  60.                         current_board.pop()
  61.  
  62.     return AR, positions_visited
  63.  
  64. # Inicializace šachové desky
  65. initial_fen = "1k6/3K1Q2/8/8/8/8/8/8 w - - 0 1"
  66. ar_dict, positions_visited = generate_positions(initial_fen)
  67.  
  68. # Výpis výsledků pro počáteční FEN
  69. initial_simplified_fen = simplify_fen_string(initial_fen)
  70. print(f"Slovník pro initial_fen: {ar_dict[initial_simplified_fen]}")
  71. print(f"Počet projitých pozic: {positions_visited}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement