es.ucm.fdi.hysteria.console
Class Engine

java.lang.Object
  extended by es.ucm.fdi.hysteria.console.Engine

public class Engine
extends java.lang.Object

Represents the game engine that controls the execution of the game since the beginning to the end, dealing with the configuration, the input stream and the output stream.

Author:
MiniSandor

Field Summary
protected  GameConfig _config
          Configuration
protected  java.util.Deque<Game> _future
           
protected  Game _game
          The game
protected  Game _gameCopy
          A referential copy of the game.
protected  java.util.Deque<Game> _history
           
protected  java.io.InputStream _in
          Input Stream... just in case
protected  Message _lastMessage
           
protected  boolean _online
           
protected  java.io.ObjectInputStream _onlineIos
           
protected  java.io.ObjectOutputStream _onlineOos
           
protected  java.io.PrintStream _out
          Output Stream... just in case
protected  Parser _parser
          The parser
protected  MP3Player _player
           
protected  java.net.Socket _socket
           
static java.lang.String FLAG_AUTODESCRIBEFIRSTLOCATION
          Flag for auto-describing first location (the name of the property).
static java.lang.String FLAG_PLAYMUSIC
          Flag for playing music (the name of the property).
static java.lang.String FLAG_SHOWCONNECTIONS
          Flag for showing connections (the name of the property).
static java.lang.String FLAG_SHOWCONNECTIONS_STATE
           
static java.lang.String FLAG_SHOWENGINEINFO
          Flag for showing engine information (the name of the property).
static java.lang.String FLAG_SHOWGAMEINFO
          Flag for showing game information (the name of the property).
static java.lang.String FLAG_SHOWITEMVALUES
          Flag for showing the numerical value of the game items (the name of the property).
static java.lang.String FLAG_SHOWITEMWEIGHTS
          Flag for showing the weight of the game items (the name of the property).
static java.lang.String FLAG_SHOWLOCATIONITEMS
          Flag for showing the items of each location (the name of the property).
static java.lang.String LIMIT_COMMANDHISTORYSIZE
          Limit of command history size for the undo command (the name of the property).
static java.lang.String LIMIT_INVENTORYCAPACITY
          Limit of inventory capacity (the name of the property).
static java.lang.String MESSAGE_ENGINEHELP
          Message of help information from the engine (the name of the property).
static java.lang.String MESSAGE_GAMEOVER
          Message of game over (the name of the property).
static java.lang.String MESSAGE_LOCATIONWITHEXITS
           
static java.lang.String MESSAGE_LOCATIONWITHITEMS
          Message of location with items (the name of the property).
static java.lang.String MESSAGE_LOCATIONWITHOUTEXITS
           
static java.lang.String MESSAGE_LOCATIONWITHOUTITEMS
          Message of location without items (the name of the property).
static java.lang.String MESSAGE_OBSTACLE_CLOSED
          Message of obstacle closed found (the name of the property).
static java.lang.String MESSAGE_OBSTACLE_FREE
          Message of no obstacle found (the name of the property).
static java.lang.String MESSAGE_OBSTACLE_OPEN
          Message of obstacle opened found (the name of the property).
static java.lang.String MESSAGE_PLAYERSCORE
          Message of the player score (the name of the property).
static java.lang.String MESSAGE_PROMPT
          Message of prompt for the player (the name of the property).
static java.lang.String MESSAGE_UNKNOWNCOMMAND
          Message of unknown command (the name of the property).
static java.lang.String UNIT_POINTS
          Unit of measurement for points in-game This property is called "unit.points" and its default value is "points".
static java.lang.String UNIT_POINTS_AB
          Unit of measurement for weights in-game abbreviated This property is called "unit.points.ab" and its default value is "p's".
static java.lang.String UNIT_WEIGHT
          Unit of measurement for weights in-game This property is called "unit.weight" and its default value is "kilos".
static java.lang.String UNIT_WEIGHT_AB
          Unit of measurement for weights in-game abbreviated This property is called "unit.weight.ab" and its default value is "k's".
 
Constructor Summary
Engine(Game game)
          Constructs the game engine using a given game.
Engine(Game game, java.io.InputStream input)
          Constructs the game engine using a given game and a given input stream.
Engine(Game game, java.io.InputStream input, java.io.OutputStream output)
          Constructs the game engine using a given game, a given input stream and a given output stream.
Engine(Game game, java.io.OutputStream output)
          Constructs the game engine using a given game and a given output stream.
Engine(Game game, java.util.Properties config)
          Constructs the game engine using a given game and a given configuration.
Engine(Game game, java.util.Properties config, java.io.InputStream input)
          Constructs the game engine using a given game, a given configuration and a given output stream.
