【问题标题】:C++ Jeopardy Game Algorithm DesignC++危险游戏算法设计
【发布时间】:2014-01-22 21:56:52
【问题描述】:

全部。

我想编写一个 Jeopardy 游戏,它从文件中读取游戏的问题、答案、类别和问题难度级别,并根据从文件中检索到的数据生成问题板。但是,我坚持如何理解文件中的数据。

这是我目前得到的:

我的输入文件格式如下:

category
difficulty
question
answer
category
difficulty
question
answer

等等等等。可以有任意数量的类别 - 游戏将随机选择其中的 6 个。有 5 个难度级别 (0-4) 来表示原始 Jeopardy 游戏的“美元增量”,将由它们各自的整数 (0-4) 表示。接下来,当然是一个问题和一个答案。示例输入文件可能如下所示:

Star Trek
0
This is the vessel that explores the galaxy.
What is the Enterprise?
Star Wars
2
These guys always miss.
What are the storm troopers?

等等等等。如您所见,类别可以按任何顺序显示,难度级别也可以。我的问题是设计一种处理这些事情的算法。

我开始考虑将我的问题和答案放在二维数组中:第一个问题将占用单元格 [0][0],第一个答案将占用单元格 [0][1],下一个问题将占用单元格 [0][1]问题将占据单元格 [1][0],下一个答案 [1][1] 等。接下来,按难度排序,我想象了一个 3D 数组:由问题和答案组成的多个 2D 数组除以难度级别.最后,我发现通过增加一个维度并制作一个 4D 数组,我可以按类别对我的难度级别进行排序。这样的数组将首先给我所有类别,然后是每个类别的所有难度级别,然后是每个类别中每个难度级别的所有问题和答案。然后将数组传递给实际使用排序数据的不同函数。

