Skip to content

Instantly share code, notes, and snippets.

@joshskeen
Created February 13, 2018 21:57
Show Gist options
  • Save joshskeen/78a31129996a03aae2de66142e927f9f to your computer and use it in GitHub Desktop.
Save joshskeen/78a31129996a03aae2de66142e927f9f to your computer and use it in GitHub Desktop.
import kotlin.math.roundToInt
var playerGold = 10
var playerSilver = 10
fun main(args: Array<String>) {
orderDrink("ale,dragon's breath,5.932")
}
private fun orderDrink(drinkData: String) {
val indexOfClosingBracket = drinkData.indexOf("]")
//type
val drinkType = drinkData.substringBefore(",")
//price
val drinkPrice = drinkData.substringAfterLast(",")
val indexOfFirst = drinkData.indexOfFirst { it == ',' }
val indexOfLast = drinkData.indexOfLast { it == ',' }
//drinkName
val drinkName = drinkData.substring(indexOfFirst + 1 until indexOfLast)
//a faster way to show this would be to use .split - also would be more real world..and frankly
// possibly easier to understand as well with a little bit of explanation that we'll see all the details of list
println("SimTavern -> orderDrink type: $drinkType , name: $drinkName , price: $drinkPrice")
val bartenderRemarks =
"Ah, a delicious $drinkType...nothing satisfies like a $drinkName."
println("Bartender: $bartenderRemarks")
purchaseDrink(drinkPrice.toDouble())
println("You consume a $drinkName.")
println("You exclaim: " + l33tSpeak("${drinkName}s are delicious!"))
}
fun purchaseDrink(price: Double) {
displayBalance()
// exchange rate: 1 gold = 100 silvers. if a player buys a drink costing 5.932,
// it should result in a remaining balance of 4.17 after rounding .16999 to .17 (after modulus)
val balanceTotal = playerGold + (playerSilver / 100.0)
println("total purse, converted to gold: " + balanceTotal)
val remaining = balanceTotal - price
println("total purse remaining, in gold: ${String.format("%.2f", remaining)}")
val remainingGold = remaining.toInt()
val remainingSilver = (remaining % 1 * 100).roundToInt() //any way to skip the round to int?
playerGold = remainingGold
playerSilver = remainingSilver
displayBalance()
}
//FMC : deal with a negative balance
private fun displayBalance() {
println("player's account balance: gold: $playerGold , silver: $playerSilver")
}
private fun l33tSpeak(nameOfDrink: String) =
nameOfDrink.replace(Regex("[aeiou]")) {
when (it.value) {
"a" -> "4"
"e" -> "3"
"i" -> "1"
"o" -> "0"
else -> it.value
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment