bluegammon
Class Bluegammon

java.lang.Object
  extended bybluegammon.Bluegammon
All Implemented Interfaces:
javax.microedition.lcdui.CommandListener, PopupListener

public class Bluegammon
extends java.lang.Object
implements PopupListener, javax.microedition.lcdui.CommandListener

The Bluegammon class represents the Bluegammon MIDlet game altogether. It coordinates the macro state of the application.

This class collects common functionality as starting and stopping a backgammon game, showing a popup, or getting string input from user. It is invoked statically since it mainly is a mediator.

Author:
Peter Andersson

Field Summary
static int AUDIO_OFF
          Key for audio off (boolean)
static int BLACK_PREFERRED
          Key for preferred color (boolean)
static int DEVICE_ID
          Key for device id (int)
static int GAME_TYPE_LOCAL
          Represents a game that is only played on this device
static int GAME_TYPE_REMOTE_CLIENT
          Represents a distributed game where this device acts client
static int GAME_TYPE_REMOTE_SERVER
          Represents a distributed game where this device acts server
static int GAMEREC_GAME_COUNT
          First key in gamerecords, number of games against this opponent (int)
static int GAMEREC_MY_SCORE
          First key in gamerecords, player score (int)
static int GAMEREC_OP_ID
          First key in gamerecords, device ids (int)
static int GAMEREC_OP_NAME
          First key in gamerecords, opponent name (char[])
static int GAMEREC_OP_SCORE
          First key in gamerecords, opponent score (int)
static int GAMEREC_SAVED_GAME_DATA
          First key in gamerecords, saved game data (byte[])
static int GAMEREC_TIMESTAMP
          First key in gamerecords, timestamp (long)
static int GAMERECORDS_SIZE
          Max number of opponent scores that can be persistent
static int HAS_SAVED_LOCAL_GAME
          Key for saved game boolean flag (boolean)
static int PLAYER_NAME
          Key for player name (char[])
static int RULES_PREFERRED
          Key for preferred rules (int)
static int SAVED_GAME_DATA
          Key for saved game data (byte[])
static int VIBRA_OFF
          Key for vibration off (boolean)
 
Method Summary
 void commandAction(javax.microedition.lcdui.Command c, javax.microedition.lcdui.Displayable d)
           
static void exitGame()
          Exits a game.
static PopupCanvas getCanvas()
          Returns current canvas
static Popup getCurrentPopup()
          Returns current displayed popup
static int getGameType()
          Returns current game type, one of GAME_TYPE_LOCAL, GAME_TYPE_REMOTE_SERVER, GAME_TYPE_REMOTE_CLIENT.
static void getStringInput(java.lang.String title, char[] defaultText, int length, int constraints, StringInputHandler ih)
          Gets input from the user.
static boolean hasSavedLocalGame()
          Returns true if there exists a saved phone game
static void init(BluegammonMIDlet bg, javax.microedition.lcdui.Display display)
          Initializes this class.
static boolean isShowingPopup()
          Returns true if a popup is currently displayed to the user
static void resumeSavedLocalGame()
          Resumes a saved game
 void selectedChoice(byte choice, boolean timeOut)
          Called when an selection has been made.
static void setBackgammonConnection(BackgammonConnection conn)
          Sets the connection used in remote game.
static void setCanvas(PopupCanvas c)
          Sets specified canvas as current
static Popup showPopup(char[] text, char[][] altTexts, int timeOutInSeconds, int defaultChoice, int timeOutChoice, PopupListener listener)
          Shows a popup on the device display, enabling the user to make a choice amongst specified alternatives.
static void showRules()
          Displays a popup with current rule settings.
static void shutdown()
          Shuts down the MIDlet.
static void startLocalGame()
          Start a new game for two players on one phone.
static void startRemoteGame(boolean server, char[] localName)
          Starts a remote game, either as server or as client.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEVICE_ID

public static final int DEVICE_ID
Key for device id (int)

See Also:
Constant Field Values

PLAYER_NAME

public static final int PLAYER_NAME
Key for player name (char[])

See Also:
Constant Field Values

HAS_SAVED_LOCAL_GAME

public static final int HAS_SAVED_LOCAL_GAME
Key for saved game boolean flag (boolean)

See Also:
Constant Field Values

SAVED_GAME_DATA

public static final int SAVED_GAME_DATA
Key for saved game data (byte[])

See Also:
Constant Field Values

BLACK_PREFERRED

public static final int BLACK_PREFERRED
Key for preferred color (boolean)

See Also:
Constant Field Values

AUDIO_OFF

public static final int AUDIO_OFF
Key for audio off (boolean)

See Also:
Constant Field Values

VIBRA_OFF

public static final int VIBRA_OFF
Key for vibration off (boolean)

See Also:
Constant Field Values

RULES_PREFERRED

public static final int RULES_PREFERRED
Key for preferred rules (int)

See Also:
Constant Field Values

GAMERECORDS_SIZE

public static final int GAMERECORDS_SIZE
Max number of opponent scores that can be persistent

See Also:
Constant Field Values

GAMEREC_OP_ID

public static final int GAMEREC_OP_ID
First key in gamerecords, device ids (int)

See Also:
Constant Field Values

GAMEREC_OP_NAME

public static final int GAMEREC_OP_NAME
First key in gamerecords, opponent name (char[])

See Also:
Constant Field Values

GAMEREC_MY_SCORE

public static final int GAMEREC_MY_SCORE
First key in gamerecords, player score (int)

