org.moyoman.framework
Class Controller

java.lang.Object
  |
  +--org.moyoman.framework.Controller
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
GenerateController, ValidateController

public class Controller
extends Object
implements Serializable

This class and its derived classes controls the execution of commands to the server. This class and its derived classes act as the interface between the Moyoman server and the client api. See for example the org.moyoman.comm.client.DirectCommandExecutor class.

There is one Controller object for each game that is being played by the server. Note that in a computer - computer game, there would be one Controller object for each player.

There are also static methods in this class, such as for file management, or other tasks not related to a particular game.

See Also:
Serialized Form

Field Summary
private  Color color
          The color of the player in this game.
private static HashMap controllers
          The key is the GameId object, and the value is the Controller object.
protected  GameRecord gr
          The game record of the game.
private  Handicap handicap
          The handicap for this game.
protected  GameId id
          The id of the game.
private  float komi
          The komi for this game.
private  Mode mode
          The mode for the player in this game.
protected static Persister persister
          The persister is used for loading/saving games.
private static HashMap properties
          The key is the GameId object, and the value is a Properties object.
private  Rules rules
          The rules for this game.
protected  Scheduler scheduler
          The scheduler for this game.
 
Constructor Summary
protected Controller(Color color, Handicap handicap, Mode m)
          Create a new Controller object.
protected Controller(Color color, Handicap handicap, Mode mode, Rules rules, float komi)
          Create a new Controller object.
 
Method Summary
static void deleteGame(String name)
          Delete the specified game from permanent storage.
static void endGame(String name)
          Delete the game from temporary storage.
static boolean gameExists(GameId gid)
          Determine if the given game exists.
 Color getColor()
          Get the color of this player.
protected static Controller getController(GameId id)
           
 GameRecord getGameRecord()
          Get the game record for the specified game.
 Handicap getHandicap()
          Get the handicap of the game.
 GameId getId()
          Get the game id for this Controller.
 float getKomi()
          Get the komi for this game.
 Stone getLastMoveKo()
           
 Mode getMode()
          Get the mode for this player.
static Mode getMode(GameId gid)
          Get the mode for a particular game.
 short getMoveNumber()
          Get the number of the next move in this game.
protected static Persister getPersister()
          Get the Persister object.
 Properties getProperties()
          Get the properties for the specified game.
 Rules getRules()
          Get the rules for this game.
static Rules getRules(GameId gid)
          Get the rules for a particular game.
 Stone[] getSuicideMoves()
           
 Stone[] getSuperKoMoves()
           
static String[] listActiveGames()
          Get a list of all of the active games on the server.
static String[] listSavedGames()
          Get a list of all of the games in permanent storage on the server.
static Controller[] loadGame(String name)
          Make a game in permanent storage on the server active.
private  MoveDescriptor makeMove(Move move)
          Update the specified game with this move from the client.
 MoveDescriptor makeMove(Stone stone)
          Update the specified game with this move from the client.
static void onExit()
           
 MoveDescriptor pass()
          The client passes in the specified game.
 void resignGame()
          The client is resigning the specified game.
static Controller[] resumeActiveGame()
          Continue play from the most recent temporary game stored.
static Controller resumeActiveGame(GameId id)
          Continue play from the temporary game stored.
static void saveGame(String name, GameId black, GameId white)
          Save the specified game to permanent storage.
 void setProperties(Properties p)
          Set the properties for the specified game.
private  void setVars(Color color, Handicap handicap, Mode mode, Rules rules, float komi, GameId gid)
          Set the variables for the Controller object on construction.
 void takeBackMove()
          Take back a move in the specified game.
static void writeLastGameInformation(String blackId, String whiteId)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

id

protected GameId id
The id of the game.


gr

protected GameRecord gr
The game record of the game.


scheduler

protected Scheduler scheduler
The scheduler for this game.


persister

protected static Persister persister
The persister is used for loading/saving games.


controllers

private static HashMap controllers
The key is the GameId object, and the value is the Controller object.


properties

private static HashMap properties
The key is the GameId object, and the value is a Properties object.


color

private Color color
The color of the player in this game.


handicap

private Handicap handicap
The handicap for this game.


mode

private Mode mode
The mode for the player in this game.


rules

private Rules rules
The rules for this game.


komi

private float komi
The komi for this game.

Constructor Detail

Controller

protected Controller(Color color,
                     Handicap handicap,
                     Mode mode,
                     Rules rules,
                     float komi)
              throws InternalErrorException
Create a new Controller object.

Parameters:
color - The color of the player using the server to play this game.
handicap - The handicap of this game.
mode - The mode used by the player using the server to play this game.
rules - The rules for this game.
komi - The komi for this game.
Throws:
InternalErrorException - Thrown if the operation fails for any reason.

Controller

protected Controller(Color color,
                     Handicap handicap,
                     Mode m)
              throws InternalErrorException
Create a new Controller object.

Parameters:
color - The color of the player using the server to play this game.
handicap - The handicap of this game.
Throws:
InternalErrorException - Thrown if the operation fails for any reason.
Method Detail

setVars

private void setVars(Color color,
                     Handicap handicap,
                     Mode mode,
                     Rules rules,
                     float komi,
                     GameId gid)
              throws InternalErrorException
Set the variables for the Controller object on construction.

Parameters:
color - The color of the player using the server to play this game.
handicap - The handicap of this game.
mode - The mode used by the player using the server to play this game.
rules - The rules for this game.
komi - The komi for this game.
Throws:
InternalErrorException - Thrown if the operation fails for any reason.

getPersister

protected static Persister getPersister()
Get the Persister object.

Returns:
The Persister object.

gameExists

public static boolean gameExists(GameId gid)
Determine if the given game exists. The game could be one active on the server, or one that is stored in temporary or permanent storage.

Parameters:
gid - The game id being checked.
Returns:
true if the game exists, or false.

deleteGame

public static void deleteGame(String name)
                       throws NoSuchDataException,
                              InternalErrorException
Delete the specified game from permanent storage.

Parameters:
name - The game to be deleted.
Throws:
NoSuchDataException - Thrown if the id does not correspond to a stored game.
InternalErrorException - Thrown if the operation fails for any reason.

endGame

public static void endGame(String name)
                    throws NoSuchDataException,
                           InternalErrorException
Delete the game from temporary storage.

Parameters:
name - The game to be deleted.
Throws:
NoSuchDataException - Thrown if the id does not correspond to a stored game.
InternalErrorException - Thrown if the operation fails for any reason.

getGameRecord

public GameRecord getGameRecord()
Get the game record for the specified game.

Returns:
The game record.

getId

public GameId getId()
Get the game id for this Controller.

Returns:
The id of the game.

setProperties

public void setProperties(Properties p)
Set the properties for the specified game.

Parameters:
p - The properties to be set.

getLastMoveKo

public Stone getLastMoveKo()

getSuicideMoves

public Stone[] getSuicideMoves()

getSuperKoMoves

public Stone[] getSuperKoMoves()

getProperties

public Properties getProperties()
Get the properties for the specified game.

Returns:
A Properties object.

listActiveGames

public static String[] listActiveGames()
                                throws InternalErrorException
Get a list of all of the active games on the server.

Returns:
An array of GameId objects.
Throws:
InternalErrorException - Thrown if the operation fails for any reason.

listSavedGames

public static String[] listSavedGames()
                               throws InternalErrorException
Get a list of all of the games in permanent storage on the server.

Returns:
An array of GameId objects.
Throws:
InternalErrorException - Thrown if the operation fails for any reason.

getController

protected static Controller getController(GameId id)

loadGame

public static Controller[] loadGame(String name)
                             throws NoSuchDataException,
                                    InternalErrorException
Make a game in permanent storage on the server active. Once this operation is successfully completed, the user can continue play.

Parameters:
name - The name of the game.
Returns:
An array of Controller objects of length 2, where index 0 is black and index 1 is white.
Throws:
NoSuchDataException - Thrown if the game id cannot be found.
InternalErrorException - Thrown if the operation fails for any reason.

makeMove

public MoveDescriptor makeMove(Stone stone)
                        throws IllegalMoveException,
                               InternalErrorException
Update the specified game with this move from the client.

