Matthew Spillman's
Processing Projects

Thirty Five vs AI

Type a key 1-9 with a square selected to make a move.

This is an implementation of "35", a board game I created, against an AI opponent. A discussion of general strategy can be found below the rules. Then, I will briefly discuss the algorithm used by the AI.
The rules are as follows:

  1. Notice that each square has some squares adjacent to it (including diagonally), called "neighbors". Squares in the middle have 8 neighbors, edge squares have 5 neighbors, and corners have 3. This will be important.
  2. During a player's turn, they may place a number from 1 to 9 in an empty square. That number will (usually) be created in that player's color.
  3. If the sum of the numbers in all of a non-empty square's neighbors equals exactly 35, all of that square's neighbors will become the same color as it. For example, imagine a red 1 is surrounded by two 9s and an 8. If the blue player placed another 9 next to the 1, the sum of the 1's neighbors would be 9+9+8+9 = 35. Therefore, all of those squares would turn red, because the 1 is red.
  4. If a square reaches 35, its empty neighbors will be highlighted in its color.
  5. If an empty square is highlighted in red or blue, any number placed there will automatically be created in the highlighted color. Therefore, if blue placed a number in a red-highlighted square, it would have the same effect as if red had placed the same number.
  6. Empty squares can also reach 35, but nothing will happen until someone places a number there, since the square must be non-empty.
  7. If two squares of opposite colors reach 35 as a result of the same move, no squares change color.
  8. Once there are no empty squares remaining, the player with the most squares of their color wins. If both players have the same number of squares, the game ends in a tie.
Since you win by having more squares of your color and there is only one way to change the color of your opponent's squares, the basic goal of the game is simple. You want to surround your squares with 35, and prevent your opponent from surrounding their squares with 35. Here are a few basic strategies for accomplishing this: This list isn't comprehensive, and most situations don't fit cleanly into one of these strategies. However, hopefully these give an idea of how the game is played and provide a good starting point. The best way to discover new strategies is to play and experiment with new situations.

The AI

The AI for this game uses an algorithm called Monte Carlo Tree Search, or MCTS. It is the same algorithm used for the AI in my Mega Tic Tac Toe project. I describe it there as well. Essentially, MCTS simulates thousands of random games starting from the current board state, and uses the outcomes of the simulations to estimate the best move. Each simulation is partially guided by the outcomes of previous simulations, so the search is focused on moves which seem promising. MCTS is good for games where there are too many possible moves to find the best one using a brute-force apporach such as minimax. In the case of this game, there are 576 available moves at the beginning of the game and 288 on average. By comparison, the average number of available moves, or "branching factor", of the board games chess and Go are 40 and 250, respectively. There are about 34 million possible board states in Go after 3 moves, and the first time a Go AI beat a human world champion on a full-size board was just in 2016. In this game, there are 182 million different possible board states after 3 moves, five times more than in Go. Thankfully, the algorithm for updating a 35 board is much simpler and faster than that for a Go board, so MCTS can run many simulations. On my laptop, it runs about 60,000 to 80,000 simulations in 30 seconds in Chrome, and 7 times more when running as a standalone program.

So how good is the AI? It's...ok. Running in a browser makes it about 7 times slower, which makes it struggle significantly in the early game when there are tons of moves to investigate. By the time about a third of the board is filled up, though, it becomes significantly better. It typically falls far behind in the early game and often makes a miraculous comeback in the mid-to-late game. It's able to beat me fairly often, though if the game's strategy is as deep as I hope it is, we're both bad players. Overall, it plays really well in the second half of the game, so don't get too confident if you get a huge lead early on. The AI will eventually play perfectly, and with the way the game works, a single good or bad move can completely turn the tables.

AAAAAAAAAAAAAAAA