93 lines
2.6 KiB
Python
93 lines
2.6 KiB
Python
import os
|
|
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = '1'
|
|
|
|
import sys
|
|
import threading
|
|
import queue
|
|
import pygame
|
|
|
|
from board import ChessBoard
|
|
from config.constants import WIDTH, HEIGHT
|
|
import re
|
|
|
|
def handle_input(board, input_str):
|
|
# I wonder if I could also use NLP for this.
|
|
# Rn I can't really handle the 'I capture your rook with my queen' queries.
|
|
try:
|
|
input_str = input_str.lower()
|
|
piece_names = ['pawn', 'rook', 'knight', 'bishop', 'queen', 'king']
|
|
|
|
# Find squares in the input (e.g. e2, d4)
|
|
squares = re.findall(r'\b[a-h][1-8]\b', input_str)
|
|
|
|
# Find the piece name
|
|
piece_type = None
|
|
for piece in piece_names:
|
|
if piece in input_str:
|
|
piece_type = piece
|
|
break
|
|
|
|
if not piece_type:
|
|
# This would be part of the reward scoring
|
|
print("Could not identify the piece type.")
|
|
return False
|
|
|
|
if len(squares) < 2:
|
|
# same
|
|
print("Could not identify both start and destination squares.")
|
|
return False
|
|
|
|
start_square = squares[0]
|
|
dest_square = squares[1]
|
|
|
|
if not board.move_piece(piece_type, start_square, dest_square):
|
|
# This checks that the query wasn't smth like "Rook to e1 from e8"
|
|
board.move_piece(piece_type, dest_square, start_square)
|
|
|
|
except Exception as e:
|
|
print("Invalid input format or move!", e)
|
|
|
|
def input_thread(input_queue):
|
|
while True:
|
|
user_input = input("Enter move (e.g., 'Pawn from e2 to e4'): ")
|
|
input_queue.put(user_input)
|
|
|
|
def main():
|
|
win = pygame.display.set_mode((WIDTH, HEIGHT))
|
|
pygame.display.set_caption("Chess")
|
|
|
|
board = ChessBoard()
|
|
clock = pygame.time.Clock()
|
|
|
|
input_queue = queue.Queue()
|
|
|
|
# Start the input thread
|
|
threading.Thread(target=input_thread, args=(input_queue,), daemon=True).start()
|
|
|
|
while True:
|
|
for event in pygame.event.get():
|
|
if event.type == pygame.QUIT:
|
|
pygame.quit()
|
|
sys.exit()
|
|
|
|
# Process input if available
|
|
if not input_queue.empty():
|
|
user_input = input_queue.get()
|
|
handle_input(board, user_input)
|
|
|
|
# Draw board and pieces
|
|
board.draw(win)
|
|
pygame.display.update()
|
|
|
|
clock.tick(30) # Limit to 30 FPS
|
|
|
|
if __name__ == "__main__":
|
|
print("""
|
|
This chessboard uses natural language to describe moves.
|
|
You can enter moves like
|
|
'Pawn e2 e4',
|
|
'Move knight from b1 to c3', or
|
|
'I want to move my bishop from f1 to c4'.
|
|
""")
|
|
main()
|
|
|