|
| 1 | +# The Game Manager |
| 2 | + |
| 3 | +This document introduces the core code of the CodinGame's toolkit which includes the Game Manager and the viewer's replay engine. |
| 4 | + |
| 5 | +# Usage |
| 6 | + |
| 7 | +Include the dependency below in the pom.xml of your project. |
| 8 | +```xml |
| 9 | +<dependency> |
| 10 | + <groupId>com.codingame.gameengine</groupId> |
| 11 | + <artifactId>core</artifactId> |
| 12 | + <version>2.4</version> |
| 13 | +</dependency> |
| 14 | +``` |
| 15 | +Or a more recent version. See the [Release Notes](misc/misc-3-release-notes.md). |
| 16 | + |
| 17 | + |
| 18 | +## Basic Implementation |
| 19 | + |
| 20 | +Your project should include the class `Player` and the class `Referee`. |
| 21 | +Your `Referee` class may then inject (using Guice) a singleton of `SoloGameManager` or `MultiplayerGameManager` (corresponding to the type of game you want to create) parameterized by your `Player` class. |
| 22 | +Your `Player` class should extend `AbstractSoloPlayer` or `AbstractMultiplayerPlayer`. |
| 23 | + |
| 24 | +Example for a **Multiplayer** game: |
| 25 | +```java |
| 26 | +class Player extends AbstractMultiplayerPlayer { |
| 27 | + @Override |
| 28 | + public int getExpectedOutputLines() { |
| 29 | + return 1; |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +public class Referee extends AbstractReferee { |
| 34 | + @Inject private MultiplayerGameManager<Player> gameManager; |
| 35 | + @Override |
| 36 | + public void init() { |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public void gameTurn(int turn) { |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public void onEnd() { |
| 45 | + } |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +Example for a **Solo** game: |
| 50 | +```java |
| 51 | +class Player extends AbstractSoloPlayer { |
| 52 | + @Override |
| 53 | + public int getExpectedOutputLines() { |
| 54 | + return 1; |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +public class Referee extends AbstractReferee { |
| 59 | + @Inject private SoloGameManager<Player> gameManager; |
| 60 | + @Override |
| 61 | + public void init() { |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public void gameTurn(int turn) { |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public void onEnd() { |
| 70 | + } |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +The Game Manager's API will thus work with your `Player` class, which you may modify at leisure. |
| 75 | + |
| 76 | +# Features |
| 77 | + |
| 78 | +## General Features |
| 79 | +This section introduces the different features of the Game Manager for any type of game. |
| 80 | + |
| 81 | +For type-specific features, see: |
| 82 | +- [Multiplayer Game Features](#multiplayer-game-features) |
| 83 | +- [Solo Game Features](#solo-game-features) |
| 84 | +- [Optimization Game Features](#optimization-game-features) |
| 85 | +>Note that an Optimization game *is* a Solo game with more settings |
| 86 | +
|
| 87 | +### Players |
| 88 | + |
| 89 | +You can get your `Player` instances from the Game Manager with `getPlayer` methods. They allow you to interact with the players' AIs. |
| 90 | + |
| 91 | +You can use the `getNicknameToken()` and `getAvatarToken()` methods to get tokens that will be converted into the real corresponding information by the viewer. |
| 92 | + |
| 93 | +To allow the AIs to play: |
| 94 | +- You must send input data to your players with `sendInputLine()`. |
| 95 | +- Execute one turn of their code with `execute()` |
| 96 | +- Finally, get their output with `getOutputs()` and use them in your game. |
| 97 | + |
| 98 | +**Timeout** |
| 99 | +If a player times out (send an invalid value, takes too long to execute ...) you will be sent a `TimeoutException`. You can use this to end the game or deactivate the player, for example. |
| 100 | + |
| 101 | +### Maximum number of turns |
| 102 | + |
| 103 | +You can set the maximum number of turns before the game ends (even if there are still active players). If you don't set this paramter, the game will end within **400** turns. |
| 104 | + |
| 105 | +```java |
| 106 | +gameManager.setMaxTurns(200); |
| 107 | +``` |
| 108 | + |
| 109 | +>This parameter is an important performance setting. See the [Guidelines](playground/misc/misc-1-guidelines.md) for more details. |
| 110 | +
|
| 111 | +### Turn maximum time |
| 112 | + |
| 113 | +You can set the maximum time allowed to a Player to execute their code for a turn. If you don't set this paramter, the players will have **50**ms to execute. |
| 114 | + |
| 115 | +```java |
| 116 | +gameManager.setTurnMaxTime(45); |
| 117 | +``` |
| 118 | + |
| 119 | +>This parameter is an important performance setting. See the [Guidelines](playground/misc/misc-1-guidelines.md) for more details. |
| 120 | +
|
| 121 | +### Tooltips |
| 122 | + |
| 123 | +Tooltips will appear on the replay of the current game. They are usually short and describe when a player loses or timeout. |
| 124 | + |
| 125 | +```java |
| 126 | +gameManager.addTooltip(player, player.getNicknameToken() + " timeout!"); |
| 127 | +``` |
| 128 | + |
| 129 | +### Game Summary |
| 130 | + |
| 131 | +You can add texts that will be displayed to all players in the game summary, under the viewer. |
| 132 | + |
| 133 | +```java |
| 134 | +gameManager.addToGameSummary(String.format("%s pushed %s!", player1.getNicknameToken(), player2.getNicknameToken())); |
| 135 | +``` |
| 136 | + |
| 137 | +### Frame duration |
| 138 | + |
| 139 | +You can modify the duration of a frame displayed in the viewer. |
| 140 | + |
| 141 | +```java |
| 142 | +gameManager.setFrameDuration(2000); |
| 143 | +``` |
| 144 | + |
| 145 | +The duration is in milliseconds and the default one is 1000ms. |
| 146 | + |
| 147 | +You can also get this value with `gameManager.getFrameDuration();`. |
| 148 | + |
| 149 | +## Multiplayer Game Features <a name="multiplayer-game-features"></a> |
| 150 | + |
| 151 | +In a Multiplayer game, you will need to use the `MultiplayerGameManager` implementation parameterized with your `Player`. |
| 152 | + |
| 153 | +### Game parameters |
| 154 | + |
| 155 | +The Multiplayer Game Manager gives you access to **Game parameters**. These are used to vary your game and allow configuration in the CodinGame IDE Options. The game parameters is a `Properties` object you can get with `getGameParameters()`. |
| 156 | + |
| 157 | +By default, the game parameters contain a randomized `seed`. |
| 158 | + |
| 159 | +### Active Players |
| 160 | + |
| 161 | +All the players are active at the beginning of a battle. If they lose or timeout, you can choose to `deactivate()` them. They will no longer be in the list of players obtained with `getActivePlayers()`. |
| 162 | + |
| 163 | +### End Game |
| 164 | + |
| 165 | +You may want to end the game before the maximum number of turns is reached. You can use `endGame()` whenever one of the players meets the victory condition. |
| 166 | + |
| 167 | +## Solo Game Features <a name="solo-game-features"></a> |
| 168 | + |
| 169 | +In a Solo game, you will need to use the `SoloGameManager` implementation parameterized with your `Player`. |
| 170 | + |
| 171 | +### Test cases |
| 172 | + |
| 173 | +Your game will need at least one test case. See [Test case files](core-4-configuration.md#test-case-file) for more details on their creation. |
| 174 | + |
| 175 | +The Solo Game Manager provides you the test case input with `getTestCase()`. |
| 176 | + |
| 177 | +### End Game |
| 178 | + |
| 179 | +In Solo games, you do not compete against other players. Therefore, you will need to decide when a player *wins* or *loses*. To do that, you have two methods: |
| 180 | +```java |
| 181 | +gameManager.winGame(); |
| 182 | +``` |
| 183 | +and |
| 184 | +```java |
| 185 | +gameManager.loseGame(); |
| 186 | +``` |
| 187 | + |
| 188 | +## Optimization Game Features <a name="optimization-game-features"></a> |
| 189 | + |
| 190 | +An Optimization game is a Solo game with a score. The only differences comes in the [configuration](core-4-configuration.md#optimization-game-configuration) and the metadata you need to send. |
| 191 | + |
| 192 | +Once your game is correctly configured, you need to send your player's score. We advise you set it at the end of the game as below: |
| 193 | +```java |
| 194 | +@Override |
| 195 | +public void onEnd() { |
| 196 | + // I set my criteria "Fuel" to what's left of the player's fuel |
| 197 | + gameManager.putMetadata("Fuel", remainingFuel); |
| 198 | +} |
| 199 | +``` |
| 200 | + |
| 201 | +### Score calculation |
| 202 | + |
| 203 | +Once the game is online, players will be able to submit their code and a score will be calculated to determine their rank in the leaderboard. |
| 204 | + |
| 205 | +This score corresponds to **the sum of all the scores obtained when running validators**. Validators are specific kinds of test cases. Make sure you [configure them correctly](core-4-configuration.md#test-case-file). |
0 commit comments