1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Building a Math Game App on Android

Math Game - Part 4

Contents

keyboard_tab
Building a Math Game App
1
Math Game - Part 1
PREVIEW7m 14s
2
Math Game - Part 2
PREVIEW10m 5s

The course is part of this learning path

Start course
Overview
Difficulty
Intermediate
Duration
1h 24m
Students
2
Description

This is a highly practical course that walks you through how to create a simple math game for school children on Android. We will make use of the Kotlin components that we covered in the Android App Development with Kotlin learning path, bringing them all together in a real-world app.

Learning Objectives

  • Learn how to use Kotlin and Android development tools to build your own app

Intended Audience

This course is intended for anyone who wants to learn how to start building their own apps on Android.

Prerequisites

To get the most out of this course, you should have some basic knowledge of the fundamentals of Android.

Resources

Course GitHub repos:

Transcript

Alright, hello everyone. We're going to continue to develop our math game app in this video. Right?

I'm sure you remember in last video we designed the game page, so in this video, we're going to write the game codes on the Kotlin site. So, I want to go right to the Kotlin page and get started. We've got six TextViews. I actually gave IDs to three of them because I will use these three TextViews. I'm not do any operations with the other three TextViews; they are just for the labels, right? So, I'll define these three TextViews here. First one is the score: Lateinit var textScore : TextView. Second one is life: Lateinit var textLife : TextView. Third one is time: Lateinit var textTime : TextView. Also I've a question TextView. So, we're going to write questions on this TextView, right? So, Lateinit var textQuestion : TextView.