See Also:
Constant Field Values

GAMEREC_OP_SCORE

public static final int GAMEREC_OP_SCORE
First key in gamerecords, opponent score (int)

See Also:
Constant Field Values

GAMEREC_GAME_COUNT

public static final int GAMEREC_GAME_COUNT
First key in gamerecords, number of games against this opponent (int)

See Also:
Constant Field Values

GAMEREC_TIMESTAMP

public static final int GAMEREC_TIMESTAMP
First key in gamerecords, timestamp (long)

See Also:
Constant Field Values

GAMEREC_SAVED_GAME_DATA

public static final int GAMEREC_SAVED_GAME_DATA
First key in gamerecords, saved game data (byte[])

See Also:
Constant Field Values

GAME_TYPE_LOCAL

public static int GAME_TYPE_LOCAL
Represents a game that is only played on this device


GAME_TYPE_REMOTE_SERVER

public static int GAME_TYPE_REMOTE_SERVER
Represents a distributed game where this device acts server


GAME_TYPE_REMOTE_CLIENT

public static int GAME_TYPE_REMOTE_CLIENT
Represents a distributed game where this device acts client

Method Detail

getGameType

public static int getGameType()
Returns current game type, one of GAME_TYPE_LOCAL, GAME_TYPE_REMOTE_SERVER, GAME_TYPE_REMOTE_CLIENT.

Returns:
The current game type.

setBackgammonConnection

public static void setBackgammonConnection(BackgammonConnection conn)
Sets the connection used in remote game. Must be set before invoking startRemoteGame

Parameters:
conn - The connection used with remote player.

hasSavedLocalGame

public static boolean hasSavedLocalGame()
Returns true if there exists a saved phone game

Returns:
true if saved game exists, false otherwise

startLocalGame

public static void startLocalGame()
Start a new game for two players on one phone.


startRemoteGame

public static void startRemoteGame(boolean server,
                                   char[] localName)
Starts a remote game, either as server or as client. This method uses the connection set in method setBackgammonConnection. It handshakes with the other device and starts a new game, or resumes a game if any of the devices has a saved game. On conflicting preferred settings (color and rules) the server wins.

Parameters:
server - True if server, false if client.
localName - The name of this player.
See Also:
setBackgammonConnection(BackgammonConnection)

resumeSavedLocalGame

public static void resumeSavedLocalGame()
Resumes a saved game


exitGame

public static void exitGame()
Exits a game. If the game is not finished it is stored, so it can be resumed later.


shutdown

public static void shutdown()
Shuts down the MIDlet.


showRules

public static void showRules()
Displays a popup with current rule settings.


getStringInput

public static void getStringInput(java.lang.String title,
                                  char[] defaultText,
                                  int length,
                                  int constraints,
                                  StringInputHandler ih)
Gets input from the user. Runs asynchronously, i.e. this method does not lock. User input is reported to specified InputHandler.

Parameters:
title - The title of the input dialog.
defaultText - The default text presented in the input dialog.
length - The maximum length of the input.
constraints - The constraints of the text as defined in TextField.
ih - The input handler, which handles the input when the user commits the text.

setCanvas

public static void setCanvas(PopupCanvas c)
Sets specified canvas as current

Parameters:
c - The canvas to set

getCanvas

public static PopupCanvas getCanvas()
Returns current canvas

Returns:
Current canvas

isShowingPopup

public static boolean isShowingPopup()
Returns true if a popup is currently displayed to the user

Returns:
true if a popup is displayed

getCurrentPopup

public static Popup getCurrentPopup()
Returns current displayed popup

Returns:
Current popup or null if no popup is displayed

showPopup

public static Popup showPopup(char[] text,
                              char[][] altTexts,
                              int timeOutInSeconds,
                              int defaultChoice,
                              int timeOutChoice,
                              PopupListener listener)
Shows a popup on the device display, enabling the user to make a choice amongst specified alternatives. Selected alternative is reported to specified listener. The popup has a timeout - if this timeout is reached the listener receives the specified timeout choice. If the user already has a popup open, a call to this method makes current popup go away and the listener to current popup is reported with the timeout choice. After this, specified popup is presented. Thus, it is the responsibility of the server or proxy layer to buffer multiple incoming popups. To check if a popup is currently displayed, use Bluegammon.getCurrentPopup() or Bluegammon..isShowingPopup()

Parameters:
text - Character array with text presented in popup.
altTexts - Array of character arrays with alternatives presented in popup. If this argument is null, no alternatives are presented.
timeOutInSeconds - The timeout in seconds. If this argument is set to zero, the popup is displayed until the a user makes a choice. If no choices are given and the timeout is zero, this popup will not show.
defaultChoice - The default selection when the popup appears.
timeOutChoice - The choice reported if the popup times out or is overridden by another popup.
listener - The listener to this popup.

init

public static void init(BluegammonMIDlet bg,
                        javax.microedition.lcdui.Display display)
Initializes this class.

Parameters:
bg - The bluegammon midlet.
display - The display.

selectedChoice

public void selectedChoice(byte choice,
                           boolean timeOut)
Description copied from interface: PopupListener
Called when an selection has been made.

Specified by:
selectedChoice in interface PopupListener
Parameters:
choice - The index of the alternatives that were chosen.
timeOut - True if the choice was made because of a time out.

commandAction

public void commandAction(javax.microedition.lcdui.Command c,
                          javax.microedition.lcdui.Displayable d)
Specified by:
commandAction in interface javax.microedition.lcdui.CommandListener