Engine(Game game, java.util.Properties config, java.io.InputStream input, java.io.OutputStream output)
          Constructs the game engine using a given game, a given configuration, a given input stream and a given output stream.
Engine(Game game, java.util.Properties config, java.io.OutputStream output)
          Constructs the game engine using a given game, a given configuration and a given input stream.
 
Method Summary
protected  int getRedoableCommands()
          It asks about the number of redo games stored
 java.net.Socket getSocket()
           
protected  int getUndoableCommands()
          It asks about the number of undo games stored
 java.util.Vector<UserFileData> getUserFileVector()
           
 boolean isOnline()
           
 boolean loadGame(java.lang.String path)
           
 boolean redo()
          It requests a cloned and future game to be the principal game
 java.lang.String reportLocationInfo()
           
 void run()
          Runs the main loop of the game execution.
 boolean saveGame(java.lang.String path)
           
 void setConfig(java.util.Properties config)
          Sets a new configuration.
 void setInput(java.io.InputStream input)
          Sets the input stream for the game engine.
 void setOutput(java.io.OutputStream output)
          Sets the output stream for the game engine.
 void setSocket(java.net.Socket sock, java.io.ObjectInputStream ios, java.io.ObjectOutputStream oos)
           
 boolean undo()
          It requests a cloned and past game to be the principal game
protected  void updateMusic()
          Manages the music.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

UNIT_WEIGHT

public static final java.lang.String UNIT_WEIGHT
Unit of measurement for weights in-game This property is called "unit.weight" and its default value is "kilos".

See Also:
Constant Field Values

UNIT_WEIGHT_AB

public static final java.lang.String UNIT_WEIGHT_AB
Unit of measurement for weights in-game abbreviated This property is called "unit.weight.ab" and its default value is "k's".

See Also:
Constant Field Values

UNIT_POINTS

public static final java.lang.String UNIT_POINTS
Unit of measurement for points in-game This property is called "unit.points" and its default value is "points".

See Also:
Constant Field Values

UNIT_POINTS_AB

public static final java.lang.String UNIT_POINTS_AB
Unit of measurement for weights in-game abbreviated This property is called "unit.points.ab" and its default value is "p's".

See Also:
Constant Field Values

FLAG_AUTODESCRIBEFIRSTLOCATION

public static final java.lang.String FLAG_AUTODESCRIBEFIRSTLOCATION
Flag for auto-describing first location (the name of the property). This property is called "flag.autodescribeFirstLocation" and its default value is "true".

See Also:
Constant Field Values

FLAG_SHOWENGINEINFO

public static final java.lang.String FLAG_SHOWENGINEINFO
Flag for showing engine information (the name of the property). This property is called "flag.showEngineInfo" and its default value is "true".

See Also:
Constant Field Values

FLAG_SHOWGAMEINFO

public static final java.lang.String FLAG_SHOWGAMEINFO
Flag for showing game information (the name of the property). This property is called "flag.showGameInfo" and its default value is "true".

See Also:
Constant Field Values

FLAG_SHOWITEMVALUES

public static final java.lang.String FLAG_SHOWITEMVALUES
Flag for showing the numerical value of the game items (the name of the property). This property is called "flag.showItemValues" and its default value is "true".

See Also:
Constant Field Values

FLAG_SHOWITEMWEIGHTS

public static final java.lang.String FLAG_SHOWITEMWEIGHTS
Flag for showing the weight of the game items (the name of the property). This property is called "flag.showItemWeights" and its default value is "true".

See Also:
Constant Field Values

FLAG_SHOWLOCATIONITEMS

public static final java.lang.String FLAG_SHOWLOCATIONITEMS
Flag for showing the items of each location (the name of the property). This property is called "flag.showLocationItems" and its default value is "true".

See Also:
Constant Field Values

FLAG_PLAYMUSIC

public static final java.lang.String FLAG_PLAYMUSIC
Flag for playing music (the name of the property). This property is called "flag.playMusic" and its default value is "true".

See Also:
Constant Field Values

LIMIT_COMMANDHISTORYSIZE

public static final java.lang.String LIMIT_COMMANDHISTORYSIZE
Limit of command history size for the undo command (the name of the property). This property is called "limit.commandHistorySize" and its default value is "1".

See Also:
Constant Field Values

LIMIT_INVENTORYCAPACITY

public static final java.lang.String LIMIT_INVENTORYCAPACITY
Limit of inventory capacity (the name of the property). This property is called "limit.inventoryCapacity" and its default value is "10".

See Also:
Constant Field Values

MESSAGE_ENGINEHELP

public static final java.lang.String MESSAGE_ENGINEHELP
Message of help information from the engine (the name of the property). This property is called "message.engineHelp" and its default value is "These are the available player commands:".

See Also:
Constant Field Values

MESSAGE_GAMEOVER

