GitHub Repo

Often tiny changes can have very desirable effects. When that happens, it may be a sign that things are in decent shape.

Someone pointed out last night that this was a bit of a pain to type whenever one wanted to define an action:

action(Phrase("take", "water")) { imp->

And that this would be easier:

action("take", "water") { imp->

action is a function in Room, like this:

    fun action(phrase: Phrase, action: Action) {
        actionMap[phrase] = action
    }

And I can overload it like this:

    fun action(verb: String, noun: String, action: Action) {
        action(Phrase(verb,noun), action)
    }

And voila! The easier syntax works.

I love it when things come together. Creating a real game with this thing would be a somewhat tedious process, and a typical such game might have hundreds of rooms. So if we can make it a bit easier, it will pay off for game makers. I truly wish that I could make a playable game out of this, but I don’t quite see the path. I don’t think many people would want to fork my repo and compile the game just to play it …

I wonder whether it would be useful to create an enum for the rooms, as Bryan suggested. I guess I could just type one and see what would happen. Kind of a spike.

enum class R {
    spring, wellhouse, woods, `woods near cave`, `cave entrance`
}

Now let’s have a new version of go that can take an enum name.

    fun go(direction: String, roomName: R, allowed: (World)->Boolean = { _:World -> true}){
        go(direction, roomName.name, allowed)
    }

    fun go(direction: String, roomName: String, allowed: (World)->Boolean = { _:World -> true}) {
        moves += direction to Pair(roomName, allowed)
    }

And try typing some in:

enum class R {
    spring, wellhouse, woods, `woods toward cave`, `cave entrance`
}

    go("e", R.wellhouse)
    go("w", R.woods)
    go("s", R.`woods toward cave`)

Oh, that works nicely: after you type the direction “e”, when you start typing “w”, you get a list popup that includes all the R possibilities that start with “w”, and you can cursor to one, type more, whatever. And you could type a raw string if you preferred.

Kotlin’s willingness to let you define a variable name with backticks is rather nice for this purpose.

That’s nice enough that I’m going to keep it. Commit: Sample room name enum R provides prompts in go command.

An interesting side effect is that if you don’t use one or more of the enum names, you get warnings when you commit.

I’m starting to think that declaring all the room names might be worth doing, given the benefit of prompting.

Enough playing for this afternoon, let’s sum up.

Summary

Ideas from the peanut gallery have paid off. Overloading existing methods with easier parameter lists amounts to one-line improvements, and the creation of the enum for rooms was trivial as well. Calling it R might be seen as a bit cryptic, but it makes the actual code read rather nicely in my opinion.

Good ideas, ZoomGang, thanks! And thanks to you, dear reader, for tuning in! See you next time!