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.
(Skip to Download -- Click Here)
Mechanism
Source Code
/*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(); }
Click here to Download HANGMANCBSE.cpp