在完全 Kotlin noob 的基于文本的游戏中引用/访问 class 中具有不同变量(字符串和集合)的 MutableSets-Items-name

Referencing / Accessing a MutableSets-Items-name in a class with different variables(Strings and Sets) in a textbased game of a total Kotlin noob

关于 Kotlin,我完全是个菜鸟,但我认为它使用起来很有趣,而且在我的空闲时间,我正在努力学习它。我买了一些书并浏览了它们,并以此为基础,为了获得更多实践经验,我正在尝试在 Kotlin JVM 中创建一个基于文本的小冒险,比如 Zork。应该有不同的房间,每个房间应该有一些物品,可以查看和拿走(稍后想实现你可以使用它们)。我的问题是:用户应该在控制台中输入“take {name of item in that room}。但我无法引用地窖可变列表中的项目名称。所有 internethelp 只有一些简单的例子,所以我找不到解决这个问题的办法。我只能让控制台做“room.name”,然后他给了我房间的名字。但我不想得到房间的名字,我想得到那个房间里物品的名称。有人可以帮我吗?

    val name: String,
    val description: String,
    val items: MutableSet<Item>,
    val interior: MutableSet<Interior>)
    {
    fun look(){
        println(description)
    }
}
data class Item(val name: String, val description: String)
data class Interior(val name: String, val description: String)
fun main() {
    println("You are in a cellar. What do you do? Type 'help', if you need help.")
    val inventory: MutableSet<Item> = mutableSetOf()
    val key = Item("key", "description of the key")
    val stone = Item("stone", "A stone.")
    val door = Interior("door", "description of the door")
    val cellar = Room(
        name = "Cellar",
        description = "A dark cellar",
        items = mutableSetOf(key, stone),
        interior = mutableSetOf(door)
    )
    var isValidChoice = false
    while (!isValidChoice){
    when (val userInput = readLine()) {
        "look" -> cellar.look()
        "look at ${
            (cellar.items!!!}" -> println(cellar.items)  <------------------Problem!! How to access the names of the items of the mutableSet in general?
        "take ${cellar.items}" -> {
            inventory.add(key)
            println("You took the ${key.name}.")
        }
        "take ${stone.name}" -> {
            inventory.add(stone)
            println("You took the ${stone.name}.")
        }
        "inventory" -> {
            if (inventory.isEmpty()) {println("Your inventory is empty.")}
            else
                println("Inventory:")
                for (item in inventory) {
                    print(" <${item.name}>")
                }
            }
        "help" -> {
            println("You can type 'look', 'look at <ITEM>', 'take <ITEM>', 'inventory'")
        }
        "end game" -> isValidChoice = true
        else -> {
            println("Don't know how to '$userInput' something. Try again.")
        }
    }
    }
}```

您可以为此使用过滤器,

cellar.items.filter { it.name == <user_input>} // will give you list of items match with given name

或者您可以映射以获取像

这样的名称
cellar.items.map{ it.name } // will give you list of names, then do contains or indexOf