📄 chap12.txt
字号:
Chapter 12 - Flyaway Adventure Game
You will notice that several of the location objects were required
to be available within this code and are listed as extern in lines
12 through 21 of the implementation of the class. The only other
thing to point out is the rest room requirement prior to boarding
the flight. Line 28 is where the global variable is defined and
initialized, then in line 77 it is set TRUE if the current location
is the rest room, since this is called once during each player
move. Finally, the state of this variable is checked in line 230
of this file and the appropriate action taken. You will note that
the main program is not aware that the rest room variable exists
or that anything happens as a result of this variable. In addition
to information hiding, we may coin a new term, something like
"Information Ignorance", since the main program did not even need
to be aware that there was a requirement to visit the rest room.
Even though this is a relatively large and complex class, it is
well commented so no further information will be given concerning
the implementation.
THE MOST USED CLASS - location
_________________________________________________________________
The file named LOCATION.H is the header file for ================
the class named location. It is the class that LOCATION.H
controls all of the moves from location to ================
location.
This class is a bit unusual in that most of the stored data is in
the form of pointers to the various entities. The first four are
the locations to which we will go if we move in one of the four
directions from the current location. You will note that they are
pointers to those four locations. Next we have pointers to two
different character strings associated with this room. Finally in
line 22, we declare the object named list_of_items which is an
object of class items defined earlier. Note that this is an
embedded class, a class embedded within the location class. It is
not a parent class which we are inheriting something from. In fact
we are instantiating an object of class items for use within the
room since the room is allowed to store any combination of the four
items contained in the class named items.
There is no constructor used with this class since we choose to
initialize the locations one by one. The method named init() has
6 variable parameters, all of which are pointers, associated with
it which it uses to initialize the first six variables of this
object. The last variable, an object of class items, is
initialized through use of the constructor associated with its
class. Referring to lines 40 through 171 of the implementation for
the map class, you will find all of the initialization code for the
19 objects of class location. If you drew a map when you played
the game, you will see the interconnections between the various
Page 12-6
Chapter 12 - Flyaway Adventure Game
locations embedded in the initialization statements. Notice there
is no way to get back to the car from the passenger drop off area,
because presumably the car leaves when you get out of it.
The next method, named move(), returns a pointer to the new
location if a move was legal, otherwise it returns a NULL value.
The observant student will also notice that there are special cases
involved with getting out of the snack bar and getting through
security. These are located here because they are part of the move
logic. If you played the game to the complete conclusion, you
surely had trouble with at least one of these situations.
The rest of the methods in this class should be self explanatory
and will not be discussed any further.
THE LOCATION MESSAGES
_________________________________________________________________
Examine the file named MESSAGE.TXT for a ===============
complete listing of the messages output to the MESSAGE.TXT
monitor when each location was entered. You ===============
will also find the text for each of the messages
output in response to a look command in this
file. These were put into a separate file only for the purpose of
reducing the size of the map class implementation file. It does
not reduce the compile time since these messages are not separately
compiled. They are included into the file and compiled each time
the map file MAP.CPP is compiled. You will note that a few of the
messages have no text at all, only the empty quote marks, but are
included in order to have something for the initialization code to
work with.
Three other messages are stored here for convenience in lines 5
through 40. Their use and meaning should be self-evident.
THE MAIN PROGRAM
_________________________________________________________________
We finally reach the main program, the one that ===============
actually does the top level control. Examine FLYAWAY.CPP
the program named FLYAWAY.CPP and we will look ===============
at some of its interesting characteristics.
Beginning with the main() entry point itself, we see that following
a call to airport.initialize(), we enter a single do while loop
which terminates when the player enters the word quit or when the
verb quit comes up some other way. There are other ways to set the
verb to quit because it is generated internally in some cases such
as at end of game.
Page 12-7
Chapter 12 - Flyaway Adventure Game
The loop itself consists of 5 method calls. First we call the
function named input_words.get_command() to get the players input
command in line 30. Next we send two messages to the object named
flight_info to shuffle the flights and gates if the proper actions
have not been performed, then we call airport.perform_action()
which we will describe in a few paragraphs. Finally, we send a
messages to the object named flight_info to check if the player has
reached one of the gates. Remember that within most of the methods
we perform checks to see if we need to do the thing requested in
the message, then either perform the action or simply return to the
caller or message sender.
THE WORKING METHOD
_________________________________________________________________
The only function we have not mentioned yet is ===============
the one that does most of the interesting work, MAP.H
the function named perform_action() which begins ===============
in line 183 of the MAP.CPP file. This function
looks at the verb and noun, if there is one, and
causes the correct action to be performed. Because of the way we
packaged all of the other routines, this function is a snap to
implement and to study. If you go through each of the else if
clauses in this function, you will have no trouble understanding
what action is taken for each of the input commands. You will
notice that many of the actions have conditional clauses before the
action is taken. For example, it is illegal to buy candy unless
the player has money, the location has candy, and the location must
be the snack_bar according to the rules of the game.
Finally, at the end of this method in line 277, we have the default
case if nothing else was accomplished. It is assumed that there
was something funny requested such as a request to get a monitor.
Both of these are legal words but they make no sense together.
FINAL COMMENTS ON FLYAWAY
_________________________________________________________________
Now that you have played the game for awhile and studied the game
in detail, you should have an appreciation for how this game can
be written. Of course, it could be written in any of several
thousand different ways of packaging and definition. This has been
only one of the ways.
Because the student may be left with the sinking feeling that this
method simply fell out of the sky or was arrived at in some other
esoteric way, it would only be fair to point out that several
earlier attempts at outlining this project were attempted and
rejected prior to this arrangement. Also, when this tutorial was
being updated from version 2.0 to 2.2, the entire program was
restructured. In version 2.0 and prior versions, about 50% of the
Page 12-8
Chapter 12 - Flyaway Adventure Game
code was in classes, but due to additional programing experience,
about 98% of the flyaway program is now encapsulated in classes.
Object oriented programming requires the same forethought as non-
object oriented programming, but the object oriented compiler will
help you in the coding and debugging phase since the compiler will
find and flag many of the oversight errors we are so good at
introducing into our code. It was observed during the coding and
debugging phase of this project that in nearly every case, when the
program finally got through the compiler, the program would
actually run without bombing out the system. This is not always
the case using any standard procedural programming language.
YOUR PROGRAMMING PROJECT
_________________________________________________________________
This programming assignment is intended to give you a little
experience in working with a relatively large project as opposed
to the very small programs we have been working with in this
tutorial.
Add a suitcase to the game, to be found in the car at arrival, and
which must be checked in at the ticket counter prior to attempting
to get through airport security. This will not be trivial since
several classes will be affected. Some of the operations you will
have to do are listed below.
1. Add the noun "suitcase" and the verb "check" to the word list.
Of course, they must be entered at the right place in the
list.
2. Add the suitcase to the items class, including additional code
to each of its methods.
3. Initialize the items at location your_car to include the
suitcase.
4. Add an additional check when passing through security to check
that the player is not carrying the suitcase. You can add any
sort of penalty desired, including death by firing squad for
attempting such an obviously crooked deed.
5. You will need to add a check when the player finally gets on
his correct airplane to see that he checked his suitcase. If
he did not, you could output any desired text indicating
stupidity or forgetfulness.
Since I have not actually added the suitcase to the game and tested
it, I am not sure that this is all that will be required, but it
should be the majority of effort required. The bottom line of this
effort is that if you understand this program enough to perform
Page 12-9
Chapter 12 - Flyaway Adventure Game
this modification, you have a good understanding of how the program
works and how objects work together to perform a task.
Once you understand this program, you should define a programming
project for yourself that will use object oriented programming
techniques and begin designing and programming it. The best way
to learn to use OOP is to actually use it.
Good luck in your OOP endeavors.
Page 12-10
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -