Advertisement
max2201111

petr OK 500

Jun 21st, 2024
676
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.09 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.                 AR[simplified_POZ2] = {'used': None, 'to_end': None}
  58.                
  59.             if AR[simplified_POZ2]['used'] is None:
  60.                 N += 1
  61.                 POZ[N] = simplified_POZ2
  62.            
  63.             board.pop()  # Vrátíme tah zpět
  64.  
  65. # Přidání kontroly pro mat, remízu a výchozí hodnotu
  66. for i in range(1, N + 1):
  67.     current_fen = POZ[i]
  68.     board = chess.Board(current_fen)
  69.     simplified_current_fen = simplify_fen_string(current_fen)
  70.    
  71.     if board.is_checkmate():
  72.         AR[simplified_current_fen]['to_end'] = -1000
  73.     elif board.is_stalemate() or board.is_insufficient_material() or board.is_seventyfive_moves() or board.is_fivefold_repetition():
  74.         AR[simplified_current_fen]['to_end'] = 0
  75.     else:
  76.         AR[simplified_current_fen]['to_end'] = 500
  77.  
  78. F = 0
  79.  
  80. # Print the results
  81. print(f"Počet pozic je {N}")
  82. for i in range(1, N + 1):
  83.     F = F+1
  84.     fen = POZ[i]
  85.     simplified_fen = simplify_fen_string(fen)
  86.     used = AR[simplified_fen]['used']
  87.     to_end = AR[simplified_fen]['to_end']
  88.     if to_end != None and to_end != 500 and to_end !=0 and F<10000:
  89.         print(f"{i}: {fen}, used: {used}, to_end: {to_end}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement