# Backgammon KG

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 keithbgould@gmail.com with any inquiries.

#### Features:

- Play against friends in two-player mode or play against the neural networked AI.
- Four different levels of computer strength: Beginner, Intermediate, Hard, and Expert.
- See statistics on your play skill, error rate, and your game or match history against the AI.
- Tutor mode helps you improve your backgammon game. Receive advice on your moves and see the AI's evaluations of all possible moves.
- Pip counter and equity meter.
- Beat the computer to unlock different boards.
- Many other configurable settings...

### 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 = x^{2}. 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:

- At each turn the AI uses it's neural network (which is a function) to evaluate how strong it's position would be in each of the available moves it can make according to the dice rolled.
- It picks the strongest move available.
- It then adjusts the neural network's evaluation of the strength of the previous position (prior to the move we just made) to be slightly closer to the evaluated strength of the current position after the move.
- It repeats this process until the end of the game.
- At the end of each game, the neural network knows whether it has won or lost (or gammoned / got gammoned) and it adjust it's evaluation of the last move it made accordingly. This last step is key, as it is the only part of the flow where actually correct (not probabilistically correct) information is given to the neural network. Over time (millions of games) this information propagates backwards through the neural network's evaluations of end game moves all the way to it's evaluations of early game moves. Slowly, the neural network becomes a complete backgammon player.

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