-2

Can anyone help me. Which is the best algorithm to implement a TicTacToe game. I need to write the code in a way that it should be generic and should be able to handle more complex levels in easier way (generic way) from wher can I start? I read that we can use MiniMax algorithm. I need to know the most. efficient algorithm

Thanks,

Deniz
  • 12,332
  • 10
  • 44
  • 62
  • 1
    By "Algorithm for implementing Tic Tac Toe", do you mean an *agent* (AI player)? UI for playing? leaderboard? Please elaborate what exactly are you after. Also, what do you mean by "most efficient?" In what terms? (it is obviously highly related to the first question) – amit Sep 19 '14 at 07:08
  • 2
    There is an example in samples of Android SDK – PageNotFound Sep 19 '14 at 07:10
  • I have implemented it, if you need it, ping me! and btw, the AI part is also done. – stack_ved Sep 19 '14 at 07:21
  • have found some useful links to learn http://www.ntu.edu.sg/home/ehchua/programming/java/JavaGame_TicTacToe_AI.html – Deniz Sep 22 '14 at 11:41

1 Answers1

1

The "Best" algorithm (which always choose the best possible play) is known to be NP-Complete.

Therefore it is really hard to implement "good" one, which use some heuristic to increase performance and it is precious enough.

Minimax is basically only approach which "really" works, so start with it, then you can think and look for optimization. The idea is simple, you try every movement and you measure the "price" of that movement (e. g. creating more same "X" or "O" in row has better price than creating "X" or "O" than most things, or stopping a long line of "X" or "O" of enemy is good too).

Then for every possible movement, you try enemy to do the every possible movement and you count the price.

How "deep" you go, the more precious it is, but the more expensive for performance algorithm is.

For that example I said, for every your movement you do every enemy movement - you suppose that opponent would play as good as possible, so you from all that movement the opponent plays you get the maximum (the maximum for him). And now for every possible movement, you have the value of best enemy play. And then you choose the minimum - it means that you want to choose the best for you and worst for enemy.

That's why they call it minimax.

Pham Trung
  • 11,204
  • 2
  • 24
  • 43
libik
  • 22,239
  • 9
  • 44
  • 87
  • Thanks. have found some helpful links also http://www.ntu.edu.sg/home/ehchua/programming/java/JavaGame_TicTacToe_AI.html, http://www.neverstopbuilding.com/minimax – Deniz Sep 22 '14 at 11:42