This template is designed for the puzzle "Skynet Revolution" but can easily be adapted to other CodinGame puzzles and contests.
# Install dev dependencies
npm install
# Compile, minify (terser), beautify (prettier), concatenate, copy to clipboard
npx node build.js
Note: copy to clipboard is not supported on Linux system using Wayland. In such cases just copy yourself the content of
build/code.js
It's quite easy actually.
The trick is simply to give up the priority on the event loop
once per game loop iteration with setTimeout()
.
Here is the skeleton of CodinGame.js
:
// Copy the initialization readline calls from the JS codingame starter template.
initData = {};
...
// Init Elm app with initial data.
const app = this.Elm.Main.init({ flags: initData });
// Transfer Elm command to STDOUT.
app.ports.stdout.subscribe(answer => console.log(answer));
// Transfer Elm debug to STDERR.
app.ports.stderr.subscribe(msg => console.error(msg));
// Game loop.
(function gameLoop() {
// Send turn data to Elm for processing.
app.ports.stdin.send(readLinesIntoTurnData());
// Give up priority on the event loop to enable
// subscription to elm outgoing port to trigger.
setTimeout(gameLoop);
})();
// Update turnData with the new turn data.
// Performs side effects (readline)
function readLinesIntoTurnData() {
...
}
It is a normal Platform.worker
program with 3 ports set up.
-- Retrieve the updated game data every turn.
port stdin : (Value -> msg) -> Sub msg
-- Port to give the new command for this turn.
port stdout : String -> Cmd msg
-- Port to help debugging, will print using console.error().
port stderr : String -> Cmd msg
In the update function, after computing our strategy,
we generate a string and send it to the stdout
port
so that CodinGame executes our command.
- In
CodinGame.js
, changeinitData
initialization code by copying stuff from codingame JS starter template. - In
Main.elm
, change theInitData
type and associated decoder. - In
CodinGame.js
, change the content ofreadLinesIntoTurnData()
function by copying stuff from codingame JS starter template. - In
Main.elm
, change theTurnData
type and associated decoder. - In
Main.elm
, change the game logic for each turn.