Вы находитесь на странице: 1из 16

COMPUTER SCIENCE

INVESTIGATORY
PROJECT

HANGMAN
USING
C++

BY
SUBASH SWAMINATHAN
CONTENTS:

1. ACKNOWLEDGEMENT
2. HEADER FILES USED
3. PROGRAM DESCRIPTION
4. CODING
5. OUTPUT
6. BIBLIOGRAPHY
ACKNOWLEDGEMENT
I sincerely thank our Computer teacher for her
guidance, encouragement and support
throughout the duration of the project. Without
her motivation and help the successful
completion of this project would not have been
possible.

I would also like to extend my gratitude to my


friends and my family for their constant support
and help.

Subash Swaminathan,
XII B.
HEADER FILES Used

 #include<fstream.h> - For file


handling, cin and cout.
 # include <conio.h> - For getch() and
clrscr() functions.
 # include <stdioo.h> - For standard I/O
operations.
 # include <string.h> - For string
handling.
 # include <iomanip.h> - For
manipulating C++ output.
 # include <stdlib.h> - For accessing the
standard library of C++ programming.
 # include <ctype.h> - For declaring
functions used for testing.
PROGRAM
DESCRIPTION
This program is a guessing game in which the
user must guess the given word in 3 attempts.
Failure to guess in 3 attempts leads to the user
losing the game.
This program essentially consists of 4 topics the
user can choose or the user can choose the fifth
option to exit the game. The five options are:

1. Sports

2. Musical Instruments

3. Cities

4. Technology terms

5. Exit
CODING
//created by SwaminathanS

//module 1 --> Introduction


//module 2 --> Read Me
//module 3 --> Menu
//module 4 --> Game Play
//module 5 --> Exit

#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<iomanip.h>
#include<stdlib.h>
#include<ctype.h>

char city[10][20]={"seoul","edinburgh","los
angeles","mumbai","rio de janeiro","amsterdam","new
york","berlin","cape town","venice"};

char sports[10][20]={"soccer","bowling","arm
wrestling","fencing","sailing","squash","javelin","hockey","formul
a one","archery"};

char music[10][20]={"trumpet","saxophone",
"xylophone","harmonica","bagpipes","banjo","organ","marimba","
bell","sitar"};
char tech[10][20]={"bandwith","cache",
"compression", "operating system","protocol",
"cyber security","cookie","bit","google","linux"};

void main()
{
clrscr();
char word[20];
int op,i,j;

// MODULE 1

cout<<"\n\n\n\n\n\n\n\n"<<setw(45)<<"W E L C O M E";
cout<<"\n\n"<<setw(40)<<"T O";
cout<<"\n\n"<<setw(45)<<"H A N G M A N";
getch();

// MODULE 2

clrscr();
cout<<"\n\n\n"<<setw(46)<<"R E A D M E\n\n";
cout<<"\n This is a 'Word Finding' GAME. In this the user
has to find the word, you \n";
cout<<"\n have only 3 chances of entering an alphabet to
check wether if it is present";
cout<<"\n or not in the word and if you fail to guess the
word by the end of the 5th\n";
cout<<"\n attempt you will lose the game.";
cout<<"\n\n\n You can chose the words of your own
category..";
cout<<"\n\n\n\n This game is under copyrights, developed
by THE SWAMINATHANs";
getch();

// MODULE 3

char ch,guss;
do
{
clrscr();
cout<<"\n\n\n\n"<<setw(44)<<"Choose Your Category";
cout<<"\n\n\n 1. Sports\n";
cout<<" 2. Musical Instruments\n";
cout<<" 3. City\n";
cout<<" 4. Technology Terms\n";
cout<<" 5. Exit()";
cout<<"\n\n\n\n\n\n\nEnter your Option : ";
cin>>op;

// MODULE 4
randomize();
int r=random(10);
switch (op)
{
case 1:
strcpy(word,sports[r]);
break;
case 2:
strcpy(word,music[r]);
break;
case 3:
strcpy(word,city[r]);
break;
case 4:
strcpy(word,tech[r]);
break;
case 5:
goto bye;
default:
cout<<"\n\n\n\n\n\n\t\t\t\t...INVALID CHOICE...";

}
int ctr=3,f=0,size,flag=0;
size=strlen(word);
char guess,inputs[26],word1[20];
word1[0]=word[0];
word1[size-1]=word[size-1];
for(i=1;i<size-1;i++)
if(word[i]==word[0])
word1[i]=word[i];
else
{
if(word[i]==' ')
word1[i]=' ';
else
word1[i]='_';
}
do
{clrscr();
cout<<"\n\tNo. of chances left : "<<ctr;
cout<<"\n\tYour word is : ";
int r=0;
for(i=0;i<size;i++)
{cout<<word1[i]<<" ";}
for(int p=0;p<strlen(word);p++)
{
if(isalpha(word[p]))
{
if(word[p]==word1[p])
r++;
}
if(word[p]==' ')
r++;
}
if(r==strlen(word))
{ cout<<"\n\n\tYOU WIN!!!!\n"; break;}
cout<<"\n\tAlphabets Entered : ";
for(i=0;i<f;i++)
{cout<<inputs[i]<<", ";}
cout<<"\n\tEnter an alphabet : ";
cin>>guess;
guess=tolower(guess);
flag=0;
for(i=0;i<f;i++)
{ if(guess==inputs[i])
{cout<<"\n\tAlpahabet already entered";
goto end;}
else
flag++;
}
if(flag==f)
{inputs[f]=guess;
f++;
}
flag=0;
if(guess==word[0])
{cout<<"\n\tLetter Already Displayed";
goto end;
}
for(i=1;i<size-1;i++)
{ if(guess==word[i])
word1[i]=word[i];
else
flag++;
}
if(flag==size-2)
ctr--;
if(ctr==0)
cout<<"\n\n\tSORRY,YOU LOST";
end:

}while(ctr>0);

cout<<"\n\n\n \t Do You Want To Return To The Menu ?


(Y/N) : ";
cin>>ch;
if(toupper(ch)=='N')
goto bye;
}while(toupper(ch)=='Y');

// MODULE 6

bye:
{ clrscr();
cout<<"\n\n\n\n\n\n\n\n\n"<<setw(46);
cout<<"T H A N K Y O U";
cout<<"\n\n\n"<<setw(57);
cout<<" ^_^ Hope To See You Play Again ^_^ " ;
cout<<"\n\n\n\n\n\n\n\n\n\n\n Hit ENTER to leave.....";
}

getch();
return;
}
OUTPUT SCREEN

1.WELCOME SCREEN:
2.INSTRUCTIONS:

3.MENU:
4.GAME SCREEN:

5.ENTERING CORRECT LETTERS:


6.ENTERING INCORRECT LETTERS:

7.END OF THE GAME:


BIBLIOGRAPHY

 https://www.google.com/

 https://www.quora.com

 https://www.c++hub.com

 COMPUTER SCIENCE WITH C++ BY


SUMITA ARORA

Вам также может понравиться