Hi everyone. Today, I'm glad to share with you the complete source code of my latest project in C++ -- The Hangman Game. I created this project as a part of the regular curriculum for CBSE XI as I, obviously study in a CBSE school.
So, to be clear, this project suits all criteria for the CBSE XI Project. It covers whatever you learnt on C++ in XI and doesn't include any external commands that's totally out of the syllabus. This is a plain and simple to understand program developed purely by me and is based on my studies so far in XI.
(Skip to Download -- Click Here)
(Skip to Download -- Click Here)
In a nutshell -- I believe this one would be one among the simplest and complete Hangman code in C++ which is complete. It includes a user menu also and would also allow to write your own words into the program and generate random words to figure out each time you play the game and also includes a custom match option for writing in a custom word when you open the game and let the other guy figure it out. So, anyone who have come this far to find this code, I'm sure you got what you need as I this works on the simplest C++ mechanism as this is entirely based on the XI CBSE syllabus of India.
Now, I'll explain the whole program in a bunch of words. As I said before, this one's purely based on XI syllabus for CBSE and fits the project criteria. It wouldn't be hard to figure the mechanism unless you aren't up to date with your XI studies. There is no reference to this project on the CBSE website and it contains some other amateur projects like Tic Tac Toe, Cows 'n Bulls etc. Though Tic Tac Toe was a fairly nice one to try, I thought to do something else than that as the second team already chose that and it'd be pretty boring to see all the teams prepare the same project. Moreover, I rather prefer variety. So, my teacher pointed me out to this game which is a popular game arising from the old times.
The rules of the game is simple. A word is chosen and it is turned into dashes, i.e, hidden. For example, let the word be KITTEN. The player would see dashes matching the number of letters of the word, i.e, _ _ _ _ _ _. The player can guess some letters of the word and if the guessed letter is present, the dashes which occupy the places of the specific letter is uncovered. However, if the letter doesn't exist, the player gets a hang, i.e, a warning. This continues till the player uncovers the word by guessing all the letters correctly or till the number of hangs of the person runs out. Ideally, 6 hangs is recommended and I've implemented the same on my code. When all the hangs run out, the challenger draws the sketch of a hanged man which marks the tradition of "Game Over".
Mechanism
Porting this old classic to C++ is fairly simple. First of all, get a clear idea of the whole game and how it should work. You can try drawing a diagram which links together the game mechanisms. It'd be much better to sort different game mechanisms to different functions. I've used a lot of functions in my code as it is always simple to modify the code whenever I need it or to rectify a bug or an error.
It is also nice to display a graphic by using dashes or pipe signs which represents each stage of hanging and when the hangs runs out, display the figure of a hanged man and flash Game Over on the screen. It's a better way to earn more points also.
I've used almost all of the common C++ header files including the time.h. Why? Because time.h contains the rand() and srand() function which is useful if you want to store some words in the code and pick a random word each time a user plays the game. These functions and the header file is also a part of the XI syllabus. I hope you are well aware of how it works.
Pre-defined words is an awesome way to make the game more realistic. I haven't used files here as that comes in XII. So, I've stringed these words together in a 2D array within the program and you can add more words if you want to by placing a comma and putting the word in double quotes as shown in the program. Make sure they are in capitals as the program wont work otherwise (I didn't add that as it wasn't necessary. Any codie would have the sense to know that as you can modify only if you could access the code of the program). Also, if you're adding more codes, make sure you change the indices as well and also the limits of the random function.
Finally, I've bound all these into a switch statement for a simple menu which also has an exit function. I used the exit() function of the process.h directive to attain a termination of the program on command. There's also two modes of game present. One against a computer where words are chosen randomly from a pre-defined series and a second mode where you can input a word and your friend can try guessing it out.
Source Code
And here's the source code for the entire program. I've used some comments here and there and I haven't modified it to remove my attributions as this was a quick project and I never got time to modify it to remove attributions referring to my team. You can remove those pieces of code yourself and add something of your own to make it look more attractive.
/*Hangman 2.0 Final -------------------- Developed by: Sangeeth Sudheer, Abel Thomas, Premjith Pradeep Time: 0 Days 4 hours 15 minutes Changelogs: -------------------- V2.0(Final) * Added vs. AI * Various bug fixes V1.9 RP * Fixed GFX issues related to spacing * Spaced hanged words * Added Changelog to the program * Many bug fixes V1.8 Beta:- * Display hanged letters * Fixed bad spacings and tabs * Minor bug fixes V1.5 Beta:- * Custom words by the playmaker * Fixed the Hangman graphics issue at the start V1.2 Beta:- * Full fledged menu with exit function * Bugfixes, improved UX V1.1 Beta:- * Added graphics for no hangs * Changed a value which prevented the player from winning * Other minor fixes Future Builds Log: -------------------- */ #include<iostream.h> #include<conio.h> #include<ctype.h> #include<string.h> #include<process.h> #include<stdio.h> #include<time.h> #include<stdlib.h> //using namespace std; void uxtitle() { cout<<"\n ---------------------- HANGMaN ----------------------"; cout<<"\n --------------------- V2.0 FINAL --------------------"; cout<<"\n -------------:::::::::::::::::::::::::::-------------"; cout<<"\n\n Coded by Sabernova: BlueEzio, LebA, PJ\n\n"; } void uxmain() { uxtitle(); cout<<"\n\n\n\t\t\t1. Play Game (vs. AI)"; cout<<"\n\t\t\t2. Custom Game"; cout<<"\n\t\t\t3. Help"; cout<<"\n\t\t\t4. About"; cout<<"\n\t\t\t5. Changelog"; cout<<"\n\t\t\t6. Exit"; cout<<"\n\n\n\tEnter your option (1-6): "; } void hang(int hang_count) { switch (hang_count) { case 0: cout<<"\n\t\t (:]) ____ ___ ____ ____ "; cout<<"\n\t\t /|\\ | | | | | "; cout<<"\n\t\t | |____ |___| |____ |____ :)"; cout<<"\n\t\t / \\ | |\\ | | "; cout<<"\n\t\t | | \\ |____ |____ "; cout<<"\n"; cout<<"\n"; cout<<"\n"; cout<<"\n"; break; case 1: cout<<"\n\t\t__________"; cout<<"\n\t\t ||"; cout<<"\n\t\t ||"; cout<<"\n\t\t ||"; cout<<"\n\t\t ||"; cout<<"\n\t\t ||"; cout<<"\n\t\t ||"; cout<<"\n\t\t ______||_____"; cout<<"\n\t\t |___OH NO!___|"; break; case 2: cout<<"\n\t\t__________"; cout<<"\n\t\t | ||"; cout<<"\n\t\t ||"; cout<<"\n\t\t ||"; cout<<"\n\t\t ||"; cout<<"\n\t\t ||"; cout<<"\n\t\t ||"; cout<<"\n\t\t ______||____"; cout<<"\n\t\t |___________|"; break; case 3: cout<<"\n\t\t__________"; cout<<"\n\t\t | ||"; cout<<"\n\t\t O ||"; cout<<"\n\t\t ||"; cout<<"\n\t\t ||"; cout<<"\n\t\t ||"; cout<<"\n\t\t ||"; cout<<"\n\t\t ______||____"; cout<<"\n\t\t |___________|"; break; case 4: cout<<"\n\t\t__________"; cout<<"\n\t\t | ||"; cout<<"\n\t\t O ||"; cout<<"\n\t\t | ||"; cout<<"\n\t\t | ||"; cout<<"\n\t\t ||"; cout<<"\n\t\t ||"; cout<<"\n\t\t ______||____"; cout<<"\n\t\t |___________|"; break; case 5: cout<<"\n\t\t__________"; cout<<"\n\t\t | ||"; cout<<"\n\t\t O ||"; cout<<"\n\t\t | ||"; cout<<"\n\t\t | ||"; cout<<"\n\t\t / \\ ||"; cout<<"\n\t\t ||"; cout<<"\n\t\t ______||__________"; cout<<"\n\t\t |___SAVE ME! :(___|"; break; case 6: cout<<"\n\t\t__________"; cout<<"\n\t\t | ||"; cout<<"\n\t\t O ||"; cout<<"\n\t\t /|\\ ||"; cout<<"\n\t\t | ||"; cout<<"\n\t\t / \\ ||"; cout<<"\n\t\t ||"; cout<<"\n\t\t ______||____"; cout<<"\n\t\t |___________|"; cout<<"\n\n\t\tYou got HANGED!"; cout<<"\n\n\t\t ~~~GAME OVER!!~~~~"; break; } } void assigncmp(int wordlen,int &hang_count,char entry,char word[],char dispword[],char hangl[]) { int flag=1; for(int i=0;i<wordlen;i++) { if(word[i]==entry) { dispword[i]=word[i]; flag=0; } } if(flag) { hangl[hang_count]=entry; hang_count++; } } int winc(char word[],char dispword[],int hang_count) { if(strcmp(dispword,word)==0) return 1; else if(hang_count==6) return 2; else return 0; } void winscreen(int hang_count,int win,char word[],char hangl[]) { clrscr(); hang(hang_count); if(win==1) { if(hang_count) { cout<<"\n\n\tHangs: "; for(int i=0;i<hang_count;i++) cout<<hangl[i]<<" "; } cout<<"\n\n\t\tAnswer: "<<word; cout<<"\n\n\tYou Won! Congratulations!"; cout<<"\n\n\tPress any key to go back to main menu."; getch(); } else if(win==2) { cout<<"\n\n\tTrials over! The correct answer was: "<<word; cout<<"\n\n\tYou are now the HANGMAN! **GAME OVER**"; cout<<"\n\n\tPress any key to go back to main menu."; getch(); } } void wordentry(char word[]) { clrscr(); uxtitle(); cout<<"\n\n\n\t\tEnter the word: "; gets(word); for(int i=0;word[i]!='\0';i++) if(islower(word[i])) word[i]=toupper(word[i]); } void main() { int user_option; int hang_count=0,win=0,i; int wordlen; char entry; char word[50],dispword[50],hangl[10]; //Add Custom words below in the same format. If you want more, change the first index first and then add the words. Add words in Capitals. char wordai[20][50]={"BERRIES","APPLE","PROCESS","QUEEN","DOCTOR","ENGINEER","DREAMS","KEYBOARD","PIANO","TELEVISION",/*10*/"MODERATE","METEOR","COMET","GALAXY","WINDOWS","SHIP","RIVER","PLANE","COUNTRY","MOUSE"/*20*/}; time_t t; unsigned int seed,rndnum; //Initialising the code start: clrscr(); uxmain(); cin>>user_option; switch (user_option) { case 1: seed=(unsigned)time(&t); srand(seed); rndnum=(rand()%19)+0; strcpy(word,wordai[rndnum]); wordlen=strlen(word); hang_count=win=0; for(i=0;i<wordlen;i++) dispword[i]='-'; dispword[i]='\0'; do { clrscr(); uxtitle(); hang(hang_count); cout<<"\n\n\n\n\t\t\t\t"<<dispword; if(hang_count) { cout<<"\n\n\tHangs: "; for(i=0;i<hang_count;i++) cout<<hangl[i]<<" "; } for(i=0;i<=hang_count;i++); cout<<"\n\n\tEnter your guess: "; cin>>entry; if(islower) entry=toupper(entry); assigncmp(wordlen,hang_count,entry,word,dispword,hangl); win=winc(word,dispword,hang_count); }while(!win); winscreen(hang_count,win,word,hangl); goto start; break; case 2: wordentry(word); wordlen=strlen(word); hang_count=win=0; for(i=0;i<wordlen;i++) dispword[i]='-'; dispword[i]='\0'; do { clrscr(); uxtitle(); hang(hang_count); cout<<"\n\n\n\n\t\t\t\t"<<dispword; if(hang_count) { cout<<"\n\n\tHangs: "; for(i=0;i<hang_count;i++) cout<<hangl[i]<<" "; } for(i=0;i<=hang_count;i++); cout<<"\n\n\tEnter your guess: "; cin>>entry; if(islower) entry=toupper(entry); assigncmp(wordlen,hang_count,entry,word,dispword,hangl); win=winc(word,dispword,hang_count); }while(!win); winscreen(hang_count,win,word,hangl); goto start; break; case 3: clrscr(); uxtitle(); cout<<"\t\t\t\t::Help::"; cout<<"\n\nWith this game, we bring back the old classic Hangman to your PC with a bit of"; cout<<"\nC++ awesomeness! The objective of the game is plain and simple. We'll explain "; cout<<"\nthat below."; cout<<"\n\nYour objective is to fill in the blanks shown on the screen behind which lies "; cout<<"\na hidden word which was chosen by your opponent. Your goal is to uncover the "; cout<<"\nword by guessing some random letters. If the letter you enter is present in "; cout<<"\nthe word, the positions will be uncovered. However, if your guess is wrong, "; cout<<"\nyou'll proceed to the hanging post to be hanged. Repeat this until you uncover"; cout<<"\nall the letters of the word which'll lead you to victory. However, beware that "; cout<<"\nyou'll only have 6 chances. If all your chances end up, you'll be hanged which "; cout<<"\nresults in a Game Over. So start guessing!"; cout<<"\n\n::Modes"; cout<<"\n\n-- Player vs AI : Figure out a random set of words selected by the program\n-- Custom Game: Enter a custom word and guess it out"; cout<<"\n\n--Press any key to go back."; getch(); goto start; break; case 4: clrscr(); uxtitle(); cout<<"\n\t\t\t\t::About::"; cout<<"\n\nHangman was made by the awesome c0ders at Sabernova. It took inspiring ideas, "; cout<<"\ncreative skills, big ambitions and a lot of hardwork to develop this amazing"; cout<<"\npiece of code. Thanks to everyone involved in the creation."; cout<<"\n----------------------------------------"; cout<<"\n\nAnd here are the awesome coders::::"; cout<<"\n\nProject Lead:: BlueEzio (Sangeeth Sudheer) -- Jedi"; cout<<"\nApprentices :: LebA (Abel Thomas), PJ (Premjith Pradeep)"; cout<<"\n\nSpecial thanks to our IT guru -- Ms. Remani :)"; cout<<"\n\nTo opponent team :: Care to give a challenge? B)"; cout<<"\n\n--Press any key to go back."; getch(); goto start; break; case 5: clrscr(); uxtitle(); cout<<"\n\t\t\t\t::Changelog::"; cout<<"\n\nV2.0 Final"; cout<<"\n* Added Player vs. AI"; cout<<"\n* Various bug fixes"; cout<<"\nV1.9 RP"; cout<<"\n* Fixed GFX issues related to spacing"; cout<<"\n* Spaced hanged words"; cout<<"\n* Added Changelog to the program"; cout<<"\n* Many bug fixes"; cout<<"\nV1.8 Beta:-"; cout<<"\n* Display hanged letters"; cout<<"\n* Fixed bad spacings and tabs"; cout<<"\n* Minor bug fixes"; cout<<"\nV1.5 Beta:-"; cout<<"\n* Custom words by the playmaker"; cout<<"\n* Fixed the Hangman graphics issue at the start"; cout<<"\nV1.2 Beta:-"; cout<<"\n* Full fledged menu with exit function"; cout<<"\n* Bugfixes, improved UX"; cout<<"\nV1.1 Beta:-"; cout<<"\n* Added graphics for no hangs"; cout<<"\n* Changed a value which prevented the player from winning"; cout<<"\n* Other minor fixes"; cout<<"\n\n--Press any key to go back."; getch(); goto start; break; case 6: exit(0); default: cout<<"\n\t\tWrong option! Press any key to continue."; getch(); goto start; } getch(); }
You can also download the .cpp file for the entire code from my Drive. After getting the file, simply open it with your C++ compiler to view the program and run to see the output.
Click here to Download HANGMANCBSE.cpp
If you like it, please rate the post and follow my blog. There's a lot of other codes to try out too in simplified versions of mine. Also, do report if there's any problem with the code or you can comment your suggestions for improving the code.