Pausing while loop until button has been pressed?

Viewed 36

I'm creating a blackjack app for a school project and I'm trying to create a function that makes it possible if the ace card is worth 1 or 11, this is what I have so far:

fun drawCard(amount: Int) {
        var loopedTimes: Int = 0

        while(loopedTimes != amount) {
            val rand = Random(System.currentTimeMillis())
            val randomValue = rand.nextInt(12)

            cardsArray[loopedTimes].setImageResource(allCards[randomValue].cardImage)
            myScore += allCards[randomValue].cardValue

            if(allCards[randomValue].cardName == "Ace") {
                val aceOneButton = findViewById<Button>(R.id.aceOneButton)
                val aceElevenButton = findViewById<Button>(R.id.aceElevenButton)

                aceOneButton.visibility = View.VISIBLE
                aceElevenButton.visibility = View.VISIBLE

                aceOneButton.setOnClickListener {
                    myScore -= 10 // Sänk värdet med 10
                }
            }

            loopedTimes++
            playerCurrentCard++
            myScoreText.text = myScore.toString()
        }
    }

The problem is that the while loop doesn't wait until the player has pressed the ace button, is there any way to make loop wait for the button to get pressed?

0 Answers
Related