Code
Build an AI Chess Engine As Fast As Possible using ChatGPT

The problem

For a while now I have wondered how much faster it would be to build a C++ application with ChatGPT than without. So I set a goal to program a C++ chess engine with an AI player as fast as possible while heavily using ChatGPT. I have done this type of project without prompt programming before, and it was tedious. In this post, I share my experience and explain why prompt programming is powerful and likely a key part of the future of software development.

The infrastructure

VS Code with a dev container open, showing devcontainer.json, a Dockerfile, and CMakeLists.txt for the chess engine
The project open in a VS Code dev container, with the dev container configuration, Dockerfile and CMake file visible.

I leveraged Docker with dev containers in Visual Studio Code, a STIG'd Red Hat Linux image from PLATFORM ONE's Iron Bank and CMake for this project. This was by far the easiest part of the project.

I started with a series of prompts telling ChatGPT that I wanted it to build a dev container in VS Code that used a STIG'd Red Hat Linux image from Iron Bank and that I needed it to generate some template files. Quickly it provided step-by-step instructions with completed devcontainer.json and Dockerfile files. I was up and running in a container in probably around 5 minutes. Next, I gave it a series of prompts telling it I wanted to leverage CMake as a build system with C++ 20. It generated the CMake files, and we chatted about how to format my directory appropriately for my needs. It generated the CMakeLists.txt files and we were quickly up and running with a Hello World example in around 10 minutes.

Throughout parts 2 and 3 I often updated this configuration simply by telling ChatGPT what I wanted to do, rather than manually doing it myself. Whenever unsure what a line of code was doing, I copied it back into ChatGPT and asked it to explain until I understood what was happening. I will caveat this by saying that I do have basic experience with Docker, dev containers, and CMake, so I wasn't going in blind. However, ChatGPT made the process seamless and simple.

First attempt: fighting with ChatGPT

In the first attempt, I started by telling ChatGPT I wanted it to generate an entire chess program in C++. However, I quickly ran into issues. The LLM generated several classes (.cpp and .h files) with a main.cpp example. However, it was clear that not all game rules were implemented as I looked through the code. It didn't have a concept of checkmate, stalemate, black-and-white pieces, player turns, a min/max solver, or even allowable moves. All it could do was move any piece to a random spot on the board. This revealed a limitation with how much of an application ChatGPT can generate.

I spent the next several hours asking it to generate as much code as possible in a given prompt to get to a full working solution. However, every time it would miss items. Within 1-2 hours I started to notice it was making poor architectural decisions that I would not have made had I programmed it myself. Combining classes in weird ways, representing the board in non-trivial ways, etc. In addition, at times there would be compiler errors. When I would copy the error into a prompt, ChatGPT usually gave a solution. However, the solution usually introduced a new bug to the functionality of the program itself.

I spent another 2 hours trying to have it rewrite large chunks of code according to my prompts. In the last hour, I noticed it was stuck calculating valid moves. As I looked through the code, which was architecturally messy by this time, I struggled to find the error in a reasonable amount of time. I asked ChatGPT to add print statements to various portions of the code so I could manually debug, however, as time went on I realized that I had reached the limit of what ChatGPT could do for me. It would take too long to debug the issue in the messy architecture. So I started fresh with a new approach.

Second attempt: symbiosis

In part 1, I tried to move as fast as possible by having ChatGPT generate and fix huge portions of the code with each prompt. Keeping the entire application context was difficult for ChatGPT. So this time, I started differently. I decided to tell ChatGPT that I was going to have it write small pieces of the application at a time, and I would architecturally design it how I wanted. If I needed to glue pieces together, I would do it manually, or I would instruct ChatGPT to do it for me.

I started small telling it I wanted a board class. Then I wanted a movement class. Then I wanted a few constraints. Then a few more constraints. At each step, I would review line-by-line what was written to ensure I understood it, and that no errors were introduced. Before too long I had a fully functioning application. I then told it I wanted to generate an AI player that solved for the best move using a min/max function with an adjustable depth. It was able to generate this piece with ease. After I instructed it to hook up the AI player, we had a fully functioning AI program.

This process only took 2-3 hours. It was significantly faster than if I had written it by hand, and also significantly faster than trying to have it generate everything with one prompt. I had found the sweet spot. There were times when I had to manually fix small bugs, but these were few and far between. For the most part, it did a decent job. It's not perfect, but it's not bad either. It's a solid starting point.

What the engine looks like

Because I designed the pieces myself, the finished program is small: a Board class, a Move class and an AIPlayer class, with main.cpp tying them together. The board is an 8 by 8 grid of squares, each holding a piece and a color. Move holds the rules of the game: the legal moves for each piece, check, checkmate, stalemate, insufficient material and threefold repetition. main.cpp reads the search depth from config.txt (it is set to 3), then loops: it checks whether the game is over, asks AIPlayer for a move, plays it, and switches sides.

Four layers of the C++ engine. main.cpp reads the search depth from config.txt and runs the game loop. It asks AIPlayer for the best move. AIPlayer uses Move to list legal moves and Board to read the squares. Move holds the rules: valid moves, check, checkmate, stalemate and repetition. Board is an 8 by 8 grid of squares.
The four parts of the engine. Each one only talks to the layer below it, which is what made it possible to have ChatGPT write one small piece at a time.

The search

AIPlayer chooses a move with a minimax search. For each legal move, it makes the move on the board, searches the opponent's replies to the configured depth, undoes the move, and keeps the move with the best value. White picks the highest value and Black picks the lowest. Every branch is searched to the full depth; the search does not prune.

A search tree of depth 3 with two moves per position. The eight leaf positions are scored with the example values 3, -1, 4, 9, 0, 2, -3 and 5. Maximizing levels take the larger child and the minimizing level takes the smaller, giving 3, 9, 2, 5 then 3 and 2, so the root value is 3 and the move leading to the left branch is chosen.
A depth-3 minimax search drawn with two moves per position. The leaf values are an example, not a real game. Scores climb from the leaves: White's levels take the larger child, Black's level takes the smaller, and the move with the best value at the top is played.

The evaluation

At the bottom of the search, evaluateBoard() adds up the material on the board. White pieces add their value and black pieces subtract it, so a positive score favors White.

Horizontal bars of the value each piece adds to the score: pawn 1, knight 3, bishop 3, rook 5, queen 9, king 1000 (its bar is cut off at 10). White pieces add their value and black pieces subtract it.
The piece values in the evaluation. The king's value of 1000 is so large that losing it outweighs anything else, so its bar is cut off to keep the others readable.

Key takeaways

  • ChatGPT is not perfect, it makes mistakes like all humans do. Don't get too lazy using it.
  • ChatGPT is significantly better at generating small chunks of code or portions of an architecture. Humans are better at maintaining the full program context.
  • ChatGPT is incredible at explaining pieces of code, concepts, or technologies in depth. When in doubt, just ask it. It will cut your learning curve dramatically.
  • I'm convinced that prompt programming will have a strong part to play in the future of software development. It is 100% worth learning how to use these tools.

The code

The code is available in the chess repository on GitHub.