TDD Tic Tac Toe

Part 3: Unbeatable Level

Esther
3 min readAug 22, 2019

Have you ever played Tic Tac Toe against someone who never loses? There’s always that one person who figured out the secrets to the 9 square board (and possibly all the secrets to life). I took the challenge of building out a level of Tic Tac Toe where the User’s opponent would prove a worthy foe and never lose. And so begins UNBEATABLE TIC TAC TOE!!!

The Unbeatable Level

If you’ve been keeping up with my previous TDD Tic Tac Toe adventures, there was a set of rules that I outlined as the foundation for the game. Additionally, there were other considerations I had to factor into the game with each level. When I got to building the unbeatable level, the main consideration I had to factor in was “Is the User trying to win?”

Tic Tac Toe Strategy

I’ve provided a link at the end of this post for a really good break down of strategy. In the mean time, here are the basics of how I tackled the strategy component of the unbeatable level.

  1. The User had the first move, the Game had the second move.
  2. On the second move, if the center square was open, the Game took that square.
  3. If the User took the center square on the first move, the Game took one of the corner squares.
  4. Every move after that, the Game checked if it could win, if not, check if the User could win and play offense or defense accordingly.

Here’s the tricky part, the Game also had to play against a User’s strategy. If the User was making moves to take over the corners, the game had to take appropriate measures. Otherwise, it would create an opening for the user to make a maneuver assuring the Game’s defeat, rendering the whole unbeatable claim moot.

For example, the User starts the game by playing a corner square, the Game takes the middle square, then the User plays the diagonal square. There are currently no winning moves on the board. Neither the User nor the Game can win on their respective next turns.

The X takes up diagonal squares with the O taking the center square
Player X is strategizing

HOWEVER, the User now has command of the corners. It would be fatal for the Game to play any of the remaining corners, because it will then allow the User to take possession of the board like in the image below. No matter where the “O” is placed, the User has the potential to win.

The X takes up 3 of the corners while the O is taking up the middle and one corner square, lose lose for O
Lose Lose situation for Player O

So the Game must now have the logic to recognize the User’s strategy and play the edge squares, which will force the User to take defensive measures and prevent them from winning.

X is taking up diagonal squares with O in the center and in an adjacent edge square

In the board above, the User (X) must play square 4 and play defense if they don’t want to lose.

No Minimax

If you search for ways to build an unbeatable Tic Tac Toe game, chances are you will come across articles for Minimax. I was given the option to build the game without it and still had a hard time wrapping my brain around the game logic. But chipping away at the game strategy, I was able to complete the challenge without the added complexity of Minimax. The next challenge is to rebuild the game in Ruby WITH Minimax (you bet I’ll blog about it!)

TDD Tic Tac Toe pt 1: TDD Immersion

TDD Tic Tac Toe pt 2: Building the Game

Resources

https://blog.ostermiller.org/tic-tac-toe-strategy

--

--

Esther
Esther

No responses yet