CS101 Lab Activity 9

Objectives

1. Requirements Specification (Problem Definition)

In this lab, you're going to design a Hangman game. The game works as follows:











The other major component to this lab is that you'll be working with is Google Android. Google Android is a bleeding edge mobile phone operating system. It is not a phone itself, but similar to Linux, MacOS, and Windows that all run PCs, Google Android is software that runs a mobile phone. It has emerged as the major competitor to the Apple iPhone, and already there are several Google Android phones that you can buy right now. Android is Linux-based (and open source, for that matter), so it works well with the Linux computers in the lab.

Since it is possible in Android to write programs using C, you will be creating an application in this lab that will be able to run on a Google Android mobile phone.



2. Getting Started With Android

The first thing to do is a little setup for Android. There is a handy little script you should run to do the boring stuff. You should copy the lab9-setup.sh file from ~cs101ta/public_html/labs/lab9 to your home folder. Then, in your home folder, type the command:

sh lab9-setup.sh

Setting up Eclipse

Eclipse is an IDE, a.k.a. Integrated Development Environment. It's like a very beefed up gedit, but it's designed to make programming as easy and organized as possible. It has a lot of rich features, and also will allow us to use Google Android much more easily than if we were working solely from the command line(although you can do everything from the command line).

To run Eclipse, open a terminal at your home directory, and type:

~cs101ta/public_html/labs/lab9/eclipse/eclipse

Eclipse should start up now. Wait a moment for the SDK to load. You can see its progress on the bottom right of the Eclipse window. A few Android-related windows may pop up automatically now, but just click past them. Choose Window->Android SDK and AVD Manager from the menu. Click "New". In "Name" type your netid. In Target choose “Android 2.2 - API Level 8”. Click “Create AVD”. Choose your netid from the "List of existing Android Virtual Devices". Click "Start". Then, click “Launch”. When the emulator window appears, you can close the Android SDK and AVD Manager window.

The Google Android emulator should start now. It will take a couple minutes. This is exactly how Google Android would look on a real mobile phone. Once it's loaded, feel free to play around with your Android phone. You can run the pre-loaded apps and browse the web. You can do anything except make calls :-)

We're going to make our own cell phone app. First, we need to start the HangmanDemo program. In the Eclipse window, look on the left side and locate the HangmanDemo project tree. Right-click on the HangmanDemo project and choose Run As->Android Application from the context menu that appears. Now, switch back to your emulator window, and the HangmanDemo program should appear in a few seconds.

Troubleshooting:
If you get an error in Eclipse that you can't figure out, you might want to try running this handy little script called lab9-reset.sh. This script will reset everything related to lab9 while saving any code you've written already. Copy lab9-reset.sh from the ~cs101ta/public_html/labs/lab9 directory to your home directory, and run it by typing:

sh lab9-reset.sh

After running the script, run Eclipse again and see if your issues are resolved.

***NOTE***: The Android emulator causes the system to become a little unstable. Logging out of your computer the normal way will cause the machine to freeze. In order to logout of your machine, open a terminal and type this command:

killall -u netid

where 'netid' is your netid.

Watch how the demo program works. You're going to write a similar program.



3. Analysis---Refine, Generalize, Decompose the problem definition

We will give you the skeleton code in ~/workspace/Hangman/jni/hangman.cpp that includes the steps that you need to go through in your program (see step 4. Implementation below). But of course, you can start to write the program from scratch if you prefer. You should consider using the following functions in your program:



4. Design---Develop Algorithm

The complete flowchart for the program is shown below. In order to write your code you have to open the file ~/workspace/Hangman/jni/hangman.cpp . Use Eclipse or gedit to code your program in hangman.cpp . You must scroll down to the hangmanIntro function. Everything from there on is code that you should edit. There are four functions that you must fill in: hangmanIntro, prepareHangman, hangmanGuess, playHangman. The part of code associated with these functions is labeled in the flowchart.

You are actually only going to write part of the structure of the code. Some of the code is written in another language called Java, and you will not be editing that, but it will be represented in the flowchart. If you're curious to see what the Java code looks like(it's very similar to C code), you can look at the Hangman.java file in Eclipse. It's in the src directory if you don't already see it. The parts of the flowchart written in Java will be colored purple. Notice that the Java code part of the flowchart forms an infinite loop. This means the only way to exit this program is by using something in Android to end the program, like the arrow return button next to the menu button.

There are three helper functions that you will need to use. In this lab we will not be using printf or scanf. Instead of scanf, we will use the function scanAndroid. It has the same parameters as scanf, but is more limited. It can only read a single character or an integer from Android. Instead of printf, we will use printAndroid to print strings, and printAndroidInt to print integers. The printAndroid and printAndroidInt functions cannot use %s or %i . Instead, pass just a string or an integer to them respectively. For example:

char *myString = “I love Google Android”;

int myInt = 7337;

char choice;

scanAndroid(“%c”, &choice);

printAndroid(myString);

printAndroid(“I also love Google Android”);

printAndroidInt(myInt);










5. Implementation --- Write the "Program" (Code)



Use the flow chart above to type your code in the file named hangman.cpp. You can use either Eclipse or gedit to write your code.

6. Run the code

Always make sure to save the code that you've written.

In order to compile the code for this system, it requires a different process than with simply compiling C code. First, make sure you've followed the above instructions for opening the Android emulator. Then, open a terminal and navigate by the command line to ~/workspace/Hangman/jni . When you've navigated there, you should type this command to compile your code:

~cs101ta/public_html/labs/lab9/android/android-ndk-r4b/ndk-build

If your code doesn't have any errors and all your files are in the right place, then this command should spit out a few lines that look like this:

Install : libhangman.so => /home/engr/mynetid/workspace/Hangman/libs/armeabi

Once you get this output, go back to Eclipse and right click on the Hangman project, and choose “Refresh”. Then you can run your program in the Android emulator and hope for the best! To do this, start the emulator if it isn't already started. Then, right click on the Hangman project and choose Run As->Android Application from the context menu.

You can check your work by running HangmanDemo as described above.


After you've completed your program, then answer questions #1, #2 and #3 on the Answer sheet.

***NOTE***: The Android emulator causes the system to become a little unstable. Logging out of your computer the normal way will cause the machine to freeze. In order to logout of your machine, open a terminal and type this command:

killall -u netid

where 'netid' is your netid.


The End!