Click here to Skip to main content
13,288,520 members (58,073 online)
Click here to Skip to main content
Add your own
alternative version


16 bookmarked
Posted 1 Nov 2012

Android Pick Ball - To x86 Emulator

, 3 Nov 2012
Rate this:
Please Sign up or sign in to vote.
A very simple game.


I am very new in the Android platform. But before some time, I tried to develop an Android app. During this period I created some Android apps at a beginner level. But in the Android game contest I saw this: Create a new, NDK-based Android game app for Intel® architecture (IA) based devices. Before this, I didn't know about NDK. By learning about NDK toolset, I found that it allows you to implement parts of your app using native-code languages such as C and C++. I have created only three functions in C: randomUnique(), charFrequencyTest(), and checkSpecialCharacter() in the file pickballjni.c in folder pickball/jni. The learning part was very important for me during the NDK-based Android game development.


It is a very simple game. The game has no levels because every time the player plays at a new level generated by the game. Player doesn't know how many chances it will take to win a game. To play this game, the player has to enter a minimum 5 and maximum 7 character string. The string can contain only a-z, A-Z and 0-9 characters. Other characters are not allowed. Click on Start button, and a peacock feather image button will appear. To find a matching character, the player has to click on a peacock feather button. If character matches then it will show in green circle otherwise shows in red circle. The game has two empty glasses. One glass is for green and one for red circle. The green or red circle will appear in the glass according to the game condition. The player will play the game while the winning message does not appear. For this player click on the Play again button for a next chance.

Technical Information

Android applications run in the Dalvik virtual machine. The NDK allows you to implement parts of your application using native-code languages such as C and C++. This can provide benefits to certain classes of applications, in the form of reuse of existing code and in some cases increased speed. More....

Native Code Compilation

  1. Download eclipse Download Link
  2. Download Android SDK Download Link
  3. Download Cygwin.
  4. Install packages Devel/make and Shells/bash
  5. Download NDK unzipped on root drive.
  6. Output Image

  7. Add pickball project in Eclipse
  8. Right click on project pickball-->Run As--->Run configuration-->Select Target Device (Inter Atom (x86))
  9. Output Image

  10. Run
  11. To check AVD configuration: Eclipse Windows Menu---->Click on AVD Manager

Output Image

Intel x86 Emulator Game View

The game screenshot in Intel x86 Emulator.

Output Image

Output Image

Output Image

Game Menu Section

Game menu has four options: Help, New, Stop, History.

  • Help: This option is for game help, playing instruction.
  • New: This option is for loading new game.
  • Stop: This option is for stopping current game.
  • History: This option is to view last game history.

Using the code (native code)

The native code file pickballjni.c is in the pickball/jni folder. The functions are randomUnique(), charFrequencyTest() and checkSpecialCharacter() in the file pickballjni.c. The method randomUnique() generates a unique random number in an array, the array length depends on the user input string and also depends on the player's next chance. String length minimum is 5 and maximum is 7.

//generate random unique no. in array
jintArray Java_com_example_pickball_MainActivity_randomUnique(JNIEnv* env,
        jobject javaThis, jint iL) {
    //return (pointer)
    jintArray ret;
    //Create the instance to return
    ret = (*env)->NewIntArray(env, (jsize) iL);
    if (ret == NULL) {
        return NULL;
    jint arrayL[iL];
    int iNew, iLT, iL1;
    for (iL1 = 0; iL1 < iL; iL1++) {
    iLT = 0;
    while (iLT < iL) {
        iNew = rand() % 10;
        do {
            if (iNew < iL)
            iNew = rand() % 10;
        } while (1);
        iL1 = 0;
        for (iL1 = 0; iL1 < iL; iL1++) {
            if (iNew == arrayL[iL1]) {
        if (iL1 == iL) {
            arrayL[iLT] = iNew;
            iLT = iLT + 1;
    (*env)->SetIntArrayRegion(env, ret, 0, iL, arrayL);

    // int array
    return ret;

The next native code function is charFrequencyTest(), in this game the max character frequency is allowed any two characters only.

// check character frequency
jboolean Java_com_example_pickball_MainActivity_charFrequencyTest(JNIEnv* env,
        jobject javaThis, jstring scheck) {
    jboolean bresult = 0;
    const char *nativeString = (*env)->GetStringUTFChars(env, scheck, 0);char ch;
    int iSL = strlen(nativeString),iStart,ichcounter;
    if (iSL > 0) {
        ch = nativeString[0];
        iStart = 1; ichcounter = 1;
        for (; iStart < iSL; iStart++) {
            char chl = nativeString[iStart];//scheck.charAt(iStart);
            if (ch == chl) {
            if (ichcounter > 2) {
                bresult = 1;
    } else {
        bresult = 1;
    return bresult;

The last native code function is checkSpecialCharacter(), user input string character is only a-z, A-Z, and 0-9. Special characters are not allowed in this game.

// check special character
jboolean Java_com_example_pickball_MainActivity_checkSpecialCharacter(JNIEnv* env,
        jobject javaThis, jstring scheck) {
    jboolean bresult = 0;
    const char *nativeString = (*env)->GetStringUTFChars(env, scheck, 0);char ch;
    int iSL = strlen(nativeString),iStart;
    for (iStart = 0; iStart < iSL; iStart++) {
        ch = nativeString[iStart];
        if ((ch >= 'A' && ch <= 'Z')
                || (ch >= 'a' && ch <= 'z')
                || (ch >= '0' && ch <= '9')) {
        } else {
            bresult = 1;
    return bresult;

Using the Code (Java code)

We load the library and call the native code function in Jva. The function name starts with Java_[package]_[Activity/OtherClass]_[function_name](). Example: Java_com_example_pickball_MainActivity_checkSpecialCharacter().

public native int[] randomUnique(int isl);

public native boolean charFrequencyTest(String scheck);

public native boolean checkSpecialCharacter(String scheck);

static {

Calling native code function in Java

The method randomUnique() is called in the gameStartOnClickBlock() function. The function location is shown by the ******* comment.

// game start on click block
private void gameStartOnClickBlock() {
    // set background and text color
    // if user want to break or quit game, not allowed in middle of
    // game
    if (bIsGameStart) {
                "Not break, please click all feather buttons.",
    iTextLength = etTemp.getText().toString().trim().length();
    // game text checking action first time, special character,
    // frequency and length
    if (bCheckText == false) {
        if (resultFrequencyTest(etTemp.getText().toString().trim())) {
            Toast.makeText(_context, sMessage, Toast.LENGTH_LONG).show();
        bCheckText = true;
    // continue if length>0
    if (iTextLength > 0) {
        if (bGameText == false) {
            // first time loading
        iTotalPlay += 1;
        iClickCount = 0;
        // game loop start again action on click

        sText = tvTemp.getText().toString().trim();
        iL = sText.length();
        iLengthFirst = iL;

        //*********** ndk function call*****************
        iA = randomUnique(iL);
        //*********** ndk function call*****************
        sTextNew = tvTemp.getText().toString();
        // text view creation for correct and random
        // set TextView empty text string
        // image button creation and click creation
    } else {
        iLengthBar = iTotalLuckRatio = iTotalPlay = iAvgLuckRatio = iMatch = iNonMatch = 0;
        bStartAgain = bGameWin = false;
        bWinner = false;
        tvTemp.setText("Enter any 9 char string.");

The methods checkSpecialCharacter() and charFrequencyTest() are called in the resultFrequencyTest() function. The function location is shown by the ******* comment.

private boolean resultFrequencyTest(String scheck) {
    boolean bresult = false;
    int isl = scheck.length();
    if (isl >= 5 && isl <= 7) {

        //*********** ndk function call*****************
        bresult = checkSpecialCharacter(scheck);
        //*********** ndk function call*****************
        if (bresult == true) {
            sMessage = "Special character is not allowed.";
        if (bresult == false) {
            for (int istart = 0; istart < isl; istart++) {

                //*********** ndk function call*****************
                bresult = charFrequencyTest(scheck);
                //*********** ndk function call*****************
                if (bresult == true) {
                    sMessage = "Any character max frequency is 2 only.";
    } else {
        sMessage = "Input string length is min 5 and max 7.";
        bresult = true;
    return bresult;

How to play

Step 1: Enter any string to game home screen text box, click on Start button.

Step 2: Click on any image button which you want to like, after clicking all image button, the play_again button will appear in the next round.

Step 3: Repeat round again and again to win game. The progress bar is updated in every round.

Step 4: After winning the game, the progress bar status has two colors: Red and Black. The Red status shows your final average result. The Black status shows your complete round progress. To show complete history of last game, click on menu and the four option will show, and for History, click on the History button.

Step 5: You can view your last game history below by clicking the History button.

Points of Interest

I am an beginner in Android app development. First I created a complete game in Anrdoid, but at the time of submitting the game in the Android App Contest that I found the game must have Native Code also. At that time I didn't know about NDK and how to use native code in Android. Finally I learnt NDK development and built a very simple game. May be it is possible the game will not be as per the Android App Contest conditions, but I learnt so many new things in game development for the Android App Contest. Thanks.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

R S Dodiya
Technical Lead Wipro Ltd.
India India

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
Franc Morales1-Oct-14 13:42
memberFranc Morales1-Oct-14 13:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171207.1 | Last Updated 3 Nov 2012
Article Copyright 2012 by R S Dodiya
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid