Login Register


Hangman, the command line game.[C++] filter_list
Author
Message
Hangman, the command line game.[C++] #2
Code:
#include <iostream> #include <string> #include <ctime> #include <cstdlib> using namespace std; const int dc_lkm = 10; // Let's create a table which includes all words const string words[dc_lkm] = {"printer", "calculator", "television", "guitar", "speakers", "shelf", "book", "cartridge", "clock", "paper"}; int main() { srand(time(0)); // Take a seed value of the time so the game is different every time while(true) { int guesses = 5; cout << "What you want to do?\n"; cout << "1.Play the game\n"; cout << "2.Turn the graphics mode on\n"; cout << "3.End the game\n"; int choice; cin >> choice; cout << string(3, '\n'); if(choice == 1) { // Raffle the word which will be asked string word = words[rand() % dc_lkm]; //Format the variable, which will be show to the player. int length = word.length(); string for_your_eyes_only(length, '-'); // This is for storing non-found characters string bad_chars; cout << "Your word: " << for_your_eyes_only << endl; while(guesses > 0 && word != for_your_eyes_only) { // Show the word and ask for the letter cout << "Guess a letter: "; char letter; cin >> letter; cout << endl; // Check if user has already guessed that letter if (bad_chars.find(letter) != string::npos || for_your_eyes_only.find(letter) != string::npos) { cout << "You've already guessed that letter! Try again.\n"; continue; } // Initialize the variable which stores information about incrementing the letter (element) is the word. int loc = word.find(letter); // Check if the letter is in the word. if (loc == string::npos) { cout << "Wrong!\n"; --guesses; // Reduce the number of guesses bad_chars += letter; // Add to non-found letters } else // Letter was found { cout << "Good guess!\n"; // Add the letter to the right place for_your_eyes_only[loc] = letter; // We have to remember to check if the letter was in the word elsewhere loc = word.find(letter, loc + 1); while(loc != string::npos) { for_your_eyes_only[loc] = letter; loc = word.find(letter, loc + 1); } } cout << "Your word: " << for_your_eyes_only << endl; if (for_your_eyes_only != word) { if (bad_chars.length() > 0) // Show the wrong letters so the game is easier cout << "Wrong guessed letters: " << bad_chars << endl; cout << guesses << " wrong guesses left.\n"; } } // Check if user has guessed the word if (guesses > 0) cout << "It's correct!\n"; else cout << "You lost\n"; cout << "Unfortunately the word is: " << word << ".\n"; cout << string(10, '\n'); } if(choice == 2) { // Raffle the word which will be asked string word = words[rand() % dc_lkm]; //Format the variable, which will be show to the player. int length = word.length(); string for_your_eyes_only(length, '-'); // This is for storing non-found characters string bad_chars; cout << "Your word: " << for_your_eyes_only << endl; while(guesses > 0 && word != for_your_eyes_only) { // Show the word and ask for the letter cout << "Guess a letter: "; char letter; cin >> letter; cout << endl; // Check if user has already guessed that letter if (bad_chars.find(letter) != string::npos || for_your_eyes_only.find(letter) != string::npos) { cout << "You've already guessed that letter! Try again.\n"; continue; } // Initialize the variable which stores information about incrementing the letter (element) is the word. int loc = word.find(letter); // Check if the letter is in the word. if (loc == string::npos) { switch(guesses) { case 1: cout << "Wrong!\n"; --guesses; bad_chars += letter; cout << " ------\n"; cout << " | |\n"; cout << " | |\n"; cout << " O |\n"; cout << "/( )\ |\n"; cout << " | | |\n"; cout << " |\n"; cout << "---------\n"; break; case 2: cout << "Wrong!\n"; --guesses; bad_chars += letter; cout << " ------\n"; cout << " | |\n"; cout << " | |\n"; cout << " O |\n"; cout << "/( )\ |\n"; cout << " |\n"; cout << " |\n"; cout << "---------\n"; break; case 3: cout << "Wrong!\n"; --guesses; bad_chars += letter; cout << " ------\n"; cout << " | |\n"; cout << " | |\n"; cout << " O |\n"; cout << " ( ) |\n"; cout << " |\n"; cout << " |\n"; cout << "---------\n"; break; case 4: cout << "Wrong!\n"; --guesses; bad_chars += letter; cout << " ------\n"; cout << " | |\n"; cout << " | |\n"; cout << " O |\n"; cout << " |\n"; cout << " |\n"; cout << " |\n"; cout << "---------\n"; break; case 5: cout << "Wrong!\n"; --guesses; bad_chars += letter; cout << " ------\n"; cout << " | |\n"; cout << " | |\n"; cout << " |\n"; cout << " |\n"; cout << " |\n"; cout << " |\n"; cout << "---------\n"; break; } } else // Letter was found { cout << "Good guess!\n"; // Add the letter to the right place for_your_eyes_only[loc] = letter; // We have to remember to check if the letter was in the word elsewhere loc = word.find(letter, loc + 1); while(loc != string::npos) { for_your_eyes_only[loc] = letter; loc = word.find(letter, loc + 1); } } cout << "Your word: " << for_your_eyes_only << endl; if (for_your_eyes_only != word) { if (bad_chars.length() > 0) // Show the wrong letters so the game is easier cout << "Wrong guessed letters: " << bad_chars << endl; cout << guesses << " wrong guesses left.\n"; } } // Check if user has guessed the word if (guesses > 0) cout << "It's correct!\n"; else cout << "You lost\n"; cout << "Unfortunately the word is: " << word << ".\n"; cout << string(10, '\n'); //this is the ducktape way of solving this :/ } if(choice == 3) { return false; } } return 0; }

Enjoy this very entertaining game.

Reply





Messages In This Thread
Hangman, the command line game.[C++] - by Slarek - 08-10-2013, 11:29 AM
Hangman, the command line game.[C++] - by Slarek - 08-10-2013, 11:29 AM



Users browsing this thread: 1 Guest(s)