Question: "Challenge: Configureable Status Format"


#1

Hello,

For this challenge are we supposed to use info we learned in the chapter, i.e. String templates or expressions, or do we need to use a “format string” like mentioned in the challenge? I’m not exactly sure where to start for this challenge; Is there anyone that can point me in the right direction?

Thanks,
Max


#2

Hey blkxltng,

In that exercise, you are intended to implement your own “format string” of the form “(HP)(A) -> H”.
That is, you provide a string like the one above and the player’s stats are used to print something along the lines of “(HP: 100)(Aura: Green) -> Madrigal is in excellent condition!”.

We don’t intend to enforce much about your implementation, but it will almost certainly give you an opportunity to put string templates/interpolation to work.


#3

I am a bit confused here. I assume that it should be possible to solve the exercise fully with the knowledge attained form the first three chapters. But in that case I am not sure how to tackle the problem since it seems to require some more advanced stuff. But I am not sure if I am over complicating what is expected of the answer here.


#4

rohdester,

That may not be a fair assumption for the challenges in the book. Sometimes, those challenges require a bit of extra digging into the Kotlin standard library or might benefit from some additional domain knowledge from other languages.

They’re not always doable in your first pass, and that’s okay. They don’t have bearing on later content, so feel free to come back to certain challenges when you feel better-equipped.


#5

Thx dgreenhalgh,

OK I see. That makes sense. By doing some digging I am sure I can solve it. I was just not sure whether that was “allowed” or if it should be doable within the first three chapters.

I will return with my solution :slight_smile:


#6

So this is my first stab at this. Please let me know if there is a more Kotlin-y way to do this.

// Player status
val statusFormatString = "(HP)(A) -> H"
val formatValueHP = "HP: $healthPoints"
val formatValueA = "Aura: $auraColor"
val formatValueH = "$name $healthStatus"
val formatValueB = if (isBlessed) "YES" else "NO"

var resultString = ""
var didReadAhead = false
for (index in statusFormatString.indices) {
    if (didReadAhead) {
        didReadAhead = false
        continue
    }

    val letter = statusFormatString[index]
    if (letter == 'A') {
        resultString += formatValueA
    } else if (letter == 'B') {
        resultString += formatValueB
    } else if (letter == 'H') {
        val nextIndex = index + 1
        if (nextIndex < statusFormatString.length && statusFormatString[nextIndex] == 'P') {
            resultString += formatValueHP
            didReadAhead = true
        } else {
            resultString += formatValueH
        }
    } else {
        resultString += letter
    }
}

println(resultString)

#8

I did the following making use a few CharSequence.replace() on statusFormatString.

Though there are only “H”, “HP” and “A” (ie. 3) to be replaced. I used 4 replaces. An extra one to replace “HP” to sth else avoiding the ‘H’ in “HP : nnn ” be replaced by “H” when replacing it with healthStatus.

Not pretty but will get the job done.

// Player Status

var hp = “HP: $healthPoints”
var aura = “Aura: $auraColor”
var health = “$name $healthStatus”

println (statusFormatString.replace(“HP”, “P”,false).replace(“H”,health,false).replace(“P”,hp,false).replace(“A”,aura,false))