所有。
我想编写一个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?
等等,等等。如您所见,类别可以按任何顺序显示,难度级别也可以。我的问题是设计一个处理这些事情的算法。
我开始考虑在2D数组中提出问题和答案:第一个问题将占用单元格[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++;
}
}
}
答案 0 :(得分:0)
从我的问题解释来看,你似乎可能过于复杂化了。我建议的是按如下方式创建一个数组:
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);
等