Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import chess
- from itertools import permutations, combinations
- def simplify_fen_string(fen):
- parts = fen.split(' ')
- simplified_fen = ' '.join(parts[:4]) # Zachováváme pouze informace o pozici
- return simplified_fen
- def generate_chess_positions(pieces):
- all_squares = [chess.SQUARES[i] for i in range(64)]
- unique_fens = set()
- for squares in combinations(all_squares, len(pieces)):
- for square_perm in permutations(squares):
- board = chess.Board(None)
- board.clear_board()
- for piece, square in zip(pieces, square_perm):
- board.set_piece_at(square, chess.Piece.from_symbol(piece))
- # Kontrola platnosti pro tah bílého
- board.turn = chess.WHITE
- if board.is_valid() or board.is_checkmate():
- unique_fens.add(simplify_fen_string(board.fen()))
- # Kontrola platnosti pro tah černého
- board.turn = chess.BLACK
- if board.is_valid() or board.is_checkmate():
- unique_fens.add(simplify_fen_string(board.fen()))
- return unique_fens
- # Generování všech možných pozic pro zadané figury
- initial_pieces = ['K', 'k', 'Q']
- unique_positions = generate_chess_positions(initial_pieces)
- # Startovní pozice
- start_fen = "6k1/8/5Q2/6K1/8/8/8/8 w - - 0 1"
- POZ = {1: start_fen}
- AR = {start_fen: {'used': None}}
- N = 1
- M = 0
- while M < N:
- M += 1
- current_fen = POZ[M]
- board = chess.Board(current_fen)
- if AR[current_fen]['used'] is None:
- AR[current_fen]['used'] = 1
- for move in board.legal_moves:
- board.push(move)
- POZ2 = board.fen()
- simplified_POZ2 = simplify_fen_string(POZ2)
- if simplified_POZ2 not in AR:
- AR[simplified_POZ2] = {'used': None}
- if AR[simplified_POZ2]['used'] is None:
- N += 1
- POZ[N] = simplified_POZ2
- board.pop() # Vrátíme tah zpět
- print(f"Počet pozic je {N}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement