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