#include<iostream.h>
#include<conio.h>
#include <graphics.h>
#include <stdlib.h>
#include<dos.h>
#include <stdio.h>
const int player1Col = RED;
const int player2Col = GREEN;
int t=0,flag=0,totalgames=0,draw=0,wins=0,lost=0;
void drawPiece(int centerX, int centerY, int playerColor, int pieceRadius)
{
setcolor(playerColor);
for(int i = 0; i < pieceRadius/3; i++)
{
circle(centerX, centerY, pieceRadius-i);
}
line(centerX-pieceRadius/3,centerY,centerX+pieceRadius/3,centerY);
line(centerX,centerY-pieceRadius/3,centerX,centerY+pieceRadius/3);
circle(centerX, centerY,2);
setcolor(getmaxcolor());
return;
}
//-----------------------------------------------------------------------
void winner(int w)
{
int main();
int op;
if(w==2)
{
lost++;
gotoxy(0,6);
cout<<" <<<<YOU LOSE>>>> ";
}
else
if(w==1)
{
wins++;
gotoxy(0,6);
cout<<" <<<<<YOU WIN>>>>>> ";
}
else if(w==0)
{
draw++;
gotoxy(0,6);
cout<<" <<<<<GAME DRAWN>>>> ";
}
delay(2000);
gotoxy(16,28);
cout<<"PRESS '1' TO CONTINUE AND '0' TO EXIT ";
cin>>op;
if(op==1)
{
totalgames++;
t--;
main();
}
else if (op==0)
{
closegraph();
}
}
//*---------------------------------------------------------
void descion(int board[][3])
{
int win=0,turn=0;
if((board[0][0]==1)&&(board[1][0]==1)&&(board[2][0]==1))
win=1;
if((board[0][0]==1)&&(board[0][1]==1)&&(board[0][2]==1))
win=1;
if((board[0][0]==1)&&(board[1][1]==1)&&(board[2][2]==1))
win=1;
if((board[1][0]==1)&&(board[1][1]==1)&&(board[1][2]==1))
win=1;
if((board[2][0]==1)&&(board[2][1]==1)&&(board[2][2]==1))
win=1;
if((board[2][0]==1)&&(board[1][1]==1)&&(board[0][2]==1))
win=1;
if((board[0][1]==1)&&(board[1][1]==1)&&(board[2][1]==1))
win=1;
if((board[0][2]==1)&&(board[1][2]==1)&&(board[2][2]==1))
win=1;
if((board[0][0]==2)&&(board[1][0]==2)&&(board[2][0]==2))
win=2;
if((board[0][0]==2)&&(board[0][1]==2)&&(board[0][2]==2))
win=2;
if((board[0][0]==2)&&(board[1][1]==2)&&(board[2][2]==2))
win=2;
if((board[1][0]==2)&&(board[1][1]==2)&&(board[1][2]==2))
win=2;
if((board[2][0]==2)&&(board[2][1]==2)&&(board[2][2]==2))
win=2;
if((board[2][0]==2)&&(board[1][1]==2)&&(board[0][2]==2))
win=2;
if((board[0][1]==2)&&(board[1][1]==2)&&(board[2][1]==2))
win=2;
if((board[0][2]==2)&&(board[1][2]==2)&&(board[2][2]==2))
win=2;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if((board[j][i]==1)||(board[j][i]==2))
turn++;
}
}
if(win!=1&&win!=2&&turn==9)
{
winner(0);
}
if(win==1||win==2)
{
winner(win);
}
if((win!=1)||(win!=2))
if(turn!=9)
return ;
}
//-----------------------------------------------------------------
void turnFlag(int board[][3])
{
void humanPlayer(int [][3]);
void computer(int [][3]);
descion(board);
if(flag==0)
{
flag=(flag+1)%2;
humanPlayer(board);
}
else
{
flag=(flag+1)%2;
computer(board);
}
}
//-------------------------------------------------------------
void humanPlayer(int board[][3])
{
void turnOutput(int [][3]);
drawPiece(20,20,player1Col, 15); // to display the turn of player
int tenth,unit,move;
gotoxy(16,28);
cout<<"enter row and column respectively:";
cin>>move;
tenth=move/10;
unit=move%10;
while(unit<1||unit>3||tenth<1||tenth>3||board[tenth-1][unit-1]!=0)
{
gotoxy(16,28);
cout<<" ";
cout<<"Please Enter Correct Value Or Invalid Input ";
cin>>move;
tenth=move/10;
unit=move%10;
}
if(board[tenth-1][unit-1]==0)
{
board[tenth-1][unit-1]=1;
turnOutput(board);
turnFlag(board) ;
}
}
//---------------------------------------------------------------
void computer(int board[][3])
{
gotoxy(16,28);
cout<<" ";
void turnOutput(int [][3]);
drawPiece(20,20,player2Col, 15); // to display the turn of player
randomize();
delay(2000);
while (t==0)
{
int x=rand()%3;
int y=rand()%3;
if(board[y][x]==0)
{
board[y][x]=2;
turnOutput(board);
t++;
turnFlag(board);
}
else
{
t=0;
}
}
if(board[0][0]==2&&board[2][0]==2&&board[1][0]==0)
{
board[1][0]=2;
turnOutput(board);
turnFlag(board);
}
else if(board[0][1]==2&&board[2][1]==2&&board[1][1]==0)
{
board[1][1]=2;
turnOutput(board);
turnFlag(board);
}
else if(board[0][0]==2&&board[1][1]==2&&board[2][2]==0)
{
board[2][2]=2;
turnOutput(board);
turnFlag(board);
}
else if(board[1][1]==2&&board[2][0]==2&&board[0][2]==0)
{
board[0][2]=2;
turnOutput(board);
turnFlag(board);
}
else if(board[0][2]==2&&board[1][1]==2&&board[2][0]==0)
{
board[2][0]=2;
turnOutput(board);
turnFlag(board);
}
else if(board[1][1]==2&&board[2][2]==2&&board[0][0]==0)
{
board[0][0]=2;
turnOutput(board);
turnFlag(board);
}
else if(board[0][0]==2&&board[0][2]==2&&board[0][1]==0)
{
board[0][1]=2;
turnOutput(board);
turnFlag(board);
}
else if(board[0][0]==2&&board[2][2]==2&&board[1][1]==0)
{
board[1][1]=2;
turnOutput(board);
turnFlag(board);
}
else if(board[1][0]==2&&board[1][2]==2&&board[1][1]==0)
{
board[1][1]=2;
turnOutput(board);
turnFlag(board);
}
else if(board[2][0]==2&&board[0][2]==2&board[1][1]==0)
{
board[1][1]=2;
turnOutput(board);
turnFlag(board);
}
else if(board[2][0]==2&&board[2][2]==2&board[2][1]==0)
{
board[2][1]=2;
turnOutput(board);
turnFlag(board);
}
else if(board[0][2]==2&&board[2][2]==2&&board[1][2]==0)
{
board[1][2]=2;
turnOutput(board);
turnFlag(board);
}
else if(board[0][0]==2&&board[1][0]==2&&board[2][0]==0)
{
board[2][0]=2;
turnOutput(board);
turnFlag(board);
}
else if(board[0][1]==2&&board[1][1]==2&&board[2][0]==0)
{
board[2][0]=2;
turnOutput(board);
turnFlag(board);
}
else if(board[0][2]==2&&board[1][2]==2&&board[2][2]==0)
{
board[2][2]=2;
turnOutput(board);
turnFlag(board);
}
else if(board[0][1]==2&&board[1][2]==2&&board[0][1]==0)
{
board[0][1]=2;
turnOutput(board);
turnFlag(board);
}
else if(board[1][1]==2&&board[1][2]==2&&board[1][0]==0)
{
board[1][0]=2;
turnOutput(board);
turnFlag(board);
}
else if(board[2][1]==2&&board[2][2]==2&&board[2][0]==0)
{
board[2][0]=2;
turnOutput(board);
turnFlag(board);
}
else if(board[0][0]==2&&board[0][1]==2&&board[0][2]==0)
{
board[0][2]=2;
turnOutput(board);
turnFlag(board);
}
else if(board[1][0]==2&&board[1][1]==2&&board[1][2]==0)
{
board[1][2]=2;
turnOutput(board);
turnFlag(board);
}
else if(board[2][0]==2&&board[2][1]==2&&board[2][2]==0)
{
board[2][2]=2;
turnOutput(board);
turnFlag(board);
}
else if(board[1][0]==2&&board[2][0]==2&&board[0][0]==0)
{
board[0][0]=2;
turnOutput(board);
turnFlag(board);
}
else if(board[1][1]==2&&board[2][1]==2&&board[0][1]==0)
{
board[0][1]=2;
turnOutput(board);
turnFlag(board);
}
else if(board[1][2]==2&&board[2][2]==2&&board[0][2]==0)
{
board[0][2]=2;
turnOutput(board);
turnFlag(board);
}
if(board[0][0]==2&&board[2][0]==2&&board[1][0]==0)
{
board[