-2

I have decided to make a C program of Tic-Tac-Toe. I am trying to make the AI invincible at the moment but I have encountered a problem.

And I wrote this:

int getFutureScoreOfMove(char square[][columns], char turn)
{
      int row, column, curBestScore, score;

      if(turn == 'C')                         curBestScore = -100;       
      else                                     curBestScore = 100;        

      for(i = 0; i < 3; i ++)
      {
           for(j = 0; j < 3; j++)
           {
                if(square[i][j] == ' ')
                {
                        if(turn == 'C')      
                        {
                                 square[i][j] = 'X';
                                 score = getFutureScoreOfMove(board, 'U');     
                                 square[i][j] = ' ';
                        }
                        else                  
                        {
                                 square[i][j] = 'O';
                                 score = getFutureScoreOfMove(board, 'C');
                                 square[i][j] = ' ';
                        }
                        if(turn == 'C' && score > curBestScore)           curBestScore = score;
                        if(turn == 'U' && score < curBestScore)           curBestScore = score;
                }       
           } 
      }  
      return(curBestScore);                    
}

There is something wrong in the code, as it is NOT invincible AI, and is ineffective. Why? And how can I fix it?

Thank you :)

2 Answers2

2

getFutureScoreOfMove calls itself, but it never modifies board with the move you're checking. This means it finds the same move again, resulting in infinite recursion. When calculating the outcome, you need to fill in the move on the board before recursing.

Dark Falcon
  • 43,592
  • 5
  • 83
  • 98
  • Thank you. I have corrected this by adding `board[row][column] = 'X';` before the recursive call, and hen reseting it after (see the edit), but it still is not invincible – Michael Ferashire Silva Aug 23 '13 at 12:55
1

The pseudo-code just implements a min/max strategy without actually computing anything to do with tic-tac-toe. Your code just recursively calls itself infinitely.

Paul Evans
  • 27,315
  • 3
  • 37
  • 54
  • Thank you. I have corrected this by adding board[row][column] = 'X'; before the recursive call, and hen reseting it after (see the edit), but it still is not invincible – Michael Ferashire Silva Aug 23 '13 at 12:59