Introduction
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.
Description
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
- Download eclipse Download Link
- Download Android SDK Download Link
- Download Cygwin.
- Install packages Devel/make and Shells/bash
- Download NDK android-ndk-r8b-windows.zip unzipped on root drive.

- Add pickball project in Eclipse
- Right click on project pickball-->Run As--->Run configuration-->Select Target Device (Inter Atom (x86))

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

Intel x86 Emulator Game View
The game screenshot in Intel x86 Emulator.



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.
jintArray Java_com_example_pickball_MainActivity_randomUnique(JNIEnv* env,
jobject javaThis, jint iL) {
jintArray ret;
ret = (*env)->NewIntArray(env, (jsize) iL);
if (ret == NULL) {
return NULL;
}
jint arrayL[iL];
int iNew, iLT, iL1;
for (iL1 = 0; iL1 < iL; iL1++) {
arrayL[iL1]=-1;
}
iLT = 0;
while (iLT < iL) {
iNew = rand() % 10;
do {
if (iNew < iL)
break;
iNew = rand() % 10;
} while (1);
iL1 = 0;
for (iL1 = 0; iL1 < iL; iL1++) {
if (iNew == arrayL[iL1]) {
break;
}
}
if (iL1 == iL) {
arrayL[iLT] = iNew;
iLT = iLT + 1;
}
}
(*env)->SetIntArrayRegion(env, ret, 0, iL, arrayL);
return ret;
}
The next native code function is charFrequencyTest(), in this game
the max character frequency is allowed any
two characters only.
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]; if (ch == chl) {
ichcounter++;
}
if (ichcounter > 2) {
bresult = 1;
break;
}
}
} 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.
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;
break;
}
}
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 {
System.loadLibrary("pickball");
}
Calling native code function in Java
The method randomUnique() is called in the gameStartOnClickBlock() function.
The function location is shown by the ******* comment.
private void gameStartOnClickBlock() {
tvTemp.setBackgroundDrawable(null);
tvTemp.setTextColor(Color.WHITE);
if (bIsGameStart) {
Toast.makeText(_context,
"Not break, please click all feather buttons.",
Toast.LENGTH_LONG).show();
return;
}
iTextLength = etTemp.getText().toString().trim().length();
if (bCheckText == false) {
if (resultFrequencyTest(etTemp.getText().toString().trim())) {
Toast.makeText(_context, sMessage, Toast.LENGTH_LONG).show();
return;
}
bCheckText = true;
}
if (iTextLength > 0) {
if (bGameText == false) {
fristTimeLoad();
}
iTotalPlay += 1;
iClickCount = 0;
llTemp.removeAllViews();
llText.removeAllViews();
llCorrect.removeAllViews();
startGameAgain();
tvTemp.setText(etTemp.getText());
sText = tvTemp.getText().toString().trim();
iL = sText.length();
iLengthFirst = iL;
iA = randomUnique(iL);
sTextNew = tvTemp.getText().toString();
textViewCreation();
tvTemp.setText("");
imageButtonCreation();
} else {
iLengthBar = iTotalLuckRatio = iTotalPlay = iAvgLuckRatio = iMatch = iNonMatch = 0;
bStartAgain = bGameWin = false;
bWinner = false;
tvTemp.setText("Enter any 9 char string.");
llTemp.removeAllViews();
llText.removeAllViews();
llCorrect.removeAllViews();
llResult.removeAllViews();
ivGreenGlass.setImageResource(R.drawable.g0);
ivRedGlass.setImageResource(R.drawable.r0);
}
}
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) {
bresult = checkSpecialCharacter(scheck);
if (bresult == true) {
sMessage = "Special character is not allowed.";
}
if (bresult == false) {
for (int istart = 0; istart < isl; istart++) {
bresult = charFrequencyTest(scheck);
if (bresult == true) {
sMessage = "Any character max frequency is 2 only.";
break;
}
}
}
} 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.