I’m taking a kotlin basics course from the android devs website and we have to create an app that allows you to roll a dice and display an image with the number that rolled.
I can make a condition for each possible roll number, but i feel like that’s dumb and with formatted reference id calls it should be done way better but I don’t know how to accomplish that in kotlin using Android Studio. Any helpers?
package com.example.diceroller
import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
/**
* Calls the onCreate() method to initiate the app in its MainActivity.
* setOnClickListener to look for activity in the window (button presses/taps).
*/
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() }
/* Toast.makeText(this,"Dice Rolled!", Toast.LENGTH_SHORT).show() | Shows alert (toast) at bottom of the screen. */
}
/*
* Initiates a 'Dice' instance with 6 sides.
* calls the roll() method to call rng between 1 and numSides.
*
*/
private fun rollDice() {
val dice = Dice(6)
val diceRoll = dice.roll()
val diceImage: ImageView = findViewById(R.id.imageView)
if (diceRoll == 1){ diceImage.setImageResource(R.drawable.dice_1) }
else if (diceRoll == 2){ diceImage.setImageResource(R.drawable.dice_2) }
//Instead of doing it the "dumb way" I want to call the id depending on diceRoll value.
}
}
class Dice(private val numSides: Int) {
fun roll(): Int {
return (1..numSides).random()
}
}
2
Answers
Create a list using those resource ids.
If you’re asking how to add
1
or whatever to"R.drawable.dice_"
and then turn that string into a resource ID, then you can follow the accepted answer here: https://stackoverflow.com/a/2414165/13598222Personally I wouldn’t recommend that – it’s brittle since the compiler can’t connect what you’re doing to a specific resource. So it can’t warn you if you change the resource name (so your code doesn’t match anymore), and things like Proguard can end up stripping out "unused" resources (because it can’t tell you’re using them) and you have to manually fix that.
Probably the most standard way to do what you’re doing is to use a
when
clause, and just return the drawable ID from that:Sure it’s a little repetitive, but it’s a standard pattern, it’s very easy to understand and work with, and you’re explicitly handling all the cases by adding the (required)
else
clause – you could also handle6
explicitly and make the elsethrow
an out-of-range message, in case yourroll
function somehow sends an invalid value (which it could, since you’re hardcoding six values here, but yourDice
class can have an arbitrary number!)Another way you could do it is to have a lookup, like a
Map
:and then your image setting code can be nice and short:
This isn’t the "dumb" way to do things, it’s neat and tidy and avoids repetition (e.g. repeating the
setImageResource
call). Getting clever with your code logic can cut down a few lines, but it can also make things much more complex and fragile, and just harder to work with. Sometimes that kind of thing is the right call, but in this situation I think this is probably the best compromise. Your call though!