org.jogre.common
Class JogreModel

java.lang.Object
  extended byjava.util.Observable
      extended byorg.jogre.common.JogreModel
All Implemented Interfaces:
IJogreModel, ITransmittable

public abstract class JogreModel
extends Observable
implements IJogreModel, ITransmittable

Abstract class which holds the state/model of a particular game when extended. This class extends the Observable class so that JogreComponent classes can update themselfs depending on changes made in this classes.

For a very simple example, to create the model for a game of tic-tac-toe a class TicTacToeModel should extend this class and add a 3x3 two dimensional int array i.e.

int [][] = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}.

A model can also store itself on a JogreServer. This is because this class implements the IJogreModel interface so if state is to be stored this method must be overewritten. The length () method must also be overwritten as the state is transferred along with a Table. To set the state of a JogreModel the setState (String inString) method must be overwritten. The constructor wasn't used (like the constructor of Comm objects) as this reference must be kept.

Version:
Alpha 0.2.1
Author:
Bob Marks

Field Summary
protected  int gameType
          Game type from IJogreModel e.g.
static String XML_NAME
          Header token used in ITransmittable.flatten() method.
 
Fields inherited from interface org.jogre.client.IJogreModel
GAME_TYPE_REAL_TIME, GAME_TYPE_TURN_BASED
 
Constructor Summary
protected JogreModel()
          Blank constructor which can be called by a sub class.
  JogreModel(int gameType)
          Create a new jogre model which sets the game type.
 
Method Summary
 XMLElement flatten()
          Returns the state/snapshot of a particular game.
 int length()
          Return the number of tokens that this class creates when flattened.
 void refreshObservers()
          Refresh observers - calls the setChanged() and notifyObservers () methods in the Observable class.
 void setState(XMLElement message)
          Set the state of the model (set values of the model equal to the values of this model by copying its values.
abstract  void start()
          Reset method which is called every time a new game is created.
 
Methods inherited from class java.util.Observable
addObserver, clearChanged, countObservers, deleteObserver, deleteObservers, hasChanged, notifyObservers, notifyObservers, setChanged
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

XML_NAME

public static final String XML_NAME
Header token used in ITransmittable.flatten() method.

See Also:
Constant Field Values

gameType

protected int gameType
Game type from IJogreModel e.g. GAME_TYPE_TURN_BASED or GAME_TYPE_REAL_TIME.

Constructor Detail

JogreModel

protected JogreModel()
Blank constructor which can be called by a sub class.


JogreModel

public JogreModel(int gameType)
Create a new jogre model which sets the game type.

Parameters:
gameType -
Method Detail

start

public abstract void start()
Reset method which is called every time a new game is created. Each sub class of this must have this method. This method is called when the game starts for the first time.


refreshObservers

public void refreshObservers()
Refresh observers - calls the setChanged() and notifyObservers () methods in the Observable class.


setState

public void setState(XMLElement message)
              throws TransmissionException

Set the state of the model (set values of the model equal to the values of this model by copying its values. The inString is created from the flatten () method of JogreModel. The implementation of this method in the subclass should call the refreshObservers() method so that the screen gets updated correctly.

Note: This is a marker method (not abstract) and only needs to be implemented if a games stores its state on the server Table object.

Parameters:
message - XMLElement created from flatten () method of this object.
Throws:
TransmissionException

length

public int length()

Return the number of tokens that this class creates when flattened. If a game doesn't hold state then this method simply returns -1.

Note: This is a marker method (not abstract) and only needs to be implemented if a games stores its state on the server Table object.

Returns:
Number of tokens that the flatten () method should create.

flatten

public XMLElement flatten()

Returns the state/snapshot of a particular game.

Note: This is a marker method (not abstract) and only needs to be implemented if a games stores its state on the server Table object.

Specified by:
flatten in interface ITransmittable
Returns:
XML version of the state of an object which can be constructed again.
See Also:
ITransmittable.flatten()