C++:刽子手游戏
我在写程序的过程中偶然发现了一些问题。 代码读取一个文本文件,其中包含20个字符串单词的列表。playHangman函数应该读取文件并随机选择一个单词,该单词在控制台中显示为星号。调用函数时,代码可以很好地处理本地字。例如,PlayhangmanstackOverflow将显示字符的确切数量,并将循环遍历这些字符,直到猜对单词为止。如果你看一下代码,我在函数中调用随机词。该单词存储为数组。我知道这不是进行随机化的正确方法,但现在,即使它一遍又一遍地选择同一个单词,也没关系,我只需要确保它确实读取数组。另一件事是,当显示所有字符时,文本文件上的所有单词都会显示出来,看起来我调用的是数组的全部内容,而不仅仅是应该生成的随机单词 任何帮助都将不胜感激,谢谢C++:刽子手游戏,c++,C++,我在写程序的过程中偶然发现了一些问题。 代码读取一个文本文件,其中包含20个字符串单词的列表。playHangman函数应该读取文件并随机选择一个单词,该单词在控制台中显示为星号。调用函数时,代码可以很好地处理本地字。例如,PlayhangmanstackOverflow将显示字符的确切数量,并将循环遍历这些字符,直到猜对单词为止。如果你看一下代码,我在函数中调用随机词。该单词存储为数组。我知道这不是进行随机化的正确方法,但现在,即使它一遍又一遍地选择同一个单词,也没关系,我只需要确保它确实读取
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
string word[20];
string randomWord = word[rand() % 20];
int playHangman(string randomWord) {
int misses = 0;
int revealedletters = 0;
string display = randomWord;
ifstream textFile;
// Open file
textFile.open("hangman.txt");
// Check if file exists
if (!textFile) {
cerr << "Unable to open text file\n";
exit(1); // Call system to stop
}
else {
cout << "File opened successfully. Program will continue...\n\n";
// Loop through the content of the file
while (textFile >> randomWord) {
for (int i = 0; i < display.length(); i++)
display[i] = '*';
while(revealedletters < randomWord.length()) {
cout << "Misses: " << misses << endl;
cout << "Enter a letter in word ";
cout << display << " : ";
char response;
cin >> response;
bool goodGuess = false;
bool duplicate = false;
for (int i = 0; i < randomWord.length(); i++)
if (response == randomWord[i]) {
if (display[i] == randomWord[i]) {
cout << response << " is already in the word." << endl;
duplicate = true;
break;
}
else {
display[i] = randomWord[i];
revealedletters++;
goodGuess = true;
}
}
if (duplicate)
continue;
if (!goodGuess) {
misses++;
cout << response << " is not in word\n";
}
}
cout << "You guessed right! The word is " << randomWord << ".\n";
}
return misses;
}
}
// TODO: Do you want to guess another word, Y/N?
int main () {
playHangman(randomWord);
// TODO: number of misses to guess the word.\n";
}
在声明和初始化全局变量时:
string word[20];
string randomWord = word[rand() % 20];
word是由20个空字符串组成的数组,因此randomWord也将始终为空
在playHangman功能中,您有:
while (textFile >> randomWord) {
for (int i = 0; i < display.length(); i++)
display[i] = '*';
while(revealedletters < randomWord.length()) {
......
如果确实必须使用阵列:
const size_t maxWords = 20;
std::string words[maxWords];
size_t wordCount = 0;
while (textFile >> word && wordCount < maxWords) {
words[wordCount++] = word;
}
randomWord = words[rand() % wordCount];
std::string display = std::string(randomWord.size(), '*');
while(revealedletters < randomWord.length()) {
......
为什么会有这些全局变量?你可能想让读取包含20个单词的文件成为自己的功能。我的建议是将word和randomWord移到main中。首先给你的随机数发生器设定种子。然后调用读取文件的函数。然后随机挑选一个单词。并在删除所有文件读取后调用play函数。play函数只需从列表中随机传递一个单词即可。@Rochnour一般经验法则:函数应尽可能少做一些事情,最好只做一件事。将文件读入列表函数应该将文件读入列表并返回该列表。没别的了。然后,您可能需要一个函数,从该列表中选择一个随机单词并返回该单词。一个函数做的东西越少,如果你做得不好,就越容易得到正确的结果并进行调试。我仍然不知道为什么它会显示该文本文件中的所有单词,而不仅仅是随机选择的单词。我建议你在带有IDE和Visual Studio等调试器的pc上尝试这段代码。这样,您就可以一步一步地浏览代码,在每一步中查看变量,以了解您的程序在做什么。如果您没有调试器,在代码中放置cout语句可以帮助理解发生了什么。@rochnour是,如果你对向量有些过敏,你可以使用数组,你只需要确保你的数组足够大,可以容纳文件中的所有单词。我会看看你建议的代码,谢谢!!我得到一个错误:分解声明“[maxWords]”需要一个初始值设定项,不能使用类型“std::string”aka“basic_string”来声明分解声明;声明类型必须是“自动”或引用“自动”分解声明是C++ 17扩展。数组大小在错误的地方,你能告诉我在真实的C++代码中我经常使用原始数组吗?@伯特勒我想了很多,但是你打败了我。你是最棒的,非常感谢!!!
const size_t maxWords = 20;
std::string words[maxWords];
size_t wordCount = 0;
while (textFile >> word && wordCount < maxWords) {
words[wordCount++] = word;
}
randomWord = words[rand() % wordCount];
std::string display = std::string(randomWord.size(), '*');
while(revealedletters < randomWord.length()) {
......