I made a Tic Tac Toe game of two types in Javascript. One is 3x3, and the other one is 10x10.
I am using Minimax algorithm with Alpha Beta Pruning to solve both the games. In 3x3, where the game tree is really small, the algorithm works fine.
But in 10x10, It takes too much time. The code cannot even make a single move in 10 mintues. I ran the algorithm, wait for 10 minutes, still, it was calculating, then I just closed the browser tab. (It might take even hours,days,weeks if I have let the code run lol)
I read in a few articles, that Minimax with Alpha Beta Pruning, can solve Tic Tac Toe 10x10 or bigger, easily. Is it false, or My code is bad?
Here is my code, but I think, it will be hard to undestand it. But code does not matter, I guess. I applied Minimax + Alpha Beta Pruning. What else can I do?
function makeBotMove(newBoard, availMoves, XorO, firstCall) { // newBoard stores board state in an array. availMoves stores Available moves in an array (0-99). XorO store either "X" or "O" depending on whoes turn it is. firstCall is used to find out If the call is made inside the function or not. I need it for Alpha Beta Pruning. It helps in storing the length of the total available moves when the call was made for
if (firstCall)
{
var originalAvailMovesLength = availMoves.length;
if (originalAvailMovesLength == board.length)
var maxPossibleResult = 0.5; // OriginalAvailMoves will be only 100, if it is the first move. And if it is first move, it is impossible to get reward of 1. The best the computer can do is, draw (0.5 reward).
else
var maxPossibleResult = 1;
}
availMoves = getAvailableMoves(newBoard);
var result = checkResult(newBoard, false); // It can return 4 values. 1 = Win, 0.5 = Draw, 0 = Game is on, -1 = Lose.
if (result != 0)
return [result];
var movesIndex = [];
var movesScore = [];
for (var i = 0; i < availMoves.length; i++)
{
var move = availMoves[i];
newBoard[move] = XorO;
availMoves.splice(availMoves.indexOf(Number(move)),1);
if (XorO == "O") // 1.) Yes
var reward = makeBotMove(newBoard, availMoves, "X", false);
else
var reward = makeBotMove(newBoard, availMoves, "O", false);
newBoard[move] = "-";
availMoves.push(move);
availMoves.sort();
movesIndex.push(move);
movesScore.push(reward[0]);
var bestMove = [];
if (originalAvailMovesLength == availMoves.length && Math.max(...movesScore) == maxPossibleResult)
{
bestMove[0] = Math.max(...movesScore);
bestMove[1] = movesScore.indexOf(bestMove[0]);
bestMove[1] = movesIndex[bestMove[1]];
return bestMove;
}
}
if (XorO == "O")
bestMove[0] = Math.max(...movesScore);
else
bestMove[0] = Math.min(...movesScore);
bestMove[1] = movesScore.indexOf(bestMove[0]);
bestMove[1] = movesIndex[bestMove[1]];
return bestMove;
}
If minimax algorithem, cannot do the job. Which algorithm do you guys recommend? It must not be very complex, i am not that good coder till now.
Edit: In 10x10, player need to place 5 moves in a row to win instead of 3.