Last active
February 14, 2018 15:40
-
-
Save joshskeen/9625795f3735f564f58a4eb62db1dedc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File | |
import java.util.Random | |
var playerGold = 10 | |
var playerSilver = 10 | |
var firstNames = listOf("Little Eli", "Gwenevere", "Mookie", "Julius") | |
var lastNames = listOf("Aurelius", "Jenkins", "Falteroy", "Skeen") | |
var patronGold: MutableMap<String, Double> = mutableMapOf() | |
fun main(args: Array<String>) { | |
val drinksData = File("data/drinkdata.csv").readText() | |
val availableDrinks = drinksData.split("\n").map { it.split(",") } | |
val patrons = generatePlayers() | |
// first we show you more of a java style to build the map, requiring a mutable map | |
patrons.map { patron -> | |
patronGold.put(patron, randomGoldAmount()) | |
} | |
// then we show another (better) way you could construct the same map and the map can be made immutable | |
// patronGold = patrons.map { Pair(it, randomGoldAmount()) }.toMap() | |
// the bank report shows one utility of the map: the patron's name associated with the price, displayed | |
// as a "patron bank report" | |
patronBankReport(patronGold) | |
//then we build a map of drink prices to drink names (that were loaded from csv). | |
// as a challenge/bonus, we might sort the map by price (.toSortedMap()) | |
val drinksWithPricesPairs = availableDrinks.map { it[2].toDouble() }.zip(availableDrinks.map { it[1] }) | |
val drinksWithPricesMap = drinksWithPricesPairs.toMap() | |
//the exercise is complete when they display for each patron the drinks that patron can afford | |
patronGold.forEach { patron, gold -> | |
//the most elegant solution: filter | |
println("$patron has $gold and can afford the following drinks: ") | |
pricesForDrinks.filter { it.key <= gold }.forEach { price, drink -> | |
println("$drink, $price gp") | |
} | |
} | |
} | |
private fun randomGoldAmount() = 2.0 + (10.0 - 2.0) * Random().nextDouble() | |
fun patronBankReport(patronGold: Map<String, Double>) { | |
println("---Patron Bank Report---") | |
patronGold.forEach { name, gold -> | |
println("name: $name, gold: $gold") | |
} | |
} | |
fun generatePlayers(): List<String> { | |
val patrons = (1..20).map { | |
firstNames.shuffled().first() + " " + lastNames.shuffled().first() | |
} | |
//an alternative - which uses set and converts back to list under the hood :D | |
//.distinct() | |
println(patrons) | |
val uniquePatrons = patrons.toSet().toList() | |
return uniquePatrons | |
} | |
//spirit,deluxe seadog,7.991 | |
private fun orderDrink(character: String = "Madrigal", drinkData: List<String>) { | |
val drinkType = drinkData[0] | |
val drinkName = drinkData[1] | |
val drinkPrice = drinkData[2] | |
//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("$character consumes a $drinkName.") | |
println("$character exclaims: " + 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