3D Noughts & Crosses

Change depth to search state-space (min 1, max 10, current 6)
Program options
Program controls

  • Current Player: X
  • Evaluation Time: N/A
  • Nodes Searched: N/A
  • Nodes Pruned: N/A
  • Depth Limit: 6
  • History
  • No moves yet. Jump to start.

  • Information

    Noughts and Crosses, or Tic-Tac-Toe, is the famous game about getting three \(X\)s or \(O\)s in a row. This website demonstrates a variant where you play in 3D, trying to get a line through a cube where each grid represents a layer.

    By default, the AI is a bit stupid. That's because it becomes possible for the first player to force a win if they take the center square. The AI will realise this if you put its depth limit to roughly 8, so it becomes quite difficult to win after that point, unless you go first.

    The algorithm used for the AI is minimax with alpha-beta pruning. This algorithm recursively considers all possible moves it could make and all possible responses to its moves so that it can pick one that it expects to maximise its chances of winning. Without alpha-beta pruning, it becomes computationally intractable much faster than it would otherwise. Since there's 27 possible moves at the start, thinking ten moves ahead without any special tricks requires considering \(27 \times 26 \times 25 \ldots \times 18 \times 17\) or \(520,431,047,136,000\) possibilities.

    If each possible game took \(1\text{ns}\) to consider, this technique means the algorithm would finish running in 6 days. Alpha-beta pruning means you can vastly cut down on the amount of options you have to consider by "pruning" states that you know won't lead to anything as good as what you've found so far. The number of states pruned is displayed in the information section.

    A transposition table is also used to speed up the program. This is effectively a hashmap that stores the results of previous searches, trading off memory usage for increased speed.


    To begin playing, just click on any of the squares on the right-hand side, or scroll down if you're on mobile. By default, the AI will automatically respond to your moves. To turn this off, toggle the "Automatically respond to player moves" checkbox. You can also force the AI to make the next move for the current player by clicking the "Play next move" button. "Analyse" will have the AI look for all possible moves, but not make any and instead report the information finds in the "Information" section.

    The slider at the top controls how many plys the AI will look ahead. One "ply" is one move by any player, a term used to avoid the ambiguity that comes with just saying "moves", which could mean either one action by one of the players, or a complete turn. The default is 6 and is reasonably easy to win against. Lowering the slider will make the AI worse but faster at making moves, and increasing the slider will make the AI better but much, much slower. Each successive notch on the slider corresponds to roughly a \(10 \times\) increase in waiting time.

    You can see a heatmap of what the AI thinks are good moves and bad moves by toggling the "Show a heatmap of predicted wins/losses" checkbox. This won't have any affect until you're a few moves into the game, as the AI can't see far ahead enough into the future to decide whether a mood is good or bad. Once it does, it will color each box depending on if there's a big chance for \(X\) or a big chance for \(O\).

    small \(X\)

    big \(X\)

    small \(O\)

    big \(O\)

    Finally, there is also the "Optimise for game length" option. This makes the AI try to avoid moves that will bring the game to an end, to see how long it's possible to go without anybody winning, including itself. If you click "Play next move" over and over, it will play against itself and you can see which player ends up making the move that wins the game. It turns out that drawing is actually impossible, see this blog post for why.

    Other Variants

    These are some other variants of noughts and crosses. This list comes from Wikipedia, "Tic-tac-toe variants":


    I hope you enjoyed the game. If you want to see the source code, it's available here on GitHub. If you want to see some of my other projects, check out my website. There's the old version of this project located here, and a demonstration of what it could actually look like in 3D here.