Search code examples
algorithmartificial-intelligencetic-tac-toe

A tic tac toe - that will make one move given an input


Assume computer is playing as C and the opponent is playing as O. The bot must be intelligent enough to win when provided an opportunity. X represents a cell that is not taken. Also assume that computer made the first move

for eg:

Input1
CCX
XOX
OXX

Output1
CCC
XOX
OXX 

What i want to know is how to approach this problem. Is there a specific algorithm to follow?. If yes please clarify it to me!


Solution

  • Use the minimax algorithm.

    Once that is implemented, define a simple heuristic, or evaluation function. It could go something like this:

    function scoreBoard(board) {
      if(board.isWin()) {
        return 1;
      }
      else if(board.isTie()) {
        return 0;
      }
      else {
        return -1;
      }
    }