Comp Proj 12
Comp Proj 12
VIVEK SWAIN
Class 12
SIGNATURE
ACKNOWLEDGEMENT
I express my deep sense of gratitude to my parents for their love and support
all over the way, giving us valuable solutions and new ideas.
(Vivek Swain)
Code:
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
#include<fstream.h>
#include<stdlib.h>
struct InputNumber
char inpno1[30];
char inpno2[30];
}s;
if(n==2)
{
while(s.inpno1[i]!=NULL)
if(s.inpno1[i]==c)
count++;
i++;
return count;
else if(n==1)
while(s.inpno2[i]!=NULL)
if(s.inpno2[i]==c)
count++;
}
i++;
return count;
else
return 0;
int i=0,same=0;
if(n==1)
for(i=0;gno[i]!=NULL;i++)
if(s.inpno1[i]==gno[i])
{
same++;
return same;
else if(n==2)
for(i=0;gno[i]!=NULL;i++)
if(s.inpno2[i]==gno[i])
same++;
return same;
else
return 0;
}
int pass (char tryno [30],int n)
int i=0,simdig=0;
if(n==2)
while(tryno[i]!=NULL)
int m=check(tryno[i],1);
simdig=simdig+m;
i++;
return simdig;
else if(n==1)
while(tryno[i]!=NULL)
{
int m=check(tryno[i],2);
simdig=simdig+m;
i++;
return simdig;
else
return 0;
if(n==2)
if(strcmpi(s.inpno1,guess)==0)
return 1;
}
else if(n==1)
if(strcmpi(s.inpno2,guess)==0)
return 1;
else
return 0;
return 0;
int player1()
char guess1[30];
cin>>guess1;
if(checkwin(guess1,1)==1)
{
cout<<"Congratulations! Player 1 won!"<<endl;
return 0;
else
int dig=pass(guess1,2);
int pldig=place(guess1,2);
return 1;
int player2()
char guess2[30];
cin>>guess2;
if(checkwin(guess2,2))
{
return 0;
else
int dig=pass(guess2,1);
int pldig=place(guess2,1);
return 1;
void startgame()
int d;
cin>>s.inpno1;
clrscr();
cin>>s.inpno2;
clrscr();
char A[3][3];
void board()
clrscr();
cout<<"_________________"<<endl<<endl;
cout<<"_________________"<<endl<<endl;
cout<<" "<<A[2][0]<<" "<<char(179)<<" "<<A[2][1]<<"
"<<char(179)<<" "<<A[2][2]<<" "<<endl;
int checkwin()
if(A[0][0]==A[0][1]&&A[0][1]==A[0][2]&&(A[0][0]=='X'||A[
0][0]=='O'))
return 1;
else
if(A[1][0]==A[1][1]&&A[1][1]==A[1][2]&&(A[1][0]=='X'||A[1][
0]=='O'))
return 1;
else
if(A[2][0]==A[2][1]&&A[2][1]==A[2][2]&&(A[2][0]=='X'||A[
2][0]=='O'))
return 1;
else
if(A[0][0]==A[1][0]&&A[1][0]==A[2][0]&&(A[0][0]=='X'||A[
0][0]=='O'))
return 1;
else
if(A[0][1]==A[1][1]&&A[1][1]==A[2][1]&&(A[0][1]=='X'||A[0][
1]=='O'))
return 1;
else
if(A[0][2]==A[1][2]&&A[1][2]==A[2][2]&&(A[0][2]=='X'||A[
0][2]=='O'))
return 1;
else
if(A[0][0]==A[1][1]&&A[1][1]==A[2][2]&&(A[0][0]=='X'||A[0
][0]=='O'))
return 1;
else
if(A[0][2]==A[1][1]&&A[1][1]==A[2][0]&&(A[0][2]=='X'||A[0
][2]=='O'))
return 1;
else
return 0;
int x,y,x1,y1,c1,c2;
void compin()
{
x1=random(3);
y1=random(3);
while(A[x1][y1]!='X'&&A[x1][y1]!='O')
return;
compin();
void uinput()
cin>>c1>>c2;
void dualuser()
for(int i=0;i<5;i++)
uinput();
A[c1][c2]='X';
board();
int a=checkwin();
if(a)
cout<<"Player 1 Won\n";
getch();
return;
if(i<4)
uinput();
A[c1][c2]='O';
board();
int b=checkwin();
if(b)
cout<<"Player 2 Won\n";
getch();
return;
}
}
cout<<"Match drawn\n";
getch(
);
int p,q;
A[0][0]='X';
board();
cin>>p>>q;
if((p==1)&&(q==0))
A[1][0]='O';
board();
A[0][2]='X';
board();
cout<<"Enter: ";
cin>>p>>q;
if((p==0)&&(q==1))
A[0][1]='O';
board();
A[2][2]='X';
board();
cout<<"Enter: ";
cin>>p>>q;
if((p==1)&&(q==1))
A[1][1]='O';
board();
A[1][2]='X';
board();
cout<<"Computer wins"<<endl;
getch();
return;
else
A[p][q]='O';
board();
A[1][1]='X';
board();
cout<<"Computer wins"<<endl;
getch();
return;
else
A[p][q]='O';
board();
A[0][1]='X';
board();
cout<<"Computer wins"<<endl;
getch();
return;
if((p==0)&&(q==1))
A[0][1]='O';
board();
A[2][0]='X';
board();
cout<<"Enter: ";
cin>>p>>q;
A[p][q]='O';
board();
if((p==1)&&(q==0))
A[2][2]='X';
board;
cout<<"Enter: ";
cin>>p>>q;
A[p][q]='O';
board();
if((p==1)&&(q==1))
A[2][1]='X';
board();
cout<<"Computer wins"<<endl;
getch();
return;
else
A[1][1]='X';
board();
cout<<"Computer wins"<<endl;
getch();
return;
}
else
A[1][0]='X';
board();
cout<<"Computer wins"<<endl;
getch();
return;
if((p==2)&&(q==0))
A[p][q]='O';
board();
A[2][2]='X';
board();
cout<<"Enter:";
cin>>p>>q;
A[p][q]='O';
if((p==1)&&(q==1))
A[0][2]='X';
board();
cout<<"Enter: ";
cin>>p>>q;
A[p][q]='O';
if((p==1)&&(q==0))
A[2][1]='X';
board();
cout<<"Computer wins"<<endl;
getch();
return;
else
{
A[1][1]='X';
board();
cout<<"Computer wins"<<endl;
getch();
return;
if((p==0)&&(q==2))
A[p][q]='O';
board();
A[2][2]='X';
board();
cout<<"Enter:";
cin>>p>>q;
A[p][q]='O';
if((p==1)&&(q==1))
A[2][0]='X';
board();
cout<<"Enter: ";
cin>>p>>q;
A[p][q]='O';
if((p==0)&&(q==1))
A[1][2]='X';
board();
cout<<"Computer wins"<<endl;
getch();
return;
else
A[1][1]='X';
board();
cout<<"Computer wins"<<endl;
getch();
return;
if((p==1)&&(q==2))
A[1][2]='O';
A[2][0]='X';
board();
cout<<"Enter: ";
cin>>p>>q;
A[p][q]='O';
board();
if((p==1)&&(q==0))
A[0][2]='X';
board();
cout<<"Enter: ";
cin>>p>>q;
A[p][q]='O';
board();
if((p==1)&&(q==1))
A[0][1]='X';
board();
cout<<"Computer wins"<<endl;
return;
else
A[1][1]='X';
board();
cout<<"Computer wins"<<endl;
return;
else
A[1][0]='X';
board();
cout<<"Computer wins"<<endl;
return;
if((p==2)&&(q==1))
A[2][1]='O';
A[2][0]='X';
board();
cout<<"Enter: ";
cin>>p>>q;
A[p][q]='O';
board();
if((p==1)&&(q==0))
A[0][2]='X';
board();
cout<<"Enter: ";
cin>>p>>q;
A[p][q]='O';
board();
if((p==1)&&(q==1))
A[0][1]='X';
board();
cout<<"Computer wins"<<endl;
return;
else
A[1][1]='X';
board();
cout<<"Computer wins"<<endl;
return;
else
{
A[1][0]='X';
board();
cout<<"Computer wins"<<endl;
return;
if((p==2)&&(q==2))
A[2][2]='O';
A[2][0]='X';
board();
cout<<"Enter: ";
cin>>p>>q;
A[p][q]='O';
board();
if((p==1)&&(q==0))
A[0][2]='X';
board ();
cout<<"Enter: ";
cin>>p>>q;
A[p][q] ='O';
board ();
if((p==1) &&(q==1))
A [0][1] ='X';
board ();
cout<<"Computer wins"<<endl;
return;
else
A [1][1]='X';
board ();
cout<<"Computer wins"<<endl;
return;
}
}
else
A [1][0] ='X';
board ();
cout<<"Computer wins"<<endl;
return;
if((p==1) &&(q==1))
A [1][1] ='O';
A [0][1] ='X';
Board ();
cout<<"Enter: ";
cin>>p>>q;
A[p][q] ='O';
if((p==0) &&(q==2))
{
A [2][0] ='X';
board ();
cout<<"Enter: ";
cin>>p>>q;
A[p][q] ='O';
if((p==1) &&(q==0))
A [1][2] ='X';
Board ();
cout<<"Enter: ";
cin>>p>>q;
A[p][q] ='O';
if((p==2) &&(q==2))
A [2][1] ='X';
Board ();
cout<<"Draw"<<endl;
getch ();
n3++;
return;
else
A [2][2] ='X';
board ();
cout<<"Draw"<<endl;
getch ();
n3++;
return;
else
A [1][0] ='X';
board ();
cout<<"Computer wins"<<endl;
getch ();
return;
else
A [0][2] ='X';
board ();
cout<<"Computer wins"<<endl;
getch ();
return;
void empty ()
struct player
int scg;
int sct1;
int sct2;
int sct3;
};
long pos;
f1.close ();
player p;
while(!f.eof())
{ pos=f.tellg();
f.read((char*)&p,sizeof(p));
if (!strcmpi(p.name,ename))
strcpy(p.name,ename);
p.sct1=p.sct1+n1;
p.sct3+=n3;
f.seekg (pos);
f.write((char*)&p,sizeof(p));
f.close ();
return;
p.sct1=p.sct1+n1;
p.sct3+=n3;
f.write((char*) &p,sizeof(p));
f.close();
}
void main ()
char ans;
gets(ename);
do
Empty ();
cin>>game;
switch(game)
case 1:
{int n, f=0;
cin>>n;
switch(n)
case 1:
Randomize ();
cin>>x>>y;
A[x][y] ='X';
Board ();
if(a)
{
cout<<"User Won\n";
getch ();
n1++;
f=1;
break;
if(i<4)
compin ();
Board ();
if(b)
cout<<"Computer Won\n";
getch ();
f=1;
break;
}
}
if (f! =1)
cout<<"Match drawn\n";
getch ();
break;
case 2:
dualuser ();
break;
case 3:
comph(n3);
break;
default:
{cout<<"Wrong choice\n";
}
break;
case 2:
{clrscr ();
startgame ();
while(player1()==1&&player2()==1);
cout<<"Game ended."<<endl;
getch();
break;
default:
cout<<"Wrong choice\n";
cin>>ans;
}while(ans=='y'||ans=='Y');
getch();
}
OUTPUT: