In this short chapter, we will continue to develop the game logic within GameActivity. More specifically, we'll look at how we track the player's score both for counting it and displaying it.
Managing the Number of Questions
First, it's up to you to decide how many questions you want to ask the user. You can do this by declaring an integer variable:
private int mNumberOfQuestions;
Initialize this value in onCreate()
:
mNumberOfQuestions = 4;
This value will help you determine when to end the game. Make sure you have at least twice as many questions in your QuestionBank to avoid repeating questions too frequently.
In our example, we believe a game ends in four questions. So we need to implement the logic that ends the game after the fourth question receives a response.
We will use mNumberOfQuestions as a counter that indicates when to stop the game. Every time the user answers a question, we will decrement this variable. As long as it remains above zero, we will ask the next question. Otherwise, the game stops.
Our logic looks like this:
// ... onClick(), after verifying the user's answer
if (--mNumberOfQuestions == 0) {
// No questions remaining, end the game
} else {
mCurrentQuestion = mQuestionBank.getQuestion();
displayQuestion(mCurrentQuestion);
}
// onClick()...
Managing the Score
Score Tracking
Declare an integer variable and then increment its value each time the player enters a correct answer.
Displaying the Score
When the player has answered all questions, the final phase of play is triggered:
The score is displayed to the user in a dialog box.
We bring the user to the Main screen after they click OK.
The dialog box is handled by the AlertDialog class. This dialog is fully configurable. Knowing that you are still young Padawans, we will use the AlertDialog in the most straight-forward way possible. Namely: display a title, a message, and a confirmation button.
Here is the code which configures and displays our dialog box:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Well done!")
.setMessage("Your score is " + mScore)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.create()
.show();
Let's break this down line-by-line:
01: The nested Builder class helps us create an AlertDialog without any fuss
03: We set the dialog's title
04: We set the text displayed within the dialog box
05: We add a dialog button and attach a listener to handle the user's click
11: We ask the Builder object to finalize our dialog, it returns an AlertDialog object
12: We display the dialog to the user
And line 8? Well the finish() method (belonging to Activity) tells the system: I'm done with the current Activity, stop it, and bring me back to the previous Activity. For example, tapping the Android back button invokes this method to move the user to the previous screen or Activity.
Let's Review With a Demo
You can see these steps in the video below:
Let's Recap!
To track the number of questions in your game, create an
int
member variable and initialize its value inonCreate()
.To track the player’s score, declare an integer variable and increment its value each time the player enters a correct answer.
You’ve finished the second part of this course! Now it’s time to perfect your app. But first, don’t forget to test your knowledge in the quiz!