Gallery
Puzzle #5: Tic Tac Toe
Share
Explore

# Puzzle #5: Tic Tac Toe

Can you make Tic Tac Toe?
Originally published 7/11/2018 by Gil Goldschlager
For , we challenged you to recreate Tic Tac Toe. Wanna skip straight to the solution? Scroll down!

## Origins: A Puzzle Ahead of Its Time

One Friday afternoon in March, our CPO (Chief Puzzling Officer) challenged me to design a new puzzle. She asked, “Could you build chess in Coda?”
“That’d be tough,” I said, “Maybe something simpler to start…how about Tic Tac Toe?”
In an hour or so, I had a working version that looked like this:
Granted using select lists to play wasn’t ideal, and clearing the board was a real pain, but it got the job done and I was pretty happy with it. Until I had the thought — could I make a bot that would play against me? It seemed impossible at first. At the time, there was no way to write a value to a cell other than, well, writing the value to the cell. So how would the bot make a move? After spending the better part of my Sunday afternoon and evening tinkering, I came up with this:
It only worked because of a complete hack. The computer couldn’t actually write a value to a cell, so I used a grouped table with separate rows for the user and the computer for each space on the board (that’s right, the table above has 18 rows, not 9).
I used a formula to calculate the computer’s move based on the state of the game, and then used a filter to flip in the computer’s row in place of the user’s row for any space that the computer wanted to play in. I also had to use a recursive formula five levels deep to formulaically generate every computer move at all times based on the set of human moves made so far. The next day I had a great time seeing if anyone at the office could figure out how I built this without looking behind the scenes. No one could.
Despite how much fun I had in making this (and watching my coworkers struggle to figure it out), it didn’t feel ready for prime-time. There were just too many hacks to make it work, and the end result, while cool, didn’t feel quite right. It felt like something was missing to really make it ... click.

## Enter Buttons

Fast forward to a few weeks ago. We were about to launch buttons, and we wanted a puzzle to go along with it. Hey, remember that time we built Tic Tac Toe in Coda? Wouldn’t that work so much better now that we have buttons? So, we tried it out, and voilà — it felt great. What was previously a hack was now a real puzzle.
The only question was, how far should we go? It seemed a little intimidating to challenge you all to write a Tic Tac Toe bot, but at the same time, we knew there’d be a few out there who would appreciate the challenge. In the end, we found a compromise. The main puzzle would have two parts. First, make a game board that uses buttons to let you play Tic Tac Toe. Second, identify the winner of the game and light up the squares of the winning combination. In addition to the main puzzle, we’d challenge some of our puzzle solvers, over email and the , to an even greater challenge- making a Coda ‘AI’ to play against.
With that settled, we updated our solutions with our shiniest buttons, made some gifs, and threw down the gauntlet to all of you.

## Directions

Set up your Tic Tac Toe board with buttons, and add a "Clear Board" feature.
Want an extra challenge? Make the board light up when somebody wins. (If you're really stumped, scroll down.)

Puzzle Constructionists: Gil Goldshlager and Adam Ginzberg
This is a tough one. We've queued up some hints to help you along.
Click the button below to reveal a hint. You'll get a new one each time you click. (There are 8 total.)
Get Next Hint
1