Build a Bot for Your 3D-Tic Tac Toe Game in SceneKit Under SwiftUI | by Mark Lucking | Aug, 2022

Playing qubic against the computer

Playing qubic against the computer

A continuation of the paper describing how to create a 3D game of tic-tac-toe.

Picking up where I left off, I needed to verify that I had all the winning lines. I added some more to the hit-winning method to show me all the possible lines I could come up with each square marked.

1*edbrP5gZ7DCg8iJ oHXZNQ
A missing winning line can be seen here diagaonally on the base of the 4x4x4

I corrected the missing links as I continued and played around with the game. Wikipedia tells me I should have 74; I got 64; if you download and try out the code, please share with me and others the missing links.

The code here named the debugX a subscription the routine to which I send copies of the winning lines I discover on selecting a node. I used different coloured lines for the other player. The code behind the new handleTap getting increasingly complex.

Bon, as I played around, it became evident that the corner nodes are the most valuable, followed closely by the edges — information I would want to consider as I thought about building an automated agent. Watching nodes change sides, too, it became apparent that mirroring corner moves looked like it was one of the best defensive strategies.


Any lines on which both nodes had a claim were, in effect, lost causes; the centre diagonal on which both the blue and orange nodes had a registered node the case in hand here — lines you can see here with a very dull yellow.

You can in see in this animated GIF the light blue coloured nodes on the lines where the blue player has at least one node selected resting. The light red coloured nodes are the lines that the orange player has at least one node selected. The yellow/green coloured nodes are effectively “lost” nodes that neither red nor blue will be able to win the game selecting. The white-coloured nodes are sitting on “free” lines that neither red nor blue has yet laid claim.

I tried a few different ideas but settled on a struct that stored the number of nodes used in each line together in three arrays. The first is the nodes contained within that line, the second the nodes used, and the third the nodes still to be selected — free as I called them.

struct MetaMatrix {
var redCount: Int = 0
var blueCount: Int = 0
var line:[SIMD3<Int>?]
var used:[SIMD3<Int>?]
var free:[SIMD3<Int>?]
private var shared:[Int:MetaMatrix] = [:]

Next Moves

I started then by adopting a simple defensive strategy using the outlined structure. The bot uses it to see which lines you would/should have made an initial selection.

It looked promising, although as I played the games, it was apparent I needed more. I needed the bot to try to win too. I ended up with just four rules, played out in the order shown.

  • If the blue player [you] has three in a row, block them.
  • If the red player [bot] has three in a row, finish the game!
  • If the red player [bot] has started a line, then continue it until you are blocked or win!
  • If rules 1–3 are not executable, select a free node at random; preferably one that hasn’t already been taken 🙂

In honesty, rules 2 and 3 are a little too simplistic; you’ll see if you play the game. In an ideal world, you need something a little more sophisticated really. Rules that look like this in code.

A complete game is shown here in this Video. I played a dozen or more fun here; although, to be honest, you need to do some more formal testing, the number of possible permutations is significant.

Blue eventually wins here — did I outsmart the bot — not sure; maybe the code isn’t quite perfect yet.

All of which brings me to the end of this short piece, a good game with just over 600 lines of code. Y

ou can download the working code from the GitHub Repository— where you are more than welcome to come up with some better rules for the bot.

All of which brings me to the end of this short paper. I hope you enjoyed reading it as much as I did writing it.

News Credit

%d bloggers like this: