Anda di halaman 1dari 34

CERTIFICATE

This is to certify that the project report entitled


Tic-Tac-Toe Game submitted by Nikita Yadav of
class XII-A, is a record of bonafide work carried
out in this department of Computer Science under
my supervision and has not been submitted anywhere
else for any other purpose.

Ms. Gulroop
Subject Teacher
Salwan Public School
Gurgaon
1

ACKNOWLEDGEMENT
The successful completion of any task would be incomplete
without mentioning the names of those persons who helped to
make it possible. I take this opportunity to express my
gratitude in few words and respect to all those who helped me
in the completion of this project.
It is my humble pleasure to acknowledge my deep senses of
gratitude to my Computer Science teacher, Ms. Gulroop for
her valuable support, constant help and guidance at each and
every stage, without which this project would not have come
forth.
I also register my sense of gratitude to our principal, Ms.
Rashmi Mehta, for her immense encouragement that has
made this project successful. I would also like to thank my
friends and family for encouraging me during the course of
this project. Last, but not the least, I would like to thank
CBSE for giving us the opportunity to undertake this project.

Nikita Yadav
Class XII-A

INDEX
CONTENTS
NO.

PAGE

Introduction

System Analysis

System Design

Source Code

Outputs

22

Conclusions

32

References
33

INTRODUCTI
ON
Tic-tac-toe (or Noughts and crosses, Xs and Os) is
a paper-and-pencil
game for
two
players, X and O, who take turns marking the
spaces in a 33 grid. The player who succeeds
in placing three respective marks in a horizontal,
vertical, or diagonal row wins the game.
Because of the simplicity of Tic-tac-toe, it is often
used as a pedagogical tool for teaching the
concepts of good sportsmanship and the branch
of artificial intelligence that deals with the
searching of game trees.
The game has a number of English names.
Tick-tack-toe, Tic-tac-toe, Tick-tat-toe, or Tittat-toe (USA, Canada)
Noughts and crosses or Naughts and crosses
(United
Kingdom, Republic
of
Ireland, Australia, New Zealand, South Africa)
Exy-Ozys, Xsie-Osies (verbal name only)
(Northern Ireland)
5

Xs
and
Os
(Egypt, Republic
Ireland, Canada, Zimbabwe, Romania)
O-X (Mauritius)

System
Analysis

HARDWARE SPECIFICATION

MEMORY : 984 KB

MICROPROCESSOR: 1.2

HARD DISK: 40 GB

PRINTER: hp Laser printer

SOFTWARE SPECIFICATION

PLATFORM : C++

WINDOWS XP
6

of

SYSTEM
DESIGN
List of all classes and user defined functions used
in the program:-

S.No

Name of Class/Function

Work of the Class/Function

class players

Contains all the variables and user defined

1.

functions required for the functioning of


the program

2.

void display(char [])

Accepts a char variable as parameter,


Used to display the tic-tac-toe grid on
the screen

3.

void win(char [])

Accepts a char variable as parameter,


Used to display the tic-tac-toe grid on
the screen when user enters either a
cross or a zero and displays the winner

4.

int play(char [])

Checks the filled spaces and the remaining


ones on the grid and ultimately decides
when the game is over

SYSTEM
DESIGN
The project is a basic two player tic-tac-toe game. It
calculates and displays the winner name and its score
after each game. Also it displays the highest score
8

and lets the user to search for a name from the


previous records and displays its details. The user
can also play a tournament of 3 games or more. On
choosing

the

option,

it

also

displays

acknowledgements released by the creators of the


program.

