es.ucm.fdi.hysteria.game
Class Game

java.lang.Object
  extended by es.ucm.fdi.hysteria.game.Game
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable

public class Game
extends java.lang.Object
implements java.lang.Cloneable, java.io.Serializable

Represents the state of all the elements of the game (locations, items, player inventory, etc.), and relevant constants for the game (the available movement directions, the keywords for a game textual definition, etc.).

Author:
Sandor
See Also:
Serialized Form

Nested Class Summary
static class Game.Direction
          It represents the compass directions: North, east, south and west.
 
Field Summary
protected  java.lang.String _author
          Author of the game
protected  GameConfig _config
           
protected  Location _currentLocation
          Room where the player stays
protected  java.lang.String _desc
          Description of the game
protected  java.util.Deque<java.lang.String> _events
          Events left to report
protected  boolean _finished
          Has the game finished?
protected  java.lang.String _gamePath
          The path of the game directory (for other assets apart from the description file)
protected  ItemRepository _playerInventory
          The player inventory
protected  java.lang.String _title
          Title of the game
protected  int _totalLocations
           
protected  int _visitedLocations
           
static java.lang.String FLAG_SHOWCONNECTIONS
           
static java.lang.String FLAG_SHOWCONNECTIONS_STATE
           
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_HIGHLIGHT
           
static java.lang.String LIMIT_LOWLIGHT
           
static java.lang.String MESSAGE_LIGHTSOFF
           
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
           
static java.lang.String MESSAGE_OBSTACLE_FREE
           
static java.lang.String MESSAGE_OBSTACLE_OPEN
           
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
Game(java.io.InputStream gameDefinition, int type, java.util.Properties config, java.lang.String gamePath)
          Equivalent to the (InputStream, boolean) constructor, but it adds the possibility of adding a custom config.
Game(java.io.InputStream gameDefinition, int type, java.lang.String gamePath)
          Constructs a game, based on a textual definition of it.
Game(java.lang.String title, java.lang.String author, java.lang.String description, Location initialLocation, java.lang.String gamePath)
          Constructs a game using some basic information (a given title, a given author and a given description) and an initial location.
 
Method Summary
 void checkEnd()
          This method checks if the requirements for ending the game has been reached.
 void clearEvents()
          Clears the events to report from the game.
 java.lang.Object clone()
           
 void end()
          Ends the game.
 java.util.Set<Item> getAllInventoryItems()
           
 java.util.Set<Item> getAllLocationItems()
           
 java.util.Vector<java.lang.String> getAvailableTopics()
           
 java.lang.String getImagePath()
          Returns the actual image path (game path + path of the image file)
 float getInventoryWeight()
          Reports the total weight of the player inventory.
 java.util.Set<Item> getItemsFromInventory(java.lang.String name)
          Gets items with a common name from the player inventory.
 java.util.Set<Item> getItemsFromLocation(java.lang.String name)
          Gets items with a common name from the player inventory.
 java.lang.String getMusicPath()
          Returns the actual music path (game path + path of the music file)
 java.lang.String getSceneImagePath()
          Returns the actual scene image path (game path + path of the scene image file)
 java.lang.String getTopic(int i)
           
 java.lang.String getTopic(java.lang.String s)
           
 float getVisitedLocations()
           
 boolean hasActiveScene()
           
 boolean hasConnectedLocation(Game.Direction direction)
          Checks whether the player location has a connected location in a given direction.
 boolean hasEnded()
          Checks whether the game has ended.
 boolean hasEvents()
          Checks whether there are new events to report from the game.
 boolean hasImage()
           
 boolean hasMusic()
          There is any music to report?
 boolean hasObstacle(Game.Direction direction)
          Checks if there is an obstacle in a given direction.
 boolean hasSceneImage()
           
 boolean hasTopic(int i)
           
 boolean hasTopic(java.lang.String s)
           
 boolean isItemInInventory(Item item)
          Checks whether an specific item is in the player inventory.
 boolean isItemInLocation(Item item)
          Checks whether an specific item is in the player location.
 boolean isLockedObstacle(Game.Direction direction)
          Checks if the obstacle in a given direction is locked.
 boolean isSimpleObstacle(Game.Direction direction)
          Checks if the obstacle in a given direction is a simple one (Non-key needed to interact).
 boolean locationIsLighted()
           
 boolean lockObstacle(Game.Direction direction)
          Tries to lock an obstacle in a given direction without the use of an item.
 boolean lockObstacle(Game.Direction direction, Item key)
          Tries to lock an obstacle in a given direction using an item.
 boolean moveItemFromInventoryToLocation(Item item)
          Moves an item from the player inventory to the player location.
 boolean moveItemFromLocationToInventory(Item item)
          Moves an item from the player location to the player inventory.
 boolean movePlayer(Game.Direction direction)
          Moves the player to a location connected to the player location in a given direction.
 java.lang.String presentItem(Item i)
           
 java.util.List<java.lang.String> reportAllInventoryItems()
          Reports the names of all the items of the player inventory.
 java.util.List<java.lang.String> reportAllLocationItems()
          Reports the names of all the items of the player location.
 java.lang.String reportEvents()
          Reports the new events from the game.
 java.lang.String reportGameTitle()
           
 java.lang.String reportInformation()
          Reports the basic information of the game (title, author and description)
 int reportInventoryValue()
          Reports the value of the player inventory.
 java.lang.String reportLocationDescription()
          Reports the description of the player location.
 java.lang.String reportLocationInfo()
          Reports the description of the actual Location, adding the necessary info from the config.
 java.lang.String reportLocationName()
          Reports the name of the player location.
 java.lang.String reportObstacleInfo(Game.Direction direction)
          Reports info of the obstacle that is in a given direction.
 java.lang.String toString()
           
 boolean unlockObstacle(Game.Direction direction)
          Tries to unlock an obstacle in a given direction without the use of an item.
 boolean unlockObstacle(Game.Direction direction, Item key)
          Tries to unlock an obstacle in a given direction using an item.
 boolean useItem(Item origin)
           
 boolean useItem(Item origin, Item destiny)
           
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, 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_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

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

