自定义Word混杂游戏

时间:2017-12-19 21:23:20

标签: c++

我正在为一个项目制作另一个版本的单词混乱游戏。我从原始代码中使用了与游戏本身相同的设置,但是这个设置与文件有所不同。像以前一样,它将文字放入文件中的string array,然后使用数组中的单词进行随机播放并打印出混乱的单词,供用户解读。

与此程序不同的是,它允许用户选择他们想要用于争夺的单词组。程序编译得很好,但在他们选择了要使用的文件类型(并在需要时创建文件)之后,程序就会停止运行。我已经发布了下面的代码,代码中是否有某些内容导致它停止运行?如果是这样,我会喜欢修复它的一些技巧以及解释它导致问题的原因。

谢谢!

#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <ctime>
#include <array>
#include <algorithm>
#include <cstring>
#include <cstdlib>
using namespace std;

void createFile(ofstream& file, string fileName, int wordCount) {
    string word;
    file.open(fileName + ".txt");
    for (int i = 0; i < wordCount; i++) {
        cout << "Enter a word: \n";
        cin >> word;
        file << word;
        file << endl;
    }
    file.close();
} 

void useFile(ifstream& file, string fileName) {
    file.open(fileName + ".txt");
}

void main() {
    srand(time(0));
    int levelChoice, wordCount, i = 0;
    string fileName, word, jumble, answer;
    string words[10000];
    ifstream infile;
    ofstream outfile;
    bool repeat = true, result, allowed = false;
    char choice;

    cout << "Let's play a word jumble game!\n";
    cin.get();
    do {
        cout << "First, choose what you want to use to play.\n";
        cout << "(1) Pre-made word list file\n(2) Custom file\n";
        cin >> levelChoice;
        do {
            if (levelChoice == 1) {
                cout << "Okay, please choose what you'd like to play!\n";
                cout << "(1) Easy\n(2) Medium\n(3) Hard\n(4) Theme\n";
                cin >> levelChoice;
                if (levelChoice == 4) {
                    cout << "Please select a theme: \n";
                    cout << "(1) Colors\n(2) Emotions\n(3) Flowers\n(4) Nature\n(5) Seasons\n(6) Animals\n";
                    cin >> levelChoice;
                    switch (levelChoice) {
                    case 1:
                        infile.open("Theme (Colors).txt");
                        while (infile.peek() != EOF) {
                            infile >> words[i];
                            i++;
                        }
                        infile.close();
                        allowed = true;
                        break;

                    case 2:
                        infile.open("Theme (Emotions).txt");
                        while (infile.peek() != EOF) {
                            infile >> words[i];
                            i++;
                        }
                        infile.close();
                        allowed = true;
                        break;

                    case 3:
                        infile.open("Theme (Flowers).txt");
                        while (infile.peek() != EOF) {
                            infile >> words[i];
                            i++;
                        }
                        infile.close();
                        allowed = true;
                        break;

                    case 4:
                        infile.open("Theme (Nature).txt");
                        while (infile.peek() != EOF) {
                            infile >> words[i];
                            i++;
                        }
                        infile.close();
                        allowed = true;
                        break;

                    case 5:
                        infile.open("Theme (Seasons).txt");
                        while (infile.peek() != EOF) {
                            infile >> words[i];
                            i++;
                        }
                        infile.close();
                        allowed = true;
                        break;

                    case 6:
                        infile.open("Theme (Animals).txt");
                        while (infile.peek() != EOF) {
                            infile >> words[i];
                            i++;
                        }
                        infile.close();
                        allowed = true;
                        break;

                    default:
                        cout << "That was not an option, please try again.\n";
                        break;
                    }
                } 
                else if (levelChoice < 1 || levelChoice > 4) {
                    cout << "Sorry, that was not a valid option, please try again.\n";
                } else {
                    switch (levelChoice) {
                    case 1:
                        infile.open("Easy.txt");
                        while (infile.peek() != EOF) {
                            infile >> words[i];
                            i++;
                        }
                        infile.close();
                        allowed = true;
                        break;

                    case 2:
                        infile.open("Medium.txt");
                        while (infile.peek() != EOF) {
                            infile >> words[i];
                            i++;
                        }
                        infile.close();
                        allowed = true;
                        break;

                    case 3:
                        infile.open("Hard.txt");
                        while (infile.peek() != EOF) {
                            infile >> words[i];
                            i++;
                        }
                        infile.close();
                        allowed = true;
                        break;
                    }
                }
            } else if (levelChoice == 2) {
                cout << "Okay, please enter the file name:\n";
                cin >> fileName;
                cout << "Now tell me how many words you'd like to enter into the file so I can create it: ";
                cin >> wordCount;
                cout << "Time to create your file!\n";
                createFile(outfile, fileName, wordCount);
                useFile(infile, fileName);
                while (infile.peek() != EOF) {
                    infile >> words[i];
                    i++;
                }
                infile.close();
                cout << "File created, you're ready to play!";
                cin.get();
                cin.get();
                allowed = true;
            } else {
                cout << "That was not a valid option, please try again!\n";
            }
        } while (allowed == false);

        int guess = 0;
        int size = sizeof(words);
        int random = rand() % size;
        jumble = words[random];
        random_shuffle(jumble.begin(), jumble.end());
        cout << "Time to start guessing!\nType \"quit\" if you want to give up and find out the word!\nCareful though, you only get 10 guesses before you lose!\n";
        cin.get();
        cin.get();
        cout << jumble << endl;
        do {
            cin >> answer;
            if (answer == "quit" || answer == "Quit") {
                cout << "Giving up? Okay! Ready to find out what the word was?";
                cin.get();
                cin.get();
                cout << "The word was " << words[random] << endl;
                cout << "Better luck next time!\n";
                break;
            } else {
                result = strcmp(answer.c_str(), words[random].c_str());
                if (result == true) {
                    cout << "Nope! Try again!" << endl;
                    guess++;
                } else {
                    cout << "You got it!" << endl;
                    break;
                }
            }
        } while (guess < 10);

        if (guess == 10) {
            cout << "Game over! You lose! Ready to find out what the jumbled word was?";
            cin.get();
            cin.get();
            cout << "The word was " << words[random] << endl << "Better luck next time!";
        }

        cout << "Wanna play again? (Y or N)\n";
        cin >> choice;
        do {
            switch (choice) {
            case 'y':
            case 'Y':
                cout << "Okay, restarting!\n";
                cin.get();
                cin.get();
                allowed = false;
                break;

            case 'n':
            case 'N':
                cout << "Okay, have a great day!\n";
                repeat = false;
                allowed = false;
                break;

            default:
                cout << "Sorry, that was not a valid option, please try again!\n";
                break;
            }
        } while (allowed == true);
    } while (repeat == true);
}

1 个答案:

答案 0 :(得分:0)

主要问题是你正在调用sizeof(words),它返回words数组包含的字节数,而不是数组中的条目数。结果是size最终变得非常大(如240,000),因此对rand % size的调用可能会生成一个大于数组中元素数量的值,并且您将索引经过数组的结尾。

通过使用调试器运行应用程序,您可以找到这些类型的问题。通常,调试器会在导致问题的行上停止您的程序,并向您显示程序变量的状态,以便您可以找出问题所在。