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 1053}}}return fPlayer->Alive();(Note the need for type casts when getting members <strong>of</strong> the collections; the functionDynamicArray::Nth() returns a void* pointer.)<strong>The</strong> CreateWindow() function creates a Window object <strong>and</strong> sets its backgroundfrom the map.void Dungeon::CreateWindow(){fDWindow = new Window(1, 1, fWidth, fHeight);for(int row = 1; row PrepareContent();fDWindow->ShowAll();}Class Dungeon has several trivial access functions:int Dungeon::Accessible(Pt p) const{return (' ' == fDRep[p.Y()-1][p.X()-1]);}Window *Dungeon::Display() { return fDWindow; }Player *Dungeon::Human() { return fPlayer; }int Dungeon::ValidPoint(Pt p) const{int x = p.X();int y = p.Y();// check x rangeif((x = fWidth)) return 0;// check y rangeif((y = fHeight)) return 0;// <strong>and</strong> accessibilityreturn Accessible(p);}<strong>The</strong>re are similar pairs <strong>of</strong> functions M_at_Pt() <strong>and</strong> PI_at_Pt(), <strong>and</strong>RemoveM() <strong>and</strong> RemoveProp() that work with the fInhabitants list <strong>of</strong> Monsters<strong>and</strong> the fProps list <strong>of</strong> Collectables. Examples <strong>of</strong> the implementations areCollectable *Dungeon::PI_at_Pt(Pt p){int n = fProps.Length();for(int i=1; iWhere();if(w.Equals(p)) return pi;

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

Saved successfully!

Ooh no, something went wrong!