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 :)

Recommended Answers

All 6 Replies

Do you have code to post? A description of the algorithm? What you are saying is not enough to help you.

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);                    
}

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

commented: Yes I'm sorry, mixed up my variables +0

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);                    
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.