Thread: Help with Tic Tac Toe with AI

  1. #1
    Registered User
    Join Date
    May 2018
    Posts
    4

    Help with Tic Tac Toe with AI

    I'm having trouble making the AI work. I would like to have the AI be competitive and make you think on where to go. The AI code starts at aimove, right now the AI just goes into the corners if the center square is taken. If the center square is not taken it would take it then go to the corners. The AI needs to go anywhere after the player moves. X=88, O=79 using ascii table. Sometimes the X's turn into a ? where you did not go to. If you guys/gals can give any tips or help it will be appreciated.
    Code:
    # include <iostream>
    # include <cstdlib>
    # include <ctime>
    using namespace std;
    ///////////////////////////////////////////////////////////////////////
    int printboard(char board[3][3])
    {
        int X,Y;
        char cyan[] = { 0x1b, '[', '0', ';', '3', '6', 'm', 0 };
        char normal[] = { 0x1b, '[', '0', ';', '3', '9', 'm', 0 };
        system("clear");
        for (Y=0;Y<3;Y++)
        {
            for (X=0;X<3;X++)
            {
                cout<<" "<<board[X][Y];
                if (X<2) cout<<cyan<<"║"<<normal;
            }
            cout<<"\n";
            if (Y<2) cout<<cyan<<"══╬══╬══\n"<<normal;
        }
        return 0;
    }
    ///////////////////////////////////////////////////////////////////////
    int initboard(char board[3][3])
    {
        int X,Y;
        for (Y=0;Y<3;Y++)
            for (X=0;X<3;X++)
                board[X][Y]=X+1+Y*3+48;
        return 0;
    }
    ///////////////////////////////////////////////////////////////////////
    int playermove(char board[3][3])
    {
        int X,Y,Z,VALID;
        do
        {
            cout<<"Where do You want to place Your X? ";
            cin>>Z;
            VALID=1;
            for (Y=0;Y<3;Y++)
                for (X=0;X<3;X++)
                    if(board[X][Y]==(Z+48))
                    {
                        board[X][Y]=88;
                        VALID=1;
                    }
        }while (!VALID);
        return 0;
    }
    ///////////////////////////////////////////////////////////////////////
    int checkforwin(char board[3][3])
    {
        int X,Y,CAT=-1;
        for (Y=0;Y<3;Y++)    //Did X win vertically
            if ((board[0][Y]+board[1][Y]+board[2][Y])==264) return 1;
        for (X=0;X<3;X++)    //Did X win Horizontally
            if ((board[X][0]+board[X][1]+board[X][2])==264) return 1;
            if ((board[0][0]+board[1][1]+board[2][2])==264) return 1;    //Did X win Diagonal1
            if ((board[2][0]+board[1][1]+board[0][2])==264) return 1;    //Did X win Diagonal2
    
    
        for (Y=0;Y<3;Y++)    //Did O win vertically
            if ((board[0][Y]+board[1][Y]+board[2][Y])==237) return 2;
        for (X=0;X<3;X++)    //Did O win Horizontally
            if ((board[X][0]+board[X][1]+board[X][2])==237) return 2;
            if ((board[0][0]+board[1][1]+board[2][2])==237) return 2;    //Did O win Diagonal1
            if ((board[2][0]+board[1][1]+board[0][2])==237) return 2;    //Did O win Diagonal2
            
        for (Y=0;Y<3;Y++)    //If board is full
            for (X=0;X<3;X++)
                if ((board[X][Y]!=88)&&(board[X][Y]!=79))    CAT=0;
        return CAT;
    }
    ///////////////////////////////////////////////////////////////////////
    int aimove(char board[3][3])
    {
        srand(time(NULL));
        int randX,randY,X,Y;
        if (board[1][1]<79)        //If center square is not taken, take it
        {
            board[1][1]=79;
            return 0;
        }
        if (board[1][1]==88)        //If center square is taken, move somewhere else
            {
                randX=rand()%2*2;
                randY=rand()%2*2;
                board[randX][randY]=79;
                return 0;
            }
        for (Y=0;Y<3;Y++)        //Looks for Horizontal block
            for (X=0;X<3;X++)
            {
                if ((board[X][Y])+(board[X+1][Y]=176)) board[X+2][Y]=79;
                cout<<board[X][Y]<<board[X+1][Y]<<board[X+2][Y]<<"\n";
                return 1;
            }
        for (Y=0;Y<3;Y++)        //Looks for Vertical block
            for (X=0;X<3;X++)
            {
                if ((board[X][Y])+(board[X][Y+1]=176)) board[X][Y+2]=79;
                return 1;
            }
        do
        {
            randX=rand()%3;
            randY=rand()%3;
            //cout<<"("<<randX<<","<<randY<<")\n";
        }while (!(board[randX][randY]<79));
            board[randX][randY]=79;
                /*if ((board[X][Y]!=88)&&(board[X][Y]!=79))
                {
                    randX=rand()%2;
                    randY=rand()%2;
                    board[randX][randY]=79;    
                }*/
        return 0;
    }
    ///////////////////////////////////////////////////////////////////////
    int main()
    {
        char board[3][3];            //Declare Variables
        //system("espeak -s 130 'Herro I am TicTacToe'");
        initboard(board);
        do
        {
            printboard(board);
            playermove(board);
            printboard(board);
            if (!checkforwin(board)) aimove(board);
        }while (!checkforwin(board));
        if (checkforwin(board)==1) cout<<"Player Wins";
        //if (checkforwin(board)==1) system("espeak -p 30 'Player Wins'");
        if (checkforwin(board)==2) cout<<"AI Wins";
        //if (checkforwin(board)==2) system("espeak -p 60 'AI Wins'");
        if (checkforwin(board)==-1) cout<<"CAT - It's a tie";
        //if (checkforwin(board)==-1) system("espeak -p 70 'MEOW!'");
        return 0;
    }
    Last edited by Gage_Hadley; 05-08-2018 at 07:58 AM.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    This line should be in the main function.

    Code:
    srand(time(NULL));
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Gage_Hadley
    X=88, O=79 using ascii table.
    Not relevant: use 'X' and 'O' instead of these magic numbers. Likewise, use '0' instead of 48 (especially since the digits are guaranteed to be contiguous in value, so adding 1 to 9 to '0' is guaranteed to result in '1' to '9' respectively).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Tags for this Thread