Проблема С ИИ в крестиках-ноликах
Я делаю крестики-нолики на С++ с использованием алгоритма альфа-бета отсечения , столкнулся с проблемой когда бот делает не выгодные ходы (вместо того что бы заблокировать игрока он продолжает выстраивать свою комбинацию),буду благодарен если кто-то сможет указать на ошибку , заранее спасибо.(также стоит упомянуть , что игра делаеться для разного размера поля ,плюс должно соблюдаться условие : Еcли поле равно размеру 5 на 5 или меньше , то количество фигур для победы 3 , в остальных случаях 5) Вот основной код ИИ:
int evaluate() {
int player_two_count = 0;
int computer_two_count = 0;
int player_three_count = 0;
int computer_three_count = 0;
int player_four_count = 0;
int computer_four_count = 0;
// перевірка по рялкам
for(int i = 0; i < n; i++) {
int player_count = 0;
int computer_count = 0;
for(int j = 0; j < m; j++) {
if(board[i][j] == player) {
player_count++;
} else if(board[i][j] == computer) {
computer_count++;
}
}
if(player_count == 2) {
player_two_count++;
} else if(player_count == 3) {
player_three_count++;
} else if(player_count == 4) {
player_four_count++;
}
if(computer_count == 2) {
computer_two_count++;
} else if(computer_count == 3) {
computer_three_count++;
} else if(computer_count == 4) {
computer_four_count++;
}
}
// Перевірка по солбцям
for(int j = 0; j < m; j++) {
int player_count = 0;
int computer_count = 0;
for(int i = 0; i < n; i++) {
if(board[i][j] == player) {
player_count++;
} else if(board[i][j] == computer) {
computer_count++;
}
}
if(player_count == 2) {
player_two_count++;
} else if(player_count == 3) {
player_three_count++;
} else if(player_count == 4) {
player_four_count++;
}
if(computer_count == 2) {
computer_two_count++;
} else if(computer_count == 3) {
computer_three_count++;
} else if(computer_count == 4) {
computer_four_count++;
}
}
// перевірка по діагоналям
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if(board[i][j] == player) {
int count = 0;
int k = 0;
while(i + k < n && j + k < m && board[i + k][j + k] == player) {
count++;
k++;
}
if(count == 2) {
player_two_count++;
} else if(count == 3) {
player_three_count++;
} else if(count == 4) {
player_four_count++;
}
count = 0;
k = 0;
while(i + k < n && j - k >= 0 && board[i + k][j - k] == player) {
count++;
k++;
}
if(count == 2) {
player_two_count++;
} else if(count == 3) {
player_three_count++;
} else if(count == 4) {
player_four_count++;
}
} else if(board[i][j] == computer) {
int count = 0;
int k = 0;
while(i + k < n && j + k < m && board[i + k][j + k] == computer) {
count++;
k++;
}
if(count == 2) {
computer_two_count++;
} else if(count == 3) {
computer_three_count++;
} else if(count == 4) {
computer_four_count++;
}
count = 0;
k = 0;
while(i + k < n && j - k >= 0 && board[i + k][j - k] == computer) {
count++;
k++;
}
if(count == 2) {
computer_two_count++;
} else if(count == 3) {
computer_three_count++;
} else if(count == 4) {
computer_four_count++;
}
}
}
}
// Визначаємо оцінку для ходу
int player_score = (n <= 5 ? player_two_count - computer_two_count : player_three_count - computer_three_count);
int computer_score = (n <= 5 ? computer_two_count - player_two_count : computer_three_count - player_three_count);
return computer_score - player_score;
}
int alpha_beta(int depth, int alpha, int beta, bool maximize) {
if(depth == 0 || is_full() || is_win(player) || is_win(computer)) {
return evaluate();
}
if(maximize) {
int max_val = -INF;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if(board[i][j] == '.') {
board[i][j] = computer;
if(is_win(computer)) {
board[i][j] = '.';
return (n <= 5 ? 100 : 1000);
}
int val = alpha_beta(depth - 1, alpha, beta, false);
board[i][j] = '.';
max_val = max(max_val, val);
alpha = max(alpha, val);
if(beta <= alpha) {
break;
}
}
}
if(beta <= alpha) {
break;
}
}
return max_val;
} else {
int min_val = INF;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if(board[i][j] == '.') {
board[i][j] = player;
if(is_win(player)) {
board[i][j] = '.';
return (n <= 5 ? -100 : -1000);
}
int val = alpha_beta(depth - 1, alpha, beta, true);
board[i][j] = '.';
min_val = min(min_val, val);
beta = min(beta, val);
if(beta <= alpha) {
break;
}
}
}
if(beta <= alpha) {
break;
}
}
return min_val;
}
}