1 <!DOCTYPE HTML PUBLIC
"-//W3O//DTD W3 HTML 2.0//EN">
4 <link rev=
"owner" href=
"mailto:scs@eskimo.com">
5 <link rev=
"made" href=
"mailto:scs@eskimo.com">
6 <title>Assignment #
1 Answers
</title>
9 <H1>Assignment #
1 Answers
</H1>
15 <B>Intermediate C Programming
18 UW Experimental College
21 <B>Assignment #
1 ANSWERS
25 <I>What are the four parts of a structure definition?
26 </I><p>The keyword
<TT>struct
</TT>,
27 the structure tag (optional),
28 the brace-enclosed list of member declarations (optional),
29 and a list of variables of this structure type to be declared (optional).
31 <I>Add a ``help'' command.
32 </I><p>Here is the code I added
33 to the big
<TT>if
</TT>/
<TT>else
</TT> chain
34 in
<TT>commands.c
</TT>:
36 else if(strcmp(verb,
"help") ==
0)
38 printf(
"Here are some useful commands:\n");
39 printf(
"n, north\tgo north\n");
40 printf(
"s, south\tgo south\n");
41 printf(
"e, east\t\tgo east\n");
42 printf(
"w, west\t\tgo west\n");
43 printf(
"look\t\tdescribe current room\n");
44 printf(
"examine obj\tdescribe an object\n");
45 printf(
"take obj\tpick up an object\n");
46 printf(
"drop obj\tdrop an object\n");
47 printf(
"i, inventory\tlist your current possessions\n");
48 printf(
"There may be other commands, too!\n");
51 (This help text also contains a description
52 of the new ``examine'' command, below.)
54 <I>Add a ``long description'' field
55 to the
<TT>object
</TT> and
<TT>room
</TT> structures.
56 </I><p>I added the line
60 at the end of the definition of
<TT>struct object
</TT> in
<TT>game.h
</TT>,
62 at the end of
<TT>struct room
</TT>.
63 I modified the initializations of the
<TT>objects
</TT> and
64 <TT>rooms
</TT> arrays in
<TT>main.c
</TT> to give most of the objects
65 and rooms descriptions:
67 static struct object objects[] =
69 /* [
0] */ {
"bed", NULL,
"It is an old, four-poster bed with a lace coverlet."},
70 /* [
1] */ {
"diamond", NULL,
71 "The diamond twinkles majestically as you hold it in the light."},
72 /* [
2] */ {
"kettle", NULL,
73 "It is a dented old kettle. You're not sure it would even hold water."},
74 /* [
3] */ {
"hammer", NULL},
75 /* [
4] */ {
"pliers",
&objects[
3]},
76 /* [
5] */ {
"doormat", NULL,
"It says, \"Welcome\
", in big, friendly letters."},
77 /* [
6] */ {
"pick", NULL,
"It is an old miner's pick."},
78 /* [
7] */ {
"shovel",
&objects[
6]},
82 static struct room rooms[] =
84 /* [
0] */ {
"field", NULL, {
&rooms[
1], NULL, NULL, NULL},
85 "You are in an open field, with a house to the north."},
86 /* [
1] */ {
"house", NULL, {
&rooms[
2],
&rooms[
0], NULL, NULL},
87 "You are standing just to the south of a plain, white house."},
88 /* [
2] */ {
"entry", NULL, {
&rooms[
3],
&rooms[
2],
&rooms[
5], NULL},
89 "You are in the entryway of the house.\n\
90 A hall leads north, and there are doors to the south and east."},
91 /* [
3] */ {
"hall", NULL, {
&rooms[
6],
&rooms[
2], NULL,
&rooms[
4]},
92 "You are in a north-south hallway, with a door to the west."},
93 /* [
4] */ {
"bedroom",
&objects[
0], {NULL, NULL,
&rooms[
3], NULL},
94 "You are in what looks like a bedroom. The exit is to the east."},
95 /* [
5] */ {
"closet",
&objects[
1], {NULL, NULL, NULL,
&rooms[
2]}},
96 /* [
6] */ {
"kitchen",
&objects[
2], {
&rooms[
9],
&rooms[
3],
&rooms[
7], NULL},
97 "You are in the kitchen of the house.\n\
98 Doorways lead north and south, and there is a stairwell to the east."},
99 /* [
7] */ {
"stairway", NULL, {NULL, NULL,
&rooms[
6],
&rooms[
8]},
100 "You are in a stairway twisting down beneath the kitchen."},
101 /* [
8] */ {
"basement",
&objects[
4], {NULL, NULL,
&rooms[
7],
&rooms[
10]},
102 "You are in a dank basement. There is a stairway to the east.\n\
103 It looks like someone has been digging at the west end!"},
104 /* [
9] */ {
"porch",
&objects[
5], {NULL,
&rooms[
6], NULL, NULL},
105 "You are on the back porth of the house.\n"
106 "There is a doorway to the south."},
107 /* [
10] */ {
"tunnel",
&objects[
7], {NULL, NULL,
&rooms[
8], NULL},
108 "You are in a low east-west tunnel, recently dug.\n"
109 "The earth seems soft; you wonder how safe the ceiling is."},
112 (Initializing long descriptions in this way is
<em>not
</em> convenient,
113 because the strings are, well, too long.
114 I even embedded
<TT>\n
</TT> within some of the strings,
115 to make them print on several lines,
116 and I continued some of them them over two lines in the source code,
117 just to make them fit on the page.
118 You will notice that there are two ways of doing this:
119 The descriptions of the entry, kitchen, and basement
120 are extended onto the next line
121 using
<TT>\
</TT> as a continuation character.
122 The descriptions of the porch and tunnel are split into two strings,
123 on two separate lines but with no comma or other punctuation between them;
124 this means that the strings are automatically spliced together at compile time,
125 to form the equivalent of a single string.
126 But if you don't use either of these methods,
127 if you break a string constant across two lines
128 without using
<TT>\
</TT> or splitting it into two strings to be concatenated,
129 the compiler complains.)
131 to the big
<TT>if
</TT>/
<TT>else
</TT> chain
132 in
<TT>commands.c
</TT>,
133 to implement an ``examine'' command:
135 else if(strcmp(verb,
"examine") ==
0)
139 printf(
"You must tell me what to examine.\n");
142 objp = findobject(player, object);
145 printf(
"I see no %s here.\n", object);
148 if(objp-
>desc == NULL || *objp-
>desc == '\
0')
149 printf(
"You see nothing special about the %s.\n", object);
150 else printf(
"%s\n", objp-
>desc);
153 Finally, I rewrote the
<TT>listroom
</TT> function in
154 <TT>rooms.c
</TT> to look like this:
157 listroom(struct actor *actor)
159 struct room *roomp = actor-
>location;
162 printf(
"Where are you?\n");
165 printf(
"%s\n", roomp-
>name);
166 if(roomp-
>desc != NULL
&& *roomp-
>desc != '\
0')
167 printf(
"%s\n", roomp-
>desc);
168 if(roomp-
>contents != NULL)
170 printf(
"room contains:\n");
171 listobjects(roomp-
>contents);
178 This page by
<a href=
"http://www.eskimo.com/~scs/">Steve Summit
</a>
179 //
<a href=
"copyright.html">Copyright
</a> 1995-
9
180 //
<a href=
"mailto:scs@eskimo.com">mail feedback
</a>