0% found this document useful (0 votes)
67 views

Comp Proj 12

The document contains code for two programs: (1) A guessing number game between two players where they try to guess each other's randomly generated numbers. (2) A tic-tac-toe game that can be played between two human players or a human player against the computer. The code defines the logic and rules for both games.

Uploaded by

Vivek Swain
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

Comp Proj 12

The document contains code for two programs: (1) A guessing number game between two players where they try to guess each other's randomly generated numbers. (2) A tic-tac-toe game that can be played between two human players or a human player against the computer. The code defines the logic and rules for both games.

Uploaded by

Vivek Swain
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 45

PROGRAM::

TIC-TAC-TOE & GUESS THE NUMBER

DAV MODEL SCHOOL,


DURGAPUR

VIVEK SWAIN
Class 12

Board Roll no-


___________
TEACHER’s CERTIFICATE

This is to certify that Vivek Swain of class XII of section-A studying in


D.A.V. Model School, Durgapur has completed his project under the
guidance and supervision of Sir _________________.

SIGNATURE
ACKNOWLEDGEMENT

I would like to appreciate the co-operation and vivacity offered by my friends


that made this project easy and feasible. They helped me in domains of
creativeness, designing, managing and in many other ways to accomplish my
tasks.

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.

Sincere thanks to our helpful computer science teacher Sir ________________


who always encouraged us in our programs and its completion and in the
rectification of our doubts.

(Vivek Swain)
Code:

#include<iostream.h>

#include<conio.h>

#include<string.h>

#include<stdio.h>

#include<fstream.h>

#include<stdlib.h>

//This is the code for the guessing game

struct InputNumber

char inpno1[30];

char inpno2[30];

}s;

int check (char c, int n)

int i =0, count=0;

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 place(char gno[30],int n)

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;

int checkwin(char guess[30],int n)

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

cout<<"Enter Player 1 guess\n";

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

cout<<"Number of digits guessed right : "<<dig<<endl;

cout<<"Number of digits guessed in the correct place :


"<<pldig<<endl;

return 1;

int player2()

char guess2[30];

cout<<"Enter Player 2 guess\n";

cin>>guess2;

if(checkwin(guess2,2))
{

cout<<"Congratulations! Player 2 won!"<<endl;

return 0;

else

int dig=pass(guess2,1);

int pldig=place(guess2,1);

cout<<"Number of digits guessed right : "<<dig<<endl;

cout<<"Number of digits guessed in the correct place :


"<<pldig<<endl;

return 1;

void startgame()

int d;

cout<<"Enter the number of digits of the number you want


to play with: ";
cin>>d;

cout<<"Enter Player 1's number: "<<endl;

cin>>s.inpno1;

clrscr();

cout<<"Enter Player 2's number: "<<endl;

cin>>s.inpno2;

clrscr();

//Here starts the code for Tic tac toe

char A[3][3];

void board()

clrscr();

cout<<" "<<A[0][0]<<" "<<char(179)<<" "<<A[0][1]<<"


"<<char(179)<<" "<<A[0][2]<<" "<<endl;

cout<<"_________________"<<endl<<endl;

cout<<" "<<A[1][0]<<" "<<char(179)<<" "<<A[1][1]<<"


"<<char(179)<<" "<<A[1][2]<<" "<<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++)

cout<<"Player 1,enter the coordinates:\n";

uinput();

A[c1][c2]='X';

board();

int a=checkwin();
if(a)

cout<<"Player 1 Won\n";

getch();

return;

if(i<4)

cout<<"Player 2, Enter the coordinates:\n";

uinput();

A[c1][c2]='O';

board();

int b=checkwin();

if(b)

cout<<"Player 2 Won\n";

getch();

return;

}
}

cout<<"Match drawn\n";

getch(

);

void comph(int &n3)

int p,q;

A[0][0]='X';

board();

cout<<"Enter row and column:";

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 ()

A [0][0] =' ';

A [0][1] =' '; A [1][2] =' '; A [2][0] =' ';

A [0][2] =' ';

A [1][0] =' '; A [2][1] =' ';


A [1][1]=' ';A[2][2]=' ';

struct player

char name [30];

int scg;

int sct1;

int sct2;

int sct3;

};

void file (char ename [30], int n1, int n3)

long pos;

ofstream f1("scores.dat", ios::binary|ios::app);

f1.close ();

player p;

fstream f ("scores.dat”, ios::binary|ios::app);

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;

strcpy (p.name, ename);

p.sct1=p.sct1+n1;

p.sct3+=n3;

f.write((char*) &p,sizeof(p));

f.close();

}
void main ()

int game, n1=0, n3=0;

char ans;

char ename [30];

cout<<"Enter your name: ";

gets(ename);

do

Empty ();

cout<<"Enter your choice:\n";

cout<<"1: Tic Tac Toe\n";

cout<<"2: Guess The Number\n";

cin>>game;

switch(game)

case 1:
{int n, f=0;

cout<<"Enter 1: Play with computer(easy)\n";

cout<<"Enter 2: Play with human\n";

cout<<"Enter 3: Play with computer(hard)\n";

cin>>n;

switch(n)

case 1:

Randomize ();

for (int i=0; i<5; i++)

cout<<"Enter the coordinates\n";

cin>>x>>y;

A[x][y] ='X';

Board ();

int a=checkwin ();

if(a)

{
cout<<"User Won\n";

getch ();

n1++;

f=1;

break;

if(i<4)

compin ();

A[x1] [y1] ='O';

Board ();

int b=checkwin ();

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

cout<<"Do you want to play again? (y/n) ";

cin>>ans;

}while(ans=='y'||ans=='Y');

getch();
}

OUTPUT:

You might also like