I have developed a player v/s player game for tic tac toe..but can't understand the logic for AI ..can someone please include the AI segment in my code because i so much need it for my school project
#include<iostream.h>
#include<conio.h>
#include<string.h>
char grid [3][3];
void display_grid()
{
for(int i=0;i<3;i++)
{
cout<<"\n\t_____________\n\t| ";
for(int j=0;j<3;j++)
cout<<grid[i][j]<<" | ";
}
cout<<"\n\t_____________\n\t";
}
int checkwin()
{
int i,j,c=0;char ch;
for(i=0;i<3;i++)
{
c=0;
ch=grid[i][0];
for(j=1;j<3;j++)
if(ch!=grid[i][j]){c=1;break;}
if(c==0){if(ch=='X')return 1; else if(ch=='O') return 2;}
}
for(j=0;j<3;j++)
{
c=0;
ch=grid[0][j];
for(i=1;i<3;i++)
if(ch!=grid[i][j]){c=1;break;}
if(c==0){if(ch=='X')return 1; else if(ch=='O')return 2;}
}
c=0;
ch=grid[0][0];
for(i=1;i<3;i++)
{
if(grid[i][i]!=ch){c=1;break;}
}
if(c==0){if(ch=='X')return 1; else if(ch=='O') return 2;}
c=0;
ch=grid[0][3-1];
for(i=1;i<3;i++)
{
if(grid[i][3-i-1]!=ch){c=1;break;}
}
if(c==0){if(ch=='X')return 1; else if(ch=='O') return 2;}
else return 0;
}
void main()
{
clrscr();
int r,c;
int flag=0;
int count=0;
while(flag==0)
{
P1:
cout<<"\n\n Player 1\n\n row=";cin>>r;cout<<" column=";cin>>c;
if((r>0 && r<4)&&(c>0 && c<4))
{grid[r-1][c-1]='X';count++;}
else
{cout<<"\n Invalid, Try Again..!";goto P1;}
display_grid();
flag=checkwin();
if(flag==1){cout<<"\n Player 1 WINS ";break;}else if(flag==2){cout<<"\n Player 2 WINS ";break;}
if(count==9){cout<<"\n Draw..!";break;}
P2:
cout<<"\n\n Player 2\n\n row=";cin>>r;cout<<" column=";cin>>c;
if((r>0 && r<4)&&(c>0 && c<4)&&grid[r-1][c-1]=='\0')
{grid[r-1][c-1]='O';count ++;}
else
{cout<<"\n Invalid, Try Again..!";goto P2;}
display_grid();
flag=checkwin();
if(flag==1){cout<<"\n Player 1 WINS ";break;}else if(flag==2){cout<<"\n Player 2 WINS ";break;}
if(count==9){cout<<"\n Draw..!";break;}
}
getch();
}