gpt4 book ai didi

c++ - 如何用字母创建猜谜游戏。例如,我需要从 fallout 这个词中猜出一个字母。哪些功能有用

转载 作者:行者123 更新时间:2023-11-28 04:11:21 25 4
gpt4 key购买 nike

我正在制作一个猜谜游戏。我需要让用户输入一个来自像 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");

我怎样才能使这段代码运行良好,有人可以帮助我吗?我在这里只需要有关此功能的建议...它会自动为用户提供++ 分数。他们对此有一个简单的解决办法吗?我是菜鸟,所以如果这里有一个基本技巧,那将会有所帮助!

    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");



}







最佳答案

首先,在 C++ 中,您有一些不同的方法来随机化一个值。 rand()强烈不推荐。

来自 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.

相反,您可以使用:

#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;
}

阅读更多关于随机的信息:https://en.cppreference.com/w/cpp/numeric/random

For 循环 - 条件问题: for (int i = 0; i < guess; i++) - 这里的条件似乎不对。为什么这个循环运行到i比用户猜的大吗?我认为您的目标更好的方法是使用 while循环,直到用户没有生命:

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

停止循环:每当用户成功猜出字母(或字母在单词中的位置)时,您可能会考虑随机生成一个新字母、一个新单词,或者干脆停止游戏并退出循环(使用 break )。

单词 FALLOUT: 目前,在您的代码中,单词 fallout ia 是一个变量名,而不是变量内容。首先将此名称替换为类似 word_to_guess 的名称, 并将值 fallout进入其中。

string fallout;

到:

string word_to_guess = "fallout";

现在您已经完成了,您可以通过在 1 之间选择一个随机数,使您的代码对另一个词更通用。至 word_to_guess.size() :

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...
}

关于c++ - 如何用字母创建猜谜游戏。例如,我需要从 fallout 这个词中猜出一个字母。哪些功能有用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57719857/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com