lyceum-env/clubs/chess/game.py

51 lines
1.4 KiB
Python

import os
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = '1'
import pygame
from board import ChessBoard
from config.constants import WIDTH, HEIGHT
# Handle input like "<Piece Name> to <square>"
def handle_input(board, input_str, turn_counter):
try:
piece, square = input_str.lower().split()
piece_name = f"{piece}"
board.move_piece(piece_name, square, turn_counter)
except Exception as e:
print("Invalid input format or move!", e)
# Main game loop
def main():
win = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Chess")
board = ChessBoard()
clock = pygame.time.Clock()
turn_counter = 0
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Take input from the user (replace this with RL agent input in future)
user_input = input("Enter move (e.g., 'Pawn e4'): ")
handle_input(board, user_input, turn_counter)
turn_counter += 1
# Draw board and pieces
board.draw(win)
pygame.display.update()
# clock.tick(30)
if __name__ == "__main__":
print("""
This chessboard uses natural language to describe moves.
Currently it supports stating actions as '<Piece Name> <square>' (ex Pawn e4).
There are plans to broaden this if need be, but for now it works ok-ish.
""")
main()