Category : C Source Code
Archive   : CPTUTOR1.ZIP
Filename : CHAP12.TXT

 
Output of file : CHAP12.TXT contained in archive : CPTUTOR1.ZIP



Chapter 12
FLYAWAY ADVENTURE GAME

PLAYING THE GAME
_________________________________________________________________

Prior to studying the source code for this game, ===============
it would be to your advantage to spend some time FLYAWAY.EXE
playing the game to get familiar with what the ===============
game does. Load the file FLYAWAY.EXE and begin
the adventure through the airport. The
executable file is precompiled for you so you can begin executing
the program before you have to compile and link the whole thing.
The entire program is composed of 12 files and will take a little
effort on your part to properly compile and execute it, but that
will come later.

If you have played adventure games before, sometimes called
interactive fiction, you should begin trying various commands to
find your way through the airport to your proper plane. If you
have not played before, a few hints are in order concerning how to
play the game.

The object of the game is to get to your proper plane on time so
you can fly away to your vacation. Of course there a few obstacles
and problems along the way and they will be brought up at the
appropriate time, and it will be up to you to solve the puzzles
associated with each problem. To add a little excitement, you only
have about twenty-five minutes to get to your plane, with each move
taking a minute, so you must hurry. Of course, just getting to the
plane on time is not enough, there are a few additional
requirements. You will find what they are as you progress through
the game. You will probably be required to restart the game many
times before you arrive at your destination unscathed and on time.


THE METHOD OF PLAY
_________________________________________________________________

The method of play is extremely simple. You simply wander around
the airport looking for things to do and places to go. You move
around the airport by giving the system commands to go in a
direction with four choices available, north, south, east, or west.
You can abbreviate any of these four direction commands to the
first letter only, and you can use either upper or lower case. The
system may move you to another area of the airport, or it may tell
you that you can't go that way. Try loading the game now and
typing, the four directions once each to see what happens. If this
is not clear, enter the word help to get you started.

In addition to moving around, you can pick up items or ask for more
information in any of the rooms. Try telling the system to look
around the room and see what additional information it gives you

Page 12-1

Chapter 12 - Flyaway Adventure Game

for each room, some of the clues for solving the puzzle are given
in the clues issued in response to a look command. Another
important command is inventory which will give you a list of the
items you possess at any given point in time. Type the word
inventory at this time to see what items you possess.

The remainder of the commands consist of two words, a verb and a
noun. These can be given in either order, since the system is
smart enough to know the difference, and additional words may be
given following the legal words. If you give the system a command
that is not in its limited vocabulary, it will tell you it doesn't
understand that word. Try telling the system to drop an item you
possess, or get an item that is located in the room you are
currently in.

Several friends have played this game with no more knowledge than
you have been given. One solved it in 40 minutes, but most took
about an hour to complete the game. After you play the game for
awhile, return to the text and we will study the source code for
the game. The entire source code for the game is on your
distribution disk. The game was purposely kept small so the code
could be easily grasped by a programming student. There is no
reason the game could not have been made much larger by the
addition of more rooms, items, and traps.


THE FIRST CLASS - clock
_________________________________________________________________

Examine the file named CLOCK.HPP for the ===============
definition of the clock class. This is the CLOCK.HPP
class for the game clock, and only one instance ===============
of this class will be used. It will be used for
the object time_of_day defined in line 25 of
FLYAWAY.CPP.

The class is very simple, consisting of only two variables, the
hour and the minute, and four methods. The first method is the
constructor used to initialize the clock to 8:51 as you can see if
you refer to the implementation of this class. The next two
methods are used to get the current values of the two variables.
The final method is much more interesting since it does much more.
It updates the time of day clock and outputs the user prompt to ask
for the next command. This may not be the best place to output the
user prompt since this class is devoted to the time of day and
associated operations, but this was chosen as the place to do it
since the time of day is part of the user prompt. You will notice
that the clock was initialized to 8:51, but the first time output
was 8:52 when you played the game. In order to simplify the coding
later, when we need to decide if we made it to the plane on time,
the time was incremented at the beginning of each game move. The
time is therefore the same when the command is entered and when it
is executed, hence the time is incremented prior to even the first
output.

Page 12-2

Chapter 12 - Flyaway Adventure Game


The clock class is by far the simplest class in the adventure game
and should be simple for you to understand. After you are sure you
understand it, we will go on to the next class.



THE SECOND CLASS - items
_________________________________________________________________

If you examine the files named ITEMS.HPP and ===============
ITEMS.CPP, you will find the complete ITEMS.HPP
definitions of the handling of the items that ===============
you carried around the airport in the game.
There were exactly four transportable items that
could be located in each room or carried by yourself, the keys, the
candy, the ticket, and the money. The keys and the money keep you
from getting through security and the ticket and candy are required
to get you safely on the plane and enroute to your destination.

The four items are stored in the class named items in the form of
TRUE or FALSE since that is the only required indication. A TRUE
means the item is located here, and a FALSE means the item is not
here. The values of TRUE and FALSE are defined in FLYAWAY.H.
Finally, there are six methods to operate on these items.

The first method is a constructor to set all items to FALSE, and
the next two are used to either get a specific item, or drop one.
The fourth method is used to tell us if the item is located here
and the last two are used to tell us what items are on hand in this
location. You will notice that the final two are different because
different text was desired depending on whether you are carrying
the item, or it is located in a room somewhere.

