c++ - tic-tac-toe program in cpp not running -
c++ - tic-tac-toe program in cpp not running -
i wanted implement tic-tac-toe in c++ using alpha beta pruning.i used link helping me out. http://www.ntu.edu.sg/home/ehchua/programming/java/javagame_tictactoe_ai.html
i wrote code in c++.but not running. everytime computer gets turn,it returns position [-1,-1] has been set default in minimax function.i can't figure out bug is.please help. thanks.
#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 move\n"; cin>>x>>y; if(grid[x-1][y-1]!=' '){ cout<<"enter 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=' '; (int = 0; <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==' ') homecoming false; if(win=='0'){ cout<<"you lost\n"; homecoming true; } if(win=='x'){ cout<<"you win\n"; homecoming 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]}; homecoming 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}; homecoming a; } int evaluate() { int score = 0; // evaluate score each of 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 homecoming 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; } // sec cell if (grid[row2][col2] == '0') { if (score == 1) { // cell1 '0' score = 10; } else if (score == -1) { // cell1 'x' homecoming 0; } else { // cell1 empty score = 1; } } else if (grid[row2][col2] == 'x') { if (score == -1) { // cell1 'x' score = -10; } else if (score == 1) { // cell1 '0' homecoming 0; } else { // cell1 empty score = -1; } } // 3rd cell if (grid[row3][col3] == '0') { if (score > 0) { // cell1 and/or cell2 '0' score *= 10; } else if (score < 0) { // cell1 and/or cell2 'x' homecoming 0; } else { // cell1 , cell2 empty score = 1; } } else if (grid[row3][col3] == 'x') { if (score < 0) { // cell1 and/or cell2 'x' score *= 10; } else if (score > 1) { // cell1 and/or cell2 '0' homecoming 0; } else { // cell1 , cell2 empty score = -1; } } homecoming score; }
the problem in move
function, returns pointer local variable. when function returns local variables goes out of scope, , using pointers leads undefined behavior.
c++ debugging tic-tac-toe alpha-beta-pruning
Comments
Post a Comment