c - Tic-Tac-Toe AI picks wrong position -


i making c program of tic-tac-toe. trying make ai invincible @ moment have encountered problem. problem ai prints symbol in next available question. why? , how can fix it?

here's calling function:

void determinmove(char positions[]) {       int num, i, currentbestscore, score;        currentbestscore = -100;        for(i = 0; < 9; i++)                              {                  if(positions[i] == ' ')                             {                                                    score = getfuturescoreofmove(positions, 'c');                          if(score > currentbestscore)                                 {                                 num = i;                                 currentbestscore = score;                           }                           positions[i] = ' ';                       }             }        positions[num] = comp; } 

and other recursive function:

int getfuturescoreofmove(char positions[], char turn)     {        int i, currentbestscore, score;         if(turn == 'c')                       currentbestscore = -100;               else                                     currentbestscore = 100;                 for(i=0; i<9; i++)        {                 if(positions[i] == ' ')                 {                        if(turn == 'c')                              {                            positions[i] = 'x';                            score = getfuturescoreofmove(positions, 'u');                             positions[i] = ' ';                        }                        else                        {                             positions[i] = 'o';                             score = getfuturescoreofmove(positions, 'c');                             positions[i] = ' ';                        }                        if(turn == 'c' && score > currentbestscore)                                                           currentbestscore = score;                        if(turn == 'u' && score < currentbestscore)                          currentbestscore = score;                  }                 }             return(currentbestscore);                     } 

as stated i'd know why weird behavior occurs , how fix it.

any appreciated :)

where calculating 1 player has gotten 3 in straight line? in code being calculated. you've iterated through possible board positions recursion haven't considered winning conditions. maybe code in first.


Comments

Popular posts from this blog

Line ending issue with Mercurial or Visual Studio -

java - Jtable duplicate Rows -

java - Run a .jar on Heroku -