A Complete Android Game

And here’s the source code:

package com.pdg.android.sandbox;

import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class SandboxActivity extends Activity {
    private static final int INITIAL_GUESS_COUNT = 0;
	private static final int NUMBER_MAXIMUM = 100;
	private static final int NUMBER_MINIMUM = 1;
	private int guessCount;
	private Random random = new Random();
	private int number;

	/** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {

    private void startGame() {

	private void pickNumber() {

	private void setNumber(int theNumber) {
		number = theNumber;

	private int getNumber(){
		return number;

	private void updateGuessStatus() {
    	setGuessStatus(String.format(getString(R.string.guess_count_format), getGuessCount()));

	private void setGuessStatus(String theGuessStatus) {
		TextView tv = (TextView)findViewById(R.id.guessCount);

	private int getGuessCount() {
		return guessCount;

	private void setGuessCount(int theGuessCount) {
		guessCount = theGuessCount;

	private void setPrompt(String thePrompt) {
		TextView tv = (TextView)findViewById(R.id.prompt);

	public void guessButtonPressed(View view){
		EditText et = (EditText)findViewById(R.id.currentGuess);
		int theGuess = Integer.parseInt(et.getText().toString());

Yes, it is the perennial classic “Guess My Number 1 to 100” game that just about everyone has written.

Why did I write this?

Mainly, I’m teaching myself the view based xml ui for android.

This is how I do it.

By writing an entirely too simplistic game.

But now I know how to put resources correctly into xml files, how to grab widgets by id, and how to set text values on TextViews.

Of course, the thing still blows up if you don’t put anything into the edit box and press “Guess!”, but that’s a feature!


The Final Stretch?


Well, I’ve got all of the buttons on the main menu doing something, and my last two dialogs (theme selection and options) are now in and working.

Which means I now head into polish mode.  The game has been functionally there for quite some time, and now I’ve got all of the menuing and UI support stuff finished.

Typically, where I am at right now would be about as far as I’d go with a game. While I actually touched the real game code today (the stuff that actually contains game mechanics), it was a small change to support the options I put in.

From here on out, it is all small, barely noticeable changes that  take a lot of time and, truth be told, are really annoying and frustrating.

For example, there is one tweak that will be necessary involving the menu button and how it initially shows up in internet explorer. IE and fox handle the “right” style a little differently, and making code that works in both is a little hackish.

Another thing still to do is put in a “you win” dialog or something when the puzzle is complete. I’ve long since ditched the cheerleader, and I’m most likely to just go with a simple window popping up that states “you win! congrats!” and maybe show some statistics.

For a while, I had toyed with the idea of completely ditching <img> tags everywhere, and strictly go with html tags <input> <button> <div> for my graphical elements.  I could easily replace all of the images used on the board with <div>s, and the load time would be amazing, since there would be no images to download.

I have decided not to go that route for this game (I’d like to be done someday, and this sort of constraint reminds me a bit of my ASCII graphics and hexagonal board fetishes, and nobody likes an eccentric game developer) .


Menu Dialog

I’ve got a main menu dialog coming along for Connect!

While I originally used <input> and <button> tags, I am now ditching those in favor of my own “buttons” using <img> tags.  This is mainly so that I can get a consist look and feel across browsers. Also, buttons and check boxes are not terribly difficult to implement with an <img> tag: just four images and a few javascript handlers (onmouseover, onmouseout, onmousedown, onmouseup, and onclick).

For some things, I’m going to be stuck with the inconsistent rendering of various <input> tags, especially text boxes and combo boxes. I’ll go as far as reinventing the wheel with simple controls, but not when it comes to complex controls like that.

I’m really quite pleased with how Connect! is turning out. My earlier javascript work looked very cheesy and plain, but as I improve with my mad js/html skills, it is looking like nothing is too hard to do in js.

Which means that the programming world has become something like it was when I first started approximately 20 years ago, by which I mean that anybody can write software without buying big, expensive, complicated tools and IDEs and compilers.  I can make a game with notepad and MS Paint if I want (although I don’t… I use Notepad++ and Paint .NET)