I need to make my tic tac toe AI unbeatable .
The computer AI 'O' should choose the best move.
I start by using a random number to create my first AI for the game but now the problem is to make it unbeatable. I found something call minimax algorithm but when I use it in my JavaScript code the minimax algorithm worked but it take a while to place the O and it is not choosing the best move, so what do i have to add or change to make the computer choose automatically the best move . If someone can help me find the answer is really going to help me thanks.
This is my JavaScript code:
td {
height: 100px;
width: 100px;
font-size: 20px;
text-align: center;
}
<body onload="startGame();">
<div id="message">sss</div>
<table class="squares" border = "1">
<tr>
<td class="square" id="s1" onClick="nextMove(this)"></td>
<td class="square" id="s2" onClick="nextMove(this)"></td>
<td class="square" id="s3" onClick="nextMove(this)"></td>
</tr>
<tr>
<td class="square" id="s4" onClick="nextMove(this)"></td>
<td class="square" id="s5" onClick="nextMove(this)"></td>
<td class="square" id="s6" onClick="nextMove(this)"></td>
</tr>
<tr>
<td class="square" id="s7" onClick="nextMove(this)"></td>
<td class="square" id="s8" onClick="nextMove(this)"></td>
<td class="square" id="s9" onClick="nextMove(this)"></td>
</tr>
</div>
</table>
It should choose all of the best move and never lose.