Advertisement
max2201111

kod Petr K last2

Jun 21st, 2024
608
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.70 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 = {simplify_fen_string(start_fen): {'used': None, 'to_end': 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.     simplified_current_fen = simplify_fen_string(current_fen)
  48.    
  49.     if AR[simplified_current_fen]['used'] is None:
  50.         AR[simplified_current_fen]['used'] = 1
  51.         for move in board.legal_moves:
  52.             board.push(move)
  53.             POZ2 = board.fen()
  54.             simplified_POZ2 = simplify_fen_string(POZ2)
  55.            
  56.             if simplified_POZ2 not in AR:
  57.                 to_end = AR[simplified_current_fen]['to_end']
  58.                 if board.is_checkmate():
  59.                     to_end = 1
  60.                 elif to_end is not None:
  61.                     to_end += 1
  62.                 AR[simplified_POZ2] = {'used': None, 'to_end': to_end}
  63.                
  64.             if AR[simplified_POZ2]['used'] is None:
  65.                 N += 1
  66.                 POZ[N] = simplified_POZ2
  67.            
  68.             board.pop()  # Vrátíme tah zpět
  69.  
  70. # Print the results
  71. print(f"Počet pozic je {N}")
  72. for i in range(1, N+1):
  73.     fen = POZ[i]
  74.     simplified_fen = simplify_fen_string(fen)
  75.     used = AR[simplified_fen]['used']
  76.     to_end = AR[simplified_fen]['to_end']
  77.     if to_end != None:
  78.         print(f"{i}: {fen}, used: {used}, to_end: {to_end}")
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement