Skip to content

HangMan Game in C++ #603

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions C++/HangMan/Hangman.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#include <bits/stdc++.h>

using namespace std;

const int MAX = 4;

int letters(char guess, string secretword, string &guessword)
{
int i;
int matches = 0;
int len = secretword.length();
for (i = 0; i< len; i++)
{
if (guess == guessword[i])
return 0;

if (guess == secretword[i])
{
guessword[i] = guess;
matches++;
}
}
return matches;
}

int main ()
{
string name;
char letter;
int guesses = 0;
string word;
string words[] = {"india", "pakistan", "nepal", "malaysia", "srilanka", "australia", "iran", "ethiopia","indonesia"};

srand(time(NULL));
int n = rand()% 10;
word = words[n];

string s1(word.length(),'$');


cout << "\n\nWelcome to Hangman.......\nGuess a country Name............";
cout << "\n\nEach letter is represented by a $ .";
cout << "\n\nYou have to type only one letter in one try";
cout << "\n\nYou have " << MAX << " tries to try and guess the word.";
cout << "\n-------------------------------------------------------";


while (guesses < MAX)
{
cout << "\n\n" << s1;
cout << "\n\nGuess a letter:- ";
cin >> letter;

if (letters(letter, word, s1)==0)
{
cout << endl << "Whoops! That letter isn't in there!" << endl;
guesses++;
}
else
{
cout << endl << "You found a letter! Isn't that exciting!" << endl;
}

cout << " You have " << MAX - guesses;
cout << " Guesses left." << endl;

if (word == s1)
{
cout << word << endl;
cout << "Yeah! You got it!";
break;
}

}
if(guesses == MAX)
{
cout << "\nSorry, You lose...You've been Hanged." << endl;
cout << "\nThe word was : " << word << endl;
}
cin.ignore();
cin.get();
return 0;
}

Binary file added C++/HangMan/Hangman.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions C++/HangMan/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Hangman Game
***
## Introduction
This is a Guessing Game of Names of Contries Using C++ (Programming Language)

***
## How to Use
1. Pull the code into your system.
2. Run **Hangman-Game.cpp**
3. Guess the Name and Enjoy it.

***
## Output
![](Hangman.jpg)