Making the C++ Hangman Game into a two player?

Discussion in 'C++' started by Daniel-Hansonel, May 17, 2011.

  1. Daniel-Hansonel

    Daniel-Hansonel New Member

    Joined:
    May 17, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi Guys!!:nice:

    I need to put a variable so that two players are able to play the following game, but I am unaware how to do this. Is someone able to assit me?

    PHP:
    #include <iostream>
    #include <string>
    #include <vector>
    #include <ctime>
    #include <fstream>
    using namespace std;

    void info ()
    {
        
              ;
    }

    void get_word (stringword)
    {
        
    ifstream read ("words.txt");

        
    vector <stringword_list;
        
        
    read >> word;

        while (
    word != "$"//at the end of the file there is a '$' to stop reading when it gets there!! :~)
        
    {    
            
    word_list.push_back (word);
        
            
    read >> word;    
        }

        
    srand (time (0) );

        
    int i rand() % word_list.size();

        
    word word_list [i];
    }

    void check_for_win (vector <charsecret_wordvector <charmywordboolwin)
    {
        
    //check if the played has found the word

        
    for (int i 0myword.size(); i++)
        {
            if (
    secret_word [i] == myword [i])
            {
                
    win true;
            }
            else
            {
                
    win false;
                break;
            }
        }
    }

    void process (vector <char>& secret_wordvector <charmywordinttries_left)
    {
        
    typedef vector <char> ::size_type vec_siz;
        
    vec_siz word_size myword.size();

        
    cout << "\n" << "->Enter a letter: ";

        
    char letter;

        
    cin >> letter

        
    bool exists false;
        
    bool already_exists false;

        for (
    int i 0word_sizei++) //is the letter in the word?
        
    {
            if (
    letter == myword [i])
            {
                if (
    secret_word [i] == letter)
                {
                    
    already_exists true;
                }

                
    exists true//the letter exists in the word
                
    secret_word [i] = letter;
            }
        }

        if (
    already_exists == true)
        {
            
    cout << "  The letter '" << letter << "' already exists! ";
        }
        else
        {
            if (
    exists == true)
            {
                
    cout << "  Nice! '" << letter << "' is in the word! ";
            }
            else
            {
                
    cout << "  Sorry. '" << letter << "' is not in the word.. ";
            }
            
            
    tries_left --;
        }
    }

    void print (const vector <charsecret_word/*const before 'vector' increases velocity*/
    // prints secret_word on the screen

        
    typedef vector <char> ::size_type vec_siz;
        
    vec_siz size secret_word.size();

        for (
    int i 0!= sizei++)
        {
            
    cout << secret_word [i];
        }
    }

    int main()
    {
        
    string word;

        
    info ();

        
    get_word (word);

        
    typedef string::size_type str_siz;
        
    str_siz word_size word.size(); //how many letters the word has
        
        
    vector <charmyword//storage for the letters

        
    for (int i 0!= word_sizei++) //add the word's letters to the vector
        
    {
            
    myword.push_back (word[i]);
        }

        
    vector <charsecret_word//create a vector to store the secret word, e.g. s***p
        
        //set secret_word's data
        
    for (int i 0word_sizei++)
        {
            
    secret_word.push_back ('*');
        }

        
    secret_word [0] = myword [0];
        
    secret_word [word_size 1] = myword [word_size 1]; //send the first and the second letter of 'myword' to 'secret word'

        //replace words same as the first and the last, e.g. d*dd* [daddy]
        
    for (int i 0word_sizei++)
        {
            if (
    myword [i] == myword [0]) // same as the first letter
            
    {
                
    secret_word [i] = myword [0];
            }

            if (
    word [i] == myword [word_size 1]) // same as the last letter
            
    {
                
    secret_word [i] = myword [word_size 1];
            }
        }

        
    // THE 'MAIN LOOP'
        
    int tries_left;

        if (
    word_size == 3)
        {
            
    tries_left 2;
        }
        else if ( (
    word_size 3) && (word_size <= 5) )
        {
            
    tries_left 6;
        }
        else if ( (
    word_size >=5) && (word_size  10) )
        {
            
    tries_left int (word_size 1.4);
        }
        
        
    cout << "..::HangMan::.. [by Minas Mina]"
             
    << "\n\n[";
        
        print (
    secret_word);

        
    cout << "]" << "\n";
        
        
    bool win false;

        while ( (
    win == false) && (tries_left 0) )
        {
            
    process (secret_wordmywordtries_left); //get input and check if exists in the word

            
    check_for_win (secret_wordmywordwin); //has the player won? here we check it

            
    cout << "Tries left:" << tries_left << ", [";

            print (
    secret_word);

            
    cout << "]\n";
        }

        if (
    win == true)
        {
            
    cout << "\n" << "Congratulations, you win!! ";
        }
        else
        {
            
    cout << "\n" << "You lose..Try harder next time.. ";
        }

        
    system ("PAUSE");

        return 
    0;
    }

     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    You need to do a lot more than "put a variable", whatever that means.

    Firstly, hangman is a one player game by definition. How exactly do you want it to be two player?
    Will there be two men to hang? Or do you want alternate players to enter letters? Is there a winner or is it a collaborative game? If a winner, how will that winner be determined?

    Then once you've determined how it's going to work, you'll need to add code to implement those changes. If it's as simple as two players working together to hang one man with no overall winner then you'll probably need to do little more than alternate between
    Code:
    cout << "\n" << "->Player 1 Enter a letter: ";
    
    and
    Code:
    cout << "\n" << "->Player 2 Enter a letter: ";
    
     
    shabbir likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice