core.game

## Class Game

- java.lang.Object
- - core.game.Game

- Direct Known Subclasses:
    BasicGame, ForwardModel

      public abstract class Game
      extends java.lang.Object

  Created with IntelliJ IDEA. User: Diego Date: 17/10/13 Time: 13:42 This is a Java port from Tom Schaul's VGDL - https://github.com/schaul/py-vgdl

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `protected class` | `Game.Bucket` Class for helping collision detection.

  - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected MovingAvatar` | `avatar` Quick reference to the gamer
    `protected int` | `block_size` Size of the block in pixels.
    `protected Game.Bucket[]` | `bucketList` Arraylist to hold collisions between objects in every frame
    `protected java.util.HashMap<java.lang.Character,java.util.ArrayList<java.lang.String>>` | `charMapping` Mapping between characters in the level and the entities they represent.
    `protected Content[]` | `classConst` Content objects for the different sprite types..
    `protected java.util.ArrayList<Effect>[][]` | `collisionEffects` Relationships for collisions: double array of (list of) effects.
    `protected java.util.ArrayList<Pair>` | `definedEffects` Pairs of all defined effects in the game.
    `protected java.util.ArrayList<java.lang.Integer>` | `definedEOSEffects` List of types that can trigger an EOS effect.
    `protected java.util.ArrayList<Effect>[]` | `eosEffects` List of EOS effects
    `protected int` | `frame_rate` Default frame rate of the basic game.
    `protected ForwardModel` | `fwdModel` State observation for this game.
    `protected int` | `gameTick` Game tick
    `protected java.util.TreeSet<Event>` | `historicEvents` Historic of events related to the avatar happened during the game.
    `protected boolean` | `is_stochastic` Indicates if the game is stochastic.
    `protected boolean` | `isEnded` Indicates if the game is ended.
    `protected java.util.ArrayList<java.lang.Integer>[]` | `iSubTypes` For each entry, int identifier of sprite type, a list with all the itypes this sprite belongs to.
    `static KeyInput` | `ki` Key input
    `protected java.util.ArrayList<VGDLSprite>` | `kill_list` List of sprites killed in the game.
    `protected int` | `MAX_SPRITES` Maximum number of sprites in a game.
    `protected int` | `nextSpriteID` Next ID to generate for sprites;
    `protected int` | `num_sprites` Number of sprites this game has.
    `protected java.awt.Color[]` | `resources_colors` Color for each resource
    `protected int[]` | `resources_limits` Limit number of each resource type
    `protected double` | `score` Score of the game.
    `protected java.awt.Dimension` | `screenSize` Screen size.
    `protected boolean[]` | `singletons` Singletons of the game.
    `protected java.awt.Dimension` | `size` Dimensions of the game.
    `protected SpriteGroup[]` | `spriteGroups` Groups of sprites in the level.
    `protected int[]` | `spriteOrder` z-level of sprite types (in case of overlap)
    `protected java.util.ArrayList<Termination>` | `terminations` Termination set conditions to finish the game.
    `protected Types.WINNER` | `winner` Indicates if the game has been won by the player.

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `Game()` Default constructor.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `_updateCollisionDict(VGDLSprite sprite)`
    `VGDLSprite` | `addSprite(int itype, Vector2d position)` Adds a sprite given a content and position.
    `VGDLSprite` | `addSprite(SpriteContent content, Vector2d position, int itype)` Adds a sprite given a content and position.
    `protected void` | `addSprite(VGDLSprite sprite, int itype)` Adds a new sprite to the pool of sprites of the game.
    `abstract void` | `buildLevel(java.lang.String gamelvl)` Builds a level, receiving a file name.
    `protected void` | `checkTimeOut()` Checks if the game must finish because of number of cycles played.
    `protected void` | `clearAll()` Deletes all the sprites killed in the previous step.
    `void` | `disqualify()` Disqualifies the player in the game, and also sets the isEnded flag to true.
    `protected void` | `eventHandling()` Handles collisions and triggers events.
    `MovingAvatar` | `getAvatar()` Returns the avatar of the game.
    `java.util.HashMap<java.lang.Character,java.util.ArrayList<java.lang.String>>` | `getCharMapping()` Returns the char mapping of this array, that relates characters in the level with sprite names that it references.
    `java.util.ArrayList<Effect>` | `getCollisionEffects(int spriteItype1, int spriteItype2)` Gets the array of collisions defined for two types of sprites.
    `java.util.ArrayList<Pair>` | `getDefinedEffects()` Returns all paired effects defined in the game.
    `java.util.ArrayList<java.lang.Integer>` | `getDefinedEosEffects()` Returns the list of sprite type with at least one EOS effect defined.
    `java.util.ArrayList<Effect>` | `getEosEffects(int obj1)` Returns all EOS effects defined in the game.
    `int` | `getGameTick()` Returns the current game tick of this game.
    `int` | `getNumSprites(int itype)` Returns the number of sprites of the type given by parameter, and all its subtypes
    `StateObservation` | `getObservation()` Retuns the observation of this state.
    `java.util.Random` | `getRandomGenerator()` Returns the sampleRandom object
    `java.awt.Color` | `getResourceColor(int resourceId)` Gets the color of the resource of type resourceId
    `int` | `getResourceLimit(int resourceId)` Gets the maximum amount of resources of type resourceId that are allowed by entities in the game.
    `java.awt.Dimension` | `getScreenSize()` Gets the dimensions of the screen.
    `java.util.Iterator<VGDLSprite>` | `getSpriteGroup(int spriteItype)` Gets an iterator for the collection of sprites for a particular sprite type.
    `int[]` | `getSpriteOrder()` Gets the order in which the sprites are drawn.
    `java.util.TreeMap<java.lang.Integer,VGDLSprite>` | `getSprites(int spriteItype)` Gets the collection of sprites for a particular sprite type.
    `java.util.ArrayList<Termination>` | `getTerminations()` Gets the array of termination conditions for this game.
    `Types.WINNER` | `getWinner()` Returns the winner of this game.
    `void` | `initForwardModel()` Starts the forward model for the game.
    `void` | `initSprites(java.util.ArrayList<java.lang.Integer> spOrder, java.util.ArrayList<java.lang.Integer> sings, java.util.HashMap<java.lang.Integer,SpriteContent> constructors)` Initializes the sprite structures that hold the game.
    `boolean` | `isGameOver()` Indicates if the game is over, or if it is still being played.
    `void` | `killSprite(VGDLSprite sprite)` Kills a given sprite, adding it to the list of sprites killed at this step.
    `protected void` | `parseParameters(GameContent content)` Reads the parameters of a game type.
    `double` | `playGame(AbstractPlayer player, int randomSeed)` Plays the game, graphics enabled.
    `void` | `reset()` Sets the game back to the state prior to load a level.
    `void` | `reverseDirection(VGDLSprite sprite)` Reverses the direction of a given sprite.
    `double` | `runGame(AbstractPlayer player, int randomSeed)` Runs a game, without graphics.
    `void` | `setAvatar(MovingAvatar newAvatar)` Sets the avatar of the game.
    `void` | `setStochastic(boolean stoch)` Defines this game as stochastic (or not) depending on the parameter passed.
    `protected void` | `terminationHandling()` Handles termination conditions, for every termination defined in 'terminations' array.
    `protected void` | `tick()` Performs one tick for the game: calling update(this) in all sprites.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### spriteOrder

          protected int[] spriteOrder

      z-level of sprite types (in case of overlap)

    - #### singletons

          protected boolean[] singletons

      Singletons of the game.

    - #### classConst

          protected Content[] classConst

      Content objects for the different sprite types.. The index is the type of object Content encloses information about the class of the object and its parameters.

    - #### spriteGroups

          protected SpriteGroup[] spriteGroups

      Groups of sprites in the level. Each element of the array is a collection of sprites of a given type, which is also the index of the array.

    - #### collisionEffects

          protected java.util.ArrayList<Effect>[][] collisionEffects

      Relationships for collisions: double array of (list of) effects. Interaction between two sprites can trigger more than one effect. collisionEffects[] -> int id of the FIRST element taking part on the effects. collisionEffects[][] -> int id of the SECOND element taking part on the effects.

    - #### definedEffects

          protected java.util.ArrayList<Pair> definedEffects

      Pairs of all defined effects in the game.

    - #### eosEffects

          protected java.util.ArrayList<Effect>[] eosEffects

      List of EOS effects

    - #### definedEOSEffects

          protected java.util.ArrayList<java.lang.Integer> definedEOSEffects

      List of types that can trigger an EOS effect.

    - #### historicEvents

          protected java.util.TreeSet<Event> historicEvents

      Historic of events related to the avatar happened during the game. The entries are ordered asc. by game step.

    - #### iSubTypes

          protected java.util.ArrayList<java.lang.Integer>[] iSubTypes

      For each entry, int identifier of sprite type, a list with all the itypes this sprite belongs to.

    - #### bucketList

          protected Game.Bucket[] bucketList

      Arraylist to hold collisions between objects in every frame

    - #### charMapping

          protected java.util.HashMap<java.lang.Character,java.util.ArrayList<java.lang.String>> charMapping

      Mapping between characters in the level and the entities they represent.

    - #### terminations

          protected java.util.ArrayList<Termination> terminations

      Termination set conditions to finish the game.

    - #### kill_list

          protected java.util.ArrayList<VGDLSprite> kill_list

      List of sprites killed in the game.

    - #### resources_limits

          protected int[] resources_limits

      Limit number of each resource type

    - #### resources_colors

          protected java.awt.Color[] resources_colors

      Color for each resource

    - #### screenSize

          protected java.awt.Dimension screenSize

      Screen size.

    - #### size

          protected java.awt.Dimension size

      Dimensions of the game.

    - #### is_stochastic

          protected boolean is_stochastic

      Indicates if the game is stochastic.

    - #### num_sprites

          protected int num_sprites

      Number of sprites this game has.

    - #### gameTick

          protected int gameTick

      Game tick

    - #### ki

          public static KeyInput ki

      Key input

    - #### block_size

          protected int block_size

      Size of the block in pixels.

    - #### avatar

          protected MovingAvatar avatar

      Quick reference to the gamer

    - #### isEnded

          protected boolean isEnded

      Indicates if the game is ended.

    - #### winner

          protected Types.WINNER winner

      Indicates if the game has been won by the player. See Types.WINNER for the values of this variable.

    - #### frame_rate

          protected int frame_rate

      Default frame rate of the basic game.

    - #### fwdModel

          protected ForwardModel fwdModel

      State observation for this game.

    - #### score

          protected double score

      Score of the game.

    - #### MAX_SPRITES

          protected int MAX_SPRITES

      Maximum number of sprites in a game.

    - #### nextSpriteID

          protected int nextSpriteID

      Next ID to generate for sprites;

  - ### Constructor Detail

    - #### Game

          public Game()

      Default constructor.

  - ### Method Detail

    - #### initSprites

          public void initSprites(java.util.ArrayList<java.lang.Integer> spOrder,
                         java.util.ArrayList<java.lang.Integer> sings,
                         java.util.HashMap<java.lang.Integer,SpriteContent> constructors)

      Initializes the sprite structures that hold the game.
      Parameters:
        `spOrder` - order of sprite types to be drawn on the screen.
        `sings` - sprites that are marked as singletons.
        `constructors` - map of sprite constructor's information.

    - #### reset

          public void reset()

      Sets the game back to the state prior to load a level.

    - #### initForwardModel

          public void initForwardModel()

      Starts the forward model for the game.

    - #### parseParameters

          protected void parseParameters(GameContent content)

      Reads the parameters of a game type.
      Parameters:
        `content` - list of parameter-value pairs.

    - #### addSprite

          protected void addSprite(VGDLSprite sprite,
                       int itype)

      Adds a new sprite to the pool of sprites of the game. Increments the sprite counter and also modifies is_stochastic and the avatar accordingly.
      Parameters:
        `sprite` - the new sprite to add.
        `itype` - main int type of this sprite (leaf of the hierarchy of types).

    - #### getNumSprites

          public int getNumSprites(int itype)

      Returns the number of sprites of the type given by parameter, and all its subtypes
      Parameters:
        `itype` - parent itype requested.
      Returns:
        the number of sprites of the type and subtypes.

    - #### runGame

          public double runGame(AbstractPlayer player,
                       int randomSeed)

      Runs a game, without graphics.
      Parameters:
        `player` - Player that plays this game.
        `randomSeed` - sampleRandom seed for the whole game.
      Returns:
        the score of the game played.

    - #### playGame

          public double playGame(AbstractPlayer player,
                        int randomSeed)

      Plays the game, graphics enabled.
      Parameters:
        `player` - Player that plays this game.
        `randomSeed` - sampleRandom seed for the whole game.
      Returns:
        the score of the game played.

    - #### checkTimeOut

          protected void checkTimeOut()

      Checks if the game must finish because of number of cycles played. This is a value stored in CompetitionParameters.MAX_TIMESTEPS. If the game is due to end, the winner is determined and the flag isEnded is set to true.

    - #### disqualify

          public void disqualify()

      Disqualifies the player in the game, and also sets the isEnded flag to true.

    - #### tick

          protected void tick()

      Performs one tick for the game: calling update(this) in all sprites. It follows the opposite order of the drawing order (spriteOrder[]).

    - #### eventHandling

          protected void eventHandling()

      Handles collisions and triggers events.

    - #### terminationHandling

          protected void terminationHandling()

      Handles termination conditions, for every termination defined in 'terminations' array.

    - #### clearAll

          protected void clearAll()

      Deletes all the sprites killed in the previous step. Also, clears the array of collisions from the last step.

    - #### addSprite

          public VGDLSprite addSprite(int itype,
                             Vector2d position)

      Adds a sprite given a content and position.
      Parameters:
        `itype` - integer that identifies the definition of the sprite to add
        `position` - where the sprite has to be placed.

    - #### addSprite

          public VGDLSprite addSprite(SpriteContent content,
                             Vector2d position,
                             int itype)

      Adds a sprite given a content and position. It checks for possible singletons.
      Parameters:
        `content` - definition of the sprite to add
        `position` - where the sprite has to be placed.
        `itype` - integer identifier of this type of sprite.

    - #### _updateCollisionDict

          public void _updateCollisionDict(VGDLSprite sprite)

    - #### reverseDirection

          public void reverseDirection(VGDLSprite sprite)

      Reverses the direction of a given sprite.
      Parameters:
        `sprite` - sprite to reverse.

    - #### killSprite

          public void killSprite(VGDLSprite sprite)

      Kills a given sprite, adding it to the list of sprites killed at this step.
      Parameters:
        `sprite` - the sprite to kill.

    - #### getSpriteGroup

          public java.util.Iterator<VGDLSprite> getSpriteGroup(int spriteItype)

      Gets an iterator for the collection of sprites for a particular sprite type.
      Parameters:
        `spriteItype` - type of the sprite to retrieve.
      Returns:
        sprite collection of the specified type.

    - #### getSprites

          public java.util.TreeMap<java.lang.Integer,VGDLSprite> getSprites(int spriteItype)

      Gets the collection of sprites for a particular sprite type.
      Parameters:
        `spriteItype` - type of the sprite to retrieve.
      Returns:
        sprite collection of the specified type.

    - #### getCollisionEffects

          public java.util.ArrayList<Effect> getCollisionEffects(int spriteItype1,
                                                        int spriteItype2)

      Gets the array of collisions defined for two types of sprites.
      Parameters:
        `spriteItype1` - type of the first sprite.
        `spriteItype2` - type of the second sprite.
      Returns:
        the collection of the effects defined between the two sprite types.

    - #### getDefinedEffects

          public java.util.ArrayList<Pair> getDefinedEffects()

      Returns all paired effects defined in the game.
      Returns:
        all paired effects defined in the game.

    - #### getDefinedEosEffects

          public java.util.ArrayList<java.lang.Integer> getDefinedEosEffects()

      Returns the list of sprite type with at least one EOS effect defined.
      Returns:
        the list of sprite type with at least one EOS effect defined.

    - #### getEosEffects

          public java.util.ArrayList<Effect> getEosEffects(int obj1)

      Returns all EOS effects defined in the game.
      Returns:
        all EOS effects defined in the game.

    - #### getCharMapping

          public java.util.HashMap<java.lang.Character,java.util.ArrayList<java.lang.String>> getCharMapping()

      Returns the char mapping of this array, that relates characters in the level with sprite names that it references.
      Returns:
        the char mapping of this array. For each character, there is a list of N sprite names.

    - #### getTerminations

          public java.util.ArrayList<Termination> getTerminations()

      Gets the array of termination conditions for this game.
      Returns:
        the array of termination conditions.

    - #### getResourceLimit

          public int getResourceLimit(int resourceId)

      Gets the maximum amount of resources of type resourceId that are allowed by entities in the game.
      Parameters:
        `resourceId` - the id of the resource to query for.
      Returns:
        maximum amount of resources of type resourceId.

    - #### getResourceColor

          public java.awt.Color getResourceColor(int resourceId)

      Gets the color of the resource of type resourceId
      Parameters:
        `resourceId` - id of the resource to query for.
      Returns:
        Color assigned to this resource.

    - #### getScreenSize

          public java.awt.Dimension getScreenSize()

      Gets the dimensions of the screen.
      Returns:
        the dimensions of the screen.

    - #### setStochastic

          public void setStochastic(boolean stoch)

      Defines this game as stochastic (or not) depending on the parameter passed.
      Parameters:
        `stoch` - true if the game is stochastic.

    - #### getAvatar

          public MovingAvatar getAvatar()

      Returns the avatar of the game.
      Returns:
        the avatar of the game.

    - #### setAvatar

          public void setAvatar(MovingAvatar newAvatar)

      Sets the avatar of the game.
      Parameters:
        `newAvatar` - the avatar of the game.

    - #### isGameOver

          public boolean isGameOver()

      Indicates if the game is over, or if it is still being played.
      Returns:
        true if the game is over, false if it is still being played.

    - #### getObservation

          public StateObservation getObservation()

      Retuns the observation of this state.
      Returns:
        the observation.

    - #### getRandomGenerator

          public java.util.Random getRandomGenerator()

      Returns the sampleRandom object
      Returns:
        the sampleRandom generator.

    - #### getGameTick

          public int getGameTick()

      Returns the current game tick of this game.
      Returns:
        the current game tick of this game.

    - #### getWinner

          public Types.WINNER getWinner()

      Returns the winner of this game. A value from Types.WINNER.
      Returns:
        the winner of this game.

    - #### getSpriteOrder

          public int[] getSpriteOrder()

      Gets the order in which the sprites are drawn.
      Returns:
        the order of the sprites.

    - #### buildLevel

          public abstract void buildLevel(java.lang.String gamelvl)

      Builds a level, receiving a file name.
      Parameters:
        `gamelvl` - file name containing the level.