#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
/* I'm going to attempt to create a hangman game that can be changed to suit the players weekly spelling words.
This game will be aimed at Primary School aged Children.*/
usingnamespace std;
constint MAX_TRIES = 5;
int letterFill(char, string, string&);
int main()
{
string name;
char letter;
int num_of_wrong_guesses = 0;
int amount;
int words;
string word;
srand(time(NULL));
cout << "Welcome to Hangman \n" << endl;
// Entering the amount of words to be used.
std::cout << "How many words do you have? (Please enter a number between 2 and 10)" << std::endl;
cin >> amount;
switch (amount) {
// Two words
case'2':
cout << "\n Please enter 2 words" << endl;
cin >> words;
// Three words
case'3':
cout << "\n Please enter 3 words" << endl;
cin >> words;
break;
// Four words
case'4':
cout << "\n Please enter 4 words" << endl;
cin >> words;
break;
// Five words
case'5':
cout << "\n Please enter 5 words" << endl;
cin >> words;
break;
// Six words
case'6':
cout << "\n Please enter 6 words" << endl;
cin >> words;
break;
// Seven words
case'7':
cout << "\n Please enter 7 words" << endl;
cin >> words;
break;
// Eight words
case'8':
cout << "\n Please enter 8 words" << endl;
cin >> words;
break;
// Nine words
case'9':
cout << "\n Please enter 9 words" << endl;
cin >> words;
break;
// Ten words
case'10':
cout << "\n Please enter 10 words" << endl;
cin >> words;
break;
// Any other value entered
default:
cout << "Invalid choice" << endl;
break;
}
cout << "\n Thank you. Now lets play Hangman!" << endl;
// instert a code to choose a word at random
words = rand() % 10 + 1;
// code for actual gameplay
string unknown(word.length(), '*');
cout << "\n Each letter is represented by an asterisk.";
cout << "\n You have to type only one letter in each try.";
cout << "\n You have " << MAX_TRIES << " tries to guess your word";
cout << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
while (num_of_wrong_guesses < MAX_TRIES)
{
cout << "\n\n" << unknown;
cout << "\n Guess a letter: ";
cin >> letter;
if (letterFill(letter, word, unknown) == 0)
{
cout << endl << "Sorry, that letter isn't there!" << endl;
num_of_wrong_guesses++;
}
else
{
cout << endl << "You have found a letter!" << endl;
}
cout << "You have " << MAX_TRIES - num_of_wrong_guesses;
cout << " guesses left." << endl;
if (word == unknown)
{
cout << word << endl;
cout << "You got it right!";
break;
}
}
// Game over
if (num_of_wrong_guesses == MAX_TRIES)
{
cout << "\n Sorry, you lose... you've been hanged." << endl;
cout << "The word was : " << word << endl;
}
cin.ignore();
cin.get();
return 0;
}