2014-06-24 18 views
-2

我想在C++中使用alpha beta修剪實現井字遊戲。我使用這個鏈接來幫助我。 http://www.ntu.edu.sg/home/ehchua/programming/java/JavaGame_TicTacToe_AI.htmlcpp中沒有運行的井字棋程序

我在C++中編寫了這段代碼,但是這並沒有運行。每次計算機輪到它時,它都會返回[-1,-1]的位置,這是在minimax函數中默認設置的。我找不出錯誤在哪裏。請幫忙。 謝謝。

#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

你試過了什麼?您是否嘗試過調試(使用斷點,調試打印等)? – Scis

+0

請查看http://stackoverflow.com/help/how-to-ask以獲得最佳效果。 –

+0

是否有某些東西阻止您以通常的方式進行調試,例如用調試器添加'printf'或者執行代碼? –

回答

3

問題在於你的move函數,因爲它返回一個指向局部變量的指針。當函數返回時,局部變量超出範圍,並使用指向這些變量的指針undefined behavior