Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,383 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,191 people online right now. Registration is fast and FREE... Join Now!




Implementing Artificial Intelligence on a Tic Tac Toe Game

 
Reply to this topicStart new topic

Implementing Artificial Intelligence on a Tic Tac Toe Game, Need help

sergio1
post 21 Aug, 2008 - 01:36 PM
Post #1


New D.I.C Head

*
Joined: 15 Jul, 2008
Posts: 30


My Contributions


hello. What I want to do is implement artificial intelligence on my Tic Tac Toe game, listed of just listing all the possibility.



CODE


void printboard(char arrayer[][3]);  // only need to declare the 2nd of array board
char question(char board[][3],char chosen);
bool check(char board[][3],char chosen);

int _tmain(int argc, _TCHAR* argv[])
{
    
    
     char board[3][3]={{' ',' ',' '},  // this sets the board
                       {' ',' ',' '},  
                       {' ',' ',' '}};  
      
    

    
     printboard(board);
    
    int corx,cory;
    char chosen;
    do{
    cout << "what would you like to be 'X' or 'O'?";
    cin >> chosen;
    }while(chosen != 'x' && chosen != 'X'  && chosen != 'O'&& chosen != 'o'&&chosen != '0');
    
    system("cls");
    do{
    
    cout << "welcome to tic tac toe"<<endl;
    cout << "enter the cordinates in wich you wanna play"<<endl;
    cout << "you are "<<chosen<<" in the game"<<endl;
    cout << endl;
    cout << "enter cordinate x: ";
    cin >> corx;
    cout << "enter cordinate y: ";
    cin >> cory;
    system("cls");
    }while (corx >3 || cory>3);// makes sure cordinates are correct
    
    board[cory][corx]=chosen;// sets selected cordienets to 'o'

    
    int counter=1;
    bool determin;
    char error;

    printboard(board);
    do{
        error = question(board,chosen);
        if (error == 'b'){
            system("cls");
            cout << "you must enter a number between 0-2"<<endl;
            counter --;
        }
        if(error == 'f'){
            system("cls");
            cout << "the spot you chose is taken"<<endl;
            counter--;
        }
        printboard(board);
        determin=check(board,chosen);
        counter++;
    
    }while(determin == false&&counter < 5);

    if (determin == true){
        cout << "congratulations you win!"<<endl;
    }

    if (determin == false){
        cout << "you loose!!!"<<endl;
    }
    
    check(board,chosen);
    
    system("pause");
    
    return 0;
}
void printboard(char arrayer[][3]){  // only need to declare the second
  cout <<"[" <<arrayer[0][0]<<"]";
  cout <<"[" <<arrayer[0][1]<<"]";
  cout <<"[" <<arrayer[0][2]<<"]"<<endl;
  cout <<"[" <<arrayer[1][0]<<"]";
  cout <<"[" <<arrayer[1][1]<<"]";
  cout <<"[" <<arrayer[1][2]<<"]"<<endl;
  cout <<"[" <<arrayer[2][0]<<"]";
  cout <<"[" <<arrayer[2][1]<<"]";
  cout <<"[" <<arrayer[2][2]<<"]"<<endl;
}//prints board
char question(char board[][3],char chosen)
{
    int inx,iny;
    char error_size ='b';
    char error_full ='f';
    cout << "enter cordinate x: ";
    cin >> iny;

    cout << "enter cordinate y: ";
    cin >> inx;
    
    if(inx > 3 || iny > 3){
    return(error_size);
    }
    
    if(board[inx][iny] == chosen){
        return(error_full);
    }
    system("cls");
        
    return(board[inx][iny]=chosen);//makes board = '0'

}
bool check(char board[][3],char chosen){
    
    if (board[0][0] == chosen && board[0][1]==chosen && board[0][2]==chosen){
        return(true);
    }
    if (board[0][0] == chosen && board[1][0]==chosen && board[2][0]==chosen){
        return(true);
    }
    if (board[0][2] == chosen && board[1][2]==chosen && board[2][2]==chosen){
        return(true);
    }
    if (board[2][0] == chosen && board[2][1]==chosen && board[2][2]==chosen){
        return(true);
    }    
    if (board[0][0] == chosen && board[1][1]==chosen && board[2][2]==chosen){
        return(true);
    }
    if (board[2][0] == chosen && board[1][1]==chosen && board[0][2]==chosen){
        return(true);
    }
    if (board[1][0] == chosen && board[1][1]==chosen && board[1][2]==chosen){
        return(true);
    }
    if (board[0][1] == chosen && board[1][1]==chosen && board[2][1]==chosen){
        return(true);
    }
    
    //for (int i = 0; i<3;i++)
    //for(int d = 0; d<3;d++)
        
//    if (board[i][d] != ' ')
//    {
//        cout << "its a tie"<<endl;
//        return(false);
//    }
    
    else{
         return(false);
         }
}



