-2

I am making a C program of Tic-Tac-Toe. I am trying to make the AI invincible at the moment but I have encountered a problem. The problem is that the AI just prints the symbol in the next available question. Why? And how can I fix it?

Here's my calling function:

void determinMove(char positions[])
{
      int num, i, currentBestScore, score;

      currentBestScore = -100;

      for(i = 0; i < 9; i++)                       
      {
                 if(positions[i] == ' ')           
                 {                          
                         score = getFutureScoreOfMove(positions, 'C');
                         if(score > currentBestScore)       
                         {
                                num = i;
                                currentBestScore = score;
                          }
                          positions[i] = ' ';     
                 }      
      }
       positions[num] = comp;
}

And the 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 I stated I'd like to know why this weird behavior occurs and how to fix it.

Any help would be much appreciated :)

  • 4
    As you learn to program you will encounter this situation many, many times, i.e. you have a bug, and you need to *debug* it. Now is a good time to learn how to debug, either using simple methods such as inserting strategic printfs, or by learning how to use a debugger such as gdb or whatever debugger your IDE provides. Asking other people to debug your code for you is just putting off the inevitable - you are going to have to acquire debugging skills sooner or later, so better to start now. – Paul R Aug 24 '13 at 08:30
  • Assuming a blank space is discovered and the score never surpasses `currrentBestScore`, what do you think the value of `num` is when you make the final assignment `positions[num] = comp` ? – WhozCraig Aug 24 '13 at 08:32
  • 1
    This seems to be the third question about the same program that you are writing. SO is not a site for code review but for concrete technical question. – Jens Gustedt Aug 24 '13 at 08:32
  • @PaulR I've debugged the code, and it does print the symbol, but only in the next available space. – Michael Ferashire Silva Aug 24 '13 at 08:48
  • OK - so nothing changes - you still have a bug and you need to track it down. – Paul R Aug 24 '13 at 08:49
  • @PaulR I think part of my problem in not being able to track the problem down is I don't fully understand this code (I was following pseudo code given in a book). My understanding is that it intends to pass the best score possible up the branches, but i dont know each area specifically. – Michael Ferashire Silva Aug 24 '13 at 09:03
  • OK - that's a good insight - it's hard to debug code if you don't understand how it works in the first place. Maybe it's time to take a step back from the code, study the pseudo-code/algorithm a little more to get a better understanding, then return to debugging the code one you have a better grip on how it works ? – Paul R Aug 24 '13 at 09:41
  • @PaulR Yes I will. The problem is I can't seem to grasp how to apply the minimax algorithm to Tic Tac Toe (at least by searhing on google) :( – Michael Ferashire Silva Aug 24 '13 at 09:49
  • @PaulR Do you know any good websites that can help with my understanding? – Michael Ferashire Silva Aug 24 '13 at 09:57
  • Sorry - nothing specific - try and find a good book if you can. – Paul R Aug 24 '13 at 10:01
  • 1
    @MichaelFerashireSilva do you understand minimax or is it just how to apply it to tic tac toe that you don't understand? – Jonie Shih Aug 24 '13 at 14:00
  • @Jonie Shih I bekieve I understand the minmax algorithm, in that you score each possible move, theb pick the best move, using recursion. I'll try yiur suggestion of checking a win then get back to you. Thanks :) – Michael Ferashire Silva Aug 25 '13 at 02:48

1 Answers1

3

Where are you even calculating that one player has gotten 3 in a straight line? Nowhere in your code is this being calculated. You've iterated through the possible board positions with your recursion but haven't considered the winning conditions. Maybe code that in first.

Jonie Shih
  • 252
  • 1
  • 2
  • 15
  • Thanks, I'll try this then get back to you – Michael Ferashire Silva Aug 25 '13 at 02:48
  • I added a checking function at the beginning of `getFutureScoreOfMove`, but it is still grabbing the first available space – Michael Ferashire Silva Aug 25 '13 at 07:59
  • As for answering why its grabbing first available place - it's the conditional statements on lines 3,4 inside `getFutureScoreOfMove`. They're returning 100 or -100 depending on who's turn it is for the current move. This means it's doing so regardless of board position and therefore all your the possible moves are equal in terms of score. Hence, it just takes the first square (`if(score > currentBestScore)`, if you change it to >=, you'll take the last square. Try it yourself. ) – Jonie Shih Aug 25 '13 at 10:07