7 days of WordPress plugins, themes & templates - for free!* Unlimited asset downloads! Start 7-Day Free Trial
  1. Code
  2. Android SDK

Android SDK: Create an Arithmetic Game - Gameplay Logic

Scroll to top
Read Time: 11 mins

In this series we are creating a simple arithmetic game for Android. The game is going to display a calculator-style interface to users, repeatedly presenting questions and keeping track of how many correct answers they score in a row. In the first part of the series we built the user interface, got the main Activity class setup, and laid the foundations for another three Activity class. In this part, we will work on the gameplay logic, with the user selecting from three possible levels of difficulty. We will implement the question and answer gameplay with four operators: addition, subtraction, multiplication, and division. The game will choose operator and operands randomly, with the process tweaked for each operator.

Series Format

This series on Creating an Arithmetic Game will be released in three parts:

Game Overview

The following is a screenshot of the game I'll teach you how to build:

Arithmetic GameArithmetic GameArithmetic Game

The main Activity presents three options: Play, How to Play, and High Scores. After pressing the Play option, the user must choose a difficulty level, and then gameplay will begin. Each answer entered by the user will be given a tick or cross as response, while the score will continue incrementing until the user enters a wrong answer or quits the game. We will complete the How to Play and High Scores functionality in the final part of the series, as well as saving the app instance's state.

1. Launch the Game

Step 1

Last time we prepared the main Activity class to listen for clicks on the three buttons - let's now implement what happens when the user clicks the Play button. First add the following imports to the app's main class:

We want the user to select one of three levels to start a game - add an instance variable to the class to store the level names in an array:

In your onClick method create an Alert Dialog in the if statement we created for the Play button.

Now set the details of the dialog, passing the level names array and setting up the click listener for the options:

When a level is selected, the onClick method for the Alert Dialog calls a helper method we will add next, passing the index of the selected level from the array. Before we implement the helper method, still inside the if statement for the Play button, create and show the dialog:

Choosing a LevelChoosing a LevelChoosing a Level

Now add the helper method to the class, after the Activity onClick method:

We start the gameplay Activity, passing the level number to the Intent. We will implement clicks on the other two buttons in the final part of the series.

2. Prepare for Gameplay

Step 1

Last time we created an Activity class for gameplay. Eclipse should have inserted the following outline structure, with whatever names you chose:

Add the following import statements to your class:

Insert the onCreate method and set the content view to the layout we created last time:

Extend the class declaration opening line to implement click listening:

Step 2

Let's add some instance variables to the class to facilitate gameplay. First, integers to represent the level, operands, operator and answer:

Next define some constants for the four operators, which will streamline the gameplay processing:

Now add an array for the text to display for each of these operators, with the array index corresponding to the constant in each case:

The range of operators is going to depend on the level the user chose and on the operator being used (i.e. the addition operands for the easy level will have a different possible minimum and maximum number than the operands used with other operators and on other levels). We will use a random number generator to choose the operands, with a minimum and maximum in each case. Define the minimum and maximum numbers for each operator and each level using the following 2D arrays:

Each array represents the levels and operators, with the minimum or maximum listed for the three levels for addition, then subtraction, then multiplication, then division - using the same order as the constants we defined. For example, the minimum operand for addition at medium difficulty is 11, while the maximum operand for subtraction at hard difficulty is 30. The purpose of this will become clearer when we implement the part of gameplay where we generate the questions. You can change the minimum and maximum numbers later if you like.

Next add an instance variable for a random number generator we will use throughout the class:

Finally, add instance variables for the user interface elements we defined in the layout file last time, including the Text Views for question, answer and score, the Image View for the tick or cross response image and the buttons for digits 0-9 plus enter and clear:

Step 3

Now let's turn to the onCreate method. After the existing code, retrieve references to the Text and Image Views:

Set the tick/cross response image to be invisible initially:

Next retrieve references to the number, clear, and enter buttons:

Listen for clicks on all of these buttons:

Now retrieve the level number we passed from the main Activity when launching this one:

The level variable was set initially to zero, so if no number is received the game defaults to easy level. After this if statement, still inside onCreate, initialize the random number generator:

Finally call a helper method to start the game - we will implement the method next:

3. Start Gameplay

Step 1

Add the method you called in onCreate to your class:

This method is going to execute every time we need a new question. It will choose the operator and operands at random, within the range specified for the operator and level. The method will output the question to the user interface, ready for the user's answer.

Step 2

Inside the new method, first reset the answer Text View:

This is necessary because the answer Text View is going to display the user-entered answer, so it must be reset each time a new question is generated. Select an operator at random, making sure it is within the range of the operators array (it has four in it but you could potentially change this):

Select two operands at random, using another helper method:

Step 3

After your chooseQuestion method, add the new helper method to the class, for choosing operands:

We use a helper method for this as it may be called repeatedly. The method will return an integer within the relevant range. Add the method content:

This code ensures that the integer is within the correct range for the current operator and level.

Step 4

Back in your chooseQuestion method, you now need to add some qualifications depending on the operator. For example, we don't want to allow negative answers, so the subtract operators must not produce a negative result. Add a conditional test:

If the second operand is greater than the first, we simply carry on generating operands until we get a pair in which the first operand is greater. With the divide operator, we only want answers that are whole numbers, so add a further conditional:

This time we continue until we have a division resulting in a whole number, also avoiding cases where the two operands are equal. You could of course tweak this code further to improve on the effectiveness of the game.

Step 5

Now we have a valid set of operands for the chosen operator, so let's calculate the answer using a switch statement:

We will use this to check the user's answer later. Now we can display the question to the user:

4. Handle User Input

Step 1

Now we have a question being displayed to the user, we just have to respond to their clicks on the buttons. Add an onClick method to your class:

Find out which button was clicked:

Remember that we setup click listeners for the number buttons, the enter button and the clear button. We check first for the enter then clear buttons. If the click event has fired and it wasn't either of those, it must have been a number button, which we will handle in the else block.

Step 2

Let's handle the clear button first. In the else if for the clear button, simply reset the answer Text View:

Step 3

Now let's handle the number buttons. In the else block, start by setting the tick/cross image to be hidden, as we don't want feedback displayed while the user is entering an answer:

Remember that we set tags representing the relevant numbers in each number button when we added them to the layout. This means that we can handle all number buttons in the same way, retrieving the number pressed from the tag:

Now we have two possible scenarios, the user is entering the first digit of the answer, or a subsequent digit. Check the currently displayed answer and respond accordingly, either setting or appending the digit just pressed:

Step 4

Finally, we can handle clicks on the enter button. In the if block for the enter button, first retrieve the answer entered by the user from the answer Text View:

Check if we have an answer, if not, we do nothing:

The answer text is always preceded with the text "= ", with "= ?" displayed until the user presses a number key, so we know we have an answer. Inside the if block, now we can retrieve the number entered:

When we check the answer, we will update the score, so call another helper method to retrieve it:

After the onClick method, add this new method to return the current score by reading the score Text View:

Back in the onClick block for the enter button, check whether the user's answer is correct:

Inside this block, respond to the correct answer:

We update the score text, display the tick image and set the Image View to display. After the if block, add an else for incorrect answers:

Inside this block, respond to the incorrect answer:

We reset the score to zero and display the cross image. Finally, after the else, choose another question:

Gameplay Activity ScreenGameplay Activity ScreenGameplay Activity Screen

And so the game continues...


We've now completed the second part of the series! At this stage, you should be able to run your app and play the game, although the How to Play and High Scores screens will not function yet - we will complete them in the final tutorial. In this tutorial we have implemented the gameplay logic, handling and responding to user interaction. In the final part of the series we will save and retrieve high scores using the application Shared Preferences. We will also save instance state data to ensure the app continues to function when gameplay is interrupted.

Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.