Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- def generate():
- # Step 1: Place bishops on opposite colors
- light_squares = [0, 2, 4, 6]
- dark_squares = [1, 3, 5, 7]
- board = [''] * 8
- board[random.choice(light_squares)] = 'B'
- board[random.choice(dark_squares)] = 'B'
- # Step 2: Place a random major piece (Queen, Empress, Princess)
- major_pieces = ['Q', 'C', 'A'] # using Chancellor/Archbishop notation to avoid P confusion
- remaining_squares = [i for i, piece in enumerate(board) if piece == '']
- board[random.choice(remaining_squares)] = random.choice(major_pieces)
- # Step 3: Place the knights
- remaining_squares = [i for i, piece in enumerate(board) if piece == '']
- board[random.choice(remaining_squares)] = 'N'
- remaining_squares = [i for i, piece in enumerate(board) if piece == '']
- board[random.choice(remaining_squares)] = 'N'
- # Step 4: Place Rook-King-Rook with King between the Rooks
- remaining_squares = [i for i, piece in enumerate(board) if piece == '']
- remaining_squares.sort() # Sort positions to maintain order
- board[remaining_squares[0]] = 'R' # Place first Rook
- board[remaining_squares[1]] = 'K' # Place King
- board[remaining_squares[2]] = 'R' # Place second Rook
- return ''.join(board)
- # Generate a random position
- random_start = generate()
- fen_position = f"{random_start.lower()}/pppppppp/8/8/8/8/PPPPPPPP/{random_start.upper()} w KQkq - 0 1"
- # Write to a UTF-8 encoded file
- with open("random_position.fen", "w", encoding="utf-8") as file:
- file.write(fen_position + "\n")
- # print(f"FEN position saved to random_position.fen: {fen_position}")
- print(f"Created random_position.fen with array: {random_start}")
Add Comment
Please, Sign In to add comment