You're still writing game data into the code.
Code:
#include <stdio.h>
#include <string.h>
enum { NONE, KITCHEN, HALL, STAIRS };
enum { NORTH, SOUTH, WEST, EAST };
struct room {
char *name;
int next[4]; // N,S,W,E from this location
} rooms[] = {
{ "None" },
{ "Kitchen", { NONE, HALL, NONE, NONE } },
{ "Hall", { KITCHEN, STAIRS, NONE, NONE } },
{ "Stairs", { HALL, NONE, NONE, NONE } },
};
int move ( int thisRoom, char *dir ) {
int direction;
switch ( *dir ) {
case 'n': direction = NORTH; break;
case 's': direction = SOUTH; break;
case 'w': direction = WEST; break;
case 'e': direction = EAST; break;
}
if ( rooms[thisRoom].next[direction] == NONE ) {
printf( "The way is blocked!\n" );
} else {
thisRoom = rooms[thisRoom].next[direction];
printf( "You are now in the %s\n", rooms[thisRoom].name );
}
return thisRoom;
}
int main ( ) {
char buff[BUFSIZ];
int room = KITCHEN;
while ( fgets( buff, sizeof buff, stdin ) != NULL ) {
char cmd[BUFSIZ];
int pos;
char *params;
sscanf( buff, "%s %n", cmd, &pos );
params = &buff[pos];
if ( strcmp( cmd, "move" ) == 0 ) {
room = move( room, params );
} else
if ( strcmp( cmd, "describe" ) == 0 ) {
printf( "You are in the %s\n", rooms[room].name );
}
if ( strcmp( cmd, "quit" ) == 0 ) {
break;
}
}
return 0;
}
$ ./a.exe
describe
You are in the Kitchen
move n
The way is blocked!
move s
You are now in the Hall
quit
$
Everything about the room,
- the description
- the ways out
- any secrets
- any objects
- any creatures
need to be encoded into the data in some way, and then acted upon by the code.
For my code, adding another room is dead easy, but if all the directions are hard-wired into the code (as per your example), then you're looking at rewriting the code every time you make a small change to the map.