public static final java.lang.String MESSAGE_GAMEOVER
Message of game over (the name of the property). This property is called "message.gameOver" and its default value is "GAME OVER".

See Also:
Constant Field Values

MESSAGE_LOCATIONWITHITEMS

public static final java.lang.String MESSAGE_LOCATIONWITHITEMS
Message of location with items (the name of the property). This property is called "message.locationWithItems" and its default value is "This location contains the following items: ".

See Also:
Constant Field Values

MESSAGE_LOCATIONWITHOUTITEMS

public static final java.lang.String MESSAGE_LOCATIONWITHOUTITEMS
Message of location without items (the name of the property).

See Also:
Constant Field Values

MESSAGE_PLAYERSCORE

public static final java.lang.String MESSAGE_PLAYERSCORE
Message of the player score (the name of the property). This property is called "message.playerScore" and its default value is "Player score: ".

See Also:
Constant Field Values

MESSAGE_PROMPT

public static final java.lang.String MESSAGE_PROMPT
Message of prompt for the player (the name of the property). This property is called "message.prompt" and its default value is "> ".

See Also:
Constant Field Values

MESSAGE_UNKNOWNCOMMAND

public static final java.lang.String MESSAGE_UNKNOWNCOMMAND
Message of unknown command (the name of the property). This property is called "message.unknownCommand" and its default value is "Pardon?".

See Also:
Constant Field Values

FLAG_SHOWCONNECTIONS

public static final java.lang.String FLAG_SHOWCONNECTIONS
Flag for showing connections (the name of the property). This property is called "flag.showConnections" and its default value is "true".

See Also:
Constant Field Values

FLAG_SHOWCONNECTIONS_STATE

public static final java.lang.String FLAG_SHOWCONNECTIONS_STATE
See Also:
Constant Field Values

MESSAGE_LOCATIONWITHEXITS

public static final java.lang.String MESSAGE_LOCATIONWITHEXITS
See Also:
Constant Field Values

MESSAGE_LOCATIONWITHOUTEXITS

public static final java.lang.String MESSAGE_LOCATIONWITHOUTEXITS
See Also:
Constant Field Values

MESSAGE_OBSTACLE_OPEN

public static final java.lang.String MESSAGE_OBSTACLE_OPEN
Message of obstacle opened found (the name of the property). This property is called "message.obstacle.open" and its default value is "Open".

See Also:
Constant Field Values

MESSAGE_OBSTACLE_CLOSED

public static final java.lang.String MESSAGE_OBSTACLE_CLOSED
Message of obstacle closed found (the name of the property). This property is called "message.obstacle.closed" and its default value is "Closed".

See Also:
Constant Field Values

MESSAGE_OBSTACLE_FREE

public static final java.lang.String MESSAGE_OBSTACLE_FREE
Message of no obstacle found (the name of the property). This property is called "message.obstacle.free" and its default value is "Free".

See Also:
Constant Field Values

_game

protected Game _game
The game


_gameCopy

protected Game _gameCopy
A referential copy of the game.


_parser

protected Parser _parser
The parser


_in

protected java.io.InputStream _in
Input Stream... just in case


_out

protected java.io.PrintStream _out
Output Stream... just in case


_config

protected GameConfig _config
Configuration


_history

protected java.util.Deque<Game> _history

_future

protected java.util.Deque<Game> _future

_player

protected MP3Player _player

_socket

protected java.net.Socket _socket

_online

protected boolean _online

_onlineOos

protected java.io.ObjectOutputStream _onlineOos

_onlineIos

protected java.io.ObjectInputStream _onlineIos

_lastMessage

protected Message _lastMessage
Constructor Detail

Engine

public Engine(Game game)
       throws java.lang.IllegalArgumentException
Constructs the game engine using a given game. Default configuration, standard input and standard output are established at this moment.

Parameters:
game - The game
Throws:
java.lang.IllegalArgumentException - Argument game cannot be null

Engine

public Engine(Game game,
              java.io.InputStream input)
       throws java.lang.IllegalArgumentException
Constructs the game engine using a given game and a given input stream. Default configuration and standard output are assumed at this moment.

Parameters:
game - The game
input - Input Stream
Throws:
java.lang.IllegalArgumentException - Arguments game and input cannot be null

Engine

public Engine(Game game,
              java.io.InputStream input,
              java.io.OutputStream output)
       throws java.lang.IllegalArgumentException
Constructs the game engine using a given game, a given input stream and a given output stream. Default configuration is assumed at this moment.

Parameters:
game - The game
input - Input stream
output - Output stream
Throws:
java.lang.IllegalArgumentException - Arguments game, input and output cannot be null

Engine

public Engine(Game game,
              java.io.OutputStream output)
       throws java.lang.IllegalArgumentException
