Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string>
- #include <fstream>
- #include <vector>
- #include <iostream>
- #include <algorithm>
- #include <ctime>
- #include <random>
- #include <set>
- using namespace std;
- // Global variable.
- string rand_word;
- // This function picks a random word from a text file, then returns it.
- string pick_word(const string picked_word)
- {
- vector<string> words;
- ifstream in("dictionary.txt");
- // Checks to see if the file was successfully read.
- if (!in){
- cout << "file not found." << endl;
- return 0;
- }
- while (in) {
- string word;
- in >> word;
- words.push_back(word);
- }
- //srand(time(0));
- string r = words[rand() % words.size()];
- return r;
- }
- // This function calls pick_word then starts
- // a hangman game with it.
- int hang_man(string word)
- {
- int misses = 0;
- int max_misses = 10;
- int exposed = 0;
- string display = word;
- int word_size = word.size();
- cout << "There are " << word_size << " letters." << endl;
- for (int i = 0; i < word_size; ++i)
- display[i] = '-';
- set<char> guessed;
- char guess;
- while (display != word)
- {
- // Determines wether or not the user has used all his/her guesses.
- if (misses >= max_misses)
- {
- cout << endl;
- cout << " Uh oh! You're out of guesses!n";
- cout << " The word was " << word << "!" << endl;
- cout << endl;
- return 0;
- }
- // Gives the user details on their current game.
- cout << "Already guessed: ";
- copy(guessed.begin(), guessed.end(), ostream_iterator<char>(cout, " "));
- cout << "nMisses: " << misses << endl;
- cout << "Remaining guesses " << max_misses - misses << endl;
- cout << "Guess a letter.nn ";
- cout << display << "nn";
- cin >> guess;
- cout << endl;
- // Checks input for errors such as capitalization
- // of if it's even a number.
- guess = tolower(guess);
- if (!isalpha(guess))
- {
- cout << "That is not a letter!n";
- continue;
- }
- bool right = false;
- //Checks if input has already been entered.
- if (guessed.find(guess) != guessed.end())
- {
- cout << guess << " has already been guessed.n";
- right = true;
- }
- guessed.insert(guess);
- // Compares guess with each char of word and
- // updates display accordingly.
- for (int i = 0; i < word_size; ++i)
- {
- if (guess == word[i])
- {
- display[i] = word[i];
- exposed++;
- right = true;
- }
- }
- // Checks the boolian value to determine
- // whether or not to subtract a guess.
- if (!right)
- {
- misses++;
- cout << guess << " is not in the word." << endl;
- }
- }
- // Displays final stat of game
- // and prompts the user for another try.
- cout << "That's right! The word was " << word << endl;
- cout << "You missed " << misses << " times!n";
- return 0;
- }
- int main()
- {
- // Calls the hangman function which functions as the hangman game itself
- cout << "---------- Welcome to Hangman! ----------" << endl;
- hang_man(pick_word(rand_word));
- char answer;
- char yes = 'y';
- // Prompts the user for another game.
- cout << " Would you like to play again?" << endl;
- cout << " Press y to play again or anything else to exit. " << endl;
- cin >> answer;
- if (answer != yes)
- {
- cout << "Okay! See you next time!" << endl;
- return 0;
- }
- // Calls the hangman function for another game.
- else
- while (answer = yes)
- {
- hang_man(pick_word(rand_word));
- cout << " Would you like to play again?" << endl;
- cout << " Press y to play again or anything else to exit. " << endl;
- cin >> answer;
- if (answer != yes)
- {
- cout << "Okay! See you next time!" << endl;
- return 0;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement