0

I use minimax algorithm and alphabeta pruning to implement the caro (tictactoe) game. Then i use the bestMove function to find the movement for the bot. But the index of bestmove always 0,1,2,3... so what's the wrong with my code.

Here's my minimax function

int game::minimax(int depth, bool maximizingPlayer, int scores[], int h, int board[][12], int alpha, int beta )
{
    
    int count = size * size;
    if (depth == h)
        return 0;

    if (maximizingPlayer)
    {
        int best = INT_MIN;

        for (int i = 0; i < 144; i++)
        {
            int row = i / 12;
            int col = i % 12;
            if (board[row][col] == 0)
            {
                board[row][col] = 1;
                count--;
                if (win()&&(count%2==1)) {
                    board[row][col] = 0;
                    return 10;
                }
                if (draw()) {
                    board[row][col] =0;
                    return 0;
                }
                best = max(best, minimax(depth + 1, !maximizingPlayer, scores, h, board, alpha, beta));
                alpha = max(alpha, best);
                board[row][col] = 0;
                if (beta <= alpha) {
                    break;
                }
            }
        }
        return best;
    }
    else
    {
        int best = INT_MAX;

        for (int i = 0; i < 144; i++)
        {
            int row = i / 12;
            int col = i % 12;
            if (board[row][col] == 0)
            {
                board[row][col] = 2;
                count--;
                if (win()&&(count % 2 == 0)) {
                    board[row][col] = 0;
                    return -10;
                }
                if (draw()) {
                    board[row][col] = 0;
                    return 0;
                }
                best = min(best, minimax(depth + 1, !maximizingPlayer, scores, h, board, alpha, beta));
                beta = min(beta, best);
                board[row][col] = 0;
                if (beta <= alpha) {
                    break;
                }
            }
        }
        return best;
    }
}

here is my findMove for bot function

int game::bestMove(int board[][size]) {
    int scores[1] = { 0 };
    int bestMove = -1;
    int bestValue = -1000;
    int count = size * size;
    for (int i = 0; i < size*size; i++)
    {
        int row = i / 12;
        int col = i % 12;
        if (board[row][col] == 0)
        {
            board[row][col] = 2;
            count--;
            if (win()&&(count % 2 == 0)) {
                board[row][col] = 0;
                return i;
            }
            if (draw()) {
                board[row][col] = 0;
                return i;
            }
            int moveValue = minimax(0, false, scores, 2, board, INT_MIN, INT_MAX);
            board[row][col] = 0;
            if (moveValue > bestValue)
            {
                bestValue = moveValue;
                bestMove = i;
            }
        }
    }
    return bestMove;
}

i can't know why the bestMove function always return the value in ordered begin 0,1,2,... so the move of Bot is alway consecutive row by row, line by line ( O is bot) enter image description here

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
HCMUSer
  • 19
  • 3
  • 1
    [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Jesper Juhl Mar 30 '23 at 15:38
  • Unrelated: why are you doing an extra step for minimax in your `bestMove` function? Shouldn't you just call minimax? – B Remmelzwaal Mar 30 '23 at 15:39
  • May be i don’t know how to implement . Because i see that the minimax just return a value so i don’t think it can work under call minimax. Can you help me? – HCMUSer Mar 30 '23 at 15:49
  • 2
    *i can't know why the bestMove function always return the value* -- Why not? You wrote the code, you must have had a plan on paper before you wrote the code. The next step for you is to debug the code to see where the program goes against the plan you had on paper. We are not asking you to fix the problem yourself (which would be an extra good thing), but at the very least, you should discover *where* the problem is located. You will either find the problem and fix it yourself, find the problem and the fix is "difficult" and need help, or you discover that your original plan was flawed. – PaulMcKenzie Mar 30 '23 at 15:50
  • @PaulMcKenzie i mean it return the not optimal value. – HCMUSer Mar 30 '23 at 15:55
  • @PaulMcKenzie and find for help here is the last step of mine. I have to discover it for many days. And my ability is still low so pls help me – HCMUSer Mar 30 '23 at 15:56
  • 4
    Here is the problem with what you are asking for -- what if the solution looks nothing like your original code? Or the solution requires major changes? Again, you wrote all of this code (unless you copied it from somewhere), so you *must* have had a plan before you wrote it. Never write a program unless you are ready to fix it if necessary. That's why you need to debug your code using a debugger, single-step through the code until you see what goes against your plan. Also, it would help if you used smaller test data instead of such a large matrix of values. – PaulMcKenzie Mar 30 '23 at 16:06
  • Please explain what the purpose is of the `count` variable in your code. – trincot Mar 30 '23 at 17:23
  • @trincot to determine who will win, if rest of board is even O will win – HCMUSer Mar 30 '23 at 23:53
  • Well that is wrong then, because you inspect `count` when you have not yet iterated the whole board, so you're doing `count%2` when that count is not reliable yet. – trincot Apr 03 '23 at 06:49

0 Answers0