Assignment #1 Answers

Intermediate C Programming

UW Experimental College


Assignment #1 ANSWERS

Question 1. What are the four parts of a structure definition?

The keyword struct, the structure tag (optional), the brace-enclosed list of member declarations (optional), and a list of variables of this structure type to be declared (optional).

Exercise 2. Add a ``help'' command.

Here is the code I added to the big if/else chain in commands.c:

else if(strcmp(verb, "help") == 0)
	{
	printf("Here are some useful commands:\n");
	printf("n, north\tgo north\n");
	printf("s, south\tgo south\n");
	printf("e, east\t\tgo east\n");
	printf("w, west\t\tgo west\n");
	printf("look\t\tdescribe current room\n");
	printf("examine obj\tdescribe an object\n");
	printf("take obj\tpick up an object\n");
	printf("drop obj\tdrop an object\n");
	printf("i, inventory\tlist your current possessions\n");
	printf("There may be other commands, too!\n");
	}
(This help text also contains a description of the new ``examine'' command, below.)

Exercise 3. Add a ``long description'' field to the object and room structures.

I added the line

	char *desc;
at the end of the definition of struct object in game.h, and also at the end of struct room. I modified the initializations of the objects and rooms arrays in main.c to give most of the objects and rooms descriptions:
static struct object objects[] =
	  {
/* [0] */ {"bed", NULL, "It is an old, four-poster bed with a lace coverlet."},
/* [1] */ {"diamond", NULL,
		"The diamond twinkles majestically as you hold it in the light."},
/* [2] */ {"kettle", NULL,
		"It is a dented old kettle.  You're not sure it would even hold water."},
/* [3] */ {"hammer", NULL},
/* [4] */ {"pliers", &objects[3]},
/* [5] */ {"doormat", NULL, "It says, \"Welcome\", in big, friendly letters."},
/* [6] */ {"pick", NULL, "It is an old miner's pick."},
/* [7] */ {"shovel", &objects[6]},
	  };
static struct room rooms[] =
	   {
/* [0]  */ {"field", NULL, {&rooms[1], NULL, NULL, NULL},
		"You are in an open field, with a house to the north."},
/* [1]  */ {"house", NULL, {&rooms[2], &rooms[0], NULL, NULL},
		"You are standing just to the south of a plain, white house."},
/* [2]  */ {"entry", NULL, {&rooms[3], &rooms[2], &rooms[5], NULL},
		"You are in the entryway of the house.\n\
A hall leads north, and there are doors to the south and east."},
/* [3]  */ {"hall", NULL, {&rooms[6], &rooms[2], NULL, &rooms[4]},
		"You are in a north-south hallway, with a door to the west."},
/* [4]  */ {"bedroom", &objects[0], {NULL, NULL, &rooms[3], NULL},
		"You are in what looks like a bedroom.  The exit is to the east."},
/* [5]  */ {"closet", &objects[1], {NULL, NULL, NULL, &rooms[2]}},
/* [6]  */ {"kitchen", &objects[2], {&rooms[9], &rooms[3], &rooms[7], NULL},
		"You are in the kitchen of the house.\n\
Doorways lead north and south, and there is a stairwell to the east."},
/* [7]  */ {"stairway", NULL, {NULL, NULL, &rooms[6], &rooms[8]},
		"You are in a stairway twisting down beneath the kitchen."},
/* [8]  */ {"basement", &objects[4], {NULL, NULL, &rooms[7], &rooms[10]},
		"You are in a dank basement.  There is a stairway to the east.\n\
It looks like someone has been digging at the west end!"},
/* [9]  */ {"porch", &objects[5], {NULL, &rooms[6], NULL, NULL},
		"You are on the back porth of the house.\n"
		"There is a doorway to the south."},
/* [10] */ {"tunnel", &objects[7], {NULL, NULL, &rooms[8], NULL},
		"You are in a low east-west tunnel, recently dug.\n"
		"The earth seems soft; you wonder how safe the ceiling is."},
	   };
(Initializing long descriptions in this way is not convenient, because the strings are, well, too long. I even embedded \n within some of the strings, to make them print on several lines, and I continued some of them over two lines in the source code, just to make them fit on the page. You will notice that there are two ways of doing this: The descriptions of the entry, kitchen, and basement are extended onto the next line using \ as a continuation character. The descriptions of the porch and tunnel are split into two strings, on two separate lines but with no comma or other punctuation between them; this means that the strings are automatically spliced together at compile time, to form the equivalent of a single string. But if you don't use either of these methods, if you break a string constant across two lines without using \ or splitting it into two strings to be concatenated, the compiler complains.)

I added this code to the big if/else chain in commands.c, to implement an ``examine'' command:

else if(strcmp(verb, "examine") == 0)
	{
	if(object == NULL)
		{
		printf("You must tell me what to examine.\n");
		return FALSE;
		}
	objp = findobject(player, object);
	if(objp == NULL)
		{
		printf("I see no %s here.\n", object);
		return FALSE;
		}
	if(objp->desc == NULL || *objp->desc == '\0')
		printf("You see nothing special about the %s.\n", object);
	else	printf("%s\n", objp->desc);
	}
Finally, I rewrote the listroom function in rooms.c to look like this:
void
listroom(struct actor *actor)
{
struct room *roomp = actor->location;
if(roomp == NULL)
	{
	printf("Where are you?\n");
	return;
	}
printf("%s\n", roomp->name);
if(roomp->desc != NULL && *roomp->desc != '\0')
	printf("%s\n", roomp->desc);
if(roomp->contents != NULL)
	{
	printf("room contains:\n");
	listobjects(roomp->contents);
	}
}


This page by Steve Summit // Copyright 1995-9 // mail feedback