Chess Engine Journey Ch. 0: Let's build a Chess AI!
Tags: go, chess
Published: 2021-09-26 21:02:52

---
title: Chess Engine Journey Ch. 0: Let's build a Chess AI!
published: true
date: 2021-09-26 20:43:16 UTC
tags: go,chess
canonical_url: https://aghdom.eu/posts/chess-engine-ch.0/
---

<p>So at the end of last year, I started getting more interested in chess.</p> <p>I began playing online, studying puzzles, watching videos, and generally getting engaged in the chess space. Because of this, my recommendations on YouTube started to feature quite a lot of chess related content. That&rsquo;s how I stumbled upon <a href="https://www.youtube.com/watch?v=U4ogK0MIzqk">a video</a> by <em>Sebastian Lague</em>, in which he tries to make a &ldquo;Chess AI&rdquo; to play against.</p> <p>I thought that it looked like a fun thing to try, and since I was looking for a coding side-project for a while now, I decided to give it a shot. And while I was at it, I also decided to document my journey!</p> <h2 id="surveying-the-landscape">Surveying the Landscape</h2> <p>Right, so we want to build a chess engine. But how does one actually go about doing it?</p> <p>I began by searching for resources that already exist on the topic of chess programs. Early on, I found the <a href="https://www.chessprogramming.org/Main_Page">Chess Programming Wiki</a> which proved to be a great source of information, as it covered all the important principal topics, a lot of the different ways of optimizing Chess algorithms, as well as provided a great <a href="https://www.chessprogramming.org/Getting_Started">Getting Started</a> jump-off point for anyone hoping to dive into chess programming. It also has a very extensive <a href="https://www.chessprogramming.org/Recommended_Reading">Recommended Reading</a> section, which saved me a lot of time during this phase.</p> <h3 id="chess-program-vs-chess-engine">Chess Program vs. Chess Engine</h3> <p>The first practical hurdle I faced was when deciding how to go about actually implementing the program itself. My first naive approach was that I had to obviously make the whole thing from scratch as a standalone program. I contemplated what game engine or framework would be best, but I was a bit unsatisfied since none of the better maintained frameworks and engines supported any of the languages close to my heart, like Go, or at least Python.</p> <p>Wanting to develop in my preferred language of choice, I came to the idea of decoupling the graphical interface and the engine which would be calculating the best moves, essentially splitting the single program into frontend and backend. I would write the interface in something like Unity (I still might do that in the future as a separate project) and the engine in Go.</p> <p>Luckily (and probably obviously) I wasn&rsquo;t the first person to come up with this idea.</p> <h3 id="protocols--interfaces">Protocols &amp; Interfaces</h3> <p>So turns out, people (as is more often than not the case) have already dealt with a similar issue and found a solution. There are already numerous options when it comes to Chess programs (or GUIs for our purposes) which support custom chess engines. (A nice <a href="https://www.chessprogramming.org/GUI">list</a> can be found on the <a href="https://www.chessprogramming.org/Main_Page">wiki</a>) They do so by utilizing a couple of standardized protocols to communicate with the engine code.</p> <p>The two most commonly used protocols I found were <em>Chess Engine Communication Protocol</em> (CECP), often simply dubbed <em>xboard</em> after the program which introduced it, and the <em>Universal Chess Interface</em> (UCI).</p> <h2 id="conclusion">Conclusion</h2> <p>After some time pondering this topic, I finally decided to only implement a <em>chess engine</em> for now, as opposed to a complete chess program (Which gives me the opportunity for a second project in the future maybe&hellip;), as it will likely be faster and will allow me to further my coding skills with my language of choice, Go.</p> <p>I also decided to go with UCI as the communication protocol for this project, as it seemed like the more straight-forward to implement (smaller number of individual commands, using <code>stdin</code> and <code>stdout</code> as its communication channels), as well as the better supported by a wider number of graphical interfaces (CECP slightly lacking in this regard).</p> <h2 id="next-steps">Next Steps</h2> <p>So, after getting all of this &ldquo;boring&rdquo; pre-production out of the way, I feel confident getting to work on this project. By the time I will write up another chapter in this series, I would like to have the following done:</p> <ul> <li>Think of a name for the engine</li> <li>Setup a repository for the project</li> <li>Prepare (at least stub) handlers for all the UCI commands</li> <li>Implement the Board Representation part of the chess engine</li> </ul> <p>Seems like I have my work cut out for me, and I can&rsquo;t wait to roll up my sleeves and getting my hands dirty by actually starting to implement this project. Hopefully I&rsquo;ll have something worth sharing again soon.</p> <p>In the meantime, thank you for reading the first part in this series! I hope that you enjoyed it, or that it at least gave you a brief look into the problem space of chess programs and provided you with enough information and further reading to be able to start a similar project of your own!</p> <p>Until the next chapter, thank you again for your time and take care!</p>