* remove "\r" nonsense
[mascara-docs.git] / C / the.ansi.c.programming.language / notes.accompany.ansi.c / homework / PS1a.html
blobda5a1850e7f75551ca3cd7ce89a57cf301422c47
1 <!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
2 <html>
3 <head>
4 <link rev="owner" href="mailto:scs@eskimo.com">
5 <link rev="made" href="mailto:scs@eskimo.com">
6 <title>Assignment #1 Answers</title>
7 </head>
8 <body>
9 <H1>Assignment #1 Answers</H1>
15 <B>Intermediate C Programming
16 <br>
17 <br>
18 UW Experimental College
19 </B><br>
20 <br>
21 <B>Assignment #1 ANSWERS
22 </B><br>
23 <br>
24 <p>Question 1.
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).
30 <p>Exercise 2.
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>:
35 <pre>
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");
50 </pre>
51 (This help text also contains a description
52 of the new ``examine'' command, below.)
53 <p>Exercise 3.
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
57 <pre>
58 char *desc;
59 </pre>
60 at the end of the definition of <TT>struct object</TT> in <TT>game.h</TT>,
61 and also
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:
66 <pre>
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", &amp;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", &amp;objects[6]},
80 </pre>
81 <pre>
82 static struct room rooms[] =
84 /* [0] */ {"field", NULL, {&amp;rooms[1], NULL, NULL, NULL},
85 "You are in an open field, with a house to the north."},
86 /* [1] */ {"house", NULL, {&amp;rooms[2], &amp;rooms[0], NULL, NULL},
87 "You are standing just to the south of a plain, white house."},
88 /* [2] */ {"entry", NULL, {&amp;rooms[3], &amp;rooms[2], &amp;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, {&amp;rooms[6], &amp;rooms[2], NULL, &amp;rooms[4]},
92 "You are in a north-south hallway, with a door to the west."},
93 /* [4] */ {"bedroom", &amp;objects[0], {NULL, NULL, &amp;rooms[3], NULL},
94 "You are in what looks like a bedroom. The exit is to the east."},
95 /* [5] */ {"closet", &amp;objects[1], {NULL, NULL, NULL, &amp;rooms[2]}},
96 /* [6] */ {"kitchen", &amp;objects[2], {&amp;rooms[9], &amp;rooms[3], &amp;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, &amp;rooms[6], &amp;rooms[8]},
100 "You are in a stairway twisting down beneath the kitchen."},
101 /* [8] */ {"basement", &amp;objects[4], {NULL, NULL, &amp;rooms[7], &amp;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", &amp;objects[5], {NULL, &amp;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", &amp;objects[7], {NULL, NULL, &amp;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."},
111 </pre>
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.)
130 <p>I added this code
131 to the big <TT>if</TT>/<TT>else</TT> chain
132 in <TT>commands.c</TT>,
133 to implement an ``examine'' command:
134 <pre>
135 else if(strcmp(verb, "examine") == 0)
137 if(object == NULL)
139 printf("You must tell me what to examine.\n");
140 return FALSE;
142 objp = findobject(player, object);
143 if(objp == NULL)
145 printf("I see no %s here.\n", object);
146 return FALSE;
148 if(objp-&gt;desc == NULL || *objp-&gt;desc == '\0')
149 printf("You see nothing special about the %s.\n", object);
150 else printf("%s\n", objp-&gt;desc);
152 </pre>
153 Finally, I rewrote the <TT>listroom</TT> function in
154 <TT>rooms.c</TT> to look like this:
155 <pre>
156 void
157 listroom(struct actor *actor)
159 struct room *roomp = actor-&gt;location;
160 if(roomp == NULL)
162 printf("Where are you?\n");
163 return;
165 printf("%s\n", roomp-&gt;name);
166 if(roomp-&gt;desc != NULL &amp;&amp; *roomp-&gt;desc != '\0')
167 printf("%s\n", roomp-&gt;desc);
168 if(roomp-&gt;contents != NULL)
170 printf("room contains:\n");
171 listobjects(roomp-&gt;contents);
174 </pre>
175 <hr>
176 <hr>
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>
181 </p>
182 </body>
183 </html>