October 2021
Intermediate to advanced
500 pages
16h 23m
English
Like lists and sets, maps are created using functions: mapOf and mutableMapOf. You will be using mapOf to create a map representing the amount of gold in various characters’ purses. For now, you will only track balances for Madrigal and Taernyl – you will come back to handle your randomly generated patrons later in this chapter.
Create your first map in Tavern.kt. (We will explain the argument syntax shortly.)
Listing 10.1 Creating a read-only map (Tavern.kt)
...
fun visitTavern() {
...
val patrons: MutableSet<String> = mutableSetOf()
val patronGold = mapOf(
TAVERN_MASTER to 86.00,
heroName to 4.50
)
while (patrons.size < 10) {
patrons += "${firstNames.random()} ${lastNames.random()}"
}
println(patronGold) ...Read now
Unlock full access