Combat in NyetHack
Adding combat to NyetHack will put to use all that you have learned about object-oriented programming.
Some rooms in NyetHack – the long corridor and dungeon – will contain a monster for your hero to vanquish in the most graphic way that you know how: by nullifying it.
Create a new kind of room called a MonsterRoom to define a room capable of holding a monster. Add a monster property of nullable type Monster? to the new MonsterRoom class and initialize it by assigning it a Goblin. Also, override Room’s description to let the player know whether the room has a monster to fight.
Listing 17.9 Defining rooms with monsters (Room.kt
)
open class Room(val name: String) { protected open val status = "Calm" open
Get Kotlin Programming: The Big Nerd Ranch Guide, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.