kotlin 如何使用函数或类来改进代码?

kuhbmx9i  于 2022-11-16  发布在  Kotlin
关注(0)|答案(1)|浏览(119)

我正在尝试编程一个小骰子应用程序,其中包括三个骰子,应该有一些行动,例如“滚动所有骰子”或“把一方骰子,如果它是一个1或6”。
现在我还没有完成,但我的代码并不是真的那么典型。这就是为什么我问你,如果你能帮我减少我的代码,我只是重复了很多。这肯定是可能的一半行我猜。
我很高兴得到任何帮助/提示。
这是我的主要:

package com.example.notfall_kit

import android.os.Bundle
import android.widget.Button
import android.widget.ImageButton
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.isVisible

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Find the Button, the 3 ImageButtons and the 3 ImageViews in the layout
        val rollButton: Button = findViewById(R.id.wuerfelButton)
        val centerButton: ImageButton = findViewById(R.id.imageButtonCenter)
        val leftButton: ImageButton = findViewById(R.id.imageButtonLeft)
        val rightButton: ImageButton = findViewById(R.id.imageButtonRight)
        val centerImage: ImageView = findViewById(R.id.imageViewCenter)
        val leftImage: ImageView = findViewById(R.id.imageViewLeft)
        val rightImage: ImageView = findViewById(R.id.imageViewRight)

        // What happens when pressing "WÜRFELN" Button
        rollButton.setOnClickListener { rollDice()
        }

        // What happens when pressing the center dice ImageButton
        centerButton.setOnClickListener{
            centerImage.isVisible = true
            centerButton.isVisible = false
        }

        // What happens when pressing the left dice ImageButton
        leftButton.setOnClickListener{
            leftImage.isVisible = true
            leftButton.isVisible = false
        }

        // What happens when pressing the right dice ImageButton
        rightButton.setOnClickListener{
            rightImage.isVisible = true
            rightButton.isVisible = false
        }
    }

    private fun rollDice() {
        // Create a new Dice object with 6 sides and roll it
        val dice = Dice(6)

        // Create 3 dices and roll them, see Class "Dice" for roll-function
        val diceRollCenter = dice.roll()
        val diceRollLeft = dice.roll()
        val diceRollRight = dice.roll()

        // Find the ImageButtons in the layout for the 3 dices
        val diceImageCenter: ImageButton = findViewById(R.id.imageButtonCenter)
        val diceImageLeft: ImageButton = findViewById(R.id.imageButtonLeft)
        val diceImageRight: ImageButton = findViewById(R.id.imageButtonRight)

        //Change the images of the center dice
        when (diceRollCenter) {
            1 -> diceImageCenter.setImageResource(R.drawable.dice_1)
            2 -> diceImageCenter.setImageResource(R.drawable.dice_2)
            3 -> diceImageCenter.setImageResource(R.drawable.dice_3)
            4 -> diceImageCenter.setImageResource(R.drawable.dice_4)
            5 -> diceImageCenter.setImageResource(R.drawable.dice_5)
            6 -> diceImageCenter.setImageResource(R.drawable.dice_6)
        }

        //Change the images of the left dice
        when (diceRollLeft) {
            1 -> diceImageLeft.setImageResource(R.drawable.dice_1)
            2 -> diceImageLeft.setImageResource(R.drawable.dice_2)
            3 -> diceImageLeft.setImageResource(R.drawable.dice_3)
            4 -> diceImageLeft.setImageResource(R.drawable.dice_4)
            5 -> diceImageLeft.setImageResource(R.drawable.dice_5)
            6 -> diceImageLeft.setImageResource(R.drawable.dice_6)
        }

        //Change the images of the right dice
        when (diceRollRight) {
            1 -> diceImageRight.setImageResource(R.drawable.dice_1)
            2 -> diceImageRight.setImageResource(R.drawable.dice_2)
            3 -> diceImageRight.setImageResource(R.drawable.dice_3)
            4 -> diceImageRight.setImageResource(R.drawable.dice_4)
            5 -> diceImageRight.setImageResource(R.drawable.dice_5)
            6 -> diceImageRight.setImageResource(R.drawable.dice_6)
        }

        //call compareDices function to check if there is a 1 or a 6
        compareDices(diceRollCenter,diceRollLeft, diceRollRight)
    }

    private fun compareDices(Center: Int, Left: Int, Right:Int) {
        // Create 3 Images on top of the display and find them in the layout
        val diceImageCenter: ImageView = findViewById(R.id.imageViewCenter)
        val diceImageLeft: ImageView = findViewById(R.id.imageViewLeft)
        val diceImageRight: ImageView = findViewById(R.id.imageViewRight)

        // Find the ImageButtons in the layout for the 3 dices
        val centerButton: ImageButton = findViewById(R.id.imageButtonCenter)
        val leftButton: ImageButton = findViewById(R.id.imageButtonLeft)
        val rightButton: ImageButton = findViewById(R.id.imageButtonRight)

        // Only if the center-dice shows 1 or 6, the dice can moved to the top
        if (centerButton.isVisible) {
            when (Center) {
                1 -> diceImageCenter.setImageResource(R.drawable.dice_1)
                6 -> diceImageCenter.setImageResource(R.drawable.dice_6)
                else -> println("JAUCHE")
            }
        }

        // Only if the left dice shows 1 or 6, the dice can moved to the top
        if (leftButton.isVisible) {
            when (Left) {
                1 -> diceImageLeft.setImageResource(R.drawable.dice_1)
                6 -> diceImageLeft.setImageResource(R.drawable.dice_6)
                else -> println("JAUCHE")
            }
        }
        // Only if the right dice shows 1 or 6, the dice can moved to the top
        if (rightButton.isVisible) {
            when (Right) {
                1 -> diceImageRight.setImageResource(R.drawable.dice_1)
                6 -> diceImageRight.setImageResource(R.drawable.dice_6)
                else -> println("JAUCHE")
            }
        }
    }
}

