# Building a strong Julia chess engine

**URL:** https://discourse.julialang.org/t/building-a-strong-julia-chess-engine/101726
**Category:** General Usage
**Tags:** games
**Created:** [July 18, 2023, 11:48am UTC](https://discourse.julialang.org/t/building-a-strong-julia-chess-engine/101726 "2023-07-18T11:48:01Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![Tarny\_GG\_Channie](https://avatars.discourse-cdn.com/v4/letter/t/3bc359/32.png) [@Tarny\_GG\_Channie](https://discourse.julialang.org/u/Tarny_GG_Channie)
#### Post date: [July 18, 2023, 11:48am UTC](https://discourse.julialang.org/t/building-a-strong-julia-chess-engine/101726/1 "2023-07-18T11:48:01Z")

</div>

Should we build a strong chess engine? How do I go about it? I need to get started by interfacing with UCI, but how? Then, what next? Any idea to leverage Julia’s strength? For example, I’m thinking of generating code using Julia’s metaprogramming to generate some parts of the engine.

---

<div class="post-metadata">

### Author: ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)
#### Post date: [July 18, 2023, 11:51am UTC](https://discourse.julialang.org/t/building-a-strong-julia-chess-engine/101726/2 "2023-07-18T11:51:26Z")

</div>

> [@\[ANN\] Chess.jl - A Julia chess programming library](https://discourse.julialang.org/t/ann-chess-jl-a-julia-chess-programming-library/29221):
>
> I just published the first version of [Chess.jl](https://github.com/romstad/Chess.jl), a library for computer chess. This is my first Julia project, and I am not a real programmer, so much of the code probably looks amateurish, but it works and performs surprisingly well. Notable features: Efficient board representation with fast legal move generation. Import and export of games in PGN format, including support for comments and recursive annotation variations. Opening book trees generated from PGN databases. Basic support for inte…

---

<div class="post-metadata">

### Author: ![Tarny\_GG\_Channie](https://avatars.discourse-cdn.com/v4/letter/t/3bc359/32.png) [@Tarny\_GG\_Channie](https://discourse.julialang.org/u/Tarny_GG_Channie)
#### Post date: [July 18, 2023, 12:03pm UTC](https://discourse.julialang.org/t/building-a-strong-julia-chess-engine/101726/3 "2023-07-18T12:03:56Z")

</div>

I know that one. Thanks for the suggestion. That was a utility package for representing boards and so on, and the board representation here could be useful, but this was not specifically for building a UCI chess engine or something. Most chess engines have their own board representation and move generation. This package contains utility for using other engines, but not necessarily for building one.

---

<div class="post-metadata">

### Author: ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)
#### Post date: [July 18, 2023, 12:07pm UTC](https://discourse.julialang.org/t/building-a-strong-julia-chess-engine/101726/4 "2023-07-18T12:07:03Z")

</div>

There’s also [GitHub - abahm/Chess.jl: Chess engine written in Julia](https://github.com/abahm/Chess.jl)

Perhaps tell us what you’ve already found 😂😂😂

---

<div class="post-metadata">

### Author: ![Tarny\_GG\_Channie](https://avatars.discourse-cdn.com/v4/letter/t/3bc359/32.png) [@Tarny\_GG\_Channie](https://discourse.julialang.org/u/Tarny_GG_Channie)
#### Post date: [July 18, 2023, 12:12pm UTC](https://discourse.julialang.org/t/building-a-strong-julia-chess-engine/101726/5 "2023-07-18T12:12:48Z")

</div>

I found that one too. Nice one. This unlicensed, unnamed chess engine, IDK the strength, but the last commit was 2 years ago.

---

<div class="post-metadata">

### Author: ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)
#### Post date: [July 18, 2023, 12:21pm UTC](https://discourse.julialang.org/t/building-a-strong-julia-chess-engine/101726/6 "2023-07-18T12:21:12Z")

</div>

well, some good opening gambits for both packages would be doing some tests, issues, and pull requests to get them up to current Julia status. Or, if you don’t see any activity, contemplate a (repo, not knight) fork!

---

<div class="post-metadata">

### Author: ![Tord](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tord/32/6809_2.png) [@Tord](https://discourse.julialang.org/u/Tord)
#### Post date: [July 18, 2023, 12:45pm UTC](https://discourse.julialang.org/t/building-a-strong-julia-chess-engine/101726/7 "2023-07-18T12:45:31Z")

</div>

My Chess.jl package is, as @Tarny_GG_Channie points out, not really that well suited for writing a chess engine (although you certainly could). It’s designed more for higher level-chess software. Some low-level parts of the code (like the `SquareSet` type) would probably be useful, but I wouldn’t recommend using higher-level stuff like the `Board` type.

On the other hand, as some of you probably know, I also have some not insignificant experience writing chess engines, and I’ve been thinking about writing a Julia language engine at some point. I’ve even been toying with the idea of writing a book using chess engine development as a vehicle for teaching Julia and Flux, but it’s unlikely to ever happen (I doubt there would be any significant market for such a book).

Anyway, even if a book is unrealistic, I would certainly be willing to help getting a strong Julia chess engine up and running, if the community thinks such a project would be of interest.

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [July 18, 2023, 12:55pm UTC](https://discourse.julialang.org/t/building-a-strong-julia-chess-engine/101726/8 "2023-07-18T12:55:02Z")

</div>

Disclaimer, I don’t know very much about chess programming but I have significant experience with (game of) go programming, which at a higher level should be similar enough.

> [@Tarny\_GG\_Channie](#):
>
> Should we build a strong chess engine? How do I go about it?

A word of advice is to only embark on such a project if you enjoy playing chess or enjoy coding/inventing algorithms needed for a chess engine, preferably both.

> I need to get started by interfacing with UCI, but how?

The interfacing part is never particularly difficult. Find the specification and implement it, unless there’s already something out there you can use. Presumably [API Reference · Chess.jl](https://romstad.github.io/Chess.jl/dev/api/#UCI-Chess-Engines-1) might be helpful.

> Any idea to leverage Julia’s strength?

Game programming is much more about algorithms than languages. I would say that Julia has the right features to be very productive for the purpose but there’s nothing magical about it.

> I’m thinking of generating code using Julia’s metaprogramming to generate some parts of the engine.

Unless you have specific ideas how to use this to your advantage, metaprogramming isn’t something I would consider at an early stage.

---

<div class="post-metadata">

### Author: ![Tord](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tord/32/6809_2.png) [@Tord](https://discourse.julialang.org/u/Tord)
#### Post date: [July 18, 2023, 1:16pm UTC](https://discourse.julialang.org/t/building-a-strong-julia-chess-engine/101726/9 "2023-07-18T13:16:00Z")

</div>

> Unless you have specific ideas how to use this to your advantage, metaprogramming isn’t something I would consider at an early stage.

I agree. Contrary to what most people believe, chess engines are quite small and simple programs, and metaprogramming doesn’t add a lot of benefits (apart from being fun).

---

<div class="post-metadata">

### Author: ![Tarny\_GG\_Channie](https://avatars.discourse-cdn.com/v4/letter/t/3bc359/32.png) [@Tarny\_GG\_Channie](https://discourse.julialang.org/u/Tarny_GG_Channie)
#### Post date: [July 18, 2023, 2:35pm UTC](https://discourse.julialang.org/t/building-a-strong-julia-chess-engine/101726/10 "2023-07-18T14:35:22Z")

</div>

One particular use of metaprogramming I was thinking was searching for patch that gains elo, my idea was that there would be a huge search tree searched with lots of nodes (Using long time), then compute which node was actually searched with each patch. Then, we can search for variants of the engine that searches the right branch.

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [July 18, 2023, 7:20pm UTC](https://discourse.julialang.org/t/building-a-strong-julia-chess-engine/101726/11 "2023-07-18T19:20:30Z")

</div>

Just to know, are you considering a “classical” chess engine or a Reinforcement Learning based one? In such case the first step would be likely look for a sponsor for the computational costs 🙂 🙂

---

<div class="post-metadata">

### Author: ![Tarny\_GG\_Channie](https://avatars.discourse-cdn.com/v4/letter/t/3bc359/32.png) [@Tarny\_GG\_Channie](https://discourse.julialang.org/u/Tarny_GG_Channie)
#### Post date: [July 18, 2023, 10:48pm UTC](https://discourse.julialang.org/t/building-a-strong-julia-chess-engine/101726/12 "2023-07-18T22:48:00Z")

</div>

Modern chess engines normally use a small, special neural network.

---

<div class="post-metadata">

### Author: ![mrufsvold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrufsvold/32/31600_2.png) [@mrufsvold](https://discourse.julialang.org/u/mrufsvold)
#### Post date: [July 18, 2023, 11:59pm UTC](https://discourse.julialang.org/t/building-a-strong-julia-chess-engine/101726/13 "2023-07-18T23:59:33Z")

</div>

The issue isn’t using the neural network, it is the compute for training the weights of such a network.

---

<div class="post-metadata">

### Author: ![Tord](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tord/32/6809_2.png) [@Tord](https://discourse.julialang.org/u/Tord)
#### Post date: [July 19, 2023, 12:27pm UTC](https://discourse.julialang.org/t/building-a-strong-julia-chess-engine/101726/14 "2023-07-19T12:27:44Z")

</div>

Training an NNUE style network isn’t an issue, a single reasonably fast computer would be easily enough. Generating the training data (normally done by playing a lot of self-play games) takes far more computing time, unless you take the shortcut of using somebody else’s data. But you will have to play a massive number of test games anyway.

Developing a top engine does take a lot of computing resources, but it’s not really about training a neural network. It’s mostly just the expense of running thousands or tens of thousands of test games to test whether every little change you do to the code is an improvement. In the early phases of development, when improvements are easy to find and each improvement results in a giant leap in performance, testing is cheap. As the engine gets stronger, improvements are rarely worth more than a single digit number of rating points, and testing becomes very computationally intensive.

---

<div class="post-metadata">

### Author: ![mrufsvold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrufsvold/32/31600_2.png) [@mrufsvold](https://discourse.julialang.org/u/mrufsvold)
#### Post date: [July 19, 2023, 12:39pm UTC](https://discourse.julialang.org/t/building-a-strong-julia-chess-engine/101726/15 "2023-07-19T12:39:39Z")

</div>

Ah, that makes sense! Thanks for the clarification.
