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 = {simplify_fen_string(start_fen): {'used': None, 'to_end': None}}
- N = 1
- M = 0
- while M < N:
- M += 1
- current_fen = POZ[M]
- board = chess.Board(current_fen)
- simplified_current_fen = simplify_fen_string(current_fen)
- if AR[simplified_current_fen]['used'] is None:
- AR[simplified_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:
- to_end = AR[simplified_current_fen]['to_end']
- if board.is_checkmate():
- to_end = 1
- elif to_end is not None:
- to_end += 1
- AR[simplified_POZ2] = {'used': None, 'to_end': to_end}
- if AR[simplified_POZ2]['used'] is None:
- N += 1
- POZ[N] = simplified_POZ2
- board.pop() # Vrátíme tah zpět
- # Print the results
- print(f"Počet pozic je {N}")
- for i in range(1, N+1):
- fen = POZ[i]
- simplified_fen = simplify_fen_string(fen)
- used = AR[simplified_fen]['used']
- to_end = AR[simplified_fen]['to_end']
- if to_end != None:
- print(f"{i}: {fen}, used: {used}, to_end: {to_end}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement