0

I used the minimax function and findBestMove function to find the move for the bot, but it seems to be in error and I don't know to fix it

the move of bot, it line up

here my code

int x_score = 0, o_score = 0, x_count = 0, o_count = 0, x = 0, y = 0, depth=15, value = 0, pos_i = -1, pos_j = -1;
    int board[size][size]{};
    bool x_turn = true;
    int scoreVal[size][size];
int game::minimax(bool isMaxiPlayer, int depth, int alpha, int beta) {
    int val = 0;
    bool stop = false;
    if (depth == 0) return 0;
    int i = 0, j = 0;
    if (draw()) {
        return 0;
    }
    else if (win()) {
        if (!x_turn) {
            return -10-depth;
        }
        else {
            return 10+depth;
        }
    }
    //else if (depth == 0) return value;
    if (isMaxiPlayer) {
        int bestVal = -1000;
        for (i =0; i < 12 &&(!stop); ++i)
            for (j = 0; j < 12 &&(!stop); ++j)
                if (board[i][j] == 0) {
                    board[i][j] = 1;
                    val = minimax(false, depth - 1, alpha, beta);
                    board[i][j] = 0;
                    bestVal = max(bestVal, val);
                    alpha = max(alpha, bestVal);
                    if (beta <= alpha) {
                        stop = true;
                    }
                }
        return bestVal;
    }
    else {
        int bestVal = 1000;
        for (i = 0; i < 12 &&(!stop); ++i)
            for (j = 0; j < 12 &&(!stop); ++j)
                if (board[i][j] == 0) {
                    board[i][j] = 2;
                    val = minimax(true, depth - 1, alpha, beta);
                    board[i][j] = 0;
                    bestVal = min(bestVal, val);
                    beta = min(beta, bestVal);
                    if (beta <= alpha) {
                        stop = true;
                    }
                }
        return bestVal;
    }
}
void game::findBestMove(bool isMaxiPlayer, int& pos_i, int& pos_j) {
    int bestVal = -1000;
    for (int i = 0; i < 12; ++i) {
        for (int j = 0; j < 12; ++j) {
            //cout << board[i][j];
            if (board[i][j] == 0) {
                board[i][j] = 2;
                int val = minimax(false, depth, -1000, 1000);
                if (val > bestVal) {
                    bestVal = val;
                    pos_i = i;
                    pos_j = j;
                }
                board[i][j] = 0;
                //printf("%d ", i, j);
            }
        }
    }

}

I tried to adjust the function many time but i can not find the point I expect you to help me to adjust the function that it can work smooth. My board is 12*12

Rohit Gupta
  • 4,022
  • 20
  • 31
  • 41
HCMUSer
  • 19
  • 3

0 Answers0