July 2018
Intermediate to advanced
400 pages
12h 14m
English
Next, you will apply what you have learned to refine NyetHack. Open the project and the Tavern.kt file.
Tavern.kt contains duplicate chains of logic called on several collections: shuffled().first().
...
(0..9).forEach {
val first = patronList.shuffled().first()
val last = lastName.shuffled().first()
}
uniquePatrons.forEach {
patronGold[it] = 6.0
}
var orderCount = 0
while (orderCount <= 9) {
placeOrder(uniquePatrons.shuffled().first(),
menuList.shuffled().first())
orderCount++
...
This duplication indicates an opportunity to extract the duplicate logic to a reusable extension.
Define a new extension called random at the top of Tavern.kt:
Listing 18.11 Adding a private random extension (Tavern.kt ...
Read now
Unlock full access