Tic Tac Toe AI help

The Front End Tic Tac Toe project seems to be a big spike in difficulty from the others, but I’m not complaining. However I’m curious as to how complex should (or is) the AI supposed to be?

While looking at different blogs and sites for help I’ve found projects that seem to be a complete list of every possible case on if statements and a a really elaborate AI, however I’m having trouble understanding how this (and any other design I’ve pictured) works.

The example above uses something called the minimax value to determine an optimal move, but (not being to debug so far, just mentally going through the code and maybe a notepad on the side), I don’t see how this determines the most optimal move.

Can I get any help with this or any other algorithm?

This is what I built for this challenge: http://codepen.io/NeckersBOX/pen/mAPboZ

It checks the warning cases ( users win in 1 move ) and get the solution for each case with two array:

    var warning   = [ [0, 1], [1, 2], [3, 4], [4, 5], [6, 7], [7, 8], // H
                      [0, 3], [3, 6], [1, 4], [4, 7], [2, 5], [5, 8], // V
                      [0, 4], [4, 8], [2, 4], [4, 6],                 // D
                      [0, 2], [3, 5], [6, 8], [0, 6], [1, 7], [2, 8], // H+V +1
                      [0, 8], [2, 6] ];                               // D   +1
    var solution  = [ 2, 0, 5, 3, 8, 6, 6, 0, 7, 1, 8, 2, 8, 0, 6, 2, 1, 4, 7, 3, 4, 5, 4, 4 ];

(
The numbers are for the grid:

012
345
678

)

Also I’ve saved the win cases to stop the game:

var win_case = [ [0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], 
                 [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6] ];

Free to use it if you want.

Note: Yes, I know… this is a very BRUTAL approach xD

I get that, in the very last moment, you make a move that denies the victory, but how do you choose, say, the first move? Or any other move where the player isn’t about to win? Does your code ever try to win?

Try looking over this article on Quora

These warning and solution are used both to obstacolate user moves and to find a better move for the cpu.

Only for the first move it does a random move.