FLAG_SHOWCONNECTIONS

public static final java.lang.String FLAG_SHOWCONNECTIONS
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
See Also:
Constant Field Values

MESSAGE_OBSTACLE_CLOSED

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

MESSAGE_OBSTACLE_FREE

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

MESSAGE_LIGHTSOFF

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

LIMIT_LOWLIGHT

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

LIMIT_HIGHLIGHT

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

_currentLocation

protected Location _currentLocation
Room where the player stays


_events

protected java.util.Deque<java.lang.String> _events
Events left to report


_playerInventory

protected ItemRepository _playerInventory
The player inventory


_author

protected java.lang.String _author
Author of the game


_title

protected java.lang.String _title
Title of the game


_desc

protected java.lang.String _desc
Description of the game


_gamePath

protected java.lang.String _gamePath
The path of the game directory (for other assets apart from the description file)


_finished

protected boolean _finished
Has the game finished?


_config

protected GameConfig _config

_visitedLocations

protected int _visitedLocations

_totalLocations

protected int _totalLocations
Constructor Detail

Game

public Game(java.io.InputStream gameDefinition,
            int type,
            java.lang.String gamePath)
     throws java.lang.IllegalArgumentException,
            InvalidGameDefinitionException
Constructs a game, based on a textual definition of it. A game definition consists on: * The keyword 'game' plus three strings with the title, the author/s and the initial description of the game. * One or more location definitions (the first one will be considered the initial location). A location definition consists on: o The keyword 'location' plus a unique id for the location, two strings with the name and the description of the location plus (optionally) an exit threshold and the string of the corresponding exit message for this location. o Zero or more item definitions. An item definition consists on: + The keyword 'item' plus two strings and an integer with the name, the description and the value of the item. o Zero or more connections to other locations. A connection consists on: + A direction keyword plus the id of the connected location.

Parameters:
gameDefinition - The game definition
type - Is the input stream a txt file?
gamePath - The path of the game directory (for other assets apart from the description file)
Throws:
java.lang.IllegalArgumentException - Argument gameDefinition cannot be null
InvalidGameDefinitionException - The game definition is invalid so a new game cannot be created with it. Additional information is provided about the error found in the definition and its position (e.g. "Error in Token[location], line 14: First-level structures should be locations.").

Game

public Game(java.io.InputStream gameDefinition,
            int type,
            java.util.Properties config,
            java.lang.String gamePath)
     throws java.lang.IllegalArgumentException,
            InvalidGameDefinitionException
Equivalent to the (InputStream, boolean) constructor, but it adds the possibility of adding a custom config.

Parameters:
gameDefinition - The game definition
type - Is the input stream a txt file?
config - Custom config
gamePath - The path of the game directory (for other assets apart from the description file)
Throws:
java.lang.IllegalArgumentException - Arguments cannot be null
InvalidGameDefinitionException - File must be well-formed

Game

public Game(java.lang.String title,
            java.lang.String author,
            java.lang.String description,
            Location initialLocation,
            java.lang.String gamePath)
     throws java.lang.IllegalArgumentException
Constructs a game using some basic information (a given title, a given author and a given description) and an initial location.

Parameters:
title - The title
author - The author
description - The description
initialLocation - Initial location
gamePath - The path of the game directory (for other assets apart from the description file)
Throws:
java.lang.IllegalArgumentException - Arguments title, author, description and initialLocation cannot be null
Method Detail