SOURCE CODE
#include <fstream.h>
#include <conio.h>
#include <process.h>
#include <string.h>
#include <stdio.h>
int play(char []);
int h=0,c=0,p1=0,p2=0;
class players
{
public:
char n[50];
char n1[50];
int scn;
int scn1;
void display(char []);
void win(char []);
}p[5],q,r;
void getdata()
{
strcpy(q.n,p[c-1].n);
strcpy(q.n1,p[c-1].n1);
q.scn=p[c-1].scn;
q.scn1=p[c-1].scn1;
10

}
void putdata()
{
cout<<"Name of player 1 : "<<q.n<<endl;
cout<<"Name of player 2 : "<<q.n1<<endl;
cout<<"Score of player 1 : "<<q.scn<<endl;
cout<<"Score of player 2 : "<<q.scn1<<endl;
cout<<"High Score : "<<h<<endl;
}
void main()
{
clrscr();
char ch,ch1;
int n;
cout<<"\t"<<"\t"<<"\t"<<"WELCOME TO TICTACTOE - THE
GAME"<<endl;
cout<<endl;
cout<<"Press any key to continue"<<endl;
getch();
do
{
clrscr();
cout<<"MENU"<<endl;
cout<<"1.High score"<<endl;
cout<<"2.Play Game"<<endl;
cout<<"3.Play Tournament"<<endl;
cout<<"4.Information about the game"<<endl;
11

cout<<"5.Display Previous Records"<<endl;


cout<<"6.Search for Player Name"<<endl;
cout<<"7.Exit"<<endl;
cout<<"Enter Option"<<endl;
cin>>n;
switch (n)
{
case 2:
{
cout<<"Enter name of player 1:"<<endl;
cin>>p[c].n;
cout<<"Enter name of player 2:"<<endl;
cin>>p[c].n1;
char s[]={'1','2','3','4','5','6','7','8','9'};
p[c].display(s);
p[c].win(s);
c++;
if ((p1>=1)&&(p1>p2))
{
p[c-1].scn=1;
p[c-1].scn1=0;
}
else
{
if ((p2>=1)&&(p1<p2))
{
p[c-1].scn1=1;
12

p[c-1].scn=0;
}
else
{
p[c-1].scn=0;
p[c-1].scn1=0;
}
}
cout<<"Score of "<<p[c-1].n<<" :"<<p[c-1].scn<<endl;
cout<<"Score of "<<p[c-1].n1<<" :"<<p[c-1].scn1<<endl;
if (h<1) h=1;
fstream f;
f.open("play.txt",ios::out);
f.write((char*)&q, sizeof(q));
getdata();
f.close();
break;
}
case 3:
{
cout<<"This tournament includes a set of 3 games you need to
play."<<endl;
cout<<"You can even play more than 3 times. Just enter the choice
whether to play again after end of 3 games"<<endl;
do
{
cout<<"Enter name of player 1:"<<endl;

13

cin>>p[c].n;
cout<<"Enter name of player 2:"<<endl;
cin>>p[c].n1;
for(int x=0;x<=2;x++)
{
char s[]={'1','2','3','4','5','6','7','8','9'};
p[x].display(s);
p[x].win(s);
}
cout<<endl<<endl<<endl;
cout<<"\t"<<"\t"<<"\t"<<"Want to play again??(Y/N)"<<endl;
cin>>ch;
c++;
}while((ch!='N')&&(ch!='n'));
p[c-1].scn=p1;
p[c-1].scn1=p2;
cout<<"Score of "<<p[c-1].n<<" :"<<p[c-1].scn<<endl;
cout<<"Score of "<<p[c-1].n1<<" :"<<p[c-1].scn1<<endl;
if(p[c-1].scn>p[c-1].scn1)
{
h=p[c-1].scn;
cout<<" "<<p[c-1].n<<" wins the series"<<endl;
}
else{ if(p[c-1].scn1>p[c-1].scn)
{
h=p[c-1].scn1;
cout<<" "<<p[c-1].n1<<" wins the series"<<endl;
14

}
else
{
h=p[c-1].scn;
cout<<"The result of the series is DRAW"<<endl;
}}
break;
}
case 1:
{
cout<<"Highest Score is: "<<h<<endl;
break;
}
case 4:
{
cout<<"

ACKNOWLEDGEMENT"<<endl;

cout<<endl;
cout<<"This game 'TIC-TAC-TOE' is made by Nikita Yadav XIIA,Yashna
Gupta XIIB and Ayushi XIIB of SPS,Ggn."<<endl;
cout<<"A great thanks to our computer science teacher Ms.Gulroop for
guiding us through the right path in making this project"<<endl;
cout<<"It is because of her great efforts that this project has been
successful."<<endl;
cout<<"Thank you ma'am"<<endl;
cout<<"This game is under our copyright. All rights reserved."<<endl;
break;
}
default:
15

{
cout<<"Invalid Option!"<<endl;
break;
}
case 7 :
{
exit(0);
}
case 5:
{
fstream f;
f.open("play.txt", ios::in|ios::app);
f.seekg(0, ios::beg);
while (f)
{
f.read((char*)&r, sizeof(r));
putdata();
}
f.close();
break;
}
case 6 :
{
char nm[15];
cout<<"Enter Name to be searched : ";
gets(nm);
fstream f;
16

f.open("play.txt", ios::in);
while (f)
{
f.read((char*)&r, sizeof(r));
if(strcmp(nm, q.n)==0)
{
cout<<"Name Found! "<<endl;
cout<<"Name of player : "<<q.n<<endl;
cout<<"Score of player : "<<q.scn<<endl;
}
else
{
if(strcmp(nm, q.n1)==0)
{
cout<<"Name Found!"<<endl;
cout<<"Name of player : "<<q.n1<<endl;
cout<<"Score of player : "<<q.scn1<<endl;
}
else {cout<<"Name not found!"<<endl;}
}
}
f.close();
break;
}
}
cout<<endl;
cout<<"Do you want to enter another option?(y/n)"<<endl;
17

cin>>ch1;
}while((ch1!='n')&&(ch1!='N'));
getch();
}
void players::display(char s[])
{
cout<<"\t"<<"\t"<<"\t"<<" Tictactoe"<<endl;
cout<<"\t"<<"\t"<<"\t"<<p[c].n<<"(X) and "<<p[c].n1<<"(O)"<<endl;
cout<<"\t"<<"\t"<<"\t"<<" | | "<<endl;
cout<<"\t"<<"\t"<<"\t"<<" "<<s[0]<<" | "<<s[1]<<" | "<<s[2]<<"
"<<endl;
cout<<"\t"<<"\t"<<"\t"<<"___|___|___"<<endl;
cout<<"\t"<<"\t"<<"\t"<<" | | "<<endl;
cout<<"\t"<<"\t"<<"\t"<<" "<<s[3]<<" | "<<s[4]<<" | "<<s[5]<<"
"<<endl;
cout<<"\t"<<"\t"<<"\t"<<"___|___|___"<<endl;
cout<<"\t"<<"\t"<<"\t"<<" | | "<<endl;
cout<<"\t"<<"\t"<<"\t"<<" "<<s[6]<<" | "<<s[7]<<" | "<<s[8]<<"
"<<endl;
cout<<"\t"<<"\t"<<"\t"<<" | | "<<endl;
cout<<endl;
cout<<"Enter a number corresponding to those shown on the tictactoe
grid to place your mark"<<endl;
cout<<"Let's START!!"<<endl;
}
void players::win(char s[])
{
int r,a,b;

18

for(int x=0;x<=4;x++)
{
cout<<"Enter number for "<<p[c].n<<" :"<<endl;
cin>>a;
if (a>9 || a<=0)
cout<<"Invalid entry!!"<<endl;
else
s[a-1]='X';
clrscr();
cout<<endl;
cout<<"\t"<<"\t"<<"\t"<<" | | "<<endl;
cout<<"\t"<<"\t"<<"\t"<<" "<<s[0]<<" | "<<s[1]<<" | "<<s[2]<<"
"<<endl;
cout<<"\t"<<"\t"<<"\t"<<"___|___|___"<<endl;
cout<<"\t"<<"\t"<<"\t"<<" | | "<<endl;
cout<<"\t"<<"\t"<<"\t"<<" "<<s[3]<<" | "<<s[4]<<" | "<<s[5]<<"
"<<endl;
cout<<"\t"<<"\t"<<"\t"<<"___|___|___"<<endl;
cout<<"\t"<<"\t"<<"\t"<<" | | "<<endl;
cout<<"\t"<<"\t"<<"\t"<<" "<<s[6]<<" | "<<s[7]<<" | "<<s[8]<<"
"<<endl;
cout<<"\t"<<"\t"<<"\t"<<" | | "<<endl;
r=play(s);
if (r==1)
{
cout<<endl<<endl;
cout<<"\t"<<"\t"<<"\t"<<"****** "<<p[c].n<<" WINS!!******"<<endl;
p1++;
19

break;
}
if (x>3)
break;
cout<<"Enter number for "<<p[c].n1<<" :"<<endl;
cin>>b;
s[b-1]='O';
clrscr();
cout<<endl;
cout<<"\t"<<"\t"<<"\t"<<" | | "<<endl;
cout<<"\t"<<"\t"<<"\t"<<" "<<s[0]<<" | "<<s[1]<<" | "<<s[2]<<"
"<<endl;
cout<<"\t"<<"\t"<<"\t"<<"___|___|___"<<endl;
cout<<"\t"<<"\t"<<"\t"<<" | | "<<endl;
cout<<"\t"<<"\t"<<"\t"<<" "<<s[3]<<" | "<<s[4]<<" | "<<s[5]<<"
"<<endl;
cout<<"\t"<<"\t"<<"\t"<<"___|___|___"<<endl;
cout<<"\t"<<"\t"<<"\t"<<" | | "<<endl;
cout<<"\t"<<"\t"<<"\t"<<" "<<s[6]<<" | "<<s[7]<<" | "<<s[8]<<"
"<<endl;
cout<<"\t"<<"\t"<<"\t"<<" | | "<<endl;
r=play(s);
if (r==1)
{
cout<<endl<<endl;
cout<<"\t"<<"\t"<<"\t"<<"****** "<<p[c].n1<<" WINS!!******"<<endl;
p2++;
break;
20

}
}
if (r==-1)
{
cout<<endl<<endl;
cout<<"\t"<<"\t"<<"\t"<<"******DRAW!!******"<<endl;
}
}
int play(char s[])
{
if ((s[0]==s[1]) && (s[1]==s[2]))
{
return 1;
}
else
{
if ((s[0]==s[4]) && (s[4]==s[8]))
{
return 1;
}
else
{
if ((s[0]==s[3]) && (s[3]==s[6]))
{
return 1;
}
else
21

{
if ((s[1]==s[4]) && (s[4]==s[7]))
{
return 1;
}
else
{
if ((s[2]==s[5]) && (s[5]==s[8]))
{
return 1;
}
else
{
if ((s[2]==s[4]) && (s[4]==s[6]))
{
return 1;
}
else
{
if ((s[6]==s[7]) && (s[7]==s[8]))
{
return 1;
}
else
{
if ((s[3]==s[4]) && (s[4]==s[5]))
{
22

return 1;
}
else
{
return -1;
}
}
}
}
}
}
}
}

OUTPUTS
23

24

25

26

27

28

29

30

Similarly 2 more games are played with shreya winning


the 2nd game and riya winning the last.

31

32

CONCLUSION
Hence, this program can be used
as

good

entertainment,

source
at

the

of
same

time, allows one to compete with


other

players

by

scores.
33

comparing

REFERENCES
Computer Science with C++ by
Sumita Arora

34

Anda mungkin juga menyukai