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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

DungeonItem hierarchy 1031• has a CanDetect() function that returns true there is a clear line <strong>of</strong> sightbetween it <strong>and</strong> the Player object;• has an Advance() function that causes the W<strong>and</strong>erer to move one step alongthe line <strong>of</strong> sight path toward the current position <strong>of</strong> the Player;• has a NormalMove() function that causes it to try to move in a constantdirection until blocked by a wall, when its movement is blocked, it picks a newdirection at r<strong>and</strong>om;• has a small initial "health" rating;• inflicts a moderate amount <strong>of</strong> damage when making a direct attack on anadjacent player.A W<strong>and</strong>erer will need to remember its current direction <strong>of</strong> movement so that it cankeep trying to go in the same direction. Integer data members, representing thecurrent delta-x, delta-y changes <strong>of</strong> coordinate, could be used.<strong>The</strong>re are similarities between the Monster <strong>and</strong> Player classes. Both classesdefine things that have a Pt coordinate, a health attribute <strong>and</strong> a strength attribute.<strong>The</strong>re are similarities in behaviours: both the Player <strong>and</strong> the Monsters read initialdata from file, get told that they have been hit, get asked whether they are still alive,get told to draw themselves (<strong>and</strong> erase themselves), have a "move" behaviour thatinvolves erasing their current display, changing their Pt coordinate <strong>and</strong> thenredrawing themselves. <strong>The</strong>se commonalities are sufficient to make it worthdefining a new abstraction, "active item", that subsumes both the Player <strong>and</strong> theMonsters.This process <strong>of</strong> abstracting out commonalities can be repeated. <strong>The</strong>re aresimilarities between class Collectable <strong>and</strong> the new class ActiveItem. Both arethings with Pt coordinates, draw <strong>and</strong> erase behaviours; they respond to queriesabout where they are (returning their Pt coordinate). <strong>The</strong>se common behaviourscan be defined in a base class: DungeonItem.<strong>The</strong> DungeonItem class hierarchy used in the implementation is shown in Figure<strong>29</strong>.4.Commonalitiesbetween class Player<strong>and</strong> class Monster<strong>29</strong>.2.3 Finalising the classesCompletion <strong>of</strong> the design stage involves coming up with the class declarations <strong>of</strong>all the classes, possibly diagramming some <strong>of</strong> the more complex patterns <strong>of</strong>interaction among instances <strong>of</strong> different classes, <strong>and</strong> developing algorithms for anycomplicated functions.Class Dungeon<strong>The</strong> finalised declaration for class Dungeon is:

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

Saved successfully!

Ooh no, something went wrong!