/************************************************ Author: Title: hangman.cpp Description: Usage: *************************************************/ #include #include #include #include using namespace std; void drawHangmanDamageStage (int stage); string randomFileLine(string filename,int fileLines); int main() { //Name request cout << "Please enter only your first name" << endl; string playername; cin >> playername; //Print welcome message cout << playername << ", we are going to play a game. The game is..." << endl; cout << "\n \n \t \t HUNTER HANGMAN \n \n" << endl; //Main menu options to quit or play cout << "\t \t Play [play] \n \t \t Exit [exit] \n \t \t \n Please type your choice."<> menuoption; if (menuoption == "play" || menuoption == "Play") { string word; //Asks for a difficulty and gets the secret word //from the corresponding difficulty text file bool difficultySelected = false; while (difficultySelected == false) { string difficulty; cout << "Please type a difficulty level [easy, medium, hard]:" << endl; cin >> difficulty; if (difficulty == "easy" || difficulty == "Easy") { word = randomFileLine("easy.txt", 16); difficultySelected = true; } else if (difficulty == "medium" || difficulty == "Medium") { word = randomFileLine("medium.txt", 21); difficultySelected = true; } else if (difficulty == "hard" || difficulty == "Hard") { word = randomFileLine("medium.txt" , 23); difficultySelected = true; } else continue; } string wordcopyforlose = word; string guess; string display ; //Generates the hangman gameboard for (int t = 0;t < word.size()-1;t++) { string puzzlepart = "_ "; display.append(puzzlepart); } //Sets the word and the gameboard onto the same indices for (int q = 0;q < display.size()-1;q++) { q++; string padding =" "; word.insert(q,padding); } int wrong = 0; int correct = 0; //Main Game loop while (true) { drawHangmanDamageStage(wrong); cout << "Guess a letter\n"<< display <<"\n Your Guess: "; cin >> guess; if (guess.size() < 2 ) { if (word.find(guess) != -1) { while (word.find(guess) != -1) { int indexnumber = word.find(guess); display[indexnumber] = word[indexnumber]; string empty= " "; word [indexnumber] = empty[0]; correct++; } //Informs player of victory and ends the game if (correct == (word.size()-1)*.5) { cout << display; cout << "\n \n \t \t You've won! \n \n" << endl; break; } } //Informs player of his defeat and ends the game else if (wrong > 5) { drawHangmanDamageStage(wrong); cout << playername << "...." << endl; cout << "\n \n \t \t You have lost... \n \n" << endl; cout << "\n \n The word was : " <