这是我的班级:

package com.example.notfall_kit

class Dice(val numSides: Int) {

    fun roll(): Int {
        return (1..numSides).random()
    }
}
mbzjlibv

mbzjlibv1#

你可以使用的基本模式是创建你正在重复使用的东西的列表,这样你就可以在循环中设置它们。
您可以创建描述视图和图像Id的类属性,以便它们易于重用。我们将它们 Package 在by lazy中,因此在onCreate()之前定义它们是安全的。

private val imageButtons by lazy { 
    listOf(R.id.imageButtonCenter, R.id.imageButtonLeft, R.id.imageButtonRight)
        .map { findViewById<ImageButton>(it) }
}
private val imageViews by lazy { 
    listOf(R.id.imageViewCenter, R.id.imageViewLeft, R.id.imageViewRight)
        .map { findViewById<ImageView>(it) }
}

onCreate()中,可以迭代列表来设置它们:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val rollButton: Button = findViewById(R.id.wuerfelButton)
    rollButton.setOnClickListener { 
        rollDice()
    }

    for ((button, imageView) in imageButtons zip imageViews) {
        button.setOnClickListener {
            imageView.isVisible = true
            button.isVisible = false
        }
    }
}

为了帮助您使用rollDice()函数,您应该创建要使用的图像的列表属性:

private val diceDrawables = listOf(
    R.drawable.dice_1,
    R.drawable.dice_2,
    R.drawable.dice_3,
    R.drawable.dice_4,
    R.drawable.dice_5,
    R.drawable.dice_6
)

然后在rollDice()中,你应该创建三个骰子作为一个列表,并迭代它们。因为drawables在一个列表中,你不需要用when语句设置它们。

private fun rollDice() {
    val dice = Dice(6)

    val diceRolls = List(3) { dice.roll() }

    for ((diceValue, imageButton) in diceRolls zip imageButtons) {
        imageButton.setImageResource(diceDrawables[value - 1])
    }

    compareDices(diceRolls)
}

注意,我把compareDices改成了一个骰子的列表,而不是三个参数,它也是通过迭代列表来工作的,因为我们使用了三个列表,所以我用索引来迭代,而不是用zip

private fun compareDices(values: List<Int>) {
    for (i in 0..2) {
        val button = imageButtons[i]
        val image = imageViews[i]
        val value = values[i]
        if (button.isVisible) {
            when(value) {
                1, 6 -> image.setImageResource(diceDrawables[value - 1])
                else -> println("JAUCHE")
            }
        }
    }
}

相关问题