Your First Kotlin Android App: Polishing the App

Jul 14 2022 · Kotlin 1.6, Android 12, Android Studio Bumblebee | 2021.1.1

Part 1: Build Out the App

05. Challenge: Keep Track of Game Rounds

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 04. Display the Score Next episode: 06. Improve the Alert Title

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Transcript: 05. Challenge: Keep Track of Game Rounds

You’ve done a bit of coding in this part so it’s now time for your challenge. Your challenge this time is to pause the video, and modify Bull’s Eye to keep track of the current round of the game and display it.

This is a review of even more techniques you’ve learned in this course.

To do this, you have to do three things:

  • Increment the round whenever a new round starts
  • Display the current round number in the appropriate label whenever the player dismisses the alert dialog.
  • Generate and display a new target value at the start of a new round. This should also be done when the player dismisses the dialog.

This is very much like what you did in the previous episode, so if you want a hint or inspiration, review the previous episode.

Now pause the video and give it a try.

Hint: You’ll increment the round variable using an operator assignment just like in the previous episode.

Alright, I’ll show you how to add the game round feature now. And if you were able to do it on your own, then thumbs up to you.

First, you create a variable to hold the game rounds. I’ll declare that right below the totalScore variable like so:

private var currentRound = 1

We added a currentRound variable and set to a value of 1. We use 1 here because the first round of every game is always round 1.

Next, we need to set the gameRoundTextView to the value from the currentRound variable we just declared. I’ll do that under the code where we set the targetTextView’s text:

binding.gameRoundTextView?.text = currentRound.toString()

Next, we need to increment the current round of the game whenever the player dismisses the dialog. That is; whenever the player clicks the awesome button which acts as the positive button.

I’ll scroll down to the code for the alert dialog. Then inside the lambda function of the positive button, add in the following code:

currentRound += 1
binding.gameRoundTextView?.text = currentRound.toString()

In here, we increment the current round by 1 using the operator assignment expression. Then we set the text of the gameRoundTextView to the string representation of the currentRounds value.

Finally, let’s add the code to generate a new target value. Enter the following code before the code to increment the current round:

targetValue = Random.nextInt(1, 100)
binding.targetTextView.text = targetValue.toString()

With this, whenever the player dismisses the dialog, the current round is incremented and you have a new target value.

Run your app to try it out.

Take note of the round text view, it is currently set to 1. Let’s move the slider. Then tap the hit me button. Then close the dialog by tapping the positive button. And voila!!! The game round increments accordingly to a value of 2 and we also have a new target value.

Now go ahead and play multiple rounds and be impressed with how far you’ve come with creating the bullseye app. All work and no play they say, makes Jack a dull boy. When you’re done, I’ll see you in the next episode for your next challenge.