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 #
2</title>
15 <B>Intermediate C Programming
18 UW Experimental College
23 </B></p><p><a href=
"PS2.html">Assignment #
2</a>
24 <br><a href=
"PS1a.html">Assignment #
1 Answers
</a>
25 <br><a href=
"http://www.eskimo.com/~scs/cclass/int/sx2.html">Class Notes, Chapter
16</a>
26 <br><a href=
"http://www.eskimo.com/~scs/cclass/int/sx3.html">Class Notes, Chapter
17</a>
27 <p><B>Review Questions:
28 </B></p><OL><li>List some differences between text and binary data files.
30 </B></p><OL><li>We're going to fix one of the more glaring deficiencies
31 of the initial implementation of the game,
32 namely that the description of the dungeon takes the form of
33 hard-compiled structure arrays in
<TT>main.c
</TT>,
34 such that the source code must be edited
35 (and in a tedious way, at that)
36 and the game recompiled
37 whenever we want to modify the dungeon.
38 (We're used to recompiling when we make changes to our programs,
39 but we shouldn't have to recompile when we make expected changes to our data.
40 Imagine if a spreadsheet program
41 required you to enter your worksheet expressions or data
42 with a C compiler and recompile each time!)
45 We're going to add code which can read the dungeon description
46 dynamically, at run time, from a simple, textual data file.
47 The file will describe the rooms,
48 the objects and which rooms they're initially in,
49 and the exits which interconnect the rooms.
51 we'll dynamically assign new instances
52 of
<TT>struct room
</TT> and
<TT>struct object
</TT>
53 to define the rooms and objects which the data file describes.
54 We won't know how many room and object structures we'll need,
55 and we certainly won't know what descriptions they will contain,
56 so we'll abandon the
<TT>rooms
</TT> and
<TT>objects
</TT> arrays from
<TT>main.c
</TT>,
57 along with their cumbersome initializations.
58 We'll add (in
<TT>objects.c
</TT>) an allocation function,
60 which will return a pointer to a brand-new object structure
61 which we can use to contain some new object,
63 we've just read from the data file.
64 Similarly, we'll add a
<TT>newroom()
</TT> function to
<TT>rooms.c
</TT>
65 which will return a pointer to a brand-new room structure.
66 (We're not quite ready to do full-blown dynamic memory allocation yet,
67 so for now, the room and object alloction functions
68 will dole structures out of fixed-size arrays
69 which we'll arbitrarily declare as having size
100.
70 Naturally we'll keep track of how many structures are actually in use,
71 and complain if an attempt is made to use more than
100.
72 Since the allocation code is isolated
73 down within
<TT>rooms.c
</TT> and
<TT>object.c
</TT>,
74 only those modules will need rewriting
75 when we move to a more flexible allocation scheme;
76 the code which calls
<TT>newroom()
</TT>
77 and
<TT>newobject()
</TT> won't need to change.)
80 Appended to this assignment is a listing of a new file,
<TT>io.c
</TT>,
81 for doing the data file reading.
82 (This code is also on the disk
84 in the
<TT>week2
</TT> subdirectory.)
87 Rip out the definitions and initializations of the
<TT>objects
</TT>
88 and
<TT>rooms
</TT> arrays at the top of
<TT>main.c
</TT>;
89 also remove the initialization of
<TT>actor
</TT>
90 (but leave the definition).
91 At the top of
<TT>main()
</TT>,
92 before the initial call to
<TT>listroom
</TT>,
98 gotoroom(
&actor, getentryroom()); /* put actor in initial room */
102 Add the following code to
<TT>object.c
</TT>
103 (it can go near the top):
105 #define MAXOBJECTS
100
107 static struct object objects[MAXOBJECTS];
108 static int nobjects =
0;
111 newobject(char *name)
115 if(nobjects
>= MAXOBJECTS)
117 fprintf(stderr,
"too many objects\n");
121 objp =
&objects[nobjects++];
123 strcpy(objp-
>name, name);
124 objp-
>lnext = NULL;
129 (This code is in the file
<TT>object.xc
</TT>
130 in the
<TT>week2
</TT> subdirectory.)
133 Add the following code to
<TT>rooms.c
</TT>
134 (it can go near the top):
138 static struct room rooms[MAXROOMS];
139 static int nrooms =
0;
147 if(nrooms
>= MAXROOMS)
149 fprintf(stderr,
"too many rooms\n");
153 roomp =
&rooms[nrooms++];
155 strcpy(roomp-
>name, name);
156 roomp-
>contents = NULL;
157 for(i =
0; i
< NEXITS; i++)
158 roomp-
>exits[i] = NULL;
168 for(i =
0; i
< nrooms; i++)
170 if(strcmp(rooms[i].name, name) ==
0)
171 return
&rooms[i];
182 return
&rooms[
0]; /* temporary */
185 (This code is in the file
<TT>rooms.xc
</TT>
186 in the
<TT>week2
</TT> subdirectory.)
189 You'll also need the file
<TT>fgetline.c
</TT>,
190 which is appended behind the new
<TT>io.c
</TT>,
191 and also in the
<TT>week2
</TT> subdirectory.
194 After adding all the new code,
195 and any necessary prototype declarations to
<TT>game.h
</TT>,
196 the program should again compile and run,
197 but it will expect to find a data file named
198 <TT>dungeon.dat
</TT> in the current directory.
199 A sample
<TT>dungeon.dat
</TT> file
200 (corresponding to the old, hard-compiled game)
202 (and in the
<TT>week2
</TT> subdirectory).
203 Once you see how it works, you can start extending the dungeon
204 simply by modifying the
<TT>dungeon.dat
</TT> file.
206 If you added long descriptions to rooms and objects last week,
207 devise and implement a way for those long descriptions to be
208 read from the data file.
210 Expand the set of allowable exits from rooms.
211 Add the possibility of northeast, southeast, northwest, and
212 southwest exits, or ``up'' and ``down''
214 Add commands to go in these new directions.
215 Modify the data file reading code in
<TT>io.c
</TT> to handle
216 setting up the new exits.
220 This page by
<a href=
"http://www.eskimo.com/~scs/">Steve Summit
</a>
221 //
<a href=
"copyright.html">Copyright
</a> 1995-
9
222 //
<a href=
"mailto:scs@eskimo.com">mail feedback
</a>