* remove "\r" nonsense
[mascara-docs.git] / C / the.ansi.c.programming.language / notes.accompany.ansi.c / homework / PS7a.html
blob257650254ec3edabaa99724883cd06dd0ad52f9d
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 #7 Answers</title>
7 </head>
8 <body>
9 <H1>Assignment #7 Answers</H1>
15 <B>Intermediate C Programming
16 <br>
17 <br>
18 UW Experimental College
19 </B><br>
20 <br>
21 <B>Assignment #7 ANSWERS
22 </B><br>
23 <br>
24 <p>Exercise 2.
25 <I>Rewrite the <TT>lockcmd</TT> and <TT>unlockcmd</TT> functions
26 as object functions.
27 </I><p>I stated this problem slightly incorrectly.
28 You only need to write one function,
29 which will recognize and implement
30 both the verbs ``lock'' and ``unlock''.
31 You can write the function in two ways,
32 one way so that it can be attached to objects that can be locked and unlocked,
33 and one way so that it can be attached to objects like keys
34 that do the locking and unlocking.
35 Here is the function written so as to be attachable to lockable objects:
36 <pre>
37 int lockfunc(struct actor *player, struct object *objp,
38 struct sentence *cmd)
40 if(strcmp(cmd-&gt;verb, "lock") != 0 &amp;&amp; strcmp(cmd-&gt;verb, "unlock") != 0)
41 return CONTINUE;
42 if(objp != cmd-&gt;object)
43 return CONTINUE;
44 if(cmd-&gt;preposition == NULL || strcmp(cmd-&gt;preposition, "with") != 0 ||
45 cmd-&gt;xobject == NULL)
47 printf("You must tell me what to %s with.\n", cmd-&gt;verb);
48 return FAILURE;
50 if(!hasattr(cmd-&gt;object, "lock"))
52 printf("You can't lock the %s.\n", cmd-&gt;object-&gt;name);
53 return FAILURE;
55 if(Isopen(cmd-&gt;object))
57 printf("The %s is open.\n", cmd-&gt;object-&gt;name);
58 return FAILURE;
60 if(!contains(player-&gt;contents, cmd-&gt;xobject))
62 printf("You don't have the %s.\n", cmd-&gt;xobject-&gt;name);
63 return FAILURE;
65 if(!hasattr(cmd-&gt;xobject, "key"))
67 printf("The %s won't %s the %s.\n",
68 cmd-&gt;xobject-&gt;name, cmd-&gt;verb, objp-&gt;name);
69 return FAILURE;
72 if(strcmp(cmd-&gt;verb, "lock") == 0)
74 if(hasattr(cmd-&gt;object, "locked"))
76 printf("The %s is already locked.\n", cmd-&gt;object-&gt;name);
77 return FAILURE;
80 setattr(cmd-&gt;object, "locked");
81 printf("The %s is now locked.\n", cmd-&gt;object-&gt;name);
82 return SUCCESS;
84 else if(strcmp(cmd-&gt;verb, "unlock") == 0)
86 if(!hasattr(cmd-&gt;object, "locked"))
88 printf("The %s is already unlocked.\n", cmd-&gt;object-&gt;name);
89 return FAILURE;
92 unsetattr(cmd-&gt;object, "locked");
93 printf("The %s is now unlocked.\n", cmd-&gt;object-&gt;name);
95 return SUCCESS;
98 /* shouldn't get here */
99 return CONTINUE;
101 </pre>
102 Here it is written so as to be attachable to keylike objects:
103 <pre>
104 int keyfunc(struct actor *player, struct object *objp,
105 struct sentence *cmd)
107 if(strcmp(cmd-&gt;verb, "lock") != 0 &amp;&amp; strcmp(cmd-&gt;verb, "unlock") != 0)
108 return CONTINUE;
109 if(objp != cmd-&gt;xobject)
110 return CONTINUE;
111 if(cmd-&gt;object == NULL)
113 printf("You must tell me what to %s.\n", cmd-&gt;verb);
114 return FAILURE;
116 if(!hasattr(cmd-&gt;object, "lock"))
118 printf("You can't %s the %s.\n", cmd-&gt;verb, cmd-&gt;object-&gt;name);
119 return FAILURE;
121 if(Isopen(cmd-&gt;object))
123 printf("The %s is open.\n", cmd-&gt;object-&gt;name);
124 return FAILURE;
126 if(!contains(player-&gt;contents, cmd-&gt;xobject))
128 printf("You don't have the %s.\n", cmd-&gt;xobject-&gt;name);
129 return FAILURE;
132 if(strcmp(cmd-&gt;verb, "lock") == 0)
134 if(hasattr(cmd-&gt;object, "locked"))
136 printf("The %s is already locked.\n", cmd-&gt;object-&gt;name);
137 return FAILURE;
140 setattr(cmd-&gt;object, "locked");
141 printf("The %s is now locked.\n", cmd-&gt;object-&gt;name);
142 return SUCCESS;
144 else if(strcmp(cmd-&gt;verb, "unlock") == 0)
146 if(!hasattr(cmd-&gt;object, "locked"))
148 printf("The %s is already unlocked.\n", cmd-&gt;object-&gt;name);
149 return FAILURE;
152 unsetattr(cmd-&gt;object, "locked");
153 printf("The %s is now unlocked.\n", cmd-&gt;object-&gt;name);
155 return SUCCESS;
158 /* shouldn't get here */
159 return CONTINUE;
161 </pre>
162 <p>Exercise 4.
163 <I>Invent a (silly) shower object.
164 </I><p><pre>
165 int showerfunc(struct actor *player, struct object *objp,
166 struct sentence *cmd)
168 if(strcmp(cmd-&gt;verb, "take") != 0)
169 return CONTINUE;
170 if(objp != cmd-&gt;object)
171 return CONTINUE;
173 printf("After spending 20 luxurious minutes in the shower,\n");
174 printf("singing three full-length show tunes, and using up\n");
175 printf("all the hot water, you emerge clean and refreshed.\n");
177 return SUCCESS;
179 </pre>
180 <p>Exercise 5.
181 <I>Implement a magic sword object.
182 </I><p><pre>
183 int swordfunc(struct actor *player, struct object *objp,
184 struct sentence *cmd)
186 if(strcmp(cmd-&gt;verb, "take") != 0)
187 return CONTINUE;
188 if(objp != cmd-&gt;object)
189 return CONTINUE;
190 printf("As you pick it up, the sword briefly glows blue.\n");
191 return CONTINUE;
193 </pre>
194 <p>Exercise 6.
195 <I>Implement a container of radioactive waste.
196 </I><p>Here are sample descriptions for <TT>dungeon.dat</TT>:
197 <pre>
198 object barrel
199 desc It is a large yellow barrel with a funny symbol and
200 the word "HAZARDOUS" on it.
201 func wastebarrelfunc
202 object suit
203 desc It is a hooded suit made out of some strange plasticlike yellow fabric.
204 func hazmatsuitfunc
205 </pre>
206 Here is the waste barrel function:
207 <pre>
208 int wastebarrelfunc(struct actor *player, struct object *objp,
209 struct sentence *cmd)
211 struct object *hazmatsuit;
213 if(strcmp(cmd-&gt;verb, "take") != 0)
214 return CONTINUE;
215 if(objp != cmd-&gt;object)
216 return CONTINUE;
218 hazmatsuit = findobject(player, "suit");
219 if(hazmatsuit == NULL || !contains(player-&gt;contents, hazmatsuit))
221 printf("The container is far too dangerous to pick up.\n");
222 return FAILURE;
225 return CONTINUE;
227 </pre>
228 Here is a function so that you can wear the Hazmat suit:
229 <pre>
230 int hazmatsuitfunc(struct actor *player, struct object *objp,
231 struct sentence *cmd)
233 if(strcmp(cmd-&gt;verb, "wear") != 0)
234 return CONTINUE;
235 if(objp != cmd-&gt;object)
236 return CONTINUE;
237 /* implicitly pick up, if necessary */
238 if(!contains(player-&gt;contents, cmd-&gt;object))
240 if(!takeobject(player, cmd-&gt;object))
241 return FAILURE;
243 printf("You feel much safer wearing the bright yellow Hazmat suit.\n");
244 return SUCCESS;
246 </pre>
247 <hr>
248 <hr>
250 This page by <a href="http://www.eskimo.com/~scs/">Steve Summit</a>
251 // <a href="copyright.html">Copyright</a> 1995-9
252 // <a href="mailto:scs@eskimo.com">mail feedback</a>
253 </p>
254 </body>
255 </html>