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 #
7 Answers
</title>
9 <H1>Assignment #
7 Answers
</H1>
15 <B>Intermediate C Programming
18 UW Experimental College
21 <B>Assignment #
7 ANSWERS
25 <I>Rewrite the
<TT>lockcmd
</TT> and
<TT>unlockcmd
</TT> 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:
37 int lockfunc(struct actor *player, struct object *objp,
40 if(strcmp(cmd-
>verb,
"lock") !=
0 && strcmp(cmd-
>verb,
"unlock") !=
0)
42 if(objp != cmd-
>object)
44 if(cmd-
>preposition == NULL || strcmp(cmd-
>preposition,
"with") !=
0 ||
45 cmd-
>xobject == NULL)
47 printf(
"You must tell me what to %s with.\n", cmd-
>verb);
50 if(!hasattr(cmd-
>object,
"lock"))
52 printf(
"You can't lock the %s.\n", cmd-
>object-
>name);
55 if(Isopen(cmd-
>object))
57 printf(
"The %s is open.\n", cmd-
>object-
>name);
60 if(!contains(player-
>contents, cmd-
>xobject))
62 printf(
"You don't have the %s.\n", cmd-
>xobject-
>name);
65 if(!hasattr(cmd-
>xobject,
"key"))
67 printf(
"The %s won't %s the %s.\n",
68 cmd-
>xobject-
>name, cmd-
>verb, objp-
>name);
72 if(strcmp(cmd-
>verb,
"lock") ==
0)
74 if(hasattr(cmd-
>object,
"locked"))
76 printf(
"The %s is already locked.\n", cmd-
>object-
>name);
80 setattr(cmd-
>object,
"locked");
81 printf(
"The %s is now locked.\n", cmd-
>object-
>name);
84 else if(strcmp(cmd-
>verb,
"unlock") ==
0)
86 if(!hasattr(cmd-
>object,
"locked"))
88 printf(
"The %s is already unlocked.\n", cmd-
>object-
>name);
92 unsetattr(cmd-
>object,
"locked");
93 printf(
"The %s is now unlocked.\n", cmd-
>object-
>name);
98 /* shouldn't get here */
102 Here it is written so as to be attachable to keylike objects:
104 int keyfunc(struct actor *player, struct object *objp,
105 struct sentence *cmd)
107 if(strcmp(cmd-
>verb,
"lock") !=
0 && strcmp(cmd-
>verb,
"unlock") !=
0)
109 if(objp != cmd-
>xobject)
111 if(cmd-
>object == NULL)
113 printf(
"You must tell me what to %s.\n", cmd-
>verb);
116 if(!hasattr(cmd-
>object,
"lock"))
118 printf(
"You can't %s the %s.\n", cmd-
>verb, cmd-
>object-
>name);
121 if(Isopen(cmd-
>object))
123 printf(
"The %s is open.\n", cmd-
>object-
>name);
126 if(!contains(player-
>contents, cmd-
>xobject))
128 printf(
"You don't have the %s.\n", cmd-
>xobject-
>name);
132 if(strcmp(cmd-
>verb,
"lock") ==
0)
134 if(hasattr(cmd-
>object,
"locked"))
136 printf(
"The %s is already locked.\n", cmd-
>object-
>name);
140 setattr(cmd-
>object,
"locked");
141 printf(
"The %s is now locked.\n", cmd-
>object-
>name);
144 else if(strcmp(cmd-
>verb,
"unlock") ==
0)
146 if(!hasattr(cmd-
>object,
"locked"))
148 printf(
"The %s is already unlocked.\n", cmd-
>object-
>name);
152 unsetattr(cmd-
>object,
"locked");
153 printf(
"The %s is now unlocked.\n", cmd-
>object-
>name);
158 /* shouldn't get here */
163 <I>Invent a (silly) shower object.
165 int showerfunc(struct actor *player, struct object *objp,
166 struct sentence *cmd)
168 if(strcmp(cmd-
>verb,
"take") !=
0)
170 if(objp != cmd-
>object)
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");
181 <I>Implement a magic sword object.
183 int swordfunc(struct actor *player, struct object *objp,
184 struct sentence *cmd)
186 if(strcmp(cmd-
>verb,
"take") !=
0)
188 if(objp != cmd-
>object)
190 printf(
"As you pick it up, the sword briefly glows blue.\n");
195 <I>Implement a container of radioactive waste.
196 </I><p>Here are sample descriptions for
<TT>dungeon.dat
</TT>:
199 desc It is a large yellow barrel with a funny symbol and
200 the word
"HAZARDOUS" on it.
203 desc It is a hooded suit made out of some strange plasticlike yellow fabric.
206 Here is the waste barrel function:
208 int wastebarrelfunc(struct actor *player, struct object *objp,
209 struct sentence *cmd)
211 struct object *hazmatsuit;
213 if(strcmp(cmd-
>verb,
"take") !=
0)
215 if(objp != cmd-
>object)
218 hazmatsuit = findobject(player,
"suit");
219 if(hazmatsuit == NULL || !contains(player-
>contents, hazmatsuit))
221 printf(
"The container is far too dangerous to pick up.\n");
228 Here is a function so that you can wear the Hazmat suit:
230 int hazmatsuitfunc(struct actor *player, struct object *objp,
231 struct sentence *cmd)
233 if(strcmp(cmd-
>verb,
"wear") !=
0)
235 if(objp != cmd-
>object)
237 /* implicitly pick up, if necessary */
238 if(!contains(player-
>contents, cmd-
>object))
240 if(!takeobject(player, cmd-
>object))
243 printf(
"You feel much safer wearing the bright yellow Hazmat suit.\n");
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>