You are on page 1of 3

#include<iostream.

h>
#include<conio.h>
#include<process.h>
#include<string.h>
char b1[]={"|
|
|
|\n"};
char b2[]={"|
|
|
|\n"};
char b3[]={"|
|
|
|\n"};
char c1[]={"|1
|2
|3
|\n"};
char c2[]={"|4
|5
|6
|\n"};
char c3[]={"|7
|8
|9
|\n"};
void box(char[],char[]);
void chan_p1(int);
void chan_p2(int);
int checkwin();
int a=0,i=0;
void main()
{
clrscr();
char p1[20],p2[20];
int m1[5],m2[5];
cout<<"Enter name of player 1:";
cin>>p1;
cout<<"Enter name of player 2:";
cin>>p2;
box(p1,p2);
for(int i=1;i<=5;i++)
{
x:
cout<<p1<<" : Enter your move:";
cin>>m1[i];
chan_p1(m1[i]);
box(p1,p2);
if(a==1)
{
cout<<"Invalid move!!!!\n";
goto x;
}
if(i==5)
{
//
cout<<" Game draw ";
}
else
{
y:
cout<<p2<<" : Enter your move:";
cin>>m2[i];
chan_p2(m2[i]);
box(p1,p2);
if(a==1)
{
cout<<"Invalid move!!!!\n";
goto y;
}
}
}
getch();
}
void box(char p1[],char p2[])
{
clrscr();

// X/O holder box 1


// X/O holder box 2
// X/O holder box 3
// Validity checker box 1
// Validity checker box 2
// Validity checker box 3
// Display board
// X/O input reader for player 1
// X/O input reader for player 2
// Checks who wins
//
// main function
//
// clears the screen
// declaring names of players
// declares position to enter
//
// get p1
//
// get p2
// displays board
// game starts
//
// label x for p1 (input for p1)
//
// get moves 1 by one
// function sending position
// displaying current board
// //if a is 1, it says that the
//
desired position is invalid
//
for storing and now the pgm
//
asks for re-value.
//
// //In this game, the second user
//
always gets only 4 chances.
Therefore, when i=5, means all
//
the positions are occupied.
//
So, the game is draw. And also
//
p1 ends the last move. so,
//
the condition is checked after
//
p1's move.
//
// label y for p2(input for p2)
//
Same as for p1
//
//
//
//
//
//
//
//
//
// //Function to display X/O holder
//
and validity checker box.
//

cout<<"|-----|-----|-----|\n";
cout<<c1;
cout<<b1;
cout<<"|
|
|
|\n";
cout<<"-------------------\n";
cout<<c2;
cout<<b2;
cout<<"|
|
|
|\n";
cout<<"-------------------\n";
cout<<c3;
cout<<b3;
cout<<"|
|
|
|\n";
cout<<"-------------------\n";
char pl1[20]; strcpy(pl1,p1);
char pl2[20]; strcpy(pl2,p2);
i=checkwin();
if(i==1)
{
cout<<pl1<<":
 WIN ";
getch();
exit(0);
}
else if(i==2)
{
cout<<pl2<<":
 WIN ";
getch();
exit(0);
}
else
cout<<"Contine-->\n";
}
void chan_p1(int pos)
{
if(pos==1&&c1[2]==' ')
{b1[3]='X'; c1[2]=''; a=0; }
else if(pos==2&&c1[8]==' ')
{b1[9]='X'; c1[8]=''; a=0; }
else if(pos==3&&c1[14]==' ')
{b1[15]='X'; c1[14]=''; a=0; }
else if(pos==4&&c2[2]==' ')
{b2[3]='X'; c2[2]=''; a=0; }
else if(pos==5&&c2[8]==' ')
{b2[9]='X'; c2[8]=''; a=0; }
else if(pos==6&&c2[14]==' ')
{b2[15]='X'; c2[14]=''; a=0; }
else if(pos==7&&c3[2]==' ')
{b3[3]='X'; c3[2]=''; a=0; }
else if(pos==8&&c3[8]==' ')
{b3[9]='X'; c3[8]=''; a=0; }
else if(pos==9&&c3[14]==' ')
{b3[15]='X'; c3[14]=''; a=0; }
else
a=1;
}
void chan_p2(int pos)
{
if(pos==1&&c1[2]==' ')
{b1[3]='O'; c1[2]=''; a=0; }
else if(pos==2&&c1[8]==' ')
{b1[9]='O'; c1[8]=''; a=0; }

//
//
//
//
//
//
//
//
//
//
//
//
//
// //Simply copying p1 and p2
//
to pl1 and pl2. From string.h
// Function to check who won
//
//
//
//
// From process.h
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//

//if :i: doesn't store 1 or 2


means the game continuing
Function to check availability
and changes the desired position
X
//The function checks whether
the desired position is empty
or occupied. If empty changes
to X. If not, stores a=1
else always stores 0
//The reason for initialising
is is given after the
funtion is completely
executed.

//same as chan_p1 function

else if(pos==3&&c1[14]==' ')


{b1[15]='O'; c1[14]=''; a=0;
else if(pos==4&&c2[2]==' ')
{b2[3]='O'; c2[2]=''; a=0;
else if(pos==5&&c2[8]==' ')
{b2[9]='O'; c2[8]=''; a=0;
else if(pos==6&&c2[14]==' ')
{b2[15]='O'; c2[14]=''; a=0;
else if(pos==7&&c3[2]==' ')
{b3[3]='O'; c3[2]=''; a=0;
else if(pos==8&&c3[8]==' ')
{b3[9]='O'; c3[8]=''; a=0;
else if(pos==9&&c3[14]==' ')
{b3[15]='O'; c3[14]=''; a=0;
else
a=1;
}

//
//
//
}
//
//
}
//
//
}
//
//
}
//
//
}
//
//
}
//
//
//
//
//
int checkwin()
//
{
//
if(b1[3] == b1[9] && b1[9] == b1[15]) //
{ if(b1[9]=='X')
return 1;
//
else if(b1[9]=='O') return 2; }
//
else if(b2[3]==b2[9]&&b2[9]==b2[15]) //
{if(b2[9]=='X')
return 1;
//
else if(b2[9]=='O') return 2; }
//
else if(b3[3]==b3[9]&&b3[9]==b3[15]) //
{ if(b3[9]=='X')
return 1;
//
else if(b3[9]=='O') return 2; }
//
else if(b1[3]==b2[3]&&b2[3]==b3[3])
//
{ if(b2[3]=='X')
return 1;
//
else if(b2[3]=='O') return 2; }
//
else if(b1[9]==b2[9]&&b2[9]==b3[9])
//
{ if(b1[9]=='X')
return 1;
//
else if(b1[9]=='O') return 2; }
//
else if(b1[15]==b2[15]&&b2[15]==b3[15])//
{ if(b1[15]=='X')
return 1;
//
else if(b1[15]=='O') return 2; }
//
else if(b1[3]==b2[9]&&b2[9]==b3[15]) //
{ if(b2[9]=='X')
return 1;
//
else if(b2[9]=='O') return 2; }
//
else if(b1[15]==b2[9]&&b2[9]==b3[3]) //
{ if(b2[9]=='X')
return 1;
//
else if(b2[9]=='O') return 2; }
//
return 0;
}
//
}

You might also like