简体   繁体   English

如何用字母创建猜谜游戏。 例如,我需要从辐射结果中猜出一个字母。 哪些功能有用

[英]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. 我正在使用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. 如果他们做对了,他们可以继续进行下一个完整的5个问题。 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. 我尝试将rand()&& psw.length一起应用,以便至少尝试使字母选择随机化,以便用户可能有机会从单词“ fallout”中猜测随机字母,但无济于事。

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.... 我正在尝试使第二个answer2 = rand()%word2.length函数起作用,任何人都可以在这里帮助我,它会自动运行为用户提供正分数的代码。


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. 我如何使此代码运行良好,有人可以帮我吗,我只需要这里的功能建议即可...它会自动为用户提供一个score ++。 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. 首先,在C ++中,您可以使用一些不同的方法来随机化一个值。 rand() highly not recommended. 强烈建议不要使用rand()

From cppreference : 来自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()的某些实现在产生的序列的随机性,分布和周期方面存在严重缺陷(在一个众所周知的示例中,低阶位在调用之间仅在1和0之间交替)。 rand() is not recommended for serious random-number generation needs, like cryptography. 不建议将rand()用于严重的随机数生成需求,例如密码学。

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 进一步了解随机: https//en.cppreference.com/w/cpp/numeric/random

For loop - Condition problem: for (int i = 0; i < guess; i++) - The condition here seems wrong. For循环-条件问题: for (int i = 0; i < guess; i++) -这里的条件似乎是错误的。 Why does this loop runs until i is bigger then the user guess? 为什么这个循环一直运行到i更大然后用户猜到为止? I think a better way for your target is to use while loop, until the user have no lives: 我认为对您的目标更好的方法是使用while循环,直到用户没有生命为止:

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 ). 停止循环:每当用户成功猜出字母(或字母在单词中的位置)时,您可能会考虑随机选择一个新字母,一个新单词,或者只是停止游戏并退出循环(带有break )。

The word FALLOUT: Currently, in your code, the word fallout ia a variable name, and not a variable content. FALLOUT一词:目前,在您的代码中, fallout是一个变量名称,而不是变量内容。 start with replacing this name to something like word_to_guess , and put the value fallout into it. 与更换这个名字有点像开始word_to_guess ,并把价值fallout进去。

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() : 现在,您已经完成了操作,可以通过选择介于1word_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...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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