Search code examples
kotlindice

Kotlin - Rolling 2 dice, returns the same value for both dice every time


I am working through the tutorial on android developer central. One of the challenges is to extend the tutorial dice roller to include 2 dice. I tried creating a 2nd instance of my dice with var dice2 = Dice(6) using the same class definition that I did for my first die. However, both return the same value. So I tried creating a 2nd class definition for Dice2 and created an instance of that with `var dice2 = Dice2(6). But I get the same result. Below is the full code.

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val rollButton: Button = findViewById(R.id.button)
        rollButton.setOnClickListener { rollDice() }

        //roll the dice onLoad
        rollDice()
    }

    private fun rollDice() {

        //create two 6 sided die and rolls them
        val dice = Dice(6)
        val diceRoll = dice.roll()
        val dice2 = Dice2(6)
        val diceRoll2 = dice2.roll()

        //assign the ImageViews called imageView and imageView2 to the variables diceImage and diceImage2
        val diceImage: ImageView = findViewById(R.id.imageView)
        val diceImage2: ImageView = findViewById(R.id.imageView2)

        //update the die graphic based on the outcome of the dice roll
        //assigns the resource id to a variable called drawableResource
        val drawableResource = when(diceRoll) {
            1 -> R.drawable.dice_1
            2 -> R.drawable.dice_2
            3 -> R.drawable.dice_3
            4 -> R.drawable.dice_4
            5 -> R.drawable.dice_5
            else -> R.drawable.dice_6
        }
        /* do the same for the 2nd die */
        val drawableResource2 = when(diceRoll2) {
            1 -> R.drawable.dice_1
            2 -> R.drawable.dice_2
            3 -> R.drawable.dice_3
            4 -> R.drawable.dice_4
            5 -> R.drawable.dice_5
            else -> R.drawable.dice_6
        }
        /*Update the die image source based on the variable
        convert the die roll results from int to string and update the content description for
        the die image
         */
        diceImage.setImageResource(drawableResource)
        diceImage.contentDescription = diceRoll.toString()
        diceImage2.setImageResource(drawableResource)
        diceImage2.contentDescription = diceRoll2.toString()
    }
}

//create a class for an object called Dice & defines a roll function with random number
class Dice(private val numSides: Int) {

    fun roll(): Int {
        return (1..numSides).random()
    }
}
class Dice2(private val numSides: Int) {

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

Any help is greatly appreciated.


Solution

  • Try diceImage2.setImageResource(drawableResource2) instead of diceImage2.setImageResource(drawableResource). But better:

    1. you definitely don't need Dice2; use val dice2 = Dice(6) as you had originally, or even just val diceRoll2 = dice.roll() again.

    2. you'd avoid even a chance to make that mistake if you made a function

      fun showDiceResult(viewId: Int, rollResult: Int) {
          // get view by id
          // set image resource
          // set content description
      }
      

      and then called it twice

      showDiceResult(R.id.imageView, diceRoll)
      showDiceResult(R.id.imageView2, diceRoll2)