Class GameManager<T extends AbstractPlayer>

java.lang.Object
com.codingame.gameengine.core.GameManager<T>
Type Parameters:
T - Your implementation of AbstractPlayer
Direct Known Subclasses:
MultiplayerGameManager, SoloGameManager

public abstract class GameManager<T extends AbstractPlayer> extends Object
The GameManager takes care of running each turn of the game and computing each visual frame of the replay. It provides many utility methods that handle instances of your implementation of AbstractPlayer.
  • Field Details

    • log

      protected static org.apache.commons.logging.Log log
    • players

      protected List<T extends AbstractPlayer> players
    • out

      protected PrintStream out
  • Constructor Details

    • GameManager

      public GameManager()
  • Method Details

    • allPlayersInactive

      protected abstract boolean allPlayersInactive()
    • readGameProperties

      protected abstract void readGameProperties(com.codingame.gameengine.core.InputCommand iCmd, Scanner s)
    • execute

      protected void execute(T player, int nbrOutputLines)
      Executes a player for a maximum of turnMaxTime milliseconds and store the output. Used by player.execute().
      Parameters:
      player - Player to execute.
      nbrOutputLines - The amount of expected output lines from the player.
    • dumpGameProperties

      protected void dumpGameProperties()
    • getGameSummaryOutputCommand

      protected abstract com.codingame.gameengine.core.OutputCommand getGameSummaryOutputCommand()
    • putMetadata

      public final void putMetadata(String key, double value)
      Puts a new metadata that will be included in the game's GameResult.

      Can be used for:

      • Setting the value of an optimization criteria for OPTI games, used by the CodinGame IDE
      • Dumping game statistics for local analysis after a batch run of GameRunner.simulate()
      Parameters:
      key - the property to send
      value - the property's value
    • setFrameDuration

      public void setFrameDuration(int frameDuration)
      Specifies the frameDuration in milliseconds. Default: 1000ms
      Parameters:
      frameDuration - The frame duration in milliseconds.
      Throws:
      IllegalArgumentException - if frameDuration ≤ 0
    • getFrameDuration

      public int getFrameDuration()
      Returns the duration in milliseconds for the frame currently being computed.
      Returns:
      the frame duration in milliseconds.
    • endGame

      protected void endGame()
      Set game end.
    • isGameEnd

      public boolean isGameEnd()
      Check if the game has been terminated by the referee.
      Returns:
      true if the game is over.
    • setMaxTurns

      public void setMaxTurns(int maxTurns) throws IllegalArgumentException
      Set the maximum amount of turns. Default: 400.
      Parameters:
      maxTurns - the number of turns for a game.
      Throws:
      IllegalArgumentException - if maxTurns ≤ 0
    • getMaxTurns

      public int getMaxTurns()
      Get the maximum amount of turns.
      Returns:
      the maximum number of turns.
    • setTurnMaxTime

      public void setTurnMaxTime(int turnMaxTime) throws IllegalArgumentException
      Set the timeout delay for every player. This value can be updated during a game and will be used by execute(). Default is 50ms.
      Parameters:
      turnMaxTime - Duration in milliseconds.
      Throws:
      IllegalArgumentException - if turnMaxTime < 50 or > 25000
    • setFirstTurnMaxTime

      public void setFirstTurnMaxTime(int firstTurnMaxTime) throws IllegalArgumentException
      Set the timeout delay of the first turn for every player. Default is 1000ms.
      Parameters:
      firstTurnMaxTime - Duration in milliseconds.
      Throws:
      IllegalArgumentException - if firstTurnMaxTime < 50 or > 25000
    • getTurnMaxTime

      public int getTurnMaxTime()
      Get the timeout delay for every player.
      Returns:
      the current timeout duration in milliseconds.
    • getFirstTurnMaxTime

      public int getFirstTurnMaxTime()
      Get the timeout delay of the first turn for every player.
      Returns:
      the first turn timeout duration in milliseconds.
    • setViewData

      public void setViewData(Object data)
      Set data for use by the viewer, for the current frame.
      Parameters:
      data - any object that can be serialized in JSON using gson.
    • setViewData

      public void setViewData(String moduleName, Object data)
      Set data for use by the viewer, for the current frame, for a specific module.
      Parameters:
      moduleName - the name of the module
      data - any object that can be serialized in JSON using gson.
    • setViewGlobalData

      public void setViewGlobalData(String moduleName, Object data)
      Set data for use by the viewer and not related to a specific frame. This must be use in the init only.
      Parameters:
      moduleName - the name of the module
      data - any object that can be serialized in JSON using gson.
    • addTooltip

      public void addTooltip(Tooltip tooltip)
      Adds a tooltip for the current turn.
      Parameters:
      tooltip - A tooltip that will be shown in the player.
    • addTooltip

      public void addTooltip(AbstractPlayer player, String message)
      Adds a tooltip for the current turn.
      Parameters:
      player - The player the tooltip information is about.
      message - Tooltip message.
    • addToGameSummary

      public void addToGameSummary(String summary)
      Add a new line to the game summary for the current turn.
      Parameters:
      summary - summary line to add to the current summary.
    • registerModule

      public void registerModule(Module module)
      Register a module to the gameManager. After this, the gameManager will call the module callbacks automatically.
      Parameters:
      module - the module to register
    • getLeagueLevel

      public int getLeagueLevel()
      Get current league level. The value can be set by using -Dleague.level=X where X is the league level.
      Returns:
      a strictly positive integer. 1 is the lowest level and default value.
    • formatSuccessMessage

      public static String formatSuccessMessage(String message)
      Helper function to display a colored message. Usually used at the end of the game.
      Parameters:
      message - The message to display.
      Returns:
      The formatted string.
    • formatErrorMessage

      public static String formatErrorMessage(String message)
      Helper function to display a colored message. Usually used at the end of the game.
      Parameters:
      message - The message to display.
      Returns:
      The formatted string.