Add positions.py

This commit is contained in:
Mathematical Michael 2023-09-19 03:26:08 -06:00
parent bc8474ec9b
commit 5edecb4b2b

28
positions.py Normal file
View File

@ -0,0 +1,28 @@
import chess
import chess.engine
# Initialize Stockfish engine
stockfish = chess.engine.SimpleEngine.popen_uci("./stockfish")
# Sample FEN for board position; you can replace this with reading from a file
# This FEN represents the starting position of a chess game
fen_string = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
board = chess.Board(fen_string)
# Placeholder: Uncomment the lines below if you'd like to read the FEN from a file
# with open("path_to_fen_file.txt", "r") as f:
# fen_string = f.readline().strip()
# board = chess.Board(fen_string)
# Iterate over all squares and find legal moves for each piece
for square in chess.SQUARES:
piece = board.piece_at(square)
if piece:
# Get legal moves for the current piece
legal_moves = [move for move in board.legal_moves if move.from_square == square]
target_squares = [move.to_square for move in legal_moves]
print(f"{piece.symbol()} at {chess.square_name(square)} can move to: {[chess.square_name(ts) for ts in target_squares]}")
# Don't forget to quit the engine when done
stockfish.quit()