Backgammon KG

Download in the App Store

A backgammon app with quick and smooth in-game play. Features a strong neural network based AI built using deep learning and trained exclusively via self play.

Backgammon KG was designed and developed by Keith Gould. You can reach me at with any inquiries.


What is a neural network based AI?

Imagine if you had a magic little machine where you input the current state of your backgammon board (positions of all the checkers, and who's turn it is) and it would tell you your probability of winning measured from 0 (definitely losing) to 1 (definitely winning). If you had such a machine, you could punch in all the possible moves you can make based on the dice you just rolled and check their probabilities of winning. You would then find the move with the highest probability of winning (closest to 1) and make that move. Rinse and repeat. The more accurately this magic machine predicts your chances of winning at each position, the more perfect your checker play would be.

Now imagine this machine can be turned into a mathematical function (cue the math class PTSD!). It wouldn't be something simple like y = x2. It would be ridiculously complex. In fact the only reasonable way I think you could even begin to think about doing it would be to keep a table of all the possible probability values for every position of the backgammon board and look up the answer in that table each time the function is called. The total number of possible positions of the backgammon board is not infinite, so this is possible. But the problem is that our lookup table would be HUGE. This is because there are something like 18,528,584,051,601,162,496 distinct possible states of the backgammon board. There's probably only slightly more computer memory than that in existence on the earth today. You would need to be very persuasive to convince the whole planet to lend you all their hard drive space. And even if you did, you would still be stuck figuring out how to calculate the exact probability of a win at each position. For all practical purposes, it's just not possible to do.

That's where our neural network comes in. A neural network is a mathematical concept that allows us to approximate such a function. The concept is (kinda-sorta) based on how the human brain works. In this app we combine a neural network with the TD-Lambda algorithm (originally developed by Gerald Tesauro) to create an AI that is capable of playing against itself and (really) slowly learning to get better each game.

In simple terms, here's how the process works step by step:

With enough play, the AI begins to approximate the function needed to evaluate a backgammon board at any step and understand it's chances of winning. Once you have that the rest is simple -- you just pick the best move the dice afford you at each turn. (That's a lie, in practice there's still quite a bit more to it.)

Also, I think it's important to know that the AI is not training / adjusting it's neural network when you play it in the app. All the training is done prior to the release of each version of the game and the network inside the app you download is static. The app is not learning or getting better while you are playing it.

More about Backgammon KG

I built backgammon KG as a personal learning project. Inspired by one of my favorite apps, Backgammon NJ, I had always wanted to try to build a neural network that plays high level backgammon completely from scratch. In 2018, I finally felt bored enough to do it!

The neural net was built entirely from scratch and in JavaScript. I built all the neural net classes and used no ML libraries whatsoever. I invested a lot of time optimizing the JS code to squeeze a ton performance out of it. I imagine it's still probably slightly worse than the best JS competitors, and it's no match for the raw speed of C++. But I built this project to learn. The point of it was to hand write everything from scratch.

The current neural network being used in the app trained for 40 million games of self-play.

Are you a developer?

I'm all about learning. If you are a software engineer and really interested in learning more about how backgammon KG works, reach out to me. I'm happy to help you learn about how to build your own network from scratch as well, and possibly share some code you can leverage.

Special Thanks

Special thanks to Gabriel Price for including Backgammon KG in his article Underrated Board Game To Play On IOS