The Game Board
- Cut the WS2812 strip into 9 lengths of 12 LEDs. Each strip is just long enough to wrap inside a tic-tac-toe square.
- Drill a 3/8" hole in the lower left corner of each square.
- Pass one male and one female WS2812 connector through each one from back to front.
- Gently pull the WS2812 flex circuit out of the rubber sheath to expose the copper pads.
- Solder the female red, white and green wires from WS2812 connectors to the input side of the WS2812 strip. Repeat for the male connector and output side of the strip.
- Repeat with each of the remaining 8 strips.
- Bend each strip into a circle with the lights facing in.
- Fit the light strip to fit into the square.
- Repeat for all 9 squares. Use hot glue to hold the strips in place.
- Apply copper tape to the back of the board to create power and ground rails.
- Solder the hall effect sensors power and ground pins to the respective copper tape and solder data wires to the output pins.
Hint: I cut an o and old RS232 cable for the data wires since it has 9 data wires inside.
- Connect the data wires to pins 4-12 on the Teensy.
- Connect Teensy Pin DIO2 to the OctoWS2811 board.
- Connect 'strip 1' from the OctoWS2811 board to the input of the WS2812 chain on the tic-tac-toe board.
I needed to get the code working quickly so the board could be shipped, so this is just the first pass. The main application is a simple state machine with four states.
The game has not started. Cycle random colors on each cell until an X is detected. If an X is detected, the player has just taken a turn so go to the computer turn state.
Find an open square to play an O. The Teensy generates a random number between 0 and 8 and checks if the corresponding square is open. If it is the computer plays that square, if not a new number is generated until an open square is found. After selecting a square, the game board is checked for a winner or a draw. If there is no winner or draw go to the player turn.
Read the state of the game board until it no longer matches the previous state. This means the player has claimed a square. Determine which square was claimed and mark it is owned by the player. Check for a winner or a draw. If there is no winner or draw go to the computer turn.
Determine the winner and winning row. Highlight the board with the winner's color and flash the winning row. Remain in this state until all Xs have been removed from the board, then return to pre-game.