Refactoring the Tavern

Now that you have a mechanism for expressing locations in NyetHack, it is a good time to revisit the Tavern. All the tavern’s behaviors are currently defined as top-level properties and functions. Spend a moment refactoring the tavern into a class that extends from Room.

Start by defining a new Tavern class inside your existing Tavern.kt file:

Listing 15.18  Creating a Tavern class (Tavern.kt)

...
private val menuItemTypes = menuData.associate { (type, name, _) ->
    name to type
}

class Tavern : Room(TAVERN_NAME) {
    override val status = "Busy"

    override fun enterRoom() {
    }
}

fun visitTavern() {
    ...
}
...

Next, copy and paste the implementation of visitTavern into Tavern’s enterRoom function:

Listing ...

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.