C ++:Hangman游戏

时间:2018-10-01 03:16:28

标签: c++

我正在编写程序,偶然发现了一些问题。 该代码读取一个文本文件,其中包含20个字符串单词的列表。函数playHangman()应该读取文件并随机选择一个单词,该单词在控制台中显示为星号。调用该函数时,该代码可以很好地与本地单词配合使用。例如Playhangman(“ stackOverflow”),将显示确切的字符数并循环遍历,直到猜出正确的单词为止。如果您看一下代码,我会在函数中调用随机字。该词存储为数组。我知道这不是进行随机化的正确方法,但是就目前而言,即使它一遍又一遍地选择相同的单词,也可以,我只需要确保它确实读取了数组即可。另一件事是,当显示所有字符时,将显示该文本文件上的所有单词,看起来我正在调用数组的全部内容,而不仅仅是调用应该生成的随机单词。

任何帮助将不胜感激,谢谢!

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

1 个答案:

答案 0 :(得分:0)

在声明和初始化全局变量时:

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()) {
       ......

这将从文件中读取一个单词到randomWord中,使用该单词进行游戏,然后循环读取下一个单词,然后再次使用该单词进行游戏。由于revealedletters不会被重置,因此如果第一个单词比下一个单词长,游戏将立即结束。

我认为您真正想要的代码看起来像这样(也删除全局变量):

std::string word;
std::vector<std::string> words;
while (textFile >> word) {
  words.push_back(word);
}
randomWord = words[rand() % words.size()];

std::string display = std::string(randomWord.size(), '*');

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()) {
    ......