Building a Chess Game with AI

chess
AI
typescript
react
game-development

11/30/2024


843 words · 5 min read

Share this post:


Export:

Building a Chess Game with AI

After building the 2048 game and taking it to very well polished in about 18 hours, I wanted to get chess up and rolling again.

Forever I have had a variation of chess in my mind where there are trees, ponds, and boulders in the way to make room for new piece types that have knight-like ability to get through these barriers. My good friend Nimai Malle has always wanted to polish a chess variation where each piece had its own move-timer and could move as soon as its move timer was ready. (Makes me think that it would be cool to have every piece start at a random energy value in their timer for an additional way to shake up the opening theory of a game!) There are countless other variations of chess and so I want to build my own client/server chess site that is very well featured - why? Mostly for fun, and chess is a material hobby of mine, but also maybe I can have fun and find a couple of minor commercial variations that I could release.

Chess Cover

Chess


The AI Revolution in Chess

But there are other meta-missions I am pursuing as well - one big theme of this site is what can a solo creator create in the age of these great AI tools!? Or perhaps if not solo very small teams?

Second, chess is the great foundational platform for AI research going back 5 decades. I am not going to stop and do a history on the AI chess engines - that is what google used to be for, but now your favorite LLM can do a tailored summary just perfect. Instead my direct interest is leveraging not only the great LLM / diffusion model technology, but also the incredible advances in zero-knowledge reinforcement learning where an AI can learn to "win" at any "game" where the board state can be measured and a reward function defined.

So I want my own test bed to try out my notions of blending natural language, diffusion models, reinforcement learning into elaborately orchestrated agents.

The One Shot Chess

Notice from the time stamps last night that at 9:26pm I merged 2048 into main as it was in a good enough place for now, and then 9 minutes and 33 seconds later where I have my first commit on the chess branch with the comment that I have a working chess board:

Nine Minutes

Here was my prompt:

Okay my dear friend, we just spent the day doing polish and bug hunting on the 2048 part of this project.

I am hungry for something new!

I want to play chess!

Are there any open source chess engines I could use to drive a chess board here in react / next / sstv3?

Probably use these icons for the chess pieces?

import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faChessKing, faChessQueen, faChessRook, faChessBishop, faChessKnight, faChessPawn } from "@fortawesome/free-solid-svg-icons";

How much of a chess game can you pull together in one-shot?

TIP: I have found that when working with LLMs both from ChatGPT and more so from Anthropic that I seem to get better responses - meaning deeper, more robust and more "adventurous" if I am as enthusiastic as my good friend Mike Fincke is 24/7.

In one-shot it settled on:

  • using stockfish.js as the AI engine
  • leveraging chess.js to handle move validation, game state and the FEN/PGN support
  • and then went ahead and provided the core UI in typescript in the same style of the rest of my application e.g. JOY UI
One Shot Chess Implementation

This was already amazing right here, I knew stockfish was available but I did not know about the chess.js library before and the last time I tried to pull together this project I got bogged down trying to reinvent those wheels.

Development Progress

The features came fast and furious. Here's what the initial board looked like after just 20 minutes:

20 Minutes Progress

By the time I had finished the basic implementation, we had a fully featured chess interface :

Fully Featured Chess Interface

To AI or Not to AI?

I wanted to get Chess into a v.1 state so I can push it on to my site and start playing with it. But I was mid-way through the stockfish integration and thought I should probably wait. So make me a software switch to AI or not to AI? - One shot and here is a screen shot from Cursor.AI where I play Picard and "Make it so..." is my hard work!

AI Configuration Options

Code Quality

I'm particularly proud of the code quality. Despite the rapid development, we maintained clean code standards:

Initial Lint Errors

After a quick cleanup session:

Clean Code After Fixes

Future Roadmap

I've mapped out an ambitious roadmap for where I want to take this project... in comments generated by AI in an earlier session! To then get these displayed, why not have AI generate the markdown for the roadmap?

Project Roadmap

Stockfish Integration

The next big step was getting Stockfish running. Here's a peek at the initial integration:

Stockfish Initial Setup

And some promising progress:

Stockfish Advanced Configuration

Getting stockfish up and running will be another post!



Subscribe to the Newsletter

Get notified when I publish new blog posts about game development, AI, entrepreneurship, and technology. No spam, unsubscribe anytime.

By subscribing, you agree to receive emails from Erik Bethke. You can unsubscribe at any time.

Comments

Loading comments...

Comments are powered by Giscus. You'll need a GitHub account to comment.