How I planned the AI strategy for Tic Tac Toe

Salma Elmasry
3 min readMar 5, 2018

--

It’s almost the end of week 5 for me in the Flatiron school, and I’ve been requested to build a tic-tac-toe game with AI algorithm. It was fun and very challenging to me as this is the first time to build an application and be playable via the CLI command line. I will illustrate only how I structured the AI part (computer move). I used Ruby language, but the concept can be applied in any language.

In a project like this one, you have to prepare yourself by taking few steps before starting to code. These steps are as follows:

  1. Write your coding plan in a white paper. This will help you not to get distracted, specially in designing applications like games.
  2. Conduct a comprehensive research about the application you intend to evolve. Google any idea you have in your mind. Try to explore as many tutorials related to your project as you can.
  3. Define your inputs and expected outputs. Write them down into your coding file as comments to keep you focused. Some developers prefer to save it in a separate readme file.
  4. Always do GitHub commits. a good rule of thumb is to make one commit per logical change. This will keep your repository organized and easy for your collaborators to debug or add features. Top tip : keep your commit expressive and comprehensive in describing your modification.

Understanding the AI logic

I did a research about tic-tac-toe playing strategies at wikipedia and translated it into an illustrated table as a starting step. Next, I indexed the board from 1 to 9. This helped me to standardize the way of making up the methods as illustrated below. Then, I implemented a particular strategy to define the AI logic, which can be abstracted as follow:

White paper planning scheme
  1. #Center: “5” A computer marks the center. (If it is the first move of the game, playing on a corner gives the second player more opportunities to make mistakes and may, therefore, be the best choice.)
  2. #corner: [1, 3, 7, 9] A computer can play in one of the corner positions if it is not marked by the opponent.
  3. #middle_corner: [2, 4, 6, 8] A player can play in the one of the middle corner positions if it is not marked by the opponent.
  4. #Block: If the opponent has two in a row, the computer must play the third themselves to block the opponent and lead the game to a draw.
  5. #Win_move: If the computer has two in a row, it can place a third to get three in a row and win the game.

Translate the drawing scheme into a code

It is important to start building the infrastructure of the code frame for your application. Start by defining your methods inside your class. Keep in mind to use an expressive methods’ names to make it easy for other developers to revise and debug your code.

For the #center method I defined it within If else-statement. If the position 5 in the board is a free spot then mark it. Else, do the #computer_move method as shown here:

For the #computer_move, the computer will make a move if it is a #win_move or #block or #corner or #middle_corner

Conclusion

Before starting to code give your self adequate time casing the inputs and the outputs of your desired application. Brain storm any ideas on a white paper, and if you didn’t like or use it, don’t through it away. It might be helpful later. Focus on getting your code as DRY as you can. Understand each line of the code, and be proud of it even if it wasn’t written in the most clever way.

--

--

No responses yet