home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / tutorial / cpptutor / text / chap12.txt < prev    next >
Encoding:
Text File  |  1994-05-15  |  25.2 KB  |  522 lines

  1.  
  2.  
  3.  
  4.                                                        Chapter 12
  5.                                            FLYAWAY ADVENTURE GAME
  6.  
  7. Now that you have learned lots of things about C++, and know how 
  8. to write and use a single isolated class, you have the problem of 
  9. how to build a program with several classes that work together to 
  10. accomplish some task.  After some amount of thought, it seems 
  11. that an adventure game is a good candidate for a relatively large 
  12. example program.  It has lots of input and output and requires a 
  13. good deal of flexibility while running since there are so many 
  14. things that can be included in the game as obstacles, mazes, 
  15. items to find, and puzzles to solve.
  16.  
  17. The adventure game presented in this chapter is unique as far as 
  18. I know, since I have never heard of another adventure game 
  19. featuring an airport.  The location is not nearly as important as 
  20. the code used to get through the airport.  You are advised to 
  21. play the game to get familiar with what the code does, then study 
  22. the code to see how it works.  Finally, you are given an 
  23. assignment to extend the code which will be the real test of 
  24. whether you understand its operation.
  25.  
  26.  
  27. PLAYING THE GAME
  28. -----------------------------------------------------------------
  29. Prior to studying the source code for this    ===================
  30. game, it would be to your advantage to spend      FLYAWAY.EXE
  31. some time playing the game to get familiar    ===================
  32. with what the game does.  Load the file 
  33. FLYAWAY.EXE and begin the adventure through the airport.  The 
  34. executable file is precompiled for you so you can begin executing 
  35. the program before you have to compile and link the whole thing.  
  36. The entire program is composed of 15 files and will take a little 
  37. effort on your part to properly compile and link it, but that 
  38. will come later.
  39.  
  40. If you have played adventure games before, sometimes called 
  41. interactive fiction, you should begin trying various commands to 
  42. find your way through the airport to your proper plane.  If you 
  43. have not played before, a few hints are in order concerning how 
  44. to play the game.
  45.  
  46. The object of the game is to get to your proper plane on time so 
  47. you can fly away to your vacation.  Of course there a few 
  48. obstacles and problems along the way and they will be brought up 
  49. at the appropriate time.  It will be up to you to solve the 
  50. puzzles associated with each problem.  To add a little 
  51. excitement, you only have about twenty-five minutes to get to 
  52. your plane, with each move taking a minute, so you must hurry.  
  53. Of course, just getting to the plane on time is not enough, there 
  54. are a few additional requirements.  You will find what they are 
  55. as you progress through the game.  You will probably find it 
  56.  
  57.                                                         Page 12-1
  58.  
  59.                               Chapter 12 - Flyaway Adventure Game
  60.  
  61. necessary to restart the game many times before you arrive at 
  62. your destination unscathed and on time.
  63.  
  64.  
  65. THE METHOD OF PLAY
  66. -----------------------------------------------------------------
  67. The method of play is extremely simple.  You simply wander around 
  68. the airport looking for things to do and places to go.  You move 
  69. around the airport by giving the system commands to move in a 
  70. certain direction with four choices available, north, south, 
  71. east, or west.  You can abbreviate any of these four direction 
  72. commands to the first letter only, and you can use either upper 
  73. or lower case.  The system may move you to another area of the 
  74. airport, or it may tell you that you can't go that way.  Try 
  75. loading the game now and typing the four directions once each to 
  76. see what happens.  If this is not clear, enter the word help to 
  77. get you started.
  78.  
  79. In addition to moving around, you can pick up items or ask for 
  80. more information in any of the rooms.  Try telling the system to 
  81. look around the room and see what additional information it gives 
  82. you for each room, some of the clues for solving the puzzle are 
  83. given in the clues issued in response to a look command.  Another 
  84. important command is inventory which will give you a list of the 
  85. items you possess at any given point in time.  Type the word 
  86. inventory at this time to see what items you possess.
  87.  
  88. The remainder of the commands consist of two words, a verb and a 
  89. noun.  These can be given in either order, since the system is 
  90. smart enough to know the difference, and additional words may be 
  91. given following the legal words.  If you give the system a 
  92. command that is not in its limited vocabulary, it will tell you 
  93. it doesn't understand that word.  Try telling the system to drop 
  94. an item you possess, or get an item that is located in the room 
  95. you are currently in.
  96.  
  97. Several friends have played this game with no more knowledge than 
  98. you have been given.  One solved it in 40 minutes, but most took 
  99. about an hour to complete the game.  After you play the game for 
  100. awhile, return to the text and we will study the source code for 
  101. the game.  The entire source code for the game is on your 
  102. distribution disk.  The game was purposely kept small so the code 
  103. could be easily grasped by a programming student.  There is no 
  104. reason the game could not have been made much larger by the 
  105. addition of more rooms, items, and traps.  You may choose to do 
  106. just that to gain experience in working with C++.
  107.  
  108.  
  109. A FEW SPECIAL CONSTANTS
  110. -----------------------------------------------------------------
  111. The file named FLYAWAY.H contains the         ===================
  112. definitions for TRUE and FALSE as well as          FLYAWAY.H
  113. the enumerated type defining the legal        ===================
  114.  
  115.                                                         Page 12-2
  116.  
  117.                               Chapter 12 - Flyaway Adventure Game
  118.  
  119. dictionary of words for use in playing the 
  120. game.  The list was started at a value of 1 so the value of zero 
  121. can be used to indicate that the word in question was not in the 
  122. library and hence not a legal word for use with the game.  The 
  123. #ifndef in line 4 is required because this header file is 
  124. included in many of the other files and if it is included more 
  125. than once, there will be a multiple definition, and hence an 
  126. error.  A class only needs to be defined once, so after it is 
  127. defined by one of the includes, the name FLYAWAY_H will be 
  128. defined and any other defines will be ignored.  This is necessary 
  129. because of the separate compilation capability of C++.  This was 
  130. described in more detail near the end of chapter 7.
  131.  
  132.  
  133. THE FIRST CLASS - clock
  134. -----------------------------------------------------------------
  135. Examine the file named CLOCK.H for the          =================
  136. definition of the clock class.  This is              CLOCK.H
  137. the class for the game clock, and only          =================
  138. one instance of this class will be used.  
  139. It will be used for the object time_of_day defined in line 23 of 
  140. FLYAWAY.CPP.
  141.  
  142. The class is very simple, consisting of only two variables, the 
  143. hour and the minute, and four methods.  The first method is the 
  144. constructor used to initialize the clock to 8:51 as you can see 
  145. if you refer to the implementation of this class in the file 
  146. named CLOCK.CPP.  The next two methods are used to get the 
  147. current values of the two variables.  The final method is much 
  148. more interesting since it does more.  It updates the time of day 
  149. clock and outputs the user prompt to ask for the next command.  
  150. This may not be the best place to output the user prompt since 
  151. this class is devoted to the time of day and associated 
  152. operations, but this was chosen as the place to do it since the 
  153. time of day is part of the user prompt.  You will notice that the 
  154. clock was initialized to 8:51, but the first time output was 8:52 
  155. when you played the game.  In order to simplify the coding later, 
  156. when we need to decide if we made it to the plane on time, the 
  157. time was incremented at the beginning of each game move.  The 
  158. time is therefore the same when the command is entered and when 
  159. it is executed, hence the time is incremented prior to even the 
  160. first output.
  161.  
  162. The clock class is by far the simplest class in the adventure 
  163. game and should be simple for you to understand.  After you are 
  164. sure you understand it, we will go on to the next class.
  165.  
  166.  
  167. INPUT COMMAND PARSING
  168. -----------------------------------------------------------------
  169. The input command parsing routines are          =================
  170. defined within the words class and the               WORDS.H
  171. code for the class is in WORDS.CPP.  The        =================
  172.  
  173.                                                         Page 12-3
  174.  
  175.                               Chapter 12 - Flyaway Adventure Game
  176.  
  177. code is straightforward and simple to 
  178. understand if you study it, so only a few comments will be made 
  179. about this class.
  180.  
  181. The method get_command() reads two words from the keyboard by 
  182. calling the function read_a_line() and stores the words in the 
  183. class members verb and noun.  It stores zero for either or both 
  184. of the words if it does not find a valid noun and a valid verb.
  185.  
  186. Two methods are included to provide the verb or noun which was 
  187. input as the last user input.  This allows any code that has 
  188. visibility of the object based on this class to find out what the 
  189. player would like to do.
  190.  
  191. There are four methods beginning with is_ in this class that are 
  192. used to determine if a word is a verb, a noun, a direction, or an 
  193. operation.  These are called upon from various places within the 
  194. program.  What they do should be easy for you to understand, but 
  195. it will take a little thought on your part to see why these are 
  196. needed in other parts of the code.
  197.  
  198. Finally the simple method named stop_game() is used to set the 
  199. verb to the value of quit so the game will be ended by the 
  200. control logic in the main program FLYAWAY.CPP.
  201.  
  202. All of the source code for the implementation is given in the 
  203. file named WORDS.CPP.  Since this code is fairly simple and well 
  204. commented, you will be left on your own to study it to whatever 
  205. depth you desire.
  206.  
  207.  
  208. THE SECOND CLASS - items
  209. -----------------------------------------------------------------
  210. If you examine the files named ITEMS.H and    ===================
  211. ITEMS.CPP, you will find the complete               ITEMS.H
  212. definitions of the handling of the items      ===================
  213. that you carried around the airport in the 
  214. game.  There were exactly four transportable items that could be 
  215. located in each room or carried by yourself, the keys, the candy, 
  216. the ticket, and the money.  The keys and the money keep you from 
  217. getting through security and the ticket and candy are required to 
  218. get you safely on the plane and enroute to your destination.
  219.                     
  220. The four items are stored in the class named items in the form of 
  221. TRUE or FALSE since that is the only required indication.  A TRUE 
  222. means the item is located here, and a FALSE means the item is not 
  223. here.  The values of TRUE and FALSE are defined in FLYAWAY.H.  
  224. Finally, there are six methods to operate on these items.
  225.  
  226. The first method is a constructor to set all items to FALSE, and 
  227. the next two are used to either get a specific item, or drop one.  
  228. The fourth method is used to tell us if the item is located here 
  229. and the last two are used to tell us what items are on hand in 
  230.  
  231.                                                         Page 12-4
  232.  
  233.                               Chapter 12 - Flyaway Adventure Game
  234.  
  235. this location.  You will notice that the final two are different 
  236. because different text was desired depending on whether you are 
  237. carrying the items, or they are located in a room somewhere.
  238.  
  239. This file, like all other header files, is protected from 
  240. multiple inclusion by the #ifndef construct discussed earlier.
  241.  
  242. This class is used in line 24 of FLYAWAY.CPP to define an object 
  243. for the player named personal_items which stores the list of 
  244. items the player is carrying around.  It is also used in the 
  245. class location as an embedded or nested object to store the items 
  246. that are located in each of the 19 locations in the game.
  247.  
  248. Once again, the implementation for this class is so simple that 
  249. you will have no difficulty in understanding it.
  250.  
  251.  
  252. THE FLIGHT AND GATE CLASS - schedule
  253. -----------------------------------------------------------------
  254. Examine the example files named SCHEDULE.H     ==================
  255. and SCHEDULE.CPP for our first example of a        SCHEDULE.H
  256. rather large class, the one that does the      ==================
  257. flight and gate scheduling.  You will find a 
  258. large number of variables in this class, and eight methods to 
  259. handle the variables.  Instead of a detailed description of each 
  260. variable and method, we will only give a brief overview of the 
  261. class.
  262.  
  263. Only one object of this class is declared named flight_info in 
  264. line 22 of the main program named FLYAWAY.CPP.  The constructor 
  265. initializes the flight possibilities, and the method named 
  266. shuffle_gates() shuffles all gates around if the player arrives 
  267. at his correct gate without reading the monitor in the waiting 
  268. area.  Once the monitor in the waiting area is read, the 
  269. flights_frozen variable is made TRUE.  Likewise, the players 
  270. destination is changed during every move by the method named 
  271. shuffle_flights() until the player reads his ticket invoking 
  272. the method named list_actual_destination().
  273.  
  274. This class contains the methods to list the data seen on the 
  275. monitor, as well as the data seen when invoking the command look 
  276. at one of the gates.  Finally, this class contains the method 
  277. named check_flight() which searches through the list of 
  278. requirements to see if the player has completed all requirements 
  279. to successfully reach the final destination for his vacation.
  280.  
  281. You will notice that several of the location objects were 
  282. required to be available within this code and are listed as 
  283. extern in lines 12 through 21 of the implementation of the class.  
  284. The only other thing to point out is the rest room requirement 
  285. prior to boarding the flight.  Line 28 is where the global 
  286. variable is defined and initialized, then in line 77 it is set 
  287. TRUE if the current location is the rest room, since this is 
  288.  
  289.                                                         Page 12-5
  290.  
  291.                               Chapter 12 - Flyaway Adventure Game
  292.  
  293. called once during each player move.  Finally, the state of this 
  294. variable is checked in line 230 of this file and the appropriate 
  295. action taken.  You will note that the main program is not aware 
  296. that the rest room variable exists or that anything happens as a 
  297. result of this variable.  In addition to information hiding, we 
  298. may coin a new term, something like "Information Ignorance", 
  299. since the main program did not even need to be aware that there 
  300. was a requirement to visit the rest room.
  301.  
  302. Even though this is a relatively large and complex class, it is 
  303. well commented so no further information will be given concerning 
  304. the implementation.
  305.  
  306.  
  307. THE MOST USED CLASS - location
  308. -----------------------------------------------------------------
  309. The file named LOCATION.H is the header file   ==================
  310. for the class named location.  It is the           LOCATION.H
  311. class that controls all of the moves from      ==================
  312. location to location.  This class is a bit 
  313. unusual in that most of the stored data is in the form of 
  314. pointers to the various entities.  The first four are the 
  315. locations to which we will go if we move in one of the four 
  316. directions from the current location.  You will note that they 
  317. are pointers to those four locations.  Next we have pointers to 
  318. two different character strings associated with this room.  
  319. Finally in line 22, we declare the object named list_of_items 
  320. which is an object of class items defined earlier.  Note that 
  321. this is an embedded class, a class embedded within the location 
  322. class.  It is not a parent class which we are inheriting 
  323. something from.  In fact we are instantiating an object of class 
  324. items for use within the room since the room is allowed to store 
  325. any combination of the four items contained in the class named 
  326. items.
  327.  
  328. There is no constructor used with this class since we choose to 
  329. initialize the locations one by one.  The method named init() has 
  330. 6 variable parameters, all of which are pointers, associated with 
  331. it which it uses to initialize the first six variables of this 
  332. object.  The last variable, an object of class items, is 
  333. initialized through use of the constructor associated with its 
  334. class.  Referring to lines 40 through 171 of the implementation 
  335. for the map class, you will find all of the initialization code 
  336. for the 19 objects of class location.  If you drew a map when you 
  337. played the game, you will see the interconnections between the 
  338. various locations embedded in the initialization statements.  
  339. Notice there is no way to get back to the car from the passenger 
  340. drop off area, because presumably the car leaves when you get out 
  341. of it.
  342.  
  343. The next method, named move(), returns a pointer to the new 
  344. location if a move was legal, otherwise it returns a NULL value.  
  345. The observant student will also notice that there are special 
  346.  
  347.                                                         Page 12-6
  348.  
  349.                               Chapter 12 - Flyaway Adventure Game
  350.  
  351. cases involved with getting out of the snack bar and getting 
  352. through security.  These are located here because they are part 
  353. of the move logic.  If you played the game to the complete 
  354. conclusion, you surely had trouble with at least one of these 
  355. situations.
  356.  
  357. The rest of the methods in this class should be self explanatory 
  358. and will not be discussed any further.
  359.  
  360.  
  361. THE LOCATION MESSAGES
  362. -----------------------------------------------------------------
  363. Examine the file named MESSAGE.TXT for a      ===================
  364. complete listing of the messages output to        MESSAGE.TXT
  365. the monitor when each location was entered.   ===================
  366. You will also find the text for each of the 
  367. messages output in response to a look command in this file.  
  368. These were put into a separate file only for the purpose of 
  369. reducing the size of the map class implementation file.  It does 
  370. not reduce the compile time since these messages are not 
  371. separately compiled.  They are included into the file and 
  372. compiled each time the map file MAP.CPP is compiled.  You will 
  373. note that a few of the messages have no text at all, only the 
  374. empty quote marks, but are included in order to have something 
  375. for the initialization code to work with.
  376.  
  377. Three other messages are stored here for convenience in lines 5 
  378. through 40.  Their use and meaning should be self-evident.
  379.  
  380.  
  381. THE MAIN PROGRAM
  382. -----------------------------------------------------------------
  383. We finally reach the main program, the one    ===================
  384. that actually does the top level control.         FLYAWAY.CPP
  385. Examine the program named FLYAWAY.CPP and     ===================
  386. we will look at some of its interesting 
  387. characteristics.
  388.  
  389. Beginning with the main() entry point itself, we see that 
  390. following a call to airport.initialize(), we enter a single do 
  391. while loop which terminates when the player enters the word quit 
  392. or when the verb quit comes up some other way.  There are other 
  393. ways to set the verb to quit because it is generated internally 
  394. in some cases such as at end of game.
  395.  
  396. The loop itself consists of 5 method calls.  First we call the 
  397. function named input_words.get_command() to get the players input 
  398. command in line 30.  Next we send two messages to the object 
  399. named flight_info to shuffle the flights and gates if the proper 
  400. actions have not been performed, then we call 
  401. airport.perform_action() which we will describe in a few 
  402. paragraphs.  Finally, we send a messages to the object named 
  403. flight_info to check if the player has reached one of the gates.  
  404.  
  405.                                                         Page 12-7
  406.  
  407.                               Chapter 12 - Flyaway Adventure Game
  408.  
  409. Remember that within most of the methods we perform checks to see 
  410. if we need to do the thing requested in the message, then either 
  411. perform the action or simply return to the caller or message 
  412. sender.
  413.  
  414.  
  415. THE WORKING METHOD
  416. -----------------------------------------------------------------
  417. The only function we have not mentioned yet     =================
  418. is the one that does most of the interesting          MAP.H
  419. work, the function named perform_action()       =================
  420. which begins in line 183 of the MAP.CPP file.  
  421. This function looks at the verb and noun, if there is one, and 
  422. causes the correct action to be performed.  Because of the way we 
  423. packaged all of the other routines, this function is a snap to 
  424. implement and to study.  If you go through each of the else if 
  425. clauses in this function, you will have no trouble understanding 
  426. what action is taken for each of the input commands.  You will 
  427. notice that many of the actions have conditional clauses before 
  428. the action is taken.  For example, it is illegal to buy candy 
  429. unless the player has money, the location has candy, and the 
  430. location must be the snack_bar according to the rules of the 
  431. game.
  432.  
  433. Finally, at the end of this method in line 277, we have the 
  434. default case if nothing else was accomplished.  It is assumed 
  435. that there was something funny requested such as a request to get 
  436. a monitor.  Both of these are legal words but they make no sense 
  437. together.
  438.  
  439.  
  440. FINAL COMMENTS ON FLYAWAY
  441. -----------------------------------------------------------------
  442. Now that you have played the game for awhile and studied the game 
  443. in detail, you should have an appreciation for how this game can 
  444. be written.  Of course, it could be written in any of several 
  445. thousand different ways of packaging and definition.  This has 
  446. been only one of the ways.Because the student may be left with 
  447. the sinking feeling that this method simply fell out of the sky 
  448. or was arrived at in some other esoteric way, it would only be 
  449. fair to point out that several earlier attempts at outlining 
  450. this project were attempted and rejected prior to this 
  451. arrangement.  Also, when this tutorial was being updated from 
  452. version 2.0 to 2.2, the entire program was restructured.  In 
  453. version 2.0 and prior versions, about 50% of the code was in 
  454. classes, but due to additional programming experience, about 98% 
  455. of the flyaway program is now encapsulated in classes.
  456.  
  457. Object oriented programming requires the same forethought as 
  458. non-object oriented programming, but the object oriented compiler 
  459. will help you in the coding and debugging phase since the 
  460. compiler will find and flag many of the oversight errors we are 
  461. so good at introducing into our code.  It was observed during 
  462.  
  463.                                                         Page 12-8
  464.  
  465.                               Chapter 12 - Flyaway Adventure Game
  466.  
  467. the coding and debugging phase of this project that in nearly 
  468. every case, when the program finally got through the compiler, 
  469. the program would actually run without bombing out the system.  
  470. This is not always the case using any standard procedural 
  471. programming language.
  472.  
  473.  
  474. YOUR PROGRAMMING PROJECT
  475. -----------------------------------------------------------------
  476. This programming assignment is intended to give you a little 
  477. experience in working with a relatively large project as opposed 
  478. to the very small programs we have been working with in this 
  479. tutorial.  Add a suitcase to the game, to be found in the car at 
  480. arrival, and which must be checked in at the ticket counter prior 
  481. to attempting to get through airport security.  This will not be 
  482. trivial since several classes will be affected.  Some of the 
  483. operations you will have to do are listed below.
  484.  
  485. 1.  Add the noun "suitcase" and the verb "check" to the word 
  486.     list.  Of course, they must be entered at the right place in 
  487.     the list.
  488.  
  489. 2.  Add the suitcase to the items class, including additional 
  490.     code to each of its methods.
  491.  
  492. 3.  Initialize the items at location your_car to include the 
  493.     suitcase.
  494.  
  495. 4.  Add an additional check when passing through security to 
  496.     check that the player is not carrying the suitcase.  You can 
  497.     add any sort of penalty desired, including death by firing 
  498.     squad for attempting such an obviously crooked deed.
  499.  
  500. 5.  You will need to add a check when the player finally gets on 
  501.     his correct airplane to see that he checked his suitcase.  If 
  502.     he did not, you could output any desired text indicating 
  503.     stupidity or forgetfulness.
  504.  
  505. Since I have not actually added the suitcase to the game and 
  506. tested it, I am not sure that this is all that will be required, 
  507. but it should be the majority of effort required.  The bottom 
  508. line of this effort is that if you understand this program enough 
  509. to perform this modification, you have a good understanding of 
  510. how the program works and how objects work together to perform a 
  511. task.
  512.  
  513. Once you understand this program, you should define a programming 
  514. project for yourself that will use object oriented programming 
  515. techniques and begin designing and programming it.  The best way 
  516. to learn to use OOP is to actually use it.
  517.  
  518. Good luck in your OOP endeavors.
  519.  
  520.  
  521.                                                         Page 12-9
  522.