C++ 如何创建一个字母猜谜游戏。例如,我需要从单词fallout中猜出一个字母。哪些函数有用

C++ 如何创建一个字母猜谜游戏。例如,我需要从单词fallout中猜出一个字母。哪些函数有用,c++,C++,我正在制作一个猜谜游戏。我需要让用户输入一个单词的字母,比如fallout。他们认为他们输入的字母是正确的或不正确的。我使用的函数有srand(time(NULL)),rand(),psw.length。一旦用户输入了一封信,如果他们错了,生命就会被扣除。 如果他们答对了,他们可以用整整5条生命继续下一个问题。如果我需要数组等,我不知道我缺少什么函数 我尝试将rand()&&psw.length应用在一起,以便至少尝试随机选择字母,这样用户可能有机会从单词“fallout”中猜出随机字母,但没有

我正在制作一个猜谜游戏。我需要让用户输入一个单词的字母,比如fallout。他们认为他们输入的字母是正确的或不正确的。我使用的函数有srand(time(NULL)),rand(),psw.length。一旦用户输入了一封信,如果他们错了,生命就会被扣除。 如果他们答对了,他们可以用整整5条生命继续下一个问题。如果我需要数组等,我不知道我缺少什么函数

我尝试将rand()&&psw.length应用在一起,以便至少尝试随机选择字母,这样用户可能有机会从单词“fallout”中猜出随机字母,但没有效果

我已经取得了一些进展,我从代码的数字部分开始,而不是一下子集中在整个事情上。然后现在我必须从代码本身的字母部分开始,我将我的想法组织成更简单的术语。 现在进入代码的字母顺序函数…我现在需要使用函数将字母随机化,以便用户用单词的正确字母回答。 我正在尝试使第二个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");

包括
#包括
#包括
#包括
使用名称空间std;
int=3;
智力猜测;
int猜测2;
int-answer=0;
int answer2=0;
int i;
智力得分=0;
字符字母,字母2;
string word=“fallout”;
字符串word2=“psw”;
int main()
{
srand(时间(空));

CUT< P>首先,在C++中,你有一些不同的方法来随机化一个值。<代码> RAND()>代码>高度不推荐。 发件人:

无法保证生成的随机序列的质量。在过去,rand()的一些实现在生成的序列的随机性、分布和周期方面存在严重缺陷(在一个众所周知的示例中,低阶位在调用之间只是在1和0之间交替)。rand()不建议用于严重的随机数生成需求,如加密

相反,您可以使用:

#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;
}
停止循环:每当用户成功猜到字母(或字母在单词中的位置)时,您可能会考虑随机添加一个新字母、一个新词,或者停止游戏并退出循环(使用
中断

FALLOUT一词:目前,在您的代码中,
FALLOUT一词是一个变量名,而不是一个变量内容。首先将该名称替换为类似于
word\u to\u guess
,然后将值
FALLOUT
放入其中

string fallout;
致:

现在您已经完成了,您可以通过在
1
to
word\u to\u guess.size()
之间选择一个随机数,使您的代码更通用于其他单词:

std::uniform_int_distribute uniform_dist(1,word_to_guess.size());
现在,您要将用户的猜测和计算机的猜测转换为字母:

/**
 * 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...
}
/**
*guess>=1-用户必须从单词的开头(而不是之前)猜字母。

*猜测您的
guess
变量甚至没有初始化,所以第二个循环甚至不会执行。我必须分配guess=rand()%7+1?我脑子里有太多的问题。什么是
字母[]
怎么办?为什么你要为同一个任务编写多个for循环?你能用参数
问题
正确答案
生命
,等等来创建一个函数吗?想想看,我在尝试,我觉得逻辑不容易。我应该把所有语句都嵌套在一个for循环中吗?我真的在尝试。嘿@Peace。你能做到吗ry breifly解释第一个循环在猜字母游戏中做什么?谢谢你的帮助我不确定问题是否解决了,但我仍然需要继续工作。我将使用while循环而不是for循环,谢谢你的提示。谢谢你的帮助我只需要继续工作。这些是我以前从未见过的新功能e谢谢你提供的关于rand()的信息。我不知道它已经过时了。我想我是在学习旧信息。@Peace“向量的语法”是什么意思?@Pease你不是在纠缠我,我是来帮助你的。如果还有一些不清楚的想法,请给我举个例子,我会尽力解释。我相信如果你有不清楚的地方,很多人也会有不清楚的地方。
string fallout;
string word_to_guess = "fallout";
std::uniform_int_distribution<int> uniform_dist(1, word_to_guess.size());
/**
 * 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...
}