13.07.2015 Views

29 The Power of Inheritance and Polymorphism

29 The Power of Inheritance and Polymorphism

29 The Power of Inheritance and Polymorphism

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Implementation: class Dungeon 1055int Dungeon::ClearSemiVertical(Pt p1, Pt p2, int max,Pt path[]){int ychange = p2.Y() - p1.Y();if(abs(ychange) > max) return 0;int xchange = p2.X() - p1.X();int deltax = (xchange > 0) ? 1 : -1;int deltay = (ychange > 0) ? 1 : -1;float slope = ((float)xchange)/((float)ychange);float error = slope*deltay;}int x = p1.X();int y = p1.Y();for(int i=0;i0.5) {x += deltax;error -= deltax;}error += slope*deltay;y += deltay;Pt p(x, y);if(!Accessible(p)) return 0;path[i] = p;if(p.Equals(p2)) return 1;}return 0;<strong>29</strong>.3.3 DungeonItemsDungeonItemClass DungeonItem implements a few basic behaviours shared by all variants. Itsconstructor sets the symbol used to represent the item <strong>and</strong> sets the link to theDungeon object. <strong>The</strong> body <strong>of</strong> the destructor is empty as there are no separateresources defined in the DungeonItem class.DungeonItem::DungeonItem(Dungeon *d, char sym){fSym = sym;fD = d;}DungeonItem::~DungeonItem() { }<strong>The</strong> Erase() <strong>and</strong> Draw() functions operate on the Dungeon object's main mapWindow. <strong>The</strong> call fd->Display() returns a Window* pointer. <strong>The</strong> Windowreferenced by this pointer is asked to perform the required operation.void DungeonItem::Erase(){

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!