2024-10-03 14:19:13 +00:00
|
|
|
import os
|
|
|
|
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = '1'
|
|
|
|
|
2024-10-04 15:01:06 +00:00
|
|
|
import sys
|
|
|
|
import threading
|
|
|
|
import queue
|
2024-10-03 14:19:13 +00:00
|
|
|
import pygame
|
|
|
|
|
|
|
|
from board import ChessBoard
|
|
|
|
from config.constants import WIDTH, HEIGHT
|
2024-10-04 15:01:06 +00:00
|
|
|
import re
|
2024-10-03 14:19:13 +00:00
|
|
|
|
2024-10-04 15:01:06 +00:00
|
|
|
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.
|
2024-10-03 14:19:13 +00:00
|
|
|
try:
|
2024-10-04 15:01:06 +00:00
|
|
|
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)
|
|
|
|
|
2024-10-03 14:19:13 +00:00
|
|
|
except Exception as e:
|
|
|
|
print("Invalid input format or move!", e)
|
|
|
|
|
2024-10-04 15:01:06 +00:00
|
|
|
def input_thread(input_queue):
|
|
|
|
while True:
|
|
|
|
user_input = input("Enter move (e.g., 'Pawn from e2 to e4'): ")
|
|
|
|
input_queue.put(user_input)
|
|
|
|
|
2024-10-03 14:19:13 +00:00
|
|
|
def main():
|
|
|
|
win = pygame.display.set_mode((WIDTH, HEIGHT))
|
|
|
|
pygame.display.set_caption("Chess")
|
|
|
|
|
|
|
|
board = ChessBoard()
|
|
|
|
clock = pygame.time.Clock()
|
|
|
|
|
2024-10-04 15:01:06 +00:00
|
|
|
input_queue = queue.Queue()
|
|
|
|
|
|
|
|
# Start the input thread
|
|
|
|
threading.Thread(target=input_thread, args=(input_queue,), daemon=True).start()
|
2024-10-03 14:19:13 +00:00
|
|
|
|
|
|
|
while True:
|
|
|
|
for event in pygame.event.get():
|
|
|
|
if event.type == pygame.QUIT:
|
|
|
|
pygame.quit()
|
|
|
|
sys.exit()
|
|
|
|
|
2024-10-04 15:01:06 +00:00
|
|
|
# Process input if available
|
|
|
|
if not input_queue.empty():
|
|
|
|
user_input = input_queue.get()
|
|
|
|
handle_input(board, user_input)
|
2024-10-03 14:19:13 +00:00
|
|
|
|
|
|
|
# Draw board and pieces
|
|
|
|
board.draw(win)
|
|
|
|
pygame.display.update()
|
|
|
|
|
2024-10-04 15:01:06 +00:00
|
|
|
clock.tick(30) # Limit to 30 FPS
|
2024-10-03 14:19:13 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
print("""
|
|
|
|
This chessboard uses natural language to describe moves.
|
2024-10-04 15:01:06 +00:00
|
|
|
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'.
|
2024-10-03 14:19:13 +00:00
|
|
|
""")
|
|
|
|
main()
|
2024-10-04 15:01:06 +00:00
|
|
|
|