Your First Kotlin Android App: Polishing the App

Aug 22 2023 · Kotlin 1.8.20, Android 13, Android Studio Flamingo | 2022.2.1

Part 1: Build Out the App

08. Restart the Game

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: 07. Challenge: Add Bonus Points Next episode: 09. Conclusion

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: 08. Restart the Game

For the next item on our nice to haves from our programming todo list, you’ll add the ability for the player to restart the game. To do this, we’re going to need a new function we can call to restart the game.

Create the new function called startNewGame() right below the pointsForCurrentRound() function.

fun startNewGame() {

}

Then reset all the game related variables to their defaults:

//...
totalScore = 0
currentRound = 1
sliderValue = 0.5f
targetValue = Random.nextInt(1, 100)
//...

Cool!!! Now, if you notice, we’re setting the targetValue to a random value once again. And that signals that we have a code smell and we could refactor this random generator into a function. Let’s create it right below the differenceAmount() function.

Add in the following code:

fun newTargetValue() = Random.nextInt(1, 100)

Go ahead and call it to set the targetValue variable inside the startNewGame() function.

fun startNewGame() {
  //...
  targetValue = newTargetValue() // Updated Code
}

Also, assign it to the targetValue inside the onRoundIncrement lambda argument of the ResultDialog.

onRoundIncrement = {
  currentRound += 1
  targetValue = newTargetValue() // Updated Code
}

We can also do the same thing where the random value is initialized in the targetValue state object declaration above:

var targetValue by rememberSaveable { mutableStateOf(newTargetValue()) }

But when we do that, we have an error because you must declare a reference before it can be used. And as you can see, the newTargetValue() function is below targetValue so it cant access it at that point. So, go ahead and move it up as the first line of the GameScreen composable function.

Alright, now that we have the startNewGame() function ready, its time to make use of it. You will call it when the start over button is tapped. You know the drill, right?

Okay, first, lets add a new lambda parameter for this in the GameDetail composable. So head over to GameDetail.

Then add in a new parameter called onStartOver like so:

@Composable
fun GameDetail(
  //...
  onStartOver: () -> Unit, // New Code
) {
  //...
}

This lambda has no parameter and returns nothing. And it’ll be called whenever we tap the start over button so let’s call it inside the onClick listener.

Enter the following code:

Button(onClick = { onStartOver() }) { // Updated Code
  Text(stringResource(R.string.start_over))
}

If you look at the top-right corner of the editor, you can see that we have an error. If you click on it, it opens up the Problems tab. And it says: “No value passed for parameter ‘onStartOver’.” Double click on the it to go to the line with this problem. And this is where we called GameDetail inside the GameDetailPreview composable function.

It expects an onStartOver argument. We can do two things: either we pass a default lambda to onStartOver or we implement it in the GameDetail call here. Restarting the game is an important feature so i wont pass any default lambda in its definition.

Instead, I’ll just implement the argument in here like so:

GameDetail(onStartOver = {})

Passing in an empty lambda solves this error and there’s no need to implement any logic here since this is just a preview. And if you click on the split tab, you can see the preview works fine.

Next, lets implement it where GameDetail is called inside the GameScreen composable. Head back to the GameScreen file.

Then update it to the following:

GameDetail(
  //...
  onStartOver = { startNewGame() },
  //...
)

In here, we implemented the onStartOver lambda argument and called the startNewGame() function which we created earlier inside it. With this setup, whenever the start over button is tapped, it’ll trigger this lambda which will in turn call the startNewGame() function.

Cool!!! You’re done with the restart game feature.

Run your app to try it out.

Clicking on the start over button restarts the game and you can see a new target value is generated. Let’s play one round. Wow!!! I got a perfect score this time around and you can see it added a bonus point of 100 for this round so my total score is now 200…Cool!!!

I’ll close the dialog and you can see we’re currently at round 2. I’ll go ahead and tap the start over button.

The game restarts with all the values set to their defaults as expected.

Before we end this episode, let me show you a cool way to get around your file inside Android Studio.

If you look at the tool windows bar on the left side of your screen, you should see the structure button. Go ahead and click on it if you see it. And If you dont see it just like mine you head over to the menu. Then go to View, Tool Windows, then select “Structure.” To dock it to the tools sidebar, you simply just drag it into the position you want.

The structure window as its name suggests, shows you the structure of the open file. In here, you can see the GameScreen composable function and the functions inside it. You can also see the GameScreenPreview composable function. If this was a class like the MainActivity, then you’ll see the variables and the methods declared inside it.

Clicking on them takes you to that point inside the editor window.

Go ahead and click on the newTargetValue() function. And you can see, the editor scrolls to that point in our code. You can also click on other functions.

The structure window can come in handy when working with large files and you dont want to spend time searching or trying to scroll to a particular point in your code. With the structure window, you get a bird’s eye view or should I say you hit the Bullseye of the point you want to be in your code.

Okay, enough of promoting Android Studio even when I’m not an official brand Ambassador for JetBrains. Click on the structure button to close the window.