I'm creating a text adventure with room names, descriptions and spaces for hints and items.
This is part of the solution so far, a simple struct with four string entries. A requirement is to keep the rooms initialisation data readable and editable as the 4x4 array works as a grid for my game world.
using namespace std;
#include <string>
struct Room
{
string name;
string description;
string itemHint;
string item;
};
Room rooms[4][4]= {
{{"...", "...", "...", "..."},
{"...", "...", "...", "..."},
{"...", "...", "...", "..."},
{"...", "...", "...", "..."}}, // rooms[0..3][0] data
{{"...", "...", "...", "..."}, // and so on
Now this works to some extent. But, when printing the rooms map of the game world I'm getting duplicate entries. The code is duplicating the room data four times per row [0..0][0..3].
void PrintMap()
{
cout << endl;
for (int v = 0; v < 4; v += 1)
{
for (int h = 0; h < 4; h += 1)
{
if (currentRoom.h == h && currentRoom.v == v)
cout << "@";
else
cout << " ";
cout << rooms[h, v]->name;
string charstring = rooms[h, v]->name;
int chars = charstring.length();
int endSpaces = 24 - 1 - chars;
for (int charSpaces = 1; charSpaces < endSpaces + 1; charSpaces += 1)
cout << " ";
}
cout << endl << endl << endl;
}
}
Here is a screenshot of the console application printed room map to help better explain this:
I'm almost understanding why this occurs, but perhaps someone could explain it and has a valid solution. Of course as I've said I'd like to keep the code format for the rooms data readable, I gladly accept any code design additions.