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.

Finalising the clases 1033intprivate:intintintintvoidvoidvoidClearLineOfSight(Pt p1, Pt p2, int max,Pt path[]);ClearRow(Pt p1, Pt p2, int max, Pt path[]);ClearColumn(Pt p1, Pt p2, int max, Pt path[]);ClearSemiVertical(Pt p1, Pt p2, int max,Pt path[]);ClearSemiHorizontal(Pt p1, Pt p2, int max,Pt path[]);LoadMap(ifstream& in);PopulateDungeon(ifstream& in);CreateWindow();};DynamicArrayDynamicArrayPlayercharWindowintintfProps;fInhabitants;*fPlayer;fDRep[MAXHEIGHT][MAXWIDTH];*fDWindow;fHeight;fWidth;<strong>The</strong> Dungeon object owns the map <strong>of</strong> the maze (represented by its data elementsfDRep[][], fHeight, <strong>and</strong> fWidth). It also owns the main map window(fDWindow), the Player object (fPlayer) <strong>and</strong> the collections <strong>of</strong> Monsters <strong>and</strong>Collectables. Data members that are instances <strong>of</strong> class DynamicArray are usedfor the collections (fInhabitants for the Monsters, fProps for theCollectables).<strong>The</strong> Load() <strong>and</strong> Run() functions are used by the main program. FunctionLoad() makes uses <strong>of</strong> the auxiliary private member functions LoadMap() <strong>and</strong>PopulateDungeon(); these read the various data <strong>and</strong> create Monster,Collectable, <strong>and</strong> Player object(s) as specified by the input. <strong>The</strong> auxiliary privatemember function CreateWindow() is called from Run(); it creates the mainwindow used for the map <strong>and</strong> sets its background from information in the map.Access functions like Display() <strong>and</strong> Human() allow other objects to getpointers to the main window <strong>and</strong> the Player object. <strong>The</strong> ActiveItem objects thatmove are going to need access to the main Window so as to tell it to clear <strong>and</strong> setthe character that is to appear at a particular point.<strong>The</strong> ValidPoint() function checks whether a given Pt is within the bounds <strong>of</strong>the maze <strong>and</strong> is not a "wall".<strong>The</strong> functions M_at_Pt() <strong>and</strong> PI_at_Pt() involve searches through thecollections <strong>of</strong> Monsters <strong>and</strong> Collectables respectively. <strong>The</strong>se function return thefirst member <strong>of</strong> the collection present at a Pt (or NULL if there are no objects at thatPt). <strong>The</strong> Remove… function eliminate members <strong>of</strong> the collections.Class Dungeon has been given responsibility for checking whether a "clear line<strong>of</strong> sight" exists between two Pts (this function is called in both W<strong>and</strong>erer::CanDetect() <strong>and</strong> Patrol::CanDetect()). <strong>The</strong> function takes as arguments thetwo points, a maximum range <strong>and</strong> a Pt array in which to return the Pts along theWhat does aDungeon own?What does aDungeon do?

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

Saved successfully!

Ooh no, something went wrong!