GithubHelp home page GithubHelp logo

elm-codingame's Introduction

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.