📄 chap12.htm
字号:
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Author" CONTENT="Gordon Dodrill">
<META NAME="GENERATOR" CONTENT="Mozilla/4.04 [en] (Win95; I) [Netscape]">
<TITLE>C++ Tutorial - Chapter 12</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<B>C++ Tutorial - Chapter 12</B>
<P><B><FONT SIZE=+3>F</FONT><FONT SIZE=+2>LYAWAY</FONT><FONT SIZE=+3> A</FONT><FONT SIZE=+2>DVENTURE</FONT><FONT SIZE=+3>
G</FONT><FONT SIZE=+2>AME</FONT></B>
<P>Now that you have learned lots of things about C++, and know how to
write and use a single isolated class, you have the problem of how to build
a program with several classes that work together to accomplish some task.
After some amount of thought, it seems that an adventure game is a good
candidate for a relatively large example program. It has lots of input
and output and requires a good deal of flexibility while running since
there are so many things that can be included in the game as obstacles,
mazes, items to find, and puzzles to solve.
<P>The adventure game presented in this chapter is unique as far as I know,
since I have never heard of another adventure game featuring an airport.
The location is not nearly as important as the code used to get through
the airport. You are advised to play the game to get familiar with what
the code does, then study the code to see how it works. Finally, you are
given an assignment to extend the code which will be the real test of whether
you understand its operation.
<P><B>PLAYING THE GAME</B>
<P>Example program ------> <B><A HREF="FLYAWAY.EXE">FLYAWAY.EXE</A></B>
<P>Prior to studying the source code for this game, it would be to your
advantage to spend some time playing the game to get familiar with what
the game does. Load the file FLYAWAY.EXE (available for downloading in
the same manner as the source files were downloaded) 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 15 files and will take a
little effort on your part to properly compile and link it, but that will
come later.
<P>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.
<P>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. 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 find it necessary to restart the game many times before
you arrive at your destination unscathed and on time.
<P><B>THE METHOD OF PLAY</B>
<P>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 move in a certain 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.
<P>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 for each room,
some of the clues for solving the puzzle are given in the clues issued
in response to a <B>look </B>command. Another important command is <B>inventory
</B>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.
<P>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 <B>drop </B>an item you possess, or <B>get </B>an item that is located
in the room you are currently in.
<P>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 contained in cppcrc.zip which you should have accessed
to obtain all of the example programs for this tutorial. 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. You may choose to do just that
to gain experience in working with C++.
<P><B>A FEW SPECIAL CONSTANTS</B>
<P>Example program ------> <B><A HREF="FLYAWAY.H">FLYAWAY.H</A></B>
<P>The file named FLYAWAY.H contains the definitions for TRUE and FALSE
as well as the 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.
<P>The <B>#ifndef</B> in line 4 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 FLYAWAY_H 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.
<P><B>THE FIRST CLASS - clock</B>
<P>Example program ------> <B><A HREF="CLOCK.H">CLOCK.H</A></B>
<P>Examine the file named CLOCK.H for the definition of the <B>clock </B>class.
This is the class for the game clock, and only one instance of this class
will be used. It will be used for the object <B>time_of_day</B> defined
in line 23 of FLYAWAY.CPP.
<P>The class is very simple, consisting of only two variables, the <B>hour
</B>and the <B>minute</B>, 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 in the file named CLOCK.CPP. 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 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. The <B>clock </B>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.
<P><B>INPUT COMMAND PARSING</B>
<P>Example program ------> <B><A HREF="WORDS.H">WORDS.H</A></B>
<P>The input command parsing routines are defined within the <B>words </B>class
and the code for the class is in WORDS.CPP. The code is straightforward
and simple to understand if you study it, so only a few comments will be
made about this class.
<P>The method <B>get_command()</B> reads two words from the keyboard by
calling the function <B>read_a_line()</B> and stores the words in the class
members <B>verb </B>and <B>noun</B>. It stores zero for either or both
of the words if it does not find a valid noun and a valid verb.
<P>Two methods are included to provide the verb or noun which was input
as the last user input. This allows any code that has visibility of the
object based on this class to find out what the player would like to do.
<P>There are four methods beginning with <B>is_</B> in this class 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. What they
do should be easy for you to understand, but it will take a little thought
on your part to see why these are needed in other parts of the code.
<P>Finally the simple method named <B>stop_game()</B> is used to set the
<B>verb </B>to the value of <B>quit </B>so the game will be ended by the
control logic in the main program FLYAWAY.CPP.
<P>All of the source code for the implementation is given in the file named
WORDS.CPP. Since this code is fairly simple and well commented, you will
be left on your own to study it to whatever depth you desire.
<P><B>THE SECOND CLASS - items</B>
<P>Example program ------> <B><A HREF="ITEMS.H">ITEMS.H</A></B>
<P>If you examine the files named ITEMS.H and ITEMS.CPP, you will find
the complete 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 <B>keys</B>,
the <B>candy</B>, the <B>ticket</B>, and the <B>money</B>. 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.
<P>The four items are stored in the class named <B>items </B>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.
<P>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 items, or they are located in a room somewhere.
<P>This file, like all other header files, is protected from multiple inclusion
by the <B>#ifndef</B> construct discussed earlier.
<P>This class is used in line 24 of FLYAWAY.CPP to define an object for
the player named <B>personal_items</B> which stores the list of items the
player is carrying around. It is also used in the class <B>location </B>as
an embedded or nested object to store the items that are located in each
of the 19 locations in the game.
<P>Once again, the implementation for this class is so simple that you
will have no difficulty in understanding it.
<P><B>THE FLIGHT AND GATE CLASS - schedule</B>
<P>Example program ------> <B><A HREF="SCHEDULE.H">SCHEDULE.H</A></B>
<P>Examine the example files named SCHEDULE.H and SCHEDULE.CPP for our
first example of a rather large class, the one that does the flight and
gate scheduling. You will find a large number of variables in this class,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -