(Not) Playing Snake
In middle school, we had school-issued Chromebooks. The administration had them locked down, so much so that the “dinosaur game” on the built-in Chrome browser was unavailable. Seeing as Cool Math Games was blocked as well, we turned to Snake - another built-in game, but on Google. Not as easy to block.
The game was, by default, on a 17x17 grid, but there were easter eggs/other game modes to play. Of course, there was no chance of running code locally on the Chromebook without something like shimbooting, which wasn’t really widespread when I needed it.
So, now, years later, I trained a small model to play snake on variable size grids. The project is on GitHub.
There are two processes, one of which isn’t even a neural network. The first is breadth-first-search (BFS), a fixed algorithm that follows predetermined rules. The second is deep learning (DQN), which is a neural network that outputs a “Q-value” for each action the snake could take at each step; you pick the highest one. Each game during training is referred to as an “episode,” and as the episodes go on, the “instinct” to explore decreases. This is to keep the model to stick to and refine the best strategies it already knows.
The DQN model is saved as weights.pth, which is then converted to an Open Neural Network Exchange (ONNX) file. In the browser, onnxruntime-web is used to load the weights and creates an InferenceSession to play snake.
See both BFS and DQN playback below (running locally on your browser!):
BFS
Score 0 · Episodes 0 · Best 0 · Avg —
DQN
Score 0 · Episodes 0 · Best 0 · Avg —
