-1

So, Me and my friends made this game called "Guess the Word".It's a pretty basic game with this following code.

#include <iostream>
#include<string>

using namespace std;

int playGame(string word)                         //display word
{
    int misses = 0;                               // keep track of misses
    int exposed =0;                               //guess letter exposed
    string display = word;                        //value of word stored in display
    for(int i=0; i<display.length(); i++)         //initially all letters are displayed by( * )
        display[i] = '*';

while(exposed < word.length()) {
    cout<<"Miss: "<<misses<< ": ";
    cout<<"Guess a letter in word ";
    cout<< display << ":";
    char response;                                //response = user input
    cin>>response;
    bool goodGuess = false;                       //initially all letters are false
    bool duplicate = false;
    for(int i=0; i<word.length(); i++)
    if(response == word[i])                       // if user input word is true, remove * and show the letter
    if (display[i]==word[i]){
        cout<< response <<" is already in the word.\n";
        duplicate=true;
        break;
        }
        else {
        display[i] = word[i];
        exposed ++;            //increment in exposed
        goodGuess = true;      //false value becomes true
    }
    if(duplicate)
        continue;

    if(!goodGuess){               //if  user input letter is false show the cout
        misses++;                 //increment in misses
    cout<< response << " is not in the word" <<endl;
    }


 if(misses==6){                     //if user inputs 6 wrong letter  show this cout
        cout<<"   ____       _      _         _  ____     ___             _____  _____            " <<endl;
        cout<<"  /          / \\    | \\      / | |        /   \\ \\      /  |      |     \\" <<endl;
        cout<<" /  ____    /___\\   |  \\    /  | |____   |     | \\    /   |_____ |_____/" <<endl;
        cout<<" |      |  /     \\  |   \\  /   | |       |     |  \\  /    |      |    \\" <<endl;
        cout<<"  \\___ |  /       \\ |    \\/    | |____    \\___/    \\/     |_____ |     \\" <<endl;
        break;
        }
}
if(misses != 6)        //if user inputs all true letters before 6 wrong values show this cout
{

 cout<< " YES, WORD WAS COMPUTER "<<endl;
 cout <<"    _____      ___                   ____      ______         _      ________  ____    "<<endl;
 cout <<"  /          /   \\    | \\    |    /         |      \\       / \\         |      /     "<<endl;
 cout <<"  |         |     |   |  \\   |    |   ___   |______/      /___\\        |      \\___ "<<endl;
 cout <<"  |         |     |   |   \\  |    |     |   |     \\      /     \\       |          |"<<endl;
 cout <<"  \\_____     \\___/    |    \\ |    \\_____|   |      \\    /       \\      |      ____|"<<endl;
    int i,j,k;                             /* show the star */
    for (i=1;i<=5;i++)                     //for loop for next line
    {
        for (j=16;j>=i;j--)                //nested for loop to print the spaces
        {
            cout <<" ";
        }
        for (k=1;k<=(i*2)-1;k++)           //nested for loop to print *
        {
            cout <<"*";
        }
        cout <<endl;
    }
        for (i=13;i>=11;i--)              //for loop for next line
        {
            for (j=1;j<=17-i;j++)         //nested for loop to print the spaces

            {
                cout <<" ";
            }
            for (k=1;k<=i*2-1;k++)       //nested for loop to print *
            {
                cout <<"*";
            }
            cout <<endl;
        }
        for (i=5;i>=1;i--)              //for loop for next line
{
    for (j=1;j<i*2-1;j++)               //nested for loop to print the spaces
    {
        cout <<" ";
    }
    for(k=1;k<(i*2);k++)                //nested for loop to print *
        {
            cout <<"*";
        }
        for (j=9;j>i*2-1;j--)           //nested for loop to print the spaces
        {
            cout <<"    ";
        }
        for (k=1;k<(i*2);k++)          //nested for loop to print *
        {
            cout <<"*";
        }
        cout <<endl;
}
}
return misses;
}
int main(){                           /* if all the conditions are true cout this */
cout<<"************************************GUESS THE WORD*****************************\n"<<endl;
cout<<"****************************************RULES**********************************"<<endl;
cout<<"1.Guess one letter at a time."<<endl;
cout<<"2.After 6 misses the game is over."<<endl;
cout<<"*******************************************************************************"<<endl;
cout<<"***YOU MISSED***"<<playGame("computer");  //value of string word is stored here in quotations
cout<<"****ATTEMPTS TO GUESS THE WORD COMPUTER****."<<endl;
return 0;
}

How can I use rand() and an array of words in this code so that the word is different every time someone plays the game?

  • 1
    Basically a dupe of [this](http://stackoverflow.com/questions/6942273/get-random-element-from-container) – NathanOliver May 03 '17 at 14:17
  • I would advice ditching crappy `rand()` and using the much better C++11 [random](http://en.cppreference.com/w/cpp/numeric/random) facilities. – Jesper Juhl May 03 '17 at 14:17
  • [std::shuffle](http://en.cppreference.com/w/cpp/algorithm/random_shuffle) can solve your problem. – Jesper Juhl May 03 '17 at 14:35
  • 1
    Possible duplicate of [get random element from container](http://stackoverflow.com/questions/6942273/get-random-element-from-container) – Jérôme May 03 '17 at 15:31

1 Answers1

0

You can put the words in an array and then use rand() to get a random array position. You need to limit the values generated by rand() using the % operator to guarantee that the generated value is always a valid array position.

And don't forget to update the seed of the rand() function.