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.

EXERCISES1 Complete <strong>and</strong> run the dungeon game program.2 This one is only for users <strong>of</strong> Borl<strong>and</strong>'s system.Why should the monsters wait while the user thinks? If they know what they want to do,they should be able to continue!<strong>The</strong> current program requires user input in each cycle <strong>of</strong> the game. If there is no input,the program stops <strong>and</strong> waits. <strong>The</strong> game is much more interesting if this wait is limited.If the user doesn't type any comm<strong>and</strong> within a second or so, the monsters should gettheir chance to run anyway.This is not too hard to arrange.First, the main while() loop in Dungeon::Run() should have a call WindowRep::Instance()->Delay(1). This results in a 1 second pause in each cycle.<strong>The</strong> Player::Run() function only gets called if there have been some keystrokes. If thereare no keystrokes waiting to be processed, the Dungeon::Run() function skips to the loopthat lets each monster have a chance to run.All that is required is a system function, in the "console" library package, that allows aprogram to check whether input data are available (without "blocking" like a normal readfunction). <strong>The</strong> Borl<strong>and</strong> conio library includes such a function.Using the on-line help system in the Borl<strong>and</strong> environment, <strong>and</strong> other printeddocumentation, find how to check for input. Use this function in a reorganized version<strong>of</strong> the dungeon program.(You can achieve the same result in the Symantec system but only by utilisingspecialized system calls to the "Toolbox" component <strong>of</strong> the Macintosh operating system.It is all a little obscure <strong>and</strong> clumsy.)3 Add multiple levels to the dungeon.(<strong>The</strong>re are various ways that this might be done. <strong>The</strong> easiest is probably to define a newclass DungeonLevel . <strong>The</strong> Dungeon object owns the main window, the Player, <strong>and</strong> a list<strong>of</strong> DungeonLevel objects. Each DungeonLevel object owns a map, a list <strong>of</strong> collectables,<strong>and</strong> a list <strong>of</strong> monsters. You will need some way <strong>of</strong> allowing a user to go up or downlevels. When you change level, the new DungeonLevel resets the background map inthe main window <strong>and</strong> arranges for all data to be redrawn.)4 Add more challenging Monsters <strong>and</strong> "traps".(Use your own imagination.)

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

Saved successfully!

Ooh no, something went wrong!