-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainActivity.kt
42 lines (35 loc) · 1.12 KB
/
MainActivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.example.diceroller
import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
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()
}
}
private fun rollDice() {
val Dice = Dice(6)
val diceRoll = Dice.roll()
val diceImage: ImageView = findViewById(R.id.imageView)
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
}
diceImage.setImageResource(drawableResource)
diceImage.contentDescription = diceRoll.toString()
}
}
class Dice(val numSides: Int) {
fun roll(): Int {
return (1..numSides).random()
}
}