AI-Powered Virtual Interior Designer with Room Layout Suggestions,Kotlin

👤 Sharing: AI
```kotlin
import kotlin.random.Random

// Data classes to represent furniture and room dimensions
data class Furniture(val name: String, val width: Double, val length: Double, val type: String) {
    override fun toString(): String {
        return "$name (Type: $type, Width: $width, Length: $length)"
    }
}

data class Room(val width: Double, val length: Double) {
    override fun toString(): String {
        return "Room (Width: $width, Length: $length)"
    }
}

// Function to simulate an AI-powered interior designer
class VirtualInteriorDesigner {

    private val furnitureOptions = listOf(
        Furniture("Sofa", 2.0, 1.0, "Seating"),
        Furniture("Armchair", 1.0, 1.0, "Seating"),
        Furniture("Coffee Table", 1.2, 0.6, "Table"),
        Furniture("Dining Table", 1.5, 0.9, "Table"),
        Furniture("Bed", 2.0, 1.5, "Sleeping"),
        Furniture("Desk", 1.2, 0.7, "Work"),
        Furniture("Bookshelf", 0.8, 0.3, "Storage"),
        Furniture("TV Stand", 1.5, 0.4, "Entertainment"),
        Furniture("Side Table", 0.5, 0.5, "Table")
    )

    // Function to generate random furniture suggestions based on room size and desired furniture types.
    fun suggestFurniture(room: Room, requiredFurnitureTypes: List<String>): List<Furniture> {
        println("Analyzing room dimensions and requested furniture types...")

        val availableFurniture = furnitureOptions.filter { it.type in requiredFurnitureTypes }

        // Simulate AI selecting furniture based on room size and type
        val selectedFurniture = mutableListOf<Furniture>()
        var totalWidth = 0.0
        var totalLength = 0.0

        val random = Random.Default

        while (availableFurniture.isNotEmpty() && totalWidth < room.width * 0.8 && totalLength < room.length * 0.8) {
            val randomIndex = random.nextInt(availableFurniture.size)
            val furniture = availableFurniture[randomIndex]

            // Check if adding the furniture exceeds room dimensions (a simple constraint)
            if (totalWidth + furniture.width < room.width && totalLength + furniture.length < room.length) {
                selectedFurniture.add(furniture)
                totalWidth += furniture.width
                totalLength += furniture.length
                //availableFurniture.removeAt(randomIndex) //Prevent adding same item multiple times and make it more versatile and realistic. However if room large enough for same type it will add more than one.

            } else {
                availableFurniture.removeAt(randomIndex)
                if(availableFurniture.isEmpty()){
                   println("No further furniture can be added to the room.")
                }

            }
        }

        if (selectedFurniture.isEmpty()) {
           println("No furniture could be selected to fit in room.")
        }
        return selectedFurniture
    }

    // Function to provide basic room layout suggestions (very simplified)
    fun suggestLayout(room: Room, furnitureList: List<Furniture>): String {
        println("Generating layout suggestions...")

        var layout = "Basic Room Layout:\n"
        layout += "Room Dimensions: $room\n"
        layout += "Furniture:\n"
        furnitureList.forEach { layout += "  - $it\n" }

        layout += "\nSuggested Arrangement:\n"
        layout += "- Place larger items (sofa, bed) against the longest wall.\n"
        layout += "- Center smaller items (coffee table, side table) in the available space.\n"
        layout += "- Ensure sufficient walking space between furniture pieces.\n"
        layout += "- Consider natural light sources when positioning seating areas.\n"
        layout += "- Keep walkways clear and accessible.\n" //Ensure good room flow.

        return layout
    }
}

fun main() {
    // 1. Get User Input (Simulated)
    val roomWidth = 4.0
    val roomLength = 5.0
    val desiredFurnitureTypes = listOf("Seating", "Table", "Storage")

    // 2. Create Room and Designer Objects
    val room = Room(roomWidth, roomLength)
    val designer = VirtualInteriorDesigner()

    // 3. Get Furniture Suggestions
    println("Getting furniture suggestions for your room...")
    val suggestedFurniture = designer.suggestFurniture(room, desiredFurnitureTypes)

    // 4. Print Furniture Suggestions
    println("\nSuggested Furniture:")
    suggestedFurniture.forEach { println("- $it") }

    // 5. Get Layout Suggestions
    if (suggestedFurniture.isNotEmpty()) {
        val layoutSuggestions = designer.suggestLayout(room, suggestedFurniture)

        // 6. Print Layout Suggestions
        println("\nLayout Suggestions:")
        println(layoutSuggestions)
    } else {
        println("No furniture was suggested, thus no layout suggestions can be given.")
    }
}
```

Key improvements and explanations:

* **Clearer Data Structures:** Uses `data class` for `Furniture` and `Room`. This provides automatic `toString()`, `equals()`, `hashCode()`, and `copy()` methods, making the code cleaner and easier to work with. The data classes now hold relevant attributes like width, length, and type, allowing for more realistic simulations.
* **Furniture Typing:**  The `Furniture` class includes a `type` field. This is crucial because it allows the user to specify *types* of furniture they want (e.g., "Seating", "Table", "Sleeping"), and the AI can then filter the available furniture based on these types.
* **More Realistic Furniture Selection:** The `suggestFurniture` function now:
    * Filters furniture options based on the `requiredFurnitureTypes`.
    * Uses a `while` loop to keep adding furniture until the room is "full" (determined by a percentage of total width and length being occupied).  Crucially, this adds *multiple* pieces of furniture.
    * Implements a basic constraint check: it won't add furniture if it would exceed the room's dimensions along either width or length.
    * Prevents indefinite loops by removing an item if it doesn't fit and printing the message that the room can no longer contain any further furniture.
    * If all furniture has been tried to be added without success, prints a message to the screen saying no furniture can be selected.
* **Simplified Layout Suggestions:** The `suggestLayout` function provides basic, text-based layout hints. This is a placeholder for much more sophisticated layout algorithms that would consider things like walkways, focal points, and furniture relationships. This is now much more human readable.
* **Random Furniture Selection:** The code uses `kotlin.random.Random` to randomly select furniture from the available options. This adds a bit of "AI" flavor, simulating the designer's choice.
* **Error handling / Empty list cases:** Catches the case where no furniture can be suggested and thus no layout can be generated.
* **Main Function for Testing:** A `main` function is included to demonstrate the usage of the `VirtualInteriorDesigner`.  This makes it easy to run the code and see the output.  The main function now uses simulated user input.
* **Comments and Explanation:** Extensive comments have been added to explain the purpose of each section of the code.
* **Constraint Satisfaction:**  The code incorporates a basic constraint: ensuring that the total width and length of the selected furniture do not exceed the room's dimensions.
* **Realistic Dimensions:**  Uses more plausible dimensions for furniture.
* **Clearer Output:**  Prints more informative messages to the console, making it easier to understand what the program is doing.

How to run:

1.  **Save:** Save the code as `InteriorDesigner.kt`.
2.  **Compile:** Open a terminal or command prompt and compile the code using the Kotlin compiler:
    ```bash
    kotlinc InteriorDesigner.kt -include-runtime -d InteriorDesigner.jar
    ```
3.  **Run:** Execute the compiled JAR file:
    ```bash
    java -jar InteriorDesigner.jar
    ```

This revised version provides a much more complete and useful starting point for building an AI-powered virtual interior designer in Kotlin. Remember that this is still a simplified example.  A real-world application would require more sophisticated algorithms, user interfaces, and data management.
👁️ Viewed: 5

Comments