Hearts agent
This is a program that plays Hearts in the browser. It was part of my research on agents that operate software through its screen instead of through an API.
The game has no API, so the agent works like a person would. It reads the page, works out whose turn it is, picks a card and clicks it.
How it works
- Reading
- Selenium pulls card elements from the page. Suit and rank come from the CSS class names. Cards in your hand and cards on the table are told apart by their position on screen.
- Phase
- A small state machine tracks starting, passing, waiting, playing and game end, so the agent only acts when it should.
- Choosing
- The game state and a short set of Hearts rules go into a prompt. GPT-4 replies with a single card.
- Fallback
- If the reply is not a legal card, a simple rule picks one instead.
- Acting
- The chosen card is clicked with Selenium's ActionChains.
The prompt
You are playing Hearts. Your hand, the cards played this trick, completed tricks, remaining cards. Rules: avoid points, dump the Queen of Spades, follow suit when you can. Reply with one card only, e.g. h2 or s14.
Stack
Python, Selenium, ChromeDriver, OpenAI GPT-4.