I'm conmpleatly stumped and would greatly arpricate it if someone could point me in the right direction or even show a working example.

This post has been edited by sergio1: 21 Aug, 2008 - 01:39 PM
User is offlineProfile CardPM

Go to the top of the page

BetaWar
post 21 Aug, 2008 - 02:22 PM
Post #2


#include <soul.h>

Group Icon
Joined: 7 Sep, 2006
Posts: 1,983



Thanked 78 times

Dream Kudos: 1175
My Contributions


I made one of these in 9th grade it isn't too bad if you are willing to place a few loops or a lot of if/else statements. Just have a check run through all the possible ways to win and if the user is 2/3 and it is the computer's turn have the computer take one of the spaces that the user could use to win (Not you will have to make sure that you only allow it to choose one or it will take as many as it can get).

If I can find the source code I will post it, but that was quite a while ago.

If you are trying to make the computer learn as it goes (like intelligence) you will have to create a fitness variable and have it basically look at what has been played and see if it wins or loses. If it loses then the fitness would go down (a bad result) if it wins the fitness goes up (good) and so on. Then you will also have to allow it to save the games history so it can see what was played and how that turned out for the computer. Theoretically eventually the computer will get to the point it can't be beaten.
User is offlineProfile CardPM

Go to the top of the page

sergio1
post 21 Aug, 2008 - 03:01 PM
Post #3


New D.I.C Head

*
Joined: 15 Jul, 2008
Posts: 30


My Contributions


well, that's good because I'm in 9th grade smile.gif.

Can you be a little more specific mabie, add a link or a book title?
User is offlineProfile CardPM

Go to the top of the page

KYA
post 21 Aug, 2008 - 05:01 PM
Post #4


#include <nerd.h>

Group Icon
Joined: 14 Sep, 2007
Posts: 4,205



Thanked 50 times

Dream Kudos: 1150
My Contributions


The idea is this:

cpp

//some what pseudo code
if (playerAboutToWin())
//play in the avilable space

//stuff

void playerAboutToWin()
{
//check to see if there is a combination of any two "slots" that the player
//has, if so, find the "connecting" square and play there
}
User is offlineProfile CardPM

Go to the top of the page

BetaWar
post 21 Aug, 2008 - 07:46 PM
Post #5


#include <soul.h>

Group Icon
Joined: 7 Sep, 2006
Posts: 1,983



Thanked 78 times

Dream Kudos: 1175
My Contributions


exactly what KYA said. YOu have a lot of if statements (is simple to do but time consuming) to check for every possible thing and counter it.

For example:

CODE

if(position[0] == "x" && position[] == "x" && position[2] == ''){
  position[2] = "o";
}
else if(...){
...
}
...


I didn't actually use any books, at the time I was just playing around with the language. Now I still don't use books but I have found a nice community to point me in the right direction when I get stuck.

Hope that helps.
User is offlineProfile CardPM

Go to the top of the page

dwayne
post 22 Aug, 2008 - 11:29 AM
Post #6


New D.I.C Head

*
Joined: 2 Aug, 2008
Posts: 39


My Contributions


A GUI would be a nice edition before you go learning about AI. Try "Advanced 3D Game Programming All in One". I remember seeing a book at BAM one day about C++ Game Development. It focused more on "behind the scenes" stuff too, as in, the game engine, etc. It also had AI as well though I ended up not buying it (a decision I regret).
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/22/08 06:30AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month