Skip to content

CodinGame template for the Elm programming language

Notifications You must be signed in to change notification settings

elm-toulouse/elm-codingame

Repository files navigation

CodinGame Elm Template

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

How does it work? (JS side)

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() {
  ...
}

How does it work? (Elm side)

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.

Adapt to another puzzle or contest

  1. In CodinGame.js, change initData initialization code by copying stuff from codingame JS starter template.
  2. In Main.elm, change the InitData type and associated decoder.
  3. In CodinGame.js, change the content of readLinesIntoTurnData() function by copying stuff from codingame JS starter template.
  4. In Main.elm, change the TurnData type and associated decoder.
  5. In Main.elm, change the game logic for each turn.

About

CodinGame template for the Elm programming language

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published