Android SDK

Android SDK: Create an Arithmetic Game - Gameplay Logic


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 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:

import android.content.DialogInterface;
import android.content.Intent;

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:

private String[] levelNames = {"Easy", "Medium", "Hard"};

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

AlertDialog.Builder builder = new AlertDialog.Builder(this);

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

builder.setTitle("Choose a level")
	.setSingleChoiceItems(levelNames, 0, new DialogInterface.OnClickListener() {
		public void onClick(DialogInterface dialog, int which) {
			//start gameplay

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:

AlertDialog ad = builder.create();;
Choosing a Level

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

private void startPlay(int chosenLevel)
    //start gameplay
    Intent playIntent = new Intent(this, PlayGame.class);
    playIntent.putExtra("level", chosenLevel);

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:

package com.example.youdothemath;


public class PlayGame extends Activity {
//class content

Add the following import statements to your class:

import java.util.Random;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

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

protected void onCreate(Bundle savedInstanceState) {

Extend the class declaration opening line to implement click listening:

public class PlayGame extends Activity implements OnClickListener

Step 2

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

private int level = 0, answer = 0, operator = 0, operand1 = 0, operand2 = 0;

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:

private String[] operators = {"+", "-", "x", "/"};

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:

private int[][] levelMin = {
	{1, 11, 21},
	{1, 5, 10},
	{2, 5, 10},
	{2, 3, 5}};
private int[][] levelMax = {
	{10, 25, 50},
	{10, 20, 30},
	{5, 10, 15},
	{10, 50, 100}};

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:

private Random random;

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:

private TextView question, answerTxt, scoreTxt;
private ImageView response;
private Button btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn0, enterBtn, clearBtn;

Step 3

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

question =  (TextView)findViewById(;
answerTxt = (TextView)findViewById(;
response =  (ImageView)findViewById(;
scoreTxt =  (TextView)findViewById(;

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


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

btn1 = (Button)findViewById(;
btn2 = (Button)findViewById(;
btn3 = (Button)findViewById(;
btn4 = (Button)findViewById(;
btn5 = (Button)findViewById(;
btn6 = (Button)findViewById(;
btn7 = (Button)findViewById(;
btn8 = (Button)findViewById(;
btn9 = (Button)findViewById(;
btn0 = (Button)findViewById(;
enterBtn = (Button)findViewById(;
clearBtn = (Button)findViewById(;

Listen for clicks on all of these buttons:


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

Bundle extras = getIntent().getExtras();
if(extras != null)
    int passedLevel = extras.getInt("level", -1);
    if(passedLevel>=0) level = passedLevel;

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:

random = new Random();

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:

private void chooseQuestion(){
//get a question

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:

answerTxt.setText("= ?");

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):

operator = random.nextInt(operators.length);

Select two operands at random, using another helper method:

operand1 = getOperand();
operand2 = getOperand();

Step 3

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

private int getOperand(){
//return operand number

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:

return random.nextInt(levelMax[operator][level] - levelMin[operator][level] + 1) 
	+ levelMin[operator][level];

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(operator == SUBTRACT_OPERATOR){
        operand1 = getOperand();
        operand2 = getOperand();

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:

else if(operator==DIVIDE_OPERATOR){
    while((((double)operand1/(double)operand2)%1 > 0) || (operand1==operand2))
        operand1 = getOperand();
        operand2 = getOperand();

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:

		answer = operand1+operand2;
		answer = operand1-operand2;
		answer = operand1*operand2;
		answer = operand1/operand2;

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

question.setText(operand1+" "+operators[operator]+" "+operand2);

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:

public void onClick(View view) 
//button clicked

Find out which button was clicked:

	//enter button
else if(view.getId(){
	//clear button
else {
	//number button

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:

answerTxt.setText("= ?");

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:

int enteredNum = Integer.parseInt(view.getTag().toString());

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:

	answerTxt.setText("= "+enteredNum);

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:

String answerContent = answerTxt.getText().toString();

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

    //we have an answer

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:

int enteredAnswer = Integer.parseInt(answerContent.substring(2));

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

int exScore = getScore();

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

private int getScore(){
    String scoreStr = scoreTxt.getText().toString();
    return Integer.parseInt(scoreStr.substring(scoreStr.lastIndexOf(" ")+1));

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:

scoreTxt.setText("Score: "+(exScore+1));

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:

scoreTxt.setText("Score: 0");

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

Gameplay 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.

Related Posts
  • Code
    Android SDK
    Create a Music Player on Android: User Controls0d63m preview image@2x
    We are building a simple music player app for Android in this series. So far, we have presented a list of the songs on the device and allowed the user to make selections from it, starting playback using the MediaPlayer class in a Service class. In this final part of the series, we will let the user control playback, including skipping to the next and previous tracks, fast-forwarding, rewinding, playing, pausing, and seeking to particular points in the track. We will also display a notification during playback so that the user can jump back to the music player after using other apps.Read More…
  • Code
    Android SDK
    Create a Hangman Game: User InteractionAndroid hangman game preview retina
    In this series, we are creating a Hangman game for the Android platform. So far, we've built the application's user interface, including images, drawables, and layouts. In this third and final installment, we are going to focus on user interaction.Read More…
  • Code
    Android SDK
    Create a Hangman Game: User InterfaceAndroid hangman game preview retina
    In this series, we are creating a Hangman game for the Android platform. In the first tutorial, we set the application up to present two screens to the user and we also made a start with the user interface elements, the images and shape drawables to be precise. In the second tutorial, we will zoom in on the game's layout.Read More…
  • Code
    Android SDK
    Create a Hangman Game: Project SetupAndroid hangman game preview retina
    In this series, I will show you how to create a simple Hangman game for Android. In every tutorial, you'll learn a number of fundamental skills for creating Android applications. The application will consist of two screens, include user interaction involving adapters, contain dialogs and an action bar, and leverage XML for data storage. Let's get started.Read More…
  • Code
    Android SDK
    Android SDK: Create an Arithmetic Game - High Scores and State DataMath game preview@2x
    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.Read More…
  • Code
    Android SDK
    Android SDK: Create an Arithmetic Game - Setup and Interface CreationMath game preview@2x
    The Android platform provides the ability to facilitate interaction, to carry out computation, and to save data. To explore some of these basic features with a practical focus, this series will create a simple arithmetic game for the Android platform. The series will involve creating user interface elements and layouts, four Activity classes, and one helper class. The gameplay logic will be relatively complex if you do not have much Java experience, but you will learn to use various Java control structures along the way! Read More…