Hello, I have been researching the minimax algorithm for a few days, but I can't apply it properly. I have read that for the 8 lines in Tic Tac Toe, you give certain scores. However, I don't understand what they are trying to tell me. Could I have a simple explaination of the algorithm and how to apply it?

Thanks :)

The algorithm part that I understood is to play all possible moves, and get the best possible score, so I made this piece of code:

int getFutureScore(char square[], char turn)    
{
      int num, currentBest, score;

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

       for(num=0; num<9; num++)
       {
                    if(square[num] == ' ')
                    {
                           if(turn == 'C')      
                           {
                               square[num] = 'O';
                               score = getFutureScore(positions, 'U'); 
                               square[num] = ' ';
                           }
                           else                  
                           {
                                square[num] = 'X';
                                score = getFutureScore(positions, 'C');
                                square[num] = ' ';
                           }
                           if(turn == 'C' && score > currentBest)                                    
                          currentBest= score;
                           if(turn == 'U' && score < currentBest)   
                          currentBest= score;
                    }       
          }   
         return(currentBest);                    
}

Edited 3 Years Ago by Seth Lam

This isn't complete. The variables currentBestScore and positions are not defined here. Did you mean currentBest, and square instead?

Comments
Yes I'm sorry, mixed up my variables

Here's a code update:

int getFutureScore(char square[], char turn)    
{
      int num, currentBest, score;
       if(turn == 'C')                       currentBest = -100;       
       else                                     currentBest = 100;        
       for(num=0; num<9; num++)
       {
                    if(square[num] == ' ')
                    {
                           if(turn == 'C')      
                           {
                               square[num] = 'O';
                               score = getFutureScore(square, 'U'); 
                               square[num] = ' ';
                           }
                           else                  
                           {
                                square[num] = 'X';
                                score = getFutureScore(square, 'C');
                                square[num] = ' ';
                           }
                           if(turn == 'C' && score > currentBest)                                    
                          currentBest= score;
                           if(turn == 'U' && score < currentBest)   
                          currentBest= score;
                    }       
          }   
         return(currentBest);                    
}
This article has been dead for over six months. Start a new discussion instead.