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.

<strong>Inheritance</strong> <strong>and</strong> <strong>Polymorphism</strong> 1020width <strong>and</strong> height (e.g 70 20)several (20) lines <strong>of</strong> (70) characters, e.g.##################### … … ############ # # … … # ## ######## # # … … ####### #dungeon itemsh 30 18 …human (i.e. player), coords, other dataw 2 2 10 … w<strong>and</strong>ering monster, coords, ……$ 26 6 0 30 0 collectable item, coords, valuesqend markAny additional details can be resolved once the objects have been bettercharacterized.class DungeonConsideration <strong>of</strong> the main() function identified two behaviours required <strong>of</strong> theDungeon object: loading a file, <strong>and</strong> running the game.<strong>The</strong> Dungeon::Load() function will be something along the following lines:Dungeon::LoadOpen file with name givenLoad mapLoad other dataDungeon:: load mapread sizeloop reading lines <strong>of</strong> characters that definethe rows <strong>of</strong> the mapDungeon:: load other dataread type characterwhile character != 'q'create object <strong>of</strong> appropriate typetell it to read its own dataif it is a monster, add to monster collectionif it is a collectable, add to collectableitems collectionif player, note it (make sure no existing player)check that the data defined some items to collect<strong>The</strong> Dungeon::Run() function could have the following general form:Dungeon::Run()Finalize setting up <strong>of</strong> displaysDraw initial statewhile(player "alive")player "run"

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

Saved successfully!

Ooh no, something went wrong!