программа tic-tac-toe в cpp не работает

Я хотел реализовать крестики-нолики в c ++ с использованием альфа-бета-обрезки. Я использовал эту ссылку, чтобы выручить меня.
http://www.ntu.edu.sg/home/ehchua/programming/java/JavaGame_TicTacToe_AI.html

Я написал этот код на C ++. Но это не работает. Каждый раз, когда компьютер получает свою очередь, он возвращает позицию [-1, -1], как было установлено по умолчанию в функции минимакса. Я не могу понять, где находится ошибка. Пожалуйста, помогите.
Благодарю.

#include<iostream>
using namespace std;
enum player{
comp,user
};

void print();
char grid[3][3]={{' ',' ',' '},{' ',' ',' '},{' ',' ',' '}};
int evaluateLine(int,int,int,int,int,int);
int evaluate();
int*move();
int*minimax(int,int,int&,int&);
bool has_won();
void play(int);

int main(){
play(user);
}
void print(){
for(int i=0;i<3;i++){
for(int j=0;j<3;j++)
cout<<grid[i][j]<< "  |  ";
cout<<"\n --   --   --  \n";
}
}
void play(int player)
{
int x,y;
if(player==user){
cout<<"Enter your move\n";
cin>>x>>y;
if(grid[x-1][y-1]!=' '){
cout<<"enter a valid move\n";
play(player);
}
grid[x-1][y-1]='X';
print();
if(has_won())
exit(0);
else
play(comp);
}
else{
int* x=new int[2];
//cout<<"hi\n";
x=move();
cout<<x[0]<<x[1]<<endl;
//cout<<"hi2\n";
grid[x[0]][x[1]]='0';
//cout<<"hi4\n";
print();
//cout<<"hi3\n";
if(has_won())
exit(0);
else
play(user);
}
}
bool has_won()
{
char win=' ';
for (int i = 0; i <3; i++) {
if(grid[i][0]==grid[i][1] && grid[i][1]==grid[i][2])
{
win=grid[i][1];
break;
}
else if(grid[0][i]==grid[1][i] && grid[1][i]==grid[2][i]){
win=grid[0][i];
break;
}
}
if(grid[1][1]==grid[0][0] && grid[1][1]==grid[2][2])
win=grid[0][0];
else if(grid[0][2]==grid[1][1] && grid[1][1]==grid[2][0])
win=grid[1][1];
if(win==' ')
return false;
if(win=='0'){
cout<<"You lost\n";
return true;
}
if(win=='X'){
cout<<"You win\n";
return true;
}

}
int* move(){
int *z=new int[3];
int alpha=-100000,beta=100000;
//cout<<alpha<<beta<<endl;

z=minimax(4,comp,alpha,beta);
int a[]= {z[1],z[2]};
return a;
}
int* minimax(int level,int player,int& alpha,int& beta)
{
int score=0;
int row=-1,col=-1;
if(level==0){
score=evaluate();
}else{
int count=0;
//cout<<"enter\n";
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
if(grid[i][j]==' '){
//cout<<i<<j<<endl;
count++;
grid[i][j]=(player==0)?'0':'X';
score=minimax(level-1,1-player,alpha,beta)[0];
if(player==0){
if(score>alpha){
alpha=score;
row=i;
col=j;
}
}
else{
if(score<beta){
beta=score;
row=i;
col=j;
}
}
grid[i][j]=' ';
if(alpha>=beta){
i=3;
j=3;
}
}
}
}
if(count==0){
score=evaluate();
}
else
score=(player==0)?alpha:beta;
}
int a[]={score,row,col};
return a;
}
int evaluate()
{
int score = 0;
// Evaluate score for each of the 8 lines (3 rows, 3 columns, 2 diagonals)
score += evaluateLine(0, 0, 0, 1, 0, 2);  // row 0
score += evaluateLine(1, 0, 1, 1, 1, 2);  // row 1
score += evaluateLine(2, 0, 2, 1, 2, 2);  // row 2
score += evaluateLine(0, 0, 1, 0, 2, 0);  // col 0
score += evaluateLine(0, 1, 1, 1, 2, 1);  // col 1
score += evaluateLine(0, 2, 1, 2, 2, 2);  // col 2
score += evaluateLine(0, 0, 1, 1, 2, 2);  // diagonal
score += evaluateLine(0, 2, 1, 1, 2, 0);  // alternate diagonal
return score;
}
int evaluateLine(int row1,int col1,int row2,int col2,int row3,int col3)
{
int score=0;
if (grid[row1][col1] == '0') {
score = 1;
} else if (grid[row1][col1] == 'X') {
score = -1;
}

// Second cell
if (grid[row2][col2]  == '0') {
if (score == 1) {   // cell1 is '0'
score = 10;
} else if (score == -1) {  // cell1 is 'X'
return 0;
} else {  // cell1 is empty
score = 1;
}
} else if (grid[row2][col2]  == 'X') {
if (score == -1) { // cell1 is 'X'
score = -10;
} else if (score == 1) { // cell1 is '0'
return 0;
} else {  // cell1 is empty
score = -1;
}
}

// Third cell
if (grid[row3][col3]  == '0') {
if (score > 0) {  // cell1 and/or cell2 is '0'
score *= 10;
} else if (score < 0) {  // cell1 and/or cell2 is 'X'
return 0;
} else {  // cell1 and cell2 are empty
score = 1;
}
} else if (grid[row3][col3]  == 'X') {
if (score < 0) {  // cell1 and/or cell2 is 'X'
score *= 10;
} else if (score > 1) {  // cell1 and/or cell2 is '0'
return 0;
} else {  // cell1 and cell2 are empty
score = -1;
}
}
return score;
}

-2

Решение

Проблема в вашем move функция, так как она возвращает указатель на локальную переменную. Когда функция возвращает локальные переменные выходит из области видимости, и использование указателей на них приводит к неопределенное поведение.

3

Другие решения