Home > database >  Not getting random DIE results, all 6 are showing same image and same rolled number
Not getting random DIE results, all 6 are showing same image and same rolled number

Time:10-04

I need to display 5 random die results, what am I doing wrong that they are all resulting in the same number and image? IE. Roll a 5, they all show 5 and the 5 image shows up when they all need to be random.

 val iv1 = findViewById(R.id.iv1) as ImageView
    val iv2 = findViewById(R.id.iv2) as ImageView
    val iv3 = findViewById(R.id.iv3) as ImageView
    val iv4 = findViewById(R.id.iv4) as ImageView
    val iv5 = findViewById(R.id.iv5) as ImageView
    val btnRoll = findViewById<Button>(R.id.btnRoll)

    btnRoll.setOnClickListener {
        val randomInt = Random().nextInt(5)   1
        val drawableResource = when (randomInt) {
            1 -> R.drawable.die1
            2 -> R.drawable.die2
            3 -> R.drawable.die3
            4 -> R.drawable.die4
            5 -> R.drawable.die5
            else -> R.drawable.die6

    }

        iv1.setImageResource(drawableResource)
        iv2.setImageResource(drawableResource)
        iv3.setImageResource(drawableResource)
        iv4.setImageResource(drawableResource)
        iv5.setImageResource(drawableResource)

CodePudding user response:

How to do it with minimal changes to your code: Put your views in a list so you can loop through them so each one gets a random value:

btnRoll.setOnClickListener {
  val imageViews = listOf(iv1, iv2, iv3, iv4, iv5)
  for (imageView in imageViews) {
    val randomInt = Random().nextInt(5)   1
    val drawableResource = when (randomInt) {
      1 -> R.drawable.die1
      2 -> R.drawable.die2
      3 -> R.drawable.die3
      4 -> R.drawable.die4
      5 -> R.drawable.die5
      else -> R.drawable.die6
    }
    imageView.setImageResource(drawableResource)
  }
}

But to do this with less code duplication, I would initialize all your views in a list to begin with. Also, you can eliminate the when statement by listing your drawables and calling random() on the list directly:

val imageViews = listOf(R.id.iv1, R.id.iv2, R.id.iv3, R.id.iv4, R.id.iv5)
  .map { findViewById<ImageView>(it) }
val drawables = listOf(
  R.drawable.die1,
  R.drawable.die2,
  R.drawable.die3,
  R.drawable.die4,
  R.drawable.die5,
  R.drawable.die6
)
val btnRoll = findViewById<Button>(R.id.btnRoll)
btnRoll.setOnClickListener {
  for (imageView in imageViews) {
    imageView.setImageResource(drawables.random())
  }
}

CodePudding user response:

I think before you could solve this \you could have think of it as algorithm get n distinct random numbers check my solution for this:

val views = listOf<ImageView>(
        findViewById(R.id.iv1) as ImageView,
        findViewById(R.id.iv2) as ImageView,
        findViewById(R.id.iv3) as ImageView,
        findViewById(R.id.iv4) as ImageView,
        findViewById(R.id.iv5) as ImageView,
    )

  btnRoll.setOnClickListener {
    val s: MutableSet<Int> = mutableSetOf()
    while (s.size < views.size) { s.add((views.indices).random()) }

    s.forEachIndexed { index, value ->
        val drawableResource = when (value   1) {
            1 -> R.drawable.die1
            2 -> R.drawable.die2
            3 -> R.drawable.die3
            4 -> R.drawable.die4
            5 -> R.drawable.die5
            else -> R.drawable.die6
        }
        views[index].setImageResource(drawableResource)
    }
  }
  • Related