Constructs the game engine using a given game and a given output stream. Default configuration and standard input are assumed at this moment.

Parameters:
game - The game
output - Output stream
Throws:
java.lang.IllegalArgumentException - Arguments game and output cannot be null

Engine

public Engine(Game game,
              java.util.Properties config)
       throws java.lang.IllegalArgumentException
Constructs the game engine using a given game and a given configuration. The properties defined in the given configuration override those of the default configuration. Standard input and standard output are assumed at this moment.

Parameters:
game - The game
config - The config
Throws:
java.lang.IllegalArgumentException - Arguments game and input cannot be null

Engine

public Engine(Game game,
              java.util.Properties config,
              java.io.InputStream input)
       throws java.lang.IllegalArgumentException
Constructs the game engine using a given game, a given configuration and a given output stream. Standard output is assumed at this moment.

Parameters:
game - The game
config - The config
input - Input stream
Throws:
java.lang.IllegalArgumentException - Arguments game, config and input cannot be null

Engine

public Engine(Game game,
              java.util.Properties config,
              java.io.InputStream input,
              java.io.OutputStream output)
       throws java.lang.IllegalArgumentException
Constructs the game engine using a given game, a given configuration, a given input stream and a given output stream.

Parameters:
game - The game
config - The config
input - Input stream
output - Output stream
Throws:
java.lang.IllegalArgumentException - Argument game, config, input and output cannot be null

Engine

public Engine(Game game,
              java.util.Properties config,
              java.io.OutputStream output)
       throws java.lang.IllegalArgumentException
Constructs the game engine using a given game, a given configuration and a given input stream. Standard input is assumed at this moment.

Parameters:
game - The game
config - The config
output - Output stream
Throws:
java.lang.IllegalArgumentException - Arguments game, config and output cannot be null
Method Detail

run

public void run()
Runs the main loop of the game execution. Firstly (before entering into the loop) the description of the engine and the description of the game can be shown (depending on the configuration). Then the description of the initial location or the description plus the items that are in that location can be shown (depending on the configuration). Finally three steps are repeated until the game ends: 1. Parsing the input (i.e. identifying a valid next command), 2. Trying to execute the next command (recording it if it was executed successfully) and 3. Reporting the result of the command (and the events that have ocurred in the game after the command execution) to the output stream. At the end of the game the player score can be shown (depending on the configuration).


setConfig

public void setConfig(java.util.Properties config)
               throws java.lang.IllegalArgumentException
Sets a new configuration. Properties that are not overriden maintain their previous values.

Parameters:
config - The config
Throws:
java.lang.IllegalArgumentException - Argument config cannot be null

setInput

public void setInput(java.io.InputStream input)
              throws java.lang.IllegalArgumentException
Sets the input stream for the game engine.

Parameters:
input - Input stream
Throws:
java.lang.IllegalArgumentException - Argument input cannot be null

setOutput

public void setOutput(java.io.OutputStream output)
               throws java.lang.IllegalArgumentException
Sets the output stream for the game engine.

Parameters:
output - Output stream
Throws:
java.lang.IllegalArgumentException - Argument output cannot be null

undo

public boolean undo()
It requests a cloned and past game to be the principal game

Returns:
True if it was successful, false otherwise

redo

public boolean redo()
It requests a cloned and future game to be the principal game

Returns:
True if it was successful, false otherwise

getUndoableCommands

protected int getUndoableCommands()
It asks about the number of undo games stored

Returns:
The number of possible consecutive undos

getRedoableCommands

protected int getRedoableCommands()
It asks about the number of redo games stored

Returns:
The number of possible consecutive redos

updateMusic

protected void updateMusic()
Manages the music. If it's enabled by the config, the engine asks to the game if there is music to play, and asks the player to do so. If there is no music, the player stops.


saveGame

public boolean saveGame(java.lang.String path)
                 throws java.io.FileNotFoundException,
                        java.io.IOException,
                        java.lang.ClassNotFoundException
Throws:
java.io.FileNotFoundException
java.io.IOException
java.lang.ClassNotFoundException

loadGame

public boolean loadGame(java.lang.String path)
                 throws java.io.IOException,
                        java.lang.ClassNotFoundException
Throws:
java.io.IOException
java.lang.ClassNotFoundException

getUserFileVector

public java.util.Vector<UserFileData> getUserFileVector()
                                                 throws java.io.IOException,
                                                        java.lang.ClassNotFoundException
Throws:
java.io.IOException
java.lang.ClassNotFoundException

setSocket

public void setSocket(java.net.Socket sock,
                      java.io.ObjectInputStream ios,
                      java.io.ObjectOutputStream oos)

getSocket

public java.net.Socket getSocket()

isOnline

public boolean isOnline()

reportLocationInfo

public java.lang.String reportLocationInfo()