This is incredibly messy code. It works, I can tell ya'll that, but it's messy and I believe that there as to be a way to optimize this in some sort of loop.
I had the idea of throwing all of the strcmp()'s into a for loop, but wouldn't it still have to do all of the comparisons if it did? And I wasn't sure how to work a dynamic strcmp in a while loop, though my one attempt (while(strcmp(exits, DYN_WORD))) failed miserably.
Anyone here who can help me figure out how to even start optimizing this mess?
Thank you in advance.
Edit: The reason there is a space between the letter(s) and the end quote in the strcmp is because my program grabs that space and appends it to the 'exits' string. I can't figure out how to fix that problem, so I just added the space to the comparison.
Code:
void LoadRoom(char* filename)
{
char buffer, exits[8], eflag[8], enemy[64], name[64];
char text[64] = "text/";
strcat(text, filename); // Prepends "text/" to the filename so it searches the text directory.
std::fstream file(text);
if (!file)
{
std::cout << "Error! The game has encountered an unrecoverable error.\n";
std::cout << "We apoligize for the inconvenience.\n";
std::cout << "The game shall now exit.\n\n";
errflag = 1;
quitflag = 1;
}
// The getlines will get info, get the garbage, then get more info until all the information has been gathered.
file.getline(name, 64, '|');
file.get(buffer);
file.getline(exits, 8, '|');
file.get(buffer);
file.getline(north, 16, '|');
file.get(buffer);
file.getline(south, 16, '|');
file.get(buffer);
file.getline(east, 16, '|');
file.get(buffer);
file.getline(west, 16, '|');
file.getline(eflag, 8, '|');
file.get(buffer);
file.getline(eflag, 8, '|');
file.get(buffer);
file.getline(enemy, 64);
if (strcmp(exits, "NSEW ") == 0)
{
n = 1;
s = 1;
e = 1;
w = 1;
}
if (strcmp(exits, "NSE ") == 0)
{
n = 1;
s = 1;
e = 1;
w = 0;
}
if (strcmp(exits, "NSW ") == 0)
{
n = 1;
s = 1;
e = 0;
w = 1;
}
if (strcmp(exits, "NEW ") == 0)
{
n = 1;
s = 0;
e = 1;
w = 1;
}
if (strcmp(exits, "SEW ") == 0)
{
n = 0;
s = 1;
e = 1;
w = 1;
}
if (strcmp(exits, "NS ") == 0)
{
n = 1;
s = 1;
e = 0;
w = 0;
}
if (strcmp(exits, "NE ") == 0)
{
n = 1;
s = 0;
e = 1;
w = 0;
}
if (strcmp(exits, "NW ") == 0)
{
n = 1;
s = 0;
e = 0;
w = 1;
}
if (strcmp(exits, "SE ") == 0)
{
n = 0;
s = 1;
e = 1;
w = 0;
}
if (strcmp(exits, "SW ") == 0)
{
n = 0;
s = 1;
e = 0;
w = 1;
}
if (strcmp(exits, "EW ") == 0)
{
n = 0;
s = 0;
e = 1;
w = 1;
}
if (strcmp(exits, "N ") == 0)
{
n = 1;
s = 0;
e = 0;
w = 0;
}
if (strcmp(exits, "S ") == 0)
{
n = 0;
s = 1;
e = 0;
w = 0;
}
if (strcmp(exits, "E ") == 0)
{
n = 0;
s = 0;
e = 1;
w = 0;
}
if (strcmp(exits, "W ") == 0)
{
n = 0;
s = 0;
e = 0;
w = 1;
}
if (strcmp(eflag, "T ") == 0) // Annoyingly broken.
{
std::fstream file("temp/erlist.txt");
file.seekg(std::ios::end);
file << filename << " | true";
file.close();
} // Figure out how to fix this loop someday.
while (file.get(buffer) != NULL) // While not at the end of the file.
{
std::cout << buffer;
}
file.close();
}