[英]I need to text a first word with the greatest number of different letters. How can i do it?
[英]How create a guessing game with letters. For example I need to guess a letter from the word fallout. Which functions are useful
我正在创建一个猜谜游戏。 我需要让用户输入类似辐射的单词中的字母。 他们输入的那封信是正确的还是不正确的。 我正在使用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");
我如何使此代码运行良好,有人可以帮我吗,我只需要这里的功能建议即可...它会自动为用户提供一个score ++。 这是他们的简单解决方案。 我是菜鸟,所以如果这里有个基本技巧,将会有所帮助!
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 :
不能保证所产生的随机序列的质量。 过去,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;
}
进一步了解随机: 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
是一个变量名称,而不是变量内容。 与更换这个名字有点像开始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...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.