Skip to content

Commit f0e341d

Browse files
author
Julien Poulton
committed
Merge branch 'merge-doc-with-source' into 'master'
[SDK][FIX] adding the tech.io doc in the sdk repo See merge request codingame/game-engine!153
2 parents 47adcf3 + e2469bc commit f0e341d

31 files changed

+2130
-0
lines changed

playground/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# CodinGame SDK
2+
3+
The CodinGame SDK is a Java project that allows you to write programming games for [CodinGame](https://www.codingame.com).
4+
5+
This project is a [Tech.io](https://tech.io) playground for the CodinGame SDK documentation.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Core concepts
2+
3+
This section will introduce the core concepts involved in creating games with the CodinGame Game Engine.
4+
5+
## The Game Runner
6+
7+
See how to **run your game locally** with the [Game Runner](core-2-game-runner.md).
8+
9+
## The Game Manager
10+
11+
See how to **manage players and the game flow** with the [Game Manager](core-3-game-manager.md).
12+
13+
## Configure your game
14+
15+
See how to setup your game [Configuration](core-4-configuration.md).
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# The Game Runner
2+
3+
The Game Runner lets you run your game locally during developement. It comes with a handy HTML package to watch each game's replay. The parameters you set to the Game Runner will not affect the final contribution.
4+
5+
You can create your own AI for your game and use the Game Runner to connect it to your game's implementation.
6+
7+
You can also fiddle with your game's initialization input, such as the seed for random values (for **Multiplayer** games) or a [test case file](core-4-configuration.md#test-case-file) (for **Solo** games).
8+
9+
# Usage
10+
11+
Include the dependency below in the pom.xml of your project.
12+
```xml
13+
<dependency>
14+
<groupId>com.codingame.gameengine</groupId>
15+
<artifactId>runner</artifactId>
16+
<version>2.13</version>
17+
</dependency>
18+
```
19+
Or a more recent version. See the [Release Notes](playground/misc/misc-3-release-notes.md).
20+
21+
As the Game Runner is meant for testing, you must create a Class with a `main` method in `src/test/java`.
22+
23+
Instantiate a `MultiplayerGameRunner` or a `SoloGameRunner` to launch a game with the `start()` method. This will create a temporary directory and start a server to serve the files of that directory. You don't need to stop the previous server to launch a new game.
24+
25+
In addition, you will need to set **Agents** to the Game Runner. They are programs you must code to test your game, just as if they were players' code submissions.
26+
27+
By default, you can access the game viewer for testing at [http://localhost:8888/test.html](http://localhost:8888/test.html). You may change the configuration of the game viewer by editing the `config.js` file. See the [Viewer configuration](core-4-configuration.md#viewer-configuration) for more details.
28+
29+
Warning ⚠ To use the game viewer locally, your browser must support ES6 JavaScript **modules**. For Chrome, that's version 61 or above. For Firefox, from version 54 this feature is behind the `dom.moduleScripts.enabled` preference. To change preferences in Firefox, visit `about:config`.
30+
31+
32+
# Examples
33+
34+
In order to run a game, you must have prepared a `Referee` and a `Player`. The game will surely fail to finish if they are not properly implemented. See [Game Manager](core-3-game-manager.md) for details.
35+
36+
## Running a **Multiplayer** game
37+
38+
### Using the same java class for each agent:
39+
```java
40+
MultiplayerGameRunner gameRunner = new MultiplayerGameRunner();
41+
gameRunner.addAgent(Agent.class);
42+
gameRunner.addAgent(Agent.class);
43+
gameRunner.start();
44+
45+
```
46+
_This method will prevent the agent from printing to stdout from any other class than Player._
47+
48+
### Using external python programs as agents:
49+
```java
50+
MultiplayerGameRunner gameRunner = new MultiplayerGameRunner();
51+
52+
gameRunner.addAgent("python3 /home/user/agent1.py");
53+
gameRunner.addAgent("python3 /home/user/agent2.py");
54+
gameRunner.addAgent("python3 /home/user/agent3.py");
55+
56+
gameRunner.start();
57+
```
58+
59+
### Using a custom seed:
60+
```java
61+
// I want to debug the strange case of this particuliar seed: 53295539
62+
MultiplayerGameRunner gameRunner = new MultiplayerGameRunner();
63+
gameRunner.setSeed(53295539L);
64+
gameRunner.addAgent(Agent1.class);
65+
gameRunner.addAgent(Agent2.class);
66+
gameRunner.start();
67+
```
68+
69+
## Running a **Solo** game
70+
71+
### Using a java class and a test case with its filename:
72+
```java
73+
SoloGameRunner gameRunner = new SoloGameRunner();
74+
gameRunner.setTestCase("test1.json"); // You must set a test case to run your game.
75+
gameRunner.setAgent(Solution.class);
76+
gameRunner.start();
77+
```
78+
79+
# Viewing a replay
80+
81+
Once a game is run, files are copied into a temporary folder. A server is started to serve those files on `localhost:8888`.
82+
83+
The test page `/test.html` let's you watch the replay as it would appear on CodinGame.
84+
85+
Many of the viewer's game-specific parameters may be changed by the default `config.js` file located in `src/main/resources/view` of your game's project. These parameters include:
86+
* The list of modules needed by the game.
87+
* The colours for the different players (affects the IDE).
88+
89+
See the [Viewer configuration](core-4-configuration.md#viewer-configuration) for more details.
90+
91+
# Testing
92+
93+
You can run your game without launching a server. This is useful to batch test your game in various conditions.
94+
95+
Call the GameRunner's `simulate()` function to launch such a game, it will return a `GameResult` object containing information about the game's execution.
96+
97+
```java
98+
// I want to make sure my randomly generated maps don't cause the game to crash
99+
for (int i = 0; i < 100; ++i) {
100+
MultiplayerGameRunner gameRunner = new MultiplayerGameRunner();
101+
gameRunner.setSeed((long) (i * 100));
102+
gameRunner.addAgent(Agent1.class);
103+
gameRunner.addAgent(Agent2.class);
104+
GameResult result = gameRunner.simulate();
105+
}
106+
```
107+
108+
An instance of `GameResult` exposes:
109+
* `outputs` & `errors` the standard and error outputs of all agents and the referee.
110+
* `summaries` the game summary as outputted by the GameManager.
111+
* `scores` the scores assigned to each agent.
112+
* `uinput` the game's input parameters (including the game's seed).
113+
* `metadata` extra info generated by your referee. Useful for debugging and mandatory for Optimization games.
114+
* `tooltips` the list of tooltips to be displayed on the replay's progress bar.
115+
* `agents` a list of objects representing the agents. Contains their index, avatar and nickname.
116+
* `views` the replay data, useful when creating a Module.
117+
118+
119+
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
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

Comments
 (0)