C++ C++;危险博弈算法设计
全部 我想写一个Jeopardy游戏,从文件中读取游戏的问题、答案、类别和问题难度级别,并根据从文件中检索到的数据生成问题板。然而,我被困在如何理解文件中的数据上 以下是我目前掌握的信息: 我的输入文件格式如下:C++ C++;危险博弈算法设计,c++,arrays,algorithm,file,sorting,C++,Arrays,Algorithm,File,Sorting,全部 我想写一个Jeopardy游戏,从文件中读取游戏的问题、答案、类别和问题难度级别,并根据从文件中检索到的数据生成问题板。然而,我被困在如何理解文件中的数据上 以下是我目前掌握的信息: 我的输入文件格式如下: category difficulty question answer category difficulty question answer struct Game_Node { string category; int difficulty; string qu
category
difficulty
question
answer
category
difficulty
question
answer
struct Game_Node
{
string category;
int difficulty;
string question;
string answer;
};
Game_Node jeopardy_board[];
等等等等。可以有任意数量的类别-游戏将随机选择其中6个。有5个难度等级(0-4)代表原始危险游戏的“美元增量”,这将由它们各自的整数(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++;
}
}
}
void oldReadFromFile(std::string dataArray[][5][30][4],int-length)
{
/*****************
输入文件的格式必须如下所示:
类别//资本化计数
困难//作为整数0-4
问题:
答复
类别//资本化计数
困难//作为整数0-4
问题:
答复
等
*****************/
std::ifstream-infle;
std::字符串类别;
std::字符串困难;
字符串问题;
字符串应答;
国际货币基金组织问题[30][5]={0};
int numOfCategories=0;
布尔已经在这里了;
infle.open(“JeopardyData.txt”);
//确定问题类别
而(!infle.eof()&&numOfCategories 否则{std::cout从我对您的问题的解释来看,您可能把事情复杂化了。我建议您创建一个数组,如下所示:
category
difficulty
question
answer
category
difficulty
question
answer
struct Game_Node
{
string category;
int difficulty;
string question;
string answer;
};
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);
等等。不要使用大维度数组,因为它会使阅读意图变得困难,并且容易出错。将问题/答案封装到一个类(问题
)中,该类同时存储向量和类别类(或者数组,因为一个类别中只有5个问题)关于Question
sTL;DR;Short boildown:使用std::vector
或std::array
而不是c型数组,这将使生活更加轻松。注意,2Dfloat
数组的等价物是std::vector
!可能是为了满足您的需要而使用一些std::map
容器是e