gpt4 book ai didi

C++ Jeopardy 游戏算法设计

转载 作者:太空狗 更新时间:2023-10-29 23:16:57 25 4
gpt4 key购买 nike

全部。

我想编写一个 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],下一个问题将占据单元格 [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++;
}
}

}

最佳答案

从我从您的问题中解释的内容来看,您似乎使事情过于复杂了。我的建议是创建一个数组,如下所示:

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);

等等

关于C++ Jeopardy 游戏算法设计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21294838/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com