All pastes #730196 Raw Edit

Hangman

public cpp v1 · immutable
·published 2007-10-09 00:01 UTC
rendered paste body
#include <iostream>#include <fstream>#include <vector>#include <string>#include <stdio.h>#include <cstdlib> #include <assert.h>using namespace std;struct HANGMAN{    string word;    unsigned int right;    unsigned int wrong;    unsigned int total;    string guessed;    HANGMAN() { word = ""; right = 0; wrong = 0; total = 0; guessed = ""; };    void reset() { word = ""; guessed = ""; right = 0; wrong = 0; total = 0; };};typedef struct HANGMAN HangMan;// Check to see if the user has guessed the wordbool win( HangMan game ){    bool result = true;      for ( int i  = 0; i < game.word.length(); i++ )        if ( game.guessed.find( game.word[ i ] ) == string::npos )            result = false;    return result;}// Picks a word at random from a vector by treating it like a treestring pickWord( vector<string> &words ){    string result;    int num = rand() % ( words.size() );    unsigned int index = 0;        srand((unsigned)time(0));     for ( int i = 0; i < num; i++ )    {        index = ( index + 3 > words.size() ) ? index : index + rand()%3;    }    assert( index < words.size() );    result = words[index];        // Delete the word from the vector    vector<string>::iterator toDelete = words.begin();    for( unsigned int i; i <= index; i++ )        toDelete++;    words.erase( toDelete );        return result;        }void drawGallows( int numWrong )        /* Draws gallows with hangman based on amount of wrong guesses */{    cout << " ___" << endl;      cout << "|   | " << endl << "|";    if (numWrong >= 1) cout << "   o ";        cout << endl << "|";        if (numWrong >= 2) cout << "  /";    if (numWrong >= 3) cout << "|";    if (numWrong >= 4) cout << "\\";        cout << endl << "|";        if (numWrong >= 5) cout << "  /";    if (numWrong >= 6) cout << " \\";    cout << endl << "|" << endl;    cout << "|_________" << endl;     cout << endl;}//   Get a letter from the user, and increase the correct countervoid getMove( HangMan &game ){    string s;                   // The text the user enters    char ch;                    // choice        // Primpt for a letter    cout << "Your letter: ";    getline( cin, s );    ch = s[ 0 ]; // Only use the first letter    printf( "\n" );           while ( game.guessed.find( ch ) != string::npos )    {   // Keep asking until a valid choice is entered        cout << "You have already guessed " << ch << ", please try again!" << endl;        cout << "Your letter: ";        getline( cin, s );        ch = s[ 0 ];        printf( "\n" );    }    game.guessed += ch;   // Add the choice to the list of guessed letters    game.total++;           // Increase the total guess count        // Increase the proper guess counter    if ( game.word.find( ch ) != string::npos )        game.right++;    else        game.wrong++;}int main( int argc, char *argv[] ){    HangMan game;    vector<string> words;    fstream in( "hangman.cpp", ios::in );    string s;    char playAgain = 'y';    int lines = 0;        while( getline( in, s ) )        lines++;    in.close();    in.open( "words.txt", ios::in );           // Load the words list    while ( getline( in, s ) )        words.push_back( s );    in.close();            //play game    while ( playAgain == 'y' )    {        game.reset();               // Reset all stats        game.word = pickWord( words );  // Select a word from the vector                while ( (game.wrong < 6) && !win( game ) )         {   // Play until the user guesses the word or makes 6 mistakes            system( "clear" );            cout << "C++ Hangman (VectorBased)\nLines of code: " << lines << "\nRemaining words: " << words.size() << "\n\n";            drawGallows( game.wrong );            for ( unsigned int i = 0; i < game.word.length(); i++ )            {                if ( game.guessed.find( game.word[ i ] ) != string::npos )                    printf( "%c ", game.word[ i ] );                else                    printf( "_ " );            }            cout << "\nYou have guessed: " << game.guessed << endl;                 getMove( game );                    }        system( "clear" );        cout << "C++ Hangman (Vector Based)\nLines of code: " << lines << "\nRemaining words: " << words.size() << "\n\n";        drawGallows( game.wrong );                if ( game.wrong >= 6 )            cout << "The word was: " << game.word << "\nY o u  L o s e !\nYou made " << game.total << " guesses." << endl;        else            cout << "The word was: " << game.word << "\nY o u  W i n !\nYou made " << game.total << " guesses." << endl;                cout << "Play again (y/n)? ";        cin >> playAgain;        cin.ignore();    }        printf( "Program terminating...\n" );    return 0;}