我想用alpha beta修剪在c ++中实现tic-tac-toe。我用这个链接来帮助我。 http://www.ntu.edu.sg/home/ehchua/programming/java/JavaGame_TicTacToe_AI.html
我用c ++编写了这段代码。但这并没有运行。每次计算机轮到它时,它返回默认设置在minimax函数中的位置[-1,-1]。我无法找出bug的位置。请帮助。 感谢。
#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;
}
答案 0 :(得分:3)
问题出在你的move
函数中,因为它返回一个指向局部变量的指针。当函数返回时,局部变量超出范围,并使用指向这些变量的指针导致undefined behavior。