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.
draw(), moveTowards() and
attacks() virtual in the Unit class. Update
subclasses as appropriate. For this part of the lab, the
Unit class 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 draw() to Unit.cc which
does nothing (or, if you prefer, flags an error). Recompile and run.
Junk a subclass of Robot.
Override methods moveTowards() and
attacks() to behave appropriately for Junk.
(You'll need to add a constructor to the Robot
class which is called by Junk's constructor.)
Recompile and run.
isJunk() to the
Robot class. The function should return
bool. Update subclass Junk appropriately. Recompile.
GameState
class 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 junkAt() from
the GameState class.
GameState procedure by procedure once more
to be sure all required changes were made. Then, try compiling and
debugging.
Add a static variable count to Unit.cc by
placing the following line at the top of Unit.cc
static int count = 0;This variable will have file scope.
Edit Unit.cc so that every call to any of the three
Unit::Unit(...) constructors should increment
count. Make a virtual destructor
Unit::~Unit() whose only purpose is to decrement count.
(The destructor needs to be virtual since the class is
virtual.)
Recompile and run your program in gdb. After playing
two levels, hit C-c C-c in Emacs's
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
incantation = 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.
Unit class 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
Unit object can't exist on it's own; only subclasses of
the Unit's class exist.