听起来它会起作用,对吧?好。我正在努力制作一种将所有内容放在正确位置的算法。我以前从未使用过大于 2D 的数组,因此 4D 数组的概念对我来说相当难以理解。我将在下面粘贴我当前的代码以供所有人阅读和评论,我热切地等待任何和所有建议。如有必要,我可以更改文件的格式,但这种 4 行组织方法是我想出的唯一方法。

    void oldReadFromFile(std::string dataArray[][5][30][4], int length)
{
    /*****************
     The input file must be formatted as follows:
     Category  //capitalization counts
     Dificulty //as an integer 0-4
     Question
     Answer
     Category //capitalization counts
     Dificulty //as an integer 0-4
     Question
     Answer
     etc.
     *****************/
    std::ifstream inFile;
    std::string category;
    std::string dificulty;
    std::string question;
    std::string answer;
    int numOfQuestions[30][5] = {0};
    int numOfCategories = 0;
    bool alreadyHere;
    inFile.open("JeopardyData.txt");
    //Determine question category
    while (!inFile.eof() && numOfCategories < length) {
        std::getline(inFile, category);
        std::getline(inFile, dificulty);
        std::getline(inFile, question);
        std::getline(inFile, answer);
        //inFile.get();
        alreadyHere = false;
        for (int i = 0; i < numOfCategories; i++)
        {
            if (category == dataArray[i][0][0][0])  //If the category already exists...
            {
                alreadyHere = true;
                if (dificulty == "zero" || dificulty == "Zero" || dificulty == "0") //Determine the dificulty
                {
                    dataArray[i][0][numOfQuestions[i][0]][1] = question;
                    dataArray[i][0][numOfQuestions[i][0]][2] = answer;
                    numOfQuestions[i][0]++;
                }
                else if (dificulty == "one" || dificulty == "One" || dificulty == "1")
                {
                    dataArray[i][1][numOfQuestions[i][1]][1] = question;
                    dataArray[i][1][numOfQuestions[i][1]][2] = answer;
                    numOfQuestions[i][1]++;
                }
                else if (dificulty == "two" || dificulty == "Two" || dificulty == "2")
                {
                    dataArray[i][2][numOfQuestions[i][2]][1] = question;
                    dataArray[i][2][numOfQuestions[i][2]][2] = answer;
                    numOfQuestions[i][2]++;
                }
                else if (dificulty == "three" || dificulty == "Three" || dificulty == "3")
                {
                    dataArray[i][3][numOfQuestions[i][3]][1] = question;
                    dataArray[i][3][numOfQuestions[i][3]][2] = answer;
                    numOfQuestions[i][3]++;
                }
                else if (dificulty == "four" || dificulty == "Four" || dificulty == "4")
                {
                    dataArray[i][4][numOfQuestions[i][4]][1] = question;
                    dataArray[i][4][numOfQuestions[i][4]][2] = answer;
                    numOfQuestions[i][4]++;
                }
                else { std::cout << "Bad dificulty detected in input file!\n"; }

            }
        }
        if (!alreadyHere)                       //If the category DOESN'T exist yet, make it.
        {
            for (int i = 0; i < 5; i++)
            {
                dataArray[numOfCategories][i][0][0] = category;
            }
            if (dificulty == "zero" || dificulty == "Zero" || dificulty == "0") //Determine the dificulty
            {

                dataArray[numOfCategories][0][numOfQuestions[numOfCategories][0]][1] = question;
                dataArray[numOfCategories][0][numOfQuestions[numOfCategories][0]][2] = answer;
                numOfQuestions[numOfCategories][0]++;
            }
            else if (dificulty == "one" || dificulty == "One" || dificulty == "1")
            {
                dataArray[numOfCategories][1][numOfQuestions[numOfCategories][1]][1] = question;
                dataArray[numOfCategories][1][numOfQuestions[numOfCategories][1]][2] = answer;
                numOfQuestions[numOfCategories][1]++;
            }
            else if (dificulty == "two" || dificulty == "Two" || dificulty == "2")
            {
                dataArray[numOfCategories][2][numOfQuestions[numOfCategories][2]][1] = question;
                dataArray[numOfCategories][2][numOfQuestions[numOfCategories][2]][2] = answer;
                numOfQuestions[numOfCategories][2]++;
            }
            else if (dificulty == "three" || dificulty == "Three" || dificulty == "3")
            {
                dataArray[numOfCategories][3][numOfQuestions[numOfCategories][3]][1] = question;
                dataArray[numOfCategories][3][numOfQuestions[numOfCategories][3]][2] = answer;
                numOfQuestions[numOfCategories][3]++;
            }
            else if (dificulty == "four" || dificulty == "Four" || dificulty == "4")
            {
                dataArray[numOfCategories][4][numOfQuestions[numOfCategories][4]][1] = question;
                dataArray[numOfCategories][4][numOfQuestions[numOfCategories][4]][2] = answer;
                numOfQuestions[numOfCategories][4]++;
            }
            else { std::cout << "Bad dificulty detected in input file!\n"; }
            numOfCategories++;
        }
    }

} 

【问题讨论】:

  • 不要使用大维数组,因为它很难读取意图并且容易出错。将 Question/Answer 封装到一个类 (Question) 中,该类同时存储 vector(或数组,因为一个类别中只有 5 个问题)Questions
  • TL;DR;简短的总结: 使用std::vector 或std::array 而不是c 样式的数组,这将使生活更轻松。 注意二维float数组的等价物是std::vector&lt;std::vector&lt;float&gt; &gt;!可能出于您的需要,使用一些 std::map&lt;&gt; 容器更适合让您的 4 维问题域正确。
  • 你总是可以将难度转换为小写,这样你就不需要同时检查大小写,如果它是一个数字值也不会有害,它也会像ZeRo zErO一样支持等等。
  • 遗憾的是,我从未听说过类或向量。我会用谷歌搜索他们并回复你们。

标签: c++ arrays algorithm file sorting


【解决方案1】:

从我从您的问题中解释的内容看来,您的事情可能过于复杂。我建议按如下方式创建一个数组:

struct Game_Node
{
   string category;
   int difficulty;
   string question;
   string answer;
};

Game_Node jeopardy_board[];

或

Game_Node jeopardy_board[][];

为了更好地模拟危险板。然后,您所要做的就是遍历文件并填充每个元素的变量。简单例子:

//assumes variables have values and is just for the first node
jeopardy_board[0][0].category = (value goes here);
jeopardy_board[0][0].difficulty = (value goes here);

等等

【讨论】:

    猜你喜欢
    • 2011-08-26
    • 2012-11-20
    • 1970-01-01
    • 1970-01-01
    • 2012-09-01
    • 1970-01-01
    • 2011-03-17
    • 2016-03-25
    • 2021-12-25
    相关资源
    最近更新 更多