简体   繁体   中英

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

I am a total noob when its about Kotlin, but I think it is fun to use and in my free time, I am trying to learn it. I got some books and went through them and based on that, to get more practical experiences, I am trying to create a little text-based adventure in the Kotlin JVM, like Zork. There should be different rooms and each room should have some items, that can be looked at and taken (later wanna implement that you can use them). My Problem is: The user should input "take {name of item in that room} in the console. But i am unable to reference the name of the item of the mutable list of the cellar. All internethelp only have some easy examples, so I couldnt find a solution to this. All i can let the console do is "room.name" so then he gives me the name of the room. But i dont want to get the name of the room, i want to get the name of the Item in that room. Can somebody please help me?

    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.")
        }
    }
    }
}```

You can use filter for this,

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

or you may map to get names like

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM