Guest User

Chess2880

a guest
Dec 7th, 2024
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.68 KB | None | 0 0
  1. import random
  2.  
  3. def generate():
  4.     # Step 1: Place bishops on opposite colors
  5.     light_squares = [0, 2, 4, 6]
  6.     dark_squares = [1, 3, 5, 7]
  7.     board = [''] * 8
  8.  
  9.     board[random.choice(light_squares)] = 'B'
  10.     board[random.choice(dark_squares)] = 'B'
  11.  
  12.     # Step 2: Place a random major piece (Queen, Empress, Princess)
  13.     major_pieces = ['Q', 'C', 'A']  # using Chancellor/Archbishop notation to avoid P confusion
  14.     remaining_squares = [i for i, piece in enumerate(board) if piece == '']
  15.     board[random.choice(remaining_squares)] = random.choice(major_pieces)
  16.  
  17.     # Step 3: Place the knights
  18.     remaining_squares = [i for i, piece in enumerate(board) if piece == '']
  19.     board[random.choice(remaining_squares)] = 'N'
  20.     remaining_squares = [i for i, piece in enumerate(board) if piece == '']
  21.     board[random.choice(remaining_squares)] = 'N'
  22.  
  23.     # Step 4: Place Rook-King-Rook with King between the Rooks
  24.     remaining_squares = [i for i, piece in enumerate(board) if piece == '']
  25.     remaining_squares.sort()  # Sort positions to maintain order
  26.     board[remaining_squares[0]] = 'R'  # Place first Rook
  27.     board[remaining_squares[1]] = 'K'  # Place King
  28.     board[remaining_squares[2]] = 'R'  # Place second Rook
  29.  
  30.     return ''.join(board)
  31.  
  32. # Generate a random position
  33. random_start = generate()
  34. fen_position = f"{random_start.lower()}/pppppppp/8/8/8/8/PPPPPPPP/{random_start.upper()} w KQkq - 0 1"
  35.  
  36. # Write to a UTF-8 encoded file
  37. with open("random_position.fen", "w", encoding="utf-8") as file:
  38.     file.write(fen_position + "\n")
  39.  
  40. # print(f"FEN position saved to random_position.fen: {fen_position}")
  41. print(f"Created random_position.fen with array: {random_start}")
Add Comment
Please, Sign In to add comment