What are the algorithms I could use to create an AI for the game TicTacToe. I have already used Alpha–beta pruning and Predictive modelling. What the other good Algorithms I can use to create this AI.

I want to practice a new language this would a great way to learn a new language.


  • 5
    solve it – user40980 Oct 6 '13 at 12:31
  • 2
    welcome to programmers. List questions are frowned upon on this website. – Simon Bergot Oct 6 '13 at 13:48
  • @Simon what do you mean? – Taufiq Oct 6 '13 at 13:51
  • 1
    @Taufiq please check here for more information. In particular You should only ask practical, answerable questions based on actual problems that you face. Generally, asking questions like please provide a list of items that can do X or what should I do next should be avoided. You should also try to demonstrate that you have tried to answer the question yourself, which is not the case here. – Simon Bergot Oct 6 '13 at 14:14
  • 2
    @Taufiq practical, answerable questions based on actual problems is an entire Stack Exchange network wide guideline. – user40980 Oct 6 '13 at 16:18

Since Tic-Tac-Toe is a solved game, I would recommend simply playing a perfect game every time.

The following algorithm will allow you (or the AI) to always deny your opponent victory:

  1. Win:
    If you have two in a row, you can place a third to get three in a row.
  2. Block:
    If the opponent has two in a row, you must play the third to block the opponent.
  3. Fork:
    Create an opportunity where you have two threats to win (two non-blocked lines of 2).
  4. Blocking an opponent's fork:
    If there is a configuration where the opponent can fork, you must block that fork.
  5. Center:
    You play the center if open.
  6. Opposite corner:
    If the opponent is in the corner, you play the opposite corner.
  7. Empty corner:
    You play in a corner square.
  8. Empty side:
    You play in a middle square on any of the 4 sides.

Pick the highest possible on the list

The perfect heuristic is pretty simple in terms of AI programming. The most difficult heuristic part to implement is searching for fork configurations. Once you have that the other checks are next to trivial!

| improve this answer | |
  • in fact, it's a pretty big algorithm in AI which is called "Reinforcement Learning" but writing an algorithm to the game in a few lines is pretty amazing, good answer – Muhammed Refaat Feb 28 '15 at 11:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.