Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Hangman
- #include <iostream>
- #include <string>
- #include <vector>
- #include <algorithm>
- #include <ctime>
- #include <cctype>
- #include <time.h>
- using namespace std;
- void drawing(int position); //declare conjuction, conjunction....what's your function
- int main()
- {
- const int MAX_WRONG = 6; //how many attempts user has
- vector<string> WordList = { "abruptly","absurd","abyss","affix","askew","avenue","awkward","axiom","azure","bagpipes","bandwagon","banjo","bayou","blitz","blizzard","boggle","bookworm" };
- srand(static_cast<unsigned int>(time(0)));
- random_shuffle(WordList.begin(), WordList.end());
- const string TheWord = WordList[0]; //picks word to guess
- int wrong = 0; //how many incorrect guesses there have been
- string soFar(TheWord.size(), '-'); //shows whats in the word thats been guesses
- string used = " ";
- char guess;
- cout << "Welcome To Hangman\n";
- while ((wrong < MAX_WRONG) && (soFar != TheWord)) //the main loop
- {
- cout << "\n\nYou Have " << (MAX_WRONG - wrong);
- cout << " incorrect guesses left.\n";
- cout << "\nYou've used the following letters: \n" << used << endl;
- cout << "\nEnter your guess: ";
- cin >> guess;
- guess = tolower(guess);
- while (used.find(guess) != string::npos) //checks if users input is in TheWord
- {
- cout << "You have already guessed \n" << guess << endl;
- cout << "Enter your guess: ";
- cin >> guess;
- guess = tolower(guess);
- }
- used += guess;
- if (TheWord.find(guess) != string::npos) //user is correct
- {
- cout << "Correct, " << guess << " is in the word\n";
- for (int i = 0; i < TheWord.length(); ++i)
- {
- if (TheWord[i] == guess)
- {
- soFar[i] = guess;
- }
- }
- }
- else //user is incorrect
- {
- cout << "Sorry, " << guess << " isn't in the word.\n";
- wrong++; //counts up to 6
- }
- drawing(wrong); //draws board after every guess
- cout << "\n\n" << soFar;
- }
- if (wrong == MAX_WRONG)
- {
- cout << "\nYou've been hanged\n";
- cout << "The word was " << TheWord;
- }
- else
- {
- cout << "\nCongrats!!! You guessed the word" << endl;
- }
- return 0;
- }
- void drawing(int position)
- {
- switch (position)
- {
- case 1:
- cout << " ___________" << endl;
- cout << " | }" << endl;
- cout << " | \\ " << endl;
- cout << " | " << endl;
- cout << " | " << endl;
- cout << "_|______________" << endl;
- break;
- case 2: cout << " ___________" << endl;
- cout << " | }" << endl;
- cout << " | \\ 0 " << endl;
- cout << " | " << endl;
- cout << " | " << endl;
- cout << "_|______________" << endl;
- break;
- case 3:
- cout << " ___________" << endl;
- cout << " | }" << endl;
- cout << " | \\ 0 /" << endl;
- cout << " | " << endl;
- cout << " | " << endl;
- cout << "_|______________" << endl;
- break;
- case 4:
- cout << " ___________" << endl;
- cout << " | }" << endl;
- cout << " | \\ 0 /" << endl;
- cout << " | |" << endl;
- cout << " | " << endl;
- cout << "_|______________" << endl;
- break;
- case 5:
- cout << " ___________" << endl;
- cout << " | }" << endl;
- cout << " | \\ 0 /" << endl;
- cout << " | |" << endl;
- cout << " | / " << endl;
- cout << "_|______________" << endl;
- break;
- case 6:
- cout << " ___________" << endl;
- cout << " | }" << endl;
- cout << " | \\ 0 /" << endl;
- cout << " | |" << endl;
- cout << " | / \\ " << endl;
- cout << "_|______________" << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement