First Programs I’ve written

8 Apr

Recently, as in today while cleaning up my Code folder I can across a simple Hangman game that I had written a long time ago back when I was just completing my course in C++ and trying to make sense of it all.

Mind you it is very basic and needs a lot of work to make it more efficient and meet the standards required. So without further ado, cast your eyes down upon my very cool code :).

/************************************************************************* 
	Author: toughaspixels
	Purpose: To study and understand the use of STL ad Vectors
	Date: October 26th, 2009
	Modfied: April 7th, 2010
        License: Just let me know your using it please, feed my ego
*************************************************************************/
																			
#include<iostream>
#include<vector>
#include<algorithm>
#include<ctime>
#include<cctype>

using namespace std;

int main(){
    
    const int MAX_WRONG = 8;
    
    vector<string> words;
    words.push_back("APPLES");
    words.push_back("MEGAGAMES");
    words.push_back("ZELDA");
    words.push_back("SILVER");
    words.push_back("GLUE");
    words.push_back("TINTIN");
    words.push_back("GEEK");
    words.push_back("MANHATTEN");
    words.push_back("TINTIN");
    words.push_back("ASTERIX");
    
    srand(time(0));
    random_shuffle(words.begin(), words.end());
    const string THE_WORD = words[0];
    int wrong = 0;
    string soFar(THE_WORD.size(), '-');
    string used = "";
    
    cout << "Welcome to HangMan. Good Luck!" << endl;
    
    while((wrong < MAX_WRONG) && (soFar != THE_WORD)){
        cout << "You have " << MAX_WRONG - wrong << " incorrect guesses left." << endl;
        cout << "You have used the following letters " << used << endl;
        cout << "So far, The word is:\n" << soFar << endl;
        
        char guess;
        cout << "\n\nEnter your guess: ";
        cin >> guess;
        guess = toupper(guess);
        while(used.find(guess) != string::npos){
            cout << "\nYou have already guessed " << guess << endl;
            cout << "Enter your guess: ";
            cin >> guess;
            guess = toupper(guess);
        }
        
        used += guess;
        
        if(THE_WORD.find(guess) !=  string::npos){
            cout << "That's right! " << guess << " is in the word." << endl;
            for(int i = 0; i < THE_WORD.length(); i++){
                if(THE_WORD[i] == guess)
                    soFar[i] = guess;
            }   
        }
        else{
            cout << "Sorry, " << guess << "is not in the word." << endl;
            wrong++;
        }
    }    
    
    if(wrong == MAX_WRONG)
        cout << "GAH!! GUH! NOOOOOooooooo\n Your hanged : ) HAHAHAHA" << endl;
    else
        cout << "\nYou guessed it : ) Your awesome." << endl;
        
    cout << "\nThe word is " << THE_WORD << endl;
    
    return 0;
}

There you have it folks, enjoy.

Please feel free to use my code, just make sure I get credit or at the very least let me know whose using it.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: