Posted by kahrn on Sat 12 Jul 02:47
report abuse | download | new post
- /*
- Classic game of hangman / 25th May 2008
- Programming by krazekaveman <brandonkite92@gmail.com> and Ricky Hewitt [kahrn] <kahrny@gmail.com>
- Free to use/edit/republish without restriction
- */
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <vector>
- #include <algorithm>
- #include <ctime>
- #include <cctype>
- using namespace std;
- // The location of the wordlist
- const char *WORDLIST_LOCATION = "wordlist.txt";
- // Function declarations
- void call_hangman();
- vector<string> generate_wordlist();
- int main()
- {
- cout << "Welcome to Hangman! Good luck\n";
- call_hangman(); // This calls hangman, initialises the game and stuff.
- // While loop is to determine whether or not to play again.
- while (1) {
- // Prompt for playing again, act upon..
- char cPlayAgain = '!';
- cout << "\nPlay again? (Y/N): ";
- cin.getline(&cPlayAgain, 2);
- // Play the game again.
- if (cPlayAgain == 'y' or cPlayAgain == 'Y') {
- call_hangman();
- cout << endl; // Create new line (for fancy output)
- }
- // Exit
- if (cPlayAgain == 'n' or cPlayAgain == 'N') {
- break;
- }
- }
- return 0;
- }
- vector<string> generate_wordlist()
- {
- // Initialise vector
- vector<string> words;
- // Read from file and retrieve word list
- ifstream wordfile;
- string line;
- wordfile.open(WORDLIST_LOCATION);
- if (wordfile.is_open()) {
- while (!wordfile.eof()) {
- getline(wordfile, line);
- // Discard empty lines and comment lines
- if (line != "" and line[0] != '#') {
- // Convert string to uppercase
- for (int i = 0; i < line.length(); i++) {
- line[i] = toupper(line[i]);
- }
- // Add converted string to string vector.
- words.push_back(line);
- }
- }
- wordfile.close();
- }
- return words;
- }
- void call_hangman()
- {
- // Setup
- const int MAX_WRONG = 8;
- int wrong=0;
- // Generate random word
- vector<string> words = generate_wordlist();
- srand(time(0));
- random_shuffle(words.begin(), words.end());
- string THE_WORD = words[0]; // The generated random word
- string soFar(THE_WORD.size(),'-'); // The guess so far by the user
- string used=""; // Characters that have been guessed.
- // Check if the player gets to keep guessing
- while ((wrong<MAX_WRONG) && (soFar!=THE_WORD))
- {
- cout << "\n\nYou Have " << (MAX_WRONG-wrong) << " incorrect guesses left.\n";
- cout << "\nYou've used the following letters:\n" << used << endl;
- cout << "\nSo far, the word is:\n" << soFar << endl;
- // Get the players guess
- char guess;
- cout << "\n\nEnter your guess: ";
- cin >> guess; // bug here
- guess = toupper(guess); // make uppercase
- while (used.find(guess) != string::npos)
- {
- cout << "\n You've already guessed: "<<guess<<".\n";
- cout << "\n\nEnter your guess: ";
- cin >> guess;
- guess = toupper(guess); // make uppercase
- }
- used += guess;
- // If the user guesses a correct character.
- if (THE_WORD.find(guess) != string::npos)
- {
- cout << "That's right! " << guess << " is in the word\n";
- //update soFar to include the newly guessed number
- for (unsigned int i=0; i<THE_WORD.length(); ++i)
- {
- if (THE_WORD[i]==guess)
- {
- soFar[i]=guess;
- }
- }
- }
- else
- {
- cout << "Sorry " << guess << " isn't in the word";
- ++wrong;
- }
- }
- // End of the game
- if (wrong == MAX_WRONG) {
- cout << "\nYou have been HANGED!\n";
- cout << "\b" << endl;
- }
- else {
- cout << "\nYou guessed it!\n";
- }
- cout << "The word was " << THE_WORD << endl;
- getchar();
- }
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.