clearEvents

public void clearEvents()
Clears the events to report from the game.


end

public void end()
Ends the game.


getItemsFromInventory

public java.util.Set<Item> getItemsFromInventory(java.lang.String name)
                                          throws java.lang.IllegalArgumentException
Gets items with a common name from the player inventory.

Parameters:
name - The common name.
Returns:
The set of items with a common name.
Throws:
java.lang.IllegalArgumentException - The argument 'name' cannot be null.

getAllLocationItems

public java.util.Set<Item> getAllLocationItems()

getAllInventoryItems

public java.util.Set<Item> getAllInventoryItems()

getItemsFromLocation

public java.util.Set<Item> getItemsFromLocation(java.lang.String name)
                                         throws java.lang.IllegalArgumentException
Gets items with a common name from the player inventory.

Parameters:
name - The common name.
Returns:
The set of items with a common name.
Throws:
java.lang.IllegalArgumentException - Argument 'name' cannot be null.

hasConnectedLocation

public boolean hasConnectedLocation(Game.Direction direction)
                             throws java.lang.IllegalArgumentException
Checks whether the player location has a connected location in a given direction.

Parameters:
direction - The direction.
Returns:
true if there is a connected location in the given direction; false otherwise.
Throws:
java.lang.IllegalArgumentException - The argument 'direction' cannot be null.

hasObstacle

public boolean hasObstacle(Game.Direction direction)
                    throws java.lang.IllegalArgumentException
Checks if there is an obstacle in a given direction.

Parameters:
direction - The direction
Returns:
True if there is an obstacle. False otherwise
Throws:
java.lang.IllegalArgumentException - Direction cannot be null

reportObstacleInfo

public java.lang.String reportObstacleInfo(Game.Direction direction)
Reports info of the obstacle that is in a given direction.

Parameters:
direction - The direction
Returns:
Obstacle description
Throws:
java.lang.IllegalArgumentException - Direction cannot be null

isSimpleObstacle

public boolean isSimpleObstacle(Game.Direction direction)
                         throws java.lang.IllegalArgumentException
Checks if the obstacle in a given direction is a simple one (Non-key needed to interact).

Parameters:
direction - The direction
Returns:
True if it's simple. False otherwise
Throws:
java.lang.IllegalArgumentException - Direction cannot be null

isLockedObstacle

public boolean isLockedObstacle(Game.Direction direction)
                         throws java.lang.IllegalArgumentException
Checks if the obstacle in a given direction is locked.

Parameters:
direction - The direction
Returns:
True if it's locked. False otherwise
Throws:
java.lang.IllegalArgumentException - Direction cannot be null

unlockObstacle

public boolean unlockObstacle(Game.Direction direction)
                       throws java.lang.IllegalArgumentException
Tries to unlock an obstacle in a given direction without the use of an item.

Parameters:
direction - The direction
Returns:
True if it was successful. False otherwise
Throws:
java.lang.IllegalArgumentException - Direction cannot be null

unlockObstacle

public boolean unlockObstacle(Game.Direction direction,
                              Item key)
                       throws java.lang.IllegalArgumentException
Tries to unlock an obstacle in a given direction using an item.

Parameters:
direction - The direction
key - The item
Returns:
True if it was successful. False otherwise
Throws:
java.lang.IllegalArgumentException - Direction cannot be null

lockObstacle

public boolean lockObstacle(Game.Direction direction)
                     throws java.lang.IllegalArgumentException
Tries to lock an obstacle in a given direction without the use of an item.

Parameters:
direction - The direction
Returns:
True if it was successful. False otherwise
Throws:
java.lang.IllegalArgumentException - Direction cannot be null

lockObstacle

public boolean lockObstacle(Game.Direction direction,
                            Item key)
                     throws java.lang.IllegalArgumentException
Tries to lock an obstacle in a given direction using an item.

Parameters:
direction - The direction
key - The item
Returns:
True if it was successful. False otherwise
Throws:
java.lang.IllegalArgumentException - Direction cannot be null

hasMusic

public boolean hasMusic()
There is any music to report?

Returns:
True if it's the case, false otherwise

getMusicPath

public java.lang.String getMusicPath()
Returns the actual music path (game path + path of the music file)

Returns:
The music path
Throws:
NoMusicException - There was no path

hasImage

public boolean hasImage()

getImagePath

public java.lang.String getImagePath()
Returns the actual image path (game path + path of the image file)

Returns:
The image path
Throws:
NoImageException - There was no path

hasSceneImage

public boolean hasSceneImage()

getSceneImagePath

