Journey into SuperWaba

Ever wonder what it takes to write programs for your PDA? I did, and so now I am beginning a journey to try my hand at writing something for the PalmOS. SuperWaba, a variation of Java for the PDA, is my language of choice. And I'm a novice, which makes it all the more unpredictable. Hope you'll come along for the ride! Note: You can read multiple posts on one page if you click on an archive link.

Thursday, August 19, 2004

Program Shell for New HiLo Game Version

Here's the shell of the new version I'm working on. Three classes this time!
Formatting (tabs) is lost when I post so it's a little hard to read. Anyone know how to avoid that?
There is also some remnants left from the first version of HiLo in the guessing logic. It's only there as a start for me when I add guessing functionality. The point of this code is just to get the container stuff working.

==================
import waba.ui.*;
import waba.fx.*;
import waba.util.Random;
import waba.sys.Convert;

public class HiLoGame extends MainWindow {

public static WelcomeWindow welcomeWindow;
public static GameWindow gameWindow;
MessageBox mbAbout;
public static Graphics g;
MenuBar mbar;

public HiLoGame() {
// Do generic housekeeping to get started
highResPrepared = true;
waba.sys.Settings.setPalmOSStyle(true);
g = new Graphics(this);
setDoubleBuffer(true);
// Set up this programs overall window
// Could probably replace with:
// super("HiLo Guessing Game",TAB_ONLY_BORDER);
setBorderStyle(TAB_ONLY_BORDER);
setTitle("HiLo Guessing Game");
// Prepare this for use in the about menu item
mbAbout = new MessageBox("BobSoft",
"This is Bob's HiLo Guessing Game on Palm!|Enjoy..." );
}

public void onStart() {
// gameWindow is the screen where the game is played
gameWindow = new GameWindow();
//Show the welcome window on startup
welcomeWindow = new WelcomeWindow();
swap(welcomeWindow);

// Setup a menubar
String menu[][] =
{
{"File", "NewGame", "*Options","*HiScores","Exit"},
{"Help","About", "*Test"}
};
mbar = new MenuBar(menu);
setMenuBar(mbar);
}

public void onEvent(Event event) {
switch (event.type)
{
case ControlEvent.WINDOW_CLOSED:
String sel = null;
if (event.target == mbar)
switch (mbar.getSelectedMenuItem())
{
case -1: break; // no item selected
case 101: popupModal(mbAbout); break;
case 102: break;
case 001: /*end game*/ //playGame();
setTitle("HiLo Guessing Game");
break;
case 004: exit(0); break; // End Program
}
}
}

public void onPaint(Graphics g) {

}
}
=================

/** Container class to handle the welcome screen
*/

import waba.fx.*;
import waba.sys.*;
import waba.util.*;
import waba.ui.*;

public class WelcomeWindow extends Container {
Button exitBtn, beginBtn;

public WelcomeWindow()
{
setBorderStyle(BORDER_LOWERED);
}

public void onStart()
{
add(new Label("Welcome|to|HiLo Game|by|BobSoft"),CENTER,CENTER);
add(exitBtn = new Button("Exit Program"), RIGHT,BOTTOM-5);
add(beginBtn = new Button("Begin Game"), LEFT,BOTTOM-5);
}

public void onEvent(Event event)
{
if (event.type == ControlEvent.PRESSED)
{
if (event.target == beginBtn)
{
MainWindow.getMainWindow().swap(HiLoGame.gameWindow);
}
else if (event.target == exitBtn)
MainWindow.getMainWindow().exit(0);
}
}
}

===================

/** Container class to handle the game playing screen
*/

import waba.fx.*;
import waba.sys.*;
import waba.util.*;
import waba.ui.*;

public class GameWindow extends Container {
Button exitBtn, beginBtn;
String guessDialogButtons[] = {"Guess", "Abort Game"};
InputDialog getGuess;
int target, guess, guessNbr;
int guessCnt;
String resultText, promptText;
Random randomObject;


public GameWindow()
{
setBorderStyle(BORDER_LOWERED);
randomObject = new Random();
}

public void initNewGame() {
target = randomObject.rand(1,100);
guessCnt = 1;
guess = 0;
promptText = "Enter Guess #"+guessCnt+":";
HiLoGame.g.clearScreen();
};

public void playGame() {
initNewGame();
resultText = "This is your first guess.";
do {
getGuess = new InputDialog(resultText, promptText, "", guessDialogButtons);
//popupBlockingModal(getGuess);
guess = Convert.toInt(getGuess.getValue());
resultText = "Your last guess of ("+guess+") was "+calcGuessResult();
guessCnt++;
promptText = "Enter Guess #"+guessCnt+":";
}
while (getGuess.getPressedButtonIndex()==0);
}

public String calcGuessResult() {
String result;
if (guess > target) result = new String("too high.");
else if (guess < target) result = new String("too low.");
else { result = new String("correct on "+guessCnt+" tries!");
initNewGame(); guessCnt = 0;
promptText = "Starting new game... " + promptText;
}
return result;
}

public void onStart()
{
add(new Label("Play Game Here!"),CENTER,CENTER);
add(exitBtn = new Button("Exit Game"), RIGHT,BOTTOM-5);
add(beginBtn = new Button("Begin Game"), LEFT,BOTTOM-5);
}

public void newGame()
{
//Set everything to newGame status and reset the screen widgets
}

public void onEvent(Event event)
{
if (event.type == ControlEvent.PRESSED)
{
if (event.target == beginBtn)
newGame();
else if (event.target == exitBtn)
MainWindow.getMainWindow().exit(0);
}
}
}

============================

0 Comments:

Post a Comment

<< Home