Advertisement
max2201111

dalsi kod PK

Jun 21st, 2024
563
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.08 KB | Science | 0 0
  1. import chess
  2. from itertools import permutations, combinations
  3.  
  4. def simplify_fen_string(fen):
  5.     parts = fen.split(' ')
  6.     simplified_fen = ' '.join(parts[:4])  # Zachováváme pouze informace o pozici
  7.     return simplified_fen
  8.  
  9. def generate_chess_positions(pieces):
  10.     all_squares = [chess.SQUARES[i] for i in range(64)]
  11.     unique_fens = set()
  12.    
  13.     for squares in combinations(all_squares, len(pieces)):
  14.         for square_perm in permutations(squares):
  15.             board = chess.Board(None)
  16.             board.clear_board()
  17.             for piece, square in zip(pieces, square_perm):
  18.                 board.set_piece_at(square, chess.Piece.from_symbol(piece))
  19.            
  20.             # Kontrola platnosti pro tah bílého
  21.             board.turn = chess.WHITE
  22.             if board.is_valid() or board.is_checkmate():
  23.                 unique_fens.add(simplify_fen_string(board.fen()))
  24.  
  25.             # Kontrola platnosti pro tah černého
  26.             board.turn = chess.BLACK
  27.             if board.is_valid() or board.is_checkmate():
  28.                 unique_fens.add(simplify_fen_string(board.fen()))
  29.    
  30.     return unique_fens
  31.  
  32. # Generování všech možných pozic pro zadané figury
  33. initial_pieces = ['K', 'k', 'Q']
  34. unique_positions = generate_chess_positions(initial_pieces)
  35.  
  36. # Startovní pozice
  37. start_fen = "6k1/8/5Q2/6K1/8/8/8/8 w - - 0 1"
  38. POZ = {1: start_fen}
  39. AR = {start_fen: {'used': None}}
  40. N = 1
  41. M = 0
  42.  
  43. while M < N:
  44.     M += 1
  45.     current_fen = POZ[M]
  46.     board = chess.Board(current_fen)
  47.    
  48.     if AR[current_fen]['used'] is None:
  49.         AR[current_fen]['used'] = 1
  50.         for move in board.legal_moves:
  51.             board.push(move)
  52.             POZ2 = board.fen()
  53.             simplified_POZ2 = simplify_fen_string(POZ2)
  54.            
  55.             if simplified_POZ2 not in AR:
  56.                 AR[simplified_POZ2] = {'used': None}
  57.                
  58.             if AR[simplified_POZ2]['used'] is None:
  59.                 N += 1
  60.                 POZ[N] = simplified_POZ2
  61.            
  62.             board.pop()  # Vrátíme tah zpět
  63.  
  64. print(f"Počet pozic je {N}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement