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