Member-only story
Monte Carlo Tree Search (MCTS) AI Gameplay in Swift
Plus setting up S4TF in Jupyter Notebooks!
- Swift is not just for Apple
- Game-playing AI is awesome
- Swift is not just for Apple!
In this article, I’m going to explain how I set up Swift in a Jupyter Notebook, and then how I used Swift to create a Time-Bounded Monte Carlo Tree Search algorithm agent that can play 2048 in 17 seconds. Neat!
The reason why 2048 is such an interesting game to test an AI agent against is because it is not deterministic: it’s random. Therefore — unlike some other traditional games used to test AI such as CartPole and noughts & crosses — it’s not a problem that a neural network is suited to. Instead, we can use a stochastic algorithm that asks “which move will do the least damage?” over and over again, and can run through scenarios at pace.
Pace, you say? Yeah — using Swift allows us to create an agent that can run 55 moves per second which is pretty damn fast.
The Game of 2048
Designed by Italian web developer Gabriele Cirulli, 2048 is played on a 4x4 board, where at initialization you have two random tiles with either ‘2’ or ‘4’ on them. By swiping left, right, up, or down, you cause matching value tiles that come…