The #ifndef in line 5 is required because this header file is
included in many of the other files and if it is included more than
once, there will be a multiple definition, and hence an error. A
class only needs to be defined once, so after it is defined by one
of the includes, the name ITEMSHPP will be defined and any other
defines will be ignored. This is necessary because of the separate
compilation capability of C++. This was described in more detail
near the end of chapter 7.

The #ifndef in the class clock was not required but was included
in order to make all class definitions the same.

This class is used in line 48 of FLYAWAY.CPP to define an object
for the player named personal_items which stores the list of items
the player is carrying around. It is also used in the class
location as an embedded or nested object to store the items that
are located in each of the 19 locations in the game.




Page 12-3

Chapter 12 - Flyaway Adventure Game

THE FLIGHT AND GATE CLASS - schedule
_________________________________________________________________

Examine the files named SCHEDULE.HPP and ================
SCHEDULE.CPP for our first example of a rather SCHEDULE.HPP
large class, the one that does the flight and ================
gate scheduling. You will find a large number
of variables in this class, and eight methods to
handle the variables. Instead of a detailed description of each
variable and method, we will only give a brief overview of the
class.

Only one object of this class is declared named flight_info in line
26 of the program named FLYAWAY.CPP. The constructor initializes
the flight possibilities, and the method named shuffle_gates()
shuffles all gates around if the player arrives at his correct gate
without reading the monitor in the waiting area. Once the monitor
in the waiting area is read, the flights_frozen variable is made
TRUE. Likewise, the players destination is changed every play by
the method named shuffle_flights() until the player reads his
ticket invoking the method named list_actual_destination().

This class contains the methods to list the data seen on the
monitor, as well as the data seen when invoking the command look
at one of the gates. Finally, this class contains the method named
check_flight() which searches through the list of requirements to
see if the player has completed all requirements to successfully
reach the final destination for his vacation.

You will notice that several of the location objects were required
to be available within this code and are listed as extern in lines
9 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 23 is where the global variable is defined and
initialized, then in line 74 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 237
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.



THE MOST USED CLASS - location
_________________________________________________________________

The file named LOCATION.HPP is the header file ================
for the class named location. It is the class LOCATION.HPP
that controls all of the moves from location to ================
location.


Page 12-4

Chapter 12 - Flyaway Adventure Game

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 go in one of the four
directions from the current location, moreover they are pointers
to those four locations. Next we have pointers to two different
character strings associated with this room. Finally in line 24
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. Note that a constructor could
have been used even though we would have to refer to some items
prior to their definition because it is permissible to refer to a
pointer to an object before it is declared. 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 81 through 212 of the main
program FLYAWAY.CPP, 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 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.H for a complete ===============
listing of the messages output to the monitor MESSAGE.H
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 main file. It does not reduce the compile time since

Page 12-5

Chapter 12 - Flyaway Adventure Game

these messages are not separately compiled. They are included into
the file and compiled each time the main file FLYAWAY.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.


THE PROTOTYPES FOR THE STANDARD C CODE
_________________________________________________________________

The file named FLYAWAY.H contains the ===============
definitions for TRUE and FALSE as well as the FLYAWAY.H
enumerated type defining the legal dictionary of ===============
words for use in playing the game. The list was
started at a value of 1 so the value of zero can
be used to indicate that the word in question was not in the
library and hence not a legal word for use with the game.

Finally, this file contains the prototypes for all of the functions
that are used in a normal C fashion rather than being parts of an
object. As much of this problem as was felt practical was broken
up into objects and the remainder was programmed using standard C
functions. You will find that as you develop object oriented
techniques, you will not program all of any project with objects
but will finally relegate part of it to your old favorite methods.

This file, like all other header files, is protected from multiple
inclusion by the #ifndef construct discussed earlier.


INPUT COMMAND PARSING
_________________________________________________________________

The input command parsing routines were not ===============
defined as objects but were written as standard COMMAND.CPP
C++ code to illustrate that all of a program ===============
need not be handled as objects. These routines
were written in a separate file to indicate that
it is legal and possible to do so in a program that is primarily
an object oriented program.

The code is straightforward and simple to understand if you study
it, so only a few comments will be made about this file. The
function get_command() reads two words from the keyboard by calling
the function read_a_line() and returns the words if they are a
valid verb and noun, otherwise it returns zeros for the two words.

There are four functions at the end of this file that are used to
determine if a word is a verb, a noun, a direction, or an
operation. These are called upon from various places within the
program. They should be easy for you to understand.




Page 12-6

Chapter 12 - Flyaway Adventure Game

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 initialize() and the output of a few lines of text, we
are into 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 get the quit because it is generated
internally in some cases such as at end of game.

The loop itself consists of 6 function or method calls. The first
method is called to update the time of day clock and output the
players prompt in line 66. Next we call the function get_command()
to get the players input command. We perform the required action
in line 68 by calling perform_action() which we will describe in
a few paragraphs. Finally, we send three messages to the object
named flight_info to shuffle the flights and gates and to check if
the player has reached one of the gates. Remember that within each
of the methods we perform checks to see if we need to do the thing
requested in the message and either perform the action or simply
return to the caller or message sender.



THE WORKING FUNCTION
_________________________________________________________________

The only function we have not mentioned yet is the one that does
all of the work, the function named perform_action() which begins
in line 226. This function simply 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 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 function in line 338, we have the
default case if nothing else was attempted. 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.





Page 12-7

Chapter 12 - Flyaway Adventure Game

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. 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.
































Page 12-8


  3 Responses to “Category : C Source Code
Archive   : CPTUTOR1.ZIP
Filename : CHAP12.TXT

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/