public java.lang.String getSceneImagePath()
Returns the actual scene image path (game path + path of the scene image file)

Returns:
The scene image path
Throws:
NoSceneImageException - There was no path

hasEvents

public boolean hasEvents()
Checks whether there are new events to report from the game.

Returns:
true if there are events to report; false otherwise.

hasEnded

public boolean hasEnded()
Checks whether the game has ended.

Returns:
true if the game is ended; false otherwise.

moveItemFromInventoryToLocation

public boolean moveItemFromInventoryToLocation(Item item)
                                        throws java.lang.IllegalArgumentException,
                                               ItemNotInRepositoryException,
                                               ItemAlreadyInRepositoryException
Moves an item from the player inventory to the player location.

Parameters:
item - The item.
Returns:
true if the item was successfully moved; false otherwise.
Throws:
ItemNotInRepositoryException - The item is not in this repository.
java.lang.IllegalArgumentException - Argument 'item' cannot be null.
ItemAlreadyInRepositoryException - The item is already in this repository.

moveItemFromLocationToInventory

public boolean moveItemFromLocationToInventory(Item item)
                                        throws ItemNotInRepositoryException,
                                               java.lang.IllegalArgumentException,
                                               ItemAlreadyInRepositoryException
Moves an item from the player location to the player inventory.

Parameters:
item - The item.
Returns:
true if the item was successfully moved; false otherwise.
Throws:
ItemNotInRepositoryException - The item is not in this repository.
java.lang.IllegalArgumentException - Argument 'item' cannot be null.
ItemAlreadyInRepositoryException - The item is already in this repository.

movePlayer

public boolean movePlayer(Game.Direction direction)
                   throws java.lang.IllegalArgumentException,
                          NoConnectedLocationException
Moves the player to a location connected to the player location in a given direction.

Parameters:
direction - The direction.
Returns:
true if the player was successfully moved; false otherwise.
Throws:
java.lang.IllegalArgumentException - Argument 'direction' cannot be null.
NoConnectedLocationException - There is no connected location to the player location in direction .

reportAllInventoryItems

public java.util.List<java.lang.String> reportAllInventoryItems()
Reports the names of all the items of the player inventory.

Returns:
The list of the names of all the items.

reportAllLocationItems

public java.util.List<java.lang.String> reportAllLocationItems()
Reports the names of all the items of the player location.

Returns:
The list of the names of all the items.

reportEvents

public java.lang.String reportEvents()
                              throws NoGameEventsException
Reports the new events from the game. These 'events' are implemented as a simple text that can be shown to the player, adding some extra information to the result of a previously executed command (i.e. the exit message of a location where the game ends, after a Go command).

Returns:
The events.
Throws:
NoGameEventsException - The game has no new events to report.

reportInformation

public java.lang.String reportInformation()
Reports the basic information of the game (title, author and description)

Returns:
The basic information of the game.

reportLocationName

public java.lang.String reportLocationName()
Reports the name of the player location.

Returns:
The name.

reportLocationDescription

public java.lang.String reportLocationDescription()
Reports the description of the player location.

Returns:
The description.

reportLocationInfo

public java.lang.String reportLocationInfo()
Reports the description of the actual Location, adding the necessary info from the config.

Returns:
The formatted description.

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

isItemInInventory

public boolean isItemInInventory(Item item)
Checks whether an specific item is in the player inventory.

Parameters:
item - The item.
Returns:
true if the item is in the player inventory; false otherwise.

isItemInLocation

public boolean isItemInLocation(Item item)
Checks whether an specific item is in the player location.

Parameters:
item - The item.
Returns:
true if the item is in the player inventory; false otherwise.

reportInventoryValue

public int reportInventoryValue()
Reports the value of the player inventory.

Returns:
The value.

getInventoryWeight

public float getInventoryWeight()
Reports the total weight of the player inventory.

Returns:
Total weight.

checkEnd

public void checkEnd()
This method checks if the requirements for ending the game has been reached.


reportGameTitle

public java.lang.String reportGameTitle()

getVisitedLocations

public float getVisitedLocations()

hasActiveScene

public boolean hasActiveScene()

getAvailableTopics

public java.util.Vector<java.lang.String> getAvailableTopics()

hasTopic

public boolean hasTopic(int i)

hasTopic

public boolean hasTopic(java.lang.String s)

getTopic

public java.lang.String getTopic(int i)

getTopic

public java.lang.String getTopic(java.lang.String s)

presentItem

public java.lang.String presentItem(Item i)

useItem

public boolean useItem(Item origin)

useItem

public boolean useItem(Item origin,
                       Item destiny)

locationIsLighted

public boolean locationIsLighted()

clone

public java.lang.Object clone()
Overrides:
clone in class java.lang.Object