Horse Racing (3)
- Modify your horse racing homework
and implement it with a class CHorse.
- Each horse has two (private) data members x and y.
- y - on which track is the horse running
- x - the position within the track
- The class CHorse has a static (P.397) public
data member: m_count.
It is used to count the number of horses on the screen.
So you'll need to increment it in your constructor (as in
Ex7_12.cpp).
- Define the following member functions:
- CHorse() - constructor
- If the constructor is invoked with no argument, the horse
will be randomly placed in a track (suppose we have 8 tracks).
- If a track number is specified in the argument, place the
horse in this track.
- If the number is larger than 8, you should substract 8
from it.
- For example, if the argument is 9, you should
place the horse in track 1.
- If the argument is 24, you should place the horse
in track 8.
- A newly created horse will always be placed at the starting
position of the track, which is defined as a constant START in
the program.
- Show()
- Display a horse according to its x,y value.
- When a horse is approaching the final destination,
change its color to red to indicate that it is sprinting.
- Hide()
- Erase a horse from its current position.
- Forward()
- Increase x if horses are running to the right;
decrease x if horses are running to the left.
- Final() - a Boolean function which determined whether this horse
reaches the final destination.
- Save your definition about the class CHorse in a file "horse.h".
Test it with a main program which creates horses
sequentially from 1 to 8. Save it as "horse-1.cpp" in the same
directory. Compile the
program by "g++ horse-1.cpp -lcurses". The program may run as follows.
- Test it with another main program which creates horses
randomly between 1 and 8. Save the main program as "horse-2.cpp" in the same
directory. Compile the
program by "g++ horse-2.cpp -lcurses". The program may run as follows.
- Now play it with this main program which is
more similar to a horse racing game.
Save the main program as "horse-5.cpp" in the same
directory. Compile the
program by "g++ horse-5.cpp -lcurses". The program may run as follows.