And of course, we've got an editText and the user will write the answer into it, so Lateinit var editTextAnswer : EditText. And we've got a couple of buttons. The first one is the 'OK' button and it's going to check to see the user's answer against the correct answer, so Lateinit var buttonOk : Button. The second one is the next question button, it's going to bring up the next question: Lateinit var buttonNext : Button. All right, so, once all that's done, we need to continue to define it in the onCreate function: textScore = findViewById(R.id.textViewScore). textLife = findViewById(R.id.textViewLife textTime = findViewById(R.id.textViewTime) textQuestion = findViewById(R.id.textViewQuestion) editTextAnswer = findViewById(R.id.editTextAnswer) buttonOk = findViewById(R.id.buttonOk) buttonNext = findViewById(R.id.buttonNext). You'd probably be able to do those in your sleep soon.

All right, so that finishes defining the components. So, once that's all done we can add the ClickListener to these two buttons: buttonOk.setOnClickListener buttonNext.setOnClickListener, okay? So, now the ClickListeners are added, we can continue the coding. All right, so we're going to need to ask the question of the user, right? So, how are we going to generate questions? So, we'll first need to write a function. The function will contain the game logic. So, the function name can be gameContinue. You can of course name it whatever you want, it's up to you. Just remember what it is. But this function is going to be the unit, i.e. void function. So, I'll write the fun gameContinue here. And after that, we'll need some numbers for the game. So, first, we'll need to generate a random number. And I'll use the class named Random in Kotlin, so I can write Random here with the first letter capitalized.

You can see that here, right? Now, the editor offers different options called Random  but from here I'll choose Random which is a class from the (kotlin.random) package, represented by the letter C, okay? And press 'Enter.' So, when you type .Next, as you can see, a lot of options appear. So, we can generate random numbers of the type, integer, double, or float. So, here, what do you think I'm going to choose? The nextInt method. And finally, I should specify the boundaries of the number to be generated because if I don't define boundaries it can produce a really huge number, maybe not what your five-year-old or whatever is going to want. So, for this example, we can generate a random number between zero and a 100 we'll say. So, for this, I'll just write 0, 100). All right, so generating a random number in Kotlin is that easy my friends. So, now let's transfer this randomly generated number to a variable. So, I'll write Val number1 = in the beginning of the code.

Thus, a random number between zero and 100 will be generated and passed to the variable number1. All right, so we've got a randomly generated number, but we need one more randomly generated number, right? So, I'm going to do the same that we just did for the number2. So, I'll copy the line of code here, paste it just once, and I'll change the name of the variable to number2. All right, so you see, number1 and number2 will be an integer number between zero and 100. Now, here is an important part: because it will produce another number every time and randomly. I'm not even going to know what number was generated, right? If you want to, you can go ahead and change boundaries such as between 50 and 200 or zero and 50 or something, whatever, but you get it, right? After that, I'll need to write these numbers on the question TextView. So, textQuestion.text = "$number1 + $number2".

I want to ask you, do you remember this syntax? After the dollar sign, you can write any code. Also the plus sign in the quotation marks shows this sign on textView, right? So, it will not add number1 and number2, just in case you were worried. It will just indicate that it is an addition operation. Of course, you knew that. So, the numbers will now be shown on the QuestionTextView. He's Just going to see this question and hopefully write an answer on the editText. So, after the user clicks on the 'OK' button we should take the user's answer and check it with the correct answer. So, we'll continue in the 'OK' button ClickListener method. So, when the user clicks on the 'OK' button, first, we'll take the user's answer from the editText. But I want you to think about this, if the user clicks the 'OK' button without entering any data in the editText, well, the application can't crash, right? Therefore, we should make a check with NFL statement here, all right? So, we'll need to add a new variable. So, I'm going to write val input =.... Now, let's pass a text of the editText to this variable. editTextAnswer.text.toString(). Now we can create the if-else statement. if (input == "") That is the user has not entered any data.

Then we can display a toast message to the user: Toast.makeText(applicationContext and the message might be "Please write an answer or click the next button". Toast.LENGTH_LONG).show(), okay? So, that way if the user clicks the 'OK' button without writing in any answer, well the application's not going to crash, but a toast message will be displayed to the user saying, "Hey come on, we're playing a game here." All right, so, now we can create the else block. So, in the else block since the user will now have written an answer, we should write codes accordingly. So, first, we must convert the answer that we've received from the user to an integer type and transfer to a new variable, That's what I'm typing in here: Val userAnswer. Name of this variable can be userAnswer. After the = input.toInt(), all right? So, now, I need to check the userAnswer and the correct answer. So, what does that mean? Well, I'll need another integer container for the correct answer, but I will define the variable that I will create for the correct answer in the global area. So, that means inside the Kotlin class and outside of all functions because I'll need to use the correct answer variable in more than one place. If we define it here, we can only use it here.

But if we define it in the global area we can access the correct answer variable from anywhere within the class. So, that's why I'm typing here, var correctAnswer = 0, and I'll set the initial value to 0. So, I'll use the correctAnswer in the gameContinue method. The correctAnswer = number1 + number2. So, now I can check the userAnswer and the correctAnswer. There are two conditions here. If the userAnswer == correctAnswer, need to do something and that means that the userAnswers correct. Else, it's going to do something and that means that the userAnswer is wrong. So, if the userAnswer is correct, first I will increase the score. So, that means I'll need an energy variable for the score, var userScore = 0. The initial value will be 0. userScore = userScore + 10, I'll give 10 points for every correct answer. Now secondly, we need to say to the user the answer is correct. So, I will write this on the question text view. All right, textQuestion.text = "Congratulations, your answer is correct."

So, also we have to update the score text view. So, here I can write textScore.text = userScore.toString(). So, that is if the answer is correct. So, if the user gives a wrong answer well, we'll need to decrease user life. So, that means I'll need an integer for the user life, var userLife = 3, and the user will have three lives, and its life will decrease by one for every wrong answer. Now write userLife--. Also, I'll need to say to the user the answer is wrong, textQuestion.text = "Sorry, your answer is wrong." So, now we need to update the life text view. So, I'll write textLife.text = userLife.toString(). So, we created the gameContinue() function, but we haven't used it yet anywhere. So, I need to call all the gameContinue function in the oncreate method because as soon as the game activity opens, hey, it's going to be you're on, that's when it's working.

So, now we can ask questions of the user, and if the user gives the answer we can take the user answer and check it. So, why don't we just run that much and test it out? So, there's the application opening, click on the 'ADDITION' button. So, the game page opens and sure enough the game is working. So, the game asked a question. So, first I'm not going to enter any answer. I just want to click the 'Okay' button, see what happens. And there's a toast message, right? Cool. So, why don't we just put in an answer to the question. I'll give the correct answer first so, I'll write 111 and click the 'Okay'. So, after I click on 'Okay', the game checks my answer and the correct answer, and it writes on the textView 'Congratulations your answer is correct.' And after I get over being proud of myself, the score increases by 10. So, if I click on the 'NEXT' question button, it should ask me a new question but the button doesn't work yet. So, don't worry about that, we'll just need to put in the necessary code for it soon. Now I want to click on the 'Back' button and there the first page opens. So, I click on 'ADDITION' again and as you can see, the game ask a different question, cool. That's what we were checking out. So, it generates questions randomly. So, this time I want to put in the wrong answer. So, and wrong answer writes 'Sorry, your answer is wrong.'

And also see here my life has decreased by one, it's now 2. So, my new life is written here on the life textView. So, why don't we go ahead and write the codes for the next button? Now, this is just too easy. I just need to call the gameContinue method in the next question button. So, there's your game logic, you will write the game conditions in the gameContinue method and every time you call it, the game will work. So, in this game every time I click on the 'NEXT' question button, a new question will be asked. Also when I click on the 'NEXT' button, I should delete the text of the edit text. Otherwise, when we pass on the next question, the old answer is still going to be written in the edittext. Not necessarily good thing in terms of user experience. I could see everybody getting a little confused there.

So, what does that mean? I just need to write, editTextAnswer.setText(" "). And then that will clean the edittext, and after that call the next question. You don't believe me? Well, let's run the game again. So, the application opens, I'll click on the 'ADDITION' button again. So, the game is working and it asked a question, I'll enter the correct answer. So, when I click on the 'Okay' button, it says your answer is correct. Also look at this, my score has gone up right here on the text view. So, if I click on the 'NEXT' question button, it's going to ask me a new question. I'll answer the new question. So, I'll enter 170 this time. Click the 'Okay' Button. Now my score increases to 20. So, I'll click on the 'NEXT' question button again and I'll enter the wrong answer this time. Click the 'Okay' button. So my lives have dropped by one. So, you see how that works. What do we do next? How about we create a timer? But we're going to do that in the next video. So, we're going to end here and we'll create a timer in the next one. See you then.

 

About the Author
Students
86
Courses
23
Learning Paths
1

Mehmet graduated from the Electrical & Electronics Engineering Department of the Turkish Military Academy in 2014 and then worked in the Turkish Armed Forces for four years. Later, he decided to become an instructor to share what he knew about programming with his students. He’s currently an Android instructor, is married, and has a daughter.

Covered Topics