Parameters:
stone - The move that was made.
Returns:
A MoveDescriptor object indicating prisoners, ko, etc.
Throws:
IllegalMoveException - Thrown if the move to be made is not legal.
InternalErrorException - Thrown if the operation fails for any reason.

makeMove

private MoveDescriptor makeMove(Move move)
                         throws IllegalMoveException,
                                InternalErrorException
Update the specified game with this move from the client.

Parameters:
move - The move to be made.
Returns:
A MoveDescriptor object indicating prisoners, ko, etc.
Throws:
IllegalMoveException - Thrown if the move to be made is not legal.
InternalErrorException - Thrown if the operation fails for any reason.

pass

public MoveDescriptor pass()
                    throws InternalErrorException
The client passes in the specified game.

Throws:
InternalErrorException - Thrown if the operation fails for any reason.

resignGame

public void resignGame()
                throws InternalErrorException
The client is resigning the specified game. The game is removed from the list of active games. It is saved to or deleted from permanent storage.

Throws:
InternalErrorException - Thrown if the operation fails for any reason.

resumeActiveGame

public static Controller resumeActiveGame(GameId id)
                                   throws NoSuchDataException,
                                          InternalErrorException
Continue play from the temporary game stored.

Parameters:
id - The id of the game.
Returns:
A Controller object representing this game.
Throws:
NoSuchDataException - Thrown if the operation fails for any reason.
InternalErrorException

resumeActiveGame

public static Controller[] resumeActiveGame()
                                     throws NoSuchDataException,
                                            InternalErrorException
Continue play from the most recent temporary game stored. The purpose of this method is to allow for a game to be continued if a problem occurs, such as the software or operating system crashing.

Returns:
A Controller object representing this game.
Throws:
InternalErrorException - Thrown if the operation fails for any reason.
NoSuchDataException

saveGame

public static void saveGame(String name,
                            GameId black,
                            GameId white)
                     throws DataAlreadyExistsException,
                            InternalErrorException
Save the specified game to permanent storage. Currently, it is an error to try to save a game with an id that has already been used. In the future, an override flag could be added.

Throws:
DataAlreadyExistsException - Thrown if the new id is already in use.
InternalErrorException - Thrown if the operation fails for any reason.

takeBackMove

public void takeBackMove()
                  throws NoSuchDataException,
                         InternalErrorException
Take back a move in the specified game. Actually, this method takes back two moves, because presumably the player to move wants to return to the point just before his last move. This method can be called repeatedly to rollback to an arbitrary point, except that the operation is not guaranteed to work. The amount of history that is stored is a configurable parameter, so the first three calls to takeBackMove() might succeed, while the fourth one fails.

Throws:
NoSuchDataException - Thrown if the move history is not stored.
InternalErrorException - Thrown if the operation fails for any reason.

writeLastGameInformation

public static void writeLastGameInformation(String blackId,
                                            String whiteId)

getMoveNumber

public short getMoveNumber()
                    throws InternalErrorException
Get the number of the next move in this game.

Throws:
InternalErrorException - Thrown if the operation fails for any reason.

getColor

public Color getColor()
Get the color of this player.


getHandicap

public Handicap getHandicap()
Get the handicap of the game.

Returns:
A Handicap object.

getMode

public Mode getMode()
Get the mode for this player.

Returns:
A Mode object.

getRules

public Rules getRules()
Get the rules for this game.

Returns:
A Rules object.

getKomi

public float getKomi()
Get the komi for this game. A float which is the komi.


getMode

public static Mode getMode(GameId gid)
                    throws NoSuchDataException
Get the mode for a particular game. This method only gets a mode for a game which is actively being played, not one in temporary or permanent storage.

Parameters:
gid - The game id to get the mode for.
Returns:
A Mode object.
Throws:
NoSuchDataException - Thrown if the game is not active.

getRules

public static Rules getRules(GameId gid)
                      throws NoSuchDataException
Get the rules for a particular game. This method only gets the rules for a game which is actively being played, not one in temporary or permanent storage.

Parameters:
gid - The game id to get the mode for.
Returns:
A Rules object.
Throws:
NoSuchDataException - Thrown if the game is not active.

onExit

public static void onExit()