Advertisement

Minimax applied to Tic-Tac-Toe

Started by October 17, 2005 06:30 PM
4 comments, last by White Scorpion 19 years, 1 month ago
Hi guys/girls. I've been trying to learn how to program some kind of AI for the first time now and I have troubles with the minimax algorithm. I have coded this so far, it should display the result of a game played between 2 computers, but displaying the array showed me that the array contains '1 _ _ _ _ _ _ _ _' when it should be full of '1's and '2's. Could you guys explain me where I am wrong ?
#include <iostream>

const int INFINITY=10000;
const int WIN_BOARD[8][3]={
    {0,1,2},{3,4,5},{6,7,8},{0,3,6},
    {1,4,7},{2,5,8},{0,4,8},{2,4,6}
};

void ShowBoard(char*);
int Evaluate(char*,int);
int MinMax(char*,int,int);
void Think(char*,int,int);

int main(){
    char array[9];
    for(int i=0;i<9;++i){
        array=' ';
    }
    for(int i=0;i<9;i++){
        ShowBoard(array);
        int player=(i%2)?2:1;
        if(player==1){
            int x;
            do{
                std::cin>>x;
            }while(array[x]!=' ');
            array[x]=player+'0';
        }
        else{
            Think(array,player,9-i);
        }
    }
    std::cin.get();
}

void ShowBoard(char* array){
    for(int i=0;i<9;i++){
        std::cout<<array;
        if(!((i+1)%3))
            std::cout<<".\n";
    }
    std::cout<<std::endl;
}

int Evaluate(char* array,int player){
    int best=-INFINITY;
    int sum;
    for(int i=0;i<8;i++){
        if(array[WIN_BOARD[0]]==player&&array[WIN_BOARD[1]]==player
            &&array[WIN_BOARD[2]]==player)
            return INFINITY;
    }
    return 0;
}

void Think(char* array,int player,int depth){
    int val;
    int best=-INFINITY;
    int index=0;

    for(int i=0;i<9;i++){
        if(array==' '){
            array=player+'0';
            val=-MinMax(array,player,depth-1);
            array=' ';
            if(val>best){
                best=val;
                index=i;
            }
        }
    }
    array[index]=player+'0';
}

int MinMax(char* array,int player,int depth){
    int val;
    int best=-INFINITY;
    
    if(depth<=0)
        return Evaluate(array,player);
    for(int i=0;i<9;i++){
        if(array==' '){
            array=player+'0';
            val=-MinMax(array,player,depth-1);
            array=' ';
            if(val>best){
                best=val;
            }
        }
    }
    return best;
}
[/syntax]

[Edited by - White Scorpion on October 19, 2005 8:00:52 PM]
You are not initializing the board array properly. The array is 9 chars, but you are only initializing one char.
Advertisement
It now outputs "2 1 1 1 1 _ _ _ _". :\
I've got it working, now, but the AI is stupid as hell. In fact, I can't call it AI at all. It chooses the first empty spot and chooses it as its best move. How can I modify the code so that the AI is not so stupid ? I would guess that my evaluation function is incorrect.
Your evaluate function posted above is always returning 0.
Meh, :/ I must have copied an old version of my code, I updated it. It doesn't have that problem (always returning 0).

This topic is closed to new replies.

Advertisement