I've been working on a little dungeon crawling adventure just for fun. It only consists of 25 rooms at the moment, so it's very small. My little game works perfectly, but I'm currently trying to make a little improvement to the code.
Here is what the game looks like to give you a better idea of what I'm doing:
Code:
You are standing in front of an archway that leads to the north. The
inscription above the archway reads, "Your adventure begins."
Exits: N
> n
You are standing at an intersection.
Exits: N, E, S
> e
You are standing in a small cavern. You notice a brass key lying on
the floor.
Exits: W
> get
You pick up the brass key and put it in your satchel.
You are standing in a small cavern. There is nothing in here.
Exits: W
> w
You are standing at an intersection.
Exits: N, E, S
> n
You are going around a bend.
Exits: E, S
> e
You are standing in an east-west hallway.
Exits: E, W
> e
You are standing in front of a closed door.
Exits: W
> open
You are able to unlock the door with your brass key.
You are standing in front of an open door.
Exits: E, W
>
Right now I have all the room descriptions stored as char * variables in the main source code.
Example:
Code:
char *ROOM14 =
"You enter into a mist filled room. A layer of water vapor covers the\n\
walls and ceiling. You feel heat radiating near the west wall.";
char *ROOM15 =
"The air in this room is dry and warm. The breeze you feel near the north\n\
exit is cooler. The west wall is warm to the touch.";
char *ROOM16 =
"You are in a north-south cooridor. There is writing on the walls, but\n\
it has become too faded to make out what it says.";
char *ROOM17 =
"This passageway seems familiar. Or does it? You are starting to\n\
question your sense of direction.";
I want to move these room descriptions into a plain text file. I know how to work with files, but I'm not sure how to read multiple lines of text from a file and store it as a single variable.
My room data file looks like this at the moment:
Code:
room#: 0
nExit: 1
eExit: -1
sExit: -1
wExit: -1
uExit: -1
dExit: -1
room#: 1
nExit: 3
eExit: 2
sExit: 0
wExit: -1
uExit: -1
dExit: -1
room#: 2
nExit: -1
eExit: -1
sExit: -1
wExit: 1
uExit: -1
dExit: -1
room#: 3
nExit: -1
eExit: 4
sExit: 1
wExit: -1
uExit: -1
dExit: -1
This format works perfectly well and I'm able to parse the data with no problem. I want to add another line to this room data file for the room descriptions ... something like this:
Code:
room#: 14
nExit: -1
eExit: 11
sExit: 13
wExit: 22
uExit: -1
dExit: -1
descr: "You enter into a mist filled room. A layer of water vapor covers the\n\ walls and ceiling. You feel heat radiating near the west wall."
room#: 15
nExit: 16
eExit: 9
sExit: -1
wExit: 20
uExit: -1
dExit: -1
descr: "The air in this room is dry and warm. The breeze you feel near the north\n\exit is cooler. The west wall is warm to the touch."
However, I'm not sure how to parse out the "descr" line and store it as a single variable. So my question is, how can I move these room descriptions out of the source code file and into a plain text file while still being able to retain the room descriptions as a single variable?
Thank you.
EDIT:
To add to this, here's the block of code I'm using to parse out the north, east, south, west, up, down directions.
Code:
// opening file for reading
fp = fopen("rooms.txt" , "r");
if (fp == NULL) {
perror("Error opening rooms.txt file");
return(-1);
}
// This loop reads through the rooms.txt file and sets the exit
// points for each room.
while(fgets(str, sizeof(str), fp) != NULL) {
token = strtok(str, ":");
if (strcmp(token, "room#") == 0) {
token = strtok(NULL, "");
roomNumber = atoi(token);
for (directionCount = 0; directionCount < 6; directionCount++) {
fgets(str, sizeof(str), fp);
token = strtok(str, ":");
if (strcmp(token, "nExit") == 0) {
token = strtok(NULL, "");
room[roomNumber].nExit = atoi(token);
}
if (strcmp(token, "eExit") == 0) {
token = strtok(NULL, "");
room[roomNumber].eExit = atoi(token);
}
if (strcmp(token, "sExit") == 0) {
token = strtok(NULL, "");
room[roomNumber].sExit = atoi(token);
}
if (strcmp(token, "wExit") == 0) {
token = strtok(NULL, "");
room[roomNumber].wExit = atoi(token);
}
if (strcmp(token, "uExit") == 0) {
token = strtok(NULL, "");
room[roomNumber].uExit = atoi(token);
}
if (strcmp(token, "dExit") == 0) {
token = strtok(NULL, "");
room[roomNumber].dExit = atoi(token);
}
}
}
}
// close file
fclose(fp);
And here is how I'm currently handling the room descriptions.
Code:
// Reset the roomNumber variable
roomNumber = 0;
// Setup the default room description for each room.
// room 0
strcpy(room[roomNumber].description, ROOM00);
roomNumber++;
// room 1
strcpy(room[roomNumber].description, ROOM01);
roomNumber++;
// room 2
strcpy(room[roomNumber].description, ROOM02);
roomNumber++;
// room 3
strcpy(room[roomNumber].description, ROOM03);
roomNumber++;
// room 4
strcpy(room[roomNumber].description, ROOM04);
roomNumber++;
If I can handle these room descriptions assignments inside of the loop, it will be much more elegant.