In early 2024, as this latest AI hype cycle was looking to grow into something big, I set out to write a neural network from scratch. I wanted to see how neural networks and model training worked. At the time I'd recently played a few games of Mancala, and thought it would be a good application for this project.
(Reading can wait... click here to play.)
Playing Mancala seemed like a good non-trivial task for a small model to learn, and since I had no idea how to play Mancala, it would be fun to see if a small model could learn to play well enough to beat me.
I knew I needed a game-playing UI that could run the neural network, so I began writing everything in JavaScript. I implemented the neural network, including struggling through writing a backpropagation algorithm, in JavaScript, and a crude system for the neural network to learn to play Mancala.

The obvious way for a neural network to learn to play Mancala is to play itself. For any board position, it plays all the legal moves many times, and records the eventual game outcome. Whichever move results in an eventual win most often (again, playing against itself starting from that board position) should be played when that board position in encountered. So that board position and best move are added to the training dataset. This process repeats, building up a large training dataset while also discarding old items to prevent it from training on old moves from back when the model was less trained. In theory, the neural network should generalize and also play good moves when new positions, not in the training dataset, are encountered.
This all seemed to work, but it became apparent that this was too slow in JavaScript to be of any use. I ported it to Rust by hand. The Rust code was made to output the network weights to files that the JavaScript page could use, and that part worked as expected.
But the training was still too slow, even with a relatively tiny training dataset. I shelved the project, and it sat for 2 1/2 years.
A couple months ago, I pointed Claude Code at the project. Of course, in what would have been science fiction a few years ago, it was able to read my code and immediately understand what was going on. It gave a few suggestions for how to proceed. It implemented the Adam optimization algorithm, and suddenly my old project was able to learn at a usable rate!
Rather than just train one model that gets stronger over time, I decided to stop training at discrete "levels" where the model at each level beats all previous levels in at least 2/3 of games. Each subsequent level is started as a larger, completely fresh neural network. After some experimentation with this concept, I started with a small mostly random "level 0" model, and then trained progressively larger and smarter models from there.
Claude implemented a few further optimizations to speed up the learning process, and over the last couple months I've been training models, tweaking the model parameters as we go, to create a smooth "level ladder" of better and better models.
Claude also re-styled the Mancala page, but I want to take credit for the board design and layout, and movement and arrangement of pieces within the pits, which I wrote myself in back in the pre-vibecoding days.
We now have a Mancala game page where you can pick an "AI" opponent and play against it, watch two models play each other, or even just have two humans play.
The game uses Kalah rules, which according to Wikipedia is solved. This means that if both players play flawlessly, whoever plays first wins. The models are probably nowhere near playing perfectly, but I thought it was just worth a mention.
I have a system that allows neural networks to learn to play, and they (easily!) beat me. Which was the goal all along.
At the time I'm writing this, levels 0-12 are playable. I'm hoping to finish levels 13 and 14 soon, but I'm not sure if the current system will be able to train models that beat all levels 0-12, and then 0-13, in 2/3 of games. I'll add those to the Mancala app if and when that happens.