static void withModeKeyGamesAndServer(final String mode, final String key, final int numberOfTurns, final URL serverUrl) 
    {
        System.out.println("\nConnecting to Vindinium server at " + serverUrl);

        State state = null;

        final HashMap<String,String> initParams = new HashMap<String,String>(1);
        initParams.put("key", key);

        // Construct api url
        URL url;
        try {
            if ("training".equals(mode)) {
                url = new URL(serverUrl + "/api/training");
                initParams.put("turns", String.valueOf(numberOfTurns));
            } else if ("arena".equals(mode)) {
                url = new URL(serverUrl + "/api/arena");
                System.out.println("Connecting and waiting for other players to join ...");
            } else {
                throw new RuntimeException("Invalid mode, should be arena or training.");
            }
        } catch (MalformedURLException e) {
            throw new RuntimeException("Invalid generated URL", e);
        } // end of catch

        for (int t = 0; state == null && t < 3; t++) { // Initial state
            try {
                state = IO.
                    fromPost(initParams, "UTF-8", url, "UTF-8", getState);

            } catch (IOException e) {
                System.err.println("Fails to get initial state (" + t +
                                   "). Will try again.");
                e.printStackTrace();
            } // end of catch
        } // end of for

        if (state == null) {
            throw new RuntimeException("Fails to get initial state");
        } // end of if

        // ---

        System.out.println("Playing at: " + state.viewUrl);

        //final Bot bot = new RandomBot(); // Replace by other Bot
        final Bot bot = new BeamerSauceBot_Miner();
        final HashMap<String,String> ps = new HashMap<String,String>(1);
        ps.put("key", key);

        boolean finished = state.game.finished;

        while (!finished) {
            ps.put("dir", bot.nextMove(state).toString());

            System.out.print(".");

            try {
                state = IO.
                    fromPost(ps, "UTF-8", state.playUrl, "UTF-8", getState);

                finished = state.game.finished;
            } catch (Exception e) {
                System.err.println("Fails to get next state");
                e.printStackTrace();
                finished = true;
            } // end of catch
        } // end of while
        bot.close();
    } // end of withServerUrl