简体   繁体   中英

How create a guessing game with letters. For example I need to guess a letter from the word fallout. Which functions are useful

I am creating a guessing game. I need to ask the user to input a letter from a word like fallout. The have that letter they had inputted be correct or incorrect. I am using functions like srand(time(NULL)), rand(), psw.length. once the user inputs a letter and if they are wrong a life is deducted live--. If they get it right they can move on to the next question with a full 5 lives. I don't know what functions I am missing if I need an array etc.

I have tried applying the rand() && psw.length together in order to at least try to randomize the letter choice so that the user might have a chance to guess the random letter from the word "fallout" but to no avail.

I have made some progress I started with the numerical portion of the code instead of focusing on the whole thing at once. Then now I have to start on the alphabetical portion of the code itself I am organizing my thoughts to simpler terms. Now onto the alphabetical functions of the code....I now need to randomize letters for the user to answer with the correct letter of the word using functions. I am trying to make the second answer2 = rand() % word2.length function work could anyone help me here it automatically runs the code giving a positive score to the user....


include <iostream>
#include <string>
#include <time.h>
#include <cstdlib>

using namespace std;
int lives = 3;
int guess;
int guess2;
int answer = 0;
int answer2 = 0;
int i;
int score = 0;
char letter, letter2;
string word = "fallout";
string word2 = "psw";
int main()
{
    srand(time(NULL));

    cout << "Welcome to the guessing game!" << endl;
    cout << "*****************************" << endl;
    system("PAUSE");
    system("cls");

    answer = rand() % 2 + 1;
    lives = 3;
    do {

        cout << "What is a number between 1 and 2? Can you guess it in\n" << endl << lives << endl << "tries?" << endl;
        cin >> guess;

        if (guess == answer)
        {

            cout << "You won!!" << endl;
            score++;


        }
        else if (lives == 0)
        {
            cout << "Your score" << endl << score;

            system("PAUSE");
            return 0;


        }
        else
        {
            cout << "Incorrect try again!" << endl;
            lives--;
            system("PAUSE");
            system("cls");
        }




    } while (guess != answer);

    cout << "You won your score is" << score << endl;
    system("PAUSE");
    system("cls");

    answer = rand() % 3 + 1;
    lives = 3;
    do {

        cout << "What is a number between 1 and 3? Can you guess it in" << endl << lives << "tries?" << endl;
        cin >> guess;


        if (guess == answer)
        {
            cout << "You won!!" << endl;
            score++;


        }
        else if (lives == 0)
        {
            cout << "Your score" << endl << score;

            system("PAUSE");
            return 0;


        }
        else
        {
            cout << "Incorrect try again!" << endl;
            lives--;
            system("Pause");
            system("cls");
        }




    } while (guess != answer);
    cout << "You won your score is" << score << endl;
    system("PAUSE");
    system("cls");


    answer = rand() % 5 + 1;
    lives = 3;
    do {

        cout << "What is a number between 1 and 5? Can you guess it in\n" << endl << lives << "tries?" << endl;
        cin >> guess;


        if (guess == answer)
        {
            cout << "You won!!" << endl;
            score++;


        }
        else if (lives == 0)
        {
            cout << "Your score" << endl << score;

            system("PAUSE");
            return 0;


        }
        else
        {
            cout << "Incorrect try again!" << endl;
            lives--;
            system("cls");
        }




    } while (guess != answer);
    cout << "You won your score is " << score << endl;
    system("PAUSE");
    system("cls");

    answer = rand() % word.length();
    lives = 3;
    do
    {

        cout << "Select the correct letter in the word '" << word << "': ";
        cin >> guess;
        if (guess == letter)
        {


            cout << "You Won!" << endl;
            score++;

        }
        else if (lives == 0)
        {
            cout << "The correct answer is:" << endl;
            cout << word[answer];

        }
        else
        {
            cout << "Incorrect Try Again" <<
                lives--;

        }




    } while (guess != letter);
    cout << "You won your score is " << score << endl;
    system("PAUSE");
    system("cls");

How can I make this code run well can anybody help me I just need advice on this function here... It keep giving the user a score++ automatically. Is their a simple fix for this. I am a rookie so if there is a basic trick here it would help!

    answer2 = rand() % word2.length();
    lives = 3;
    do
    {

        cout << "Select the correct letter in the word '" << word2 << "': ";
        cin >> guess2;
        if (guess2 == letter2)
        {


            cout << "You Won!" << endl;
            score++;

        }
        else if (lives == 0)
        {
            cout << "The correct answer is:" << endl;
            cout << word2[answer2];

        }
        else
        {
            cout << "Incorrect Try Again" <<
                lives--;

        }




    } while (guess2 != letter2);
    cout << "You won your score is " << score << endl;
    system("PAUSE");
    system("CLS");



}







First of all, in C++ you have some different ways to randomize a value. rand() highly not recommended.

From cppreference :

There are no guarantees as to the quality of the random sequence produced. In the past, some implementations of rand() have had serious shortcomings in the randomness, distribution and period of the sequence produced (in one well-known example, the low-order bit simply alternated between 1 and 0 between calls). rand() is not recommended for serious random-number generation needs, like cryptography.

Instead, you can use:

#include <random>

int main() {

    /*...*/

    // Seed with a real random value, if available
    std::random_device r;

    // Choose a random mean between 1 and 6
    std::default_random_engine e1(r());
    std::uniform_int_distribution<int> uniform_dist(1, 7);
    answer = uniform_dist(e1);

    /*...*/

    return 0;
}

Read more about random: https://en.cppreference.com/w/cpp/numeric/random

For loop - Condition problem: for (int i = 0; i < guess; i++) - The condition here seems wrong. Why does this loop runs until i is bigger then the user guess? I think a better way for your target is to use while loop, until the user have no lives:

int lives = 5;
size_t guess_number = 1;
/*...*/
while (lives) {
    cout << "Guess" << guess_number++ << endl;
    /*...*/
}

Stop the loop: Whenever the user successfully guess the letter (or the letter's place in the word), you might considering random a new letter, a new word, or just stop the game and exit the loop (with break ).

The word FALLOUT: Currently, in your code, the word fallout ia a variable name, and not a variable content. start with replacing this name to something like word_to_guess , and put the value fallout into it.

string fallout;

to:

string word_to_guess = "fallout";

Now that you have done it, you can make you code more generic to another words, by choosing a random number between 1 to word_to_guess.size() :

std::uniform_int_distribution<int> uniform_dist(1, word_to_guess.size());

Now you want to convert user's guess and computer's guess to letters:

/**
 * guess >= 1 - The user have to guess a letter from the beginning of the word (and not before it).
 * guess <= word_to_guess.size() - The user can't guess a letter that not exists in the word.
 * word_to_guess[guess - 1] == word_to_guess[answer - 1] - Compare the user's letter to the computer's letter
 * 
 * word_to_guess[answer - 1] - You might consider to replace this with word_to_guess[answer], and just random
 *                             a number from 0 to word_to_guess.size() - 1
 */
if (guess >= 1 && guess <= word_to_guess.size() && word_to_guess[guess - 1] == word_to_guess[answer - 1]) {
    cout << "You Won" << endl;
    break; // Or random new letter/word etc...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM