I Built a Chess AI That Plays Like Me—Here’s How It (Almost) Failed
Do you like playing chess? With the chess boom that has been happening throughout the last few years, many new people got into it including me. Have you ever played against those celebrity bots on Chess.com and wondered, why are there limited amount of them at a very limited time? I mean, can't we just built our own chess AI? Stockfish and other engines are much better at chess than us anyway. If Chess.com can adjust it to play like someone, why not anyone? That's what I've thought too, and my main drive to build DeezChess! First plan, first failure I just learned Unity, so why not build a chess game via that. You can even build it for web via WebGL and publish on itch.io. Plus, I'm sure most chess and programming fans have watched this chess bot video by Sebastian Lague and got interested in building their own bot. I mean how hard could it be?(famous last words) The plan is simple, use Unity as front end and C++ as backend cause it's fast (definitely not a good reason) and I can connect it with Unity via a DLL. Could be an awesome learning experience right? Well, yes until you realize C++ DLL won't build for WebGL! But now comes the other problem, how to make the core 'AI plays like me' to work? At first, the first thought that came to mind was to do something like what fine tuning Leela Chess Zero, but since I am still an unemployed student with no GPU, using Google Colab for experimenting is just not worth it. Well, I'll just use stockfish and fine tune it to play according to an opening book and certain strength. But stockfish.exe just simply, again, won't build for WebGL! Screw it, let's just get it working So in the end, I settled with using a FastAPI backend that will handle all the legal move generation as well as host the stockfish engine. It's a simple design that's definitely not very scalable, but it got the job done. The final flow is something like this: User upload a PGN file as well as the name of the player it want to mimic. The FastAPI server respond with two files, a .bin file for the opening book, and a .json config file which contains information that we can use to 'fine tune' stockfish, such as estimated elo and contempt score. Unity download those files and store it as persistent data path Every time a bot move need to be made, Unity make a request with those 2 files, and get a uci move string as a response. The server will reply with either the move found in the opening book, or prompt stockfish using given configs. Holy crap it actually works?! After months of implementing it (probably could have been shorter but I'm also busy with exams and studies) and countless hours of debugging, I actually managed to get it working on both WebGL and normal Windows unity build! I then containerized the backend python code using Docker and deploy it on render.com because of how fast and easy it is. I also build everything for WebGL for the last time, upload it on itch.io, and a huge sense of accomplishment when I can actually play against my own bot! Lessons, regrets, and what's next The biggest lesson for me was definitely that your plan could, and definitely would change. Your final product would be vastly different from what you had initially in mind, especially if it's your first few big projects. Just reiterate, change what you can, and actually get it to work first. No need to use fancy technology and tools that you don't understand (like I did for C++) unless you're absolutely sure you need it. In the end, I did learn a lot and enjoyed the journey. After 6 months, I think it's time to move on. There are so many things I could improve, but sometimes, you just have to say 'it's done' and move on to the next challenge. Anyways, hope you enjoy this article, learnt something, or at least entertained. Do let me know in the comments what you think of this project, any advice you want to give!

Do you like playing chess? With the chess boom that has been happening throughout the last few years, many new people got into it including me. Have you ever played against those celebrity bots on Chess.com and wondered, why are there limited amount of them at a very limited time? I mean, can't we just built our own chess AI? Stockfish and other engines are much better at chess than us anyway. If Chess.com can adjust it to play like someone, why not anyone? That's what I've thought too, and my main drive to build DeezChess!
First plan, first failure
I just learned Unity, so why not build a chess game via that. You can even build it for web via WebGL and publish on itch.io. Plus, I'm sure most chess and programming fans have watched this chess bot video by Sebastian Lague and got interested in building their own bot. I mean how hard could it be?(famous last words)
The plan is simple, use Unity as front end and C++ as backend cause it's fast (definitely not a good reason) and I can connect it with Unity via a DLL. Could be an awesome learning experience right? Well, yes until you realize C++ DLL won't build for WebGL!
But now comes the other problem, how to make the core 'AI plays like me' to work? At first, the first thought that came to mind was to do something like what fine tuning Leela Chess Zero, but since I am still an unemployed student with no GPU, using Google Colab for experimenting is just not worth it.
Well, I'll just use stockfish and fine tune it to play according to an opening book and certain strength. But stockfish.exe just simply, again, won't build for WebGL!
Screw it, let's just get it working
So in the end, I settled with using a FastAPI backend that will handle all the legal move generation as well as host the stockfish engine. It's a simple design that's definitely not very scalable, but it got the job done. The final flow is something like this:
- User upload a PGN file as well as the name of the player it want to mimic.
- The FastAPI server respond with two files, a .bin file for the opening book, and a .json config file which contains information that we can use to 'fine tune' stockfish, such as estimated elo and contempt score.
- Unity download those files and store it as persistent data path
- Every time a bot move need to be made, Unity make a request with those 2 files, and get a uci move string as a response.
- The server will reply with either the move found in the opening book, or prompt stockfish using given configs.
Holy crap it actually works?!
After months of implementing it (probably could have been shorter but I'm also busy with exams and studies) and countless hours of debugging, I actually managed to get it working on both WebGL and normal Windows unity build!
I then containerized the backend python code using Docker and deploy it on render.com because of how fast and easy it is. I also build everything for WebGL for the last time, upload it on itch.io, and a huge sense of accomplishment when I can actually play against my own bot!
Lessons, regrets, and what's next
The biggest lesson for me was definitely that your plan could, and definitely would change. Your final product would be vastly different from what you had initially in mind, especially if it's your first few big projects. Just reiterate, change what you can, and actually get it to work first. No need to use fancy technology and tools that you don't understand (like I did for C++) unless you're absolutely sure you need it.
In the end, I did learn a lot and enjoyed the journey. After 6 months, I think it's time to move on. There are so many things I could improve, but sometimes, you just have to say 'it's done' and move on to the next challenge.
Anyways, hope you enjoy this article, learnt something, or at least entertained. Do let me know in the comments what you think of this project, any advice you want to give!