cp -r ~mc38/labs/robots-polymorph/robots/ robotsCompile and play the game once to observe its behavior.
Next, familiarize yourself with the code. First, skim the README file. I recommend you look at the Unit class, the GameState class and the Game class.
attacks()virtual in the
Unitclass. Update subclasses as appropriate. For this part of the lab, the
Unitclass need not be pure-virtual. Just leave the most common case for these procedures here. Subclasses will only need to override these definitions if they need to. You'll need to add a procedure
Unit.ccwhich does nothing (or, if you prefer, flags an error). Recompile and run.
Junka subclass of
Robot. Override methods
attacks()to behave appropriately for
Junk. (You'll need to add a constructor to the
Robotclass which is called by
Junk's constructor.) Recompile and run.
Robotclass. The function should return
bool. Update subclass
GameStateclass so that only one vector of
vector<Robot *>is maintained. (There won't be a vector of Junk.) Make the appropriate changes. Most changes are simple. More significant changes to the logic are required in the constructor and in
countCollisions(). The latter procedure can be vastly simplified to make no reference to
junkAt(). In fact, you should be able to remove the procedure
GameStateprocedure by procedure once more to be sure all required changes were made. Then, try compiling and debugging.
Add a static variable
placing the following line at the top of
static int count = 0;This variable will have file scope.
Unit.cc so that every call to any of the three
Unit::Unit(...) constructors should increment
count. Make a
Unit::~Unit() whose only purpose is to decrement count.
(The destructor needs to be
virtual since the class is
Recompile and run your program in
gdb. After playing
two levels, hit
C-c C-c in
gdb buffer, set a breakpoint in one of the
Unit classes, continue, and (lastly) inspect the
variable. Is the value reasonable? If it keeps growing as you go
from level to level, your have a memory leak.
This won't solve the memory leak mentioned above, but it will reduce it. To completely solve it, you'd need to write an appropriate copy constructors, destructor and operator= for the class.
You should not treat this as an opportunity to take the afternoon off. If you're done with the day's lab, try to learn something new!
A pure virtual class declares virtual functions but does not define
them. The virtual function declaration needs to include the bizarre
= 0 as follows:
virtual void draw() = 0; // Declaration in Unit.hThis says, ``I have no intention an object of class Unit, only objects of subclasses of class Unit. Subclasses must define the procedure
draw().'' You can still accept
Unit's as parameters by reference or by pointer.
Unitclass pure virtual by making any changes you feel appropriate. You may need to add a new class, since currently the click is a
Unit. Note that
Unit's can only be passed as arguments through reference parameters (or pointers), since a
Unitobject can't exist on it's own; only subclasses of the
Unit's class exist.