// Hangman #include #include #include #include #include using namespace std; // *******************************************************************function declarations void drawBody(int num); //hangman draw function void gameOver(); //gameover function int lettersFound (char, string, string&); //user guess function int main() { // ********************************************************************variable declarations char guessedLetters[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o','p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; char userLetter; // user's guessed letter int numberWrong = 0; // number of wrong guesses by the user const int MAX_LIVES = 6; // max lives string word; //secret word string words[] = //possible word array {"alabama", "alaska", "arizona", "arkansas", "california", "colorado", "connecticut", "delaware", "florida", "georgia", "hawaii", "idaho", "illinois", "indiana", "iowa", "kansas", "kentucky", "louisiana", "maine", "maryland", "massachusetts", "michigan", "minnesota", "mississippi", "missouri", "montana", "nebraska", "nevada", "newhampshire", "newjersey", "newmexico", "newyork", "sorthcarolina", "northdakota", "ohio", "oklahoma", "oregon", "pennsylvania", "rhodeisland", "southcarolina", "southdakota", "tennessee", "texas", "utah", "vermont", "virginia", "washington", "westvirginia", "wisconsin", "wyoming"}; cout << "Enter the 'Y' key to choose your own word, the 'N' key to let the computer choose one of the 50 states" << endl; cin >> word; if (word == "Y" || word == "y") { cout << "Choose a word using lowercase-letters:" << endl; cin >> word; } else { srand (time(NULL)); //random number generator int num = rand()% 50 + 1; //chooses word from array word = words[num]; //assign chosen word as secret word } string answer(word.length(),'*'); //declares answer, using .length function assigns * for number of letters in secret word //*********************************************************************intro cout << "************************************" << endl; //game intro cout << endl; cout << "Welcome to Hangman" << endl; cout << "The secret word is one of the 50 states or a word of your choice" << endl; cout << "Each letter is represented by a *" << endl; cout << "Type only one letter in each try" << endl; cout << "Do not use capital letters" << endl; cout << "You have " << MAX_LIVES << " lives" << endl; cout << endl; cout << "************************************" << endl; cout << endl; system("pause"); //*********************************************************************loop while (numberWrong != MAX_LIVES) //start of loop, lasts while number wrong doesnt equal max lives { drawBody(numberWrong); //draws hangman for each loop cout << "Secret word: " << answer; // prints *'s of length of word cout << endl; cout << endl; //takes letters remaining in guessedLetters and puts it in letters variable string letters; for (int i = 0; i < 26; i ++) { stringstream ss; string s; ss << guessedLetters[i]; ss >> s; letters += s; } cout << "Unguessed Letters: " + letters << endl; cout << endl; cout << "Type a letter: "; cin >> userLetter; //user inputs guess //marks off letters that have/ have not been guessed for (int i = 0; i < 26; i ++) { if (userLetter == guessedLetters[i]) { guessedLetters[i] = '_'; break; } else if (i == 25 && userLetter !=guessedLetters[i]) { cout << "You've already guessed that!" << endl; system("Pause"); } } //.//////////////////////////////// cout << endl; cout << "»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»" << endl; cout << "»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»" << endl; cout << "»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»" << endl; cout << "»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»" << endl; if (lettersFound (userLetter, word, answer) == 0) //if letter isnt found in secret word { cout << endl; cout << "Letter not found " << "-1 Life" << endl; numberWrong++; cout << endl; cout << "~ " << MAX_LIVES - numberWrong << " lives remaining" << " ~" << endl; } else if (word == answer) //if user guesses all letters in word { cout << endl; cout << "You found the word!" << endl; cout << endl; cout << "Secret word: " << word << endl; cout << endl; break; } else //if user guesses a letter/s { cout << endl; cout << "You found a letter!" << endl; cout << endl; cout << "~ " << MAX_LIVES - numberWrong << " lives still remaining" << " ~" << endl; } } //end of while loop if (numberWrong == MAX_LIVES) //when all lives are lost { gameOver(); cout << "The secret word was " << word << endl; cout << endl; } system("pause"); return 0; }//end of main //*********************************************************************** //**************************************************************functions //*********************************************************************** int lettersFound (char userLetter, string word, string &answer) { int i = 0; //declare variable i int match = 0; //declare variable matches for (i = 0; i < answer.length(); i++) { if (userLetter == answer[i]) //if letter already found do nothing { return 0; } if (userLetter == word[i]) //find and return any letters found in secret word { answer[i] = userLetter; match++; } } return match; } //end of lettersFound void drawBody(int num) { if(num == 0) { cout << endl << endl <<" +----+ "<