Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using CHelper plugin in IntelliJ for a coding contest

I quite recently discovered a coding site, with coding contests : CodinGame, and in order to solve the problems, we have to hand them over only one file with a main (in the following example, the class Player), and if other classes are needed, we include them in this file.

For this purpose (and seen to be working for another coding site), I have downloaded intelliJ and the plugin CHelper in order to put all the source files into one java file (it is supposed to be the purpose of the CHelper plugin). The problem is: I don't understand how to use/setup this plugin for my coding site. I know it should work because another user of this site has already used the plugin for this purpose.

What I want

For a more detailed example of what I want, here is the class with a main:

// Class Player in file Player.java
public class Player {
    public static void main(String[] args) {
        System.out.println(new Cell(1,2).toString());
    }
}

And this class Cell is in another java file :

// Class Cell in file Cell.java
public class Cell {
    int x,y;
    public Cell(int x, int y) {
        this.x = x;
        this.y = y;
    }
    public String toString() {
        return "["+x+","+y+"]";
    }
}

And I would like the plugin to merge the two (or more) java files in order to have this :

// Generated : 2 files merged into one file: Player.java
public class Player {
    public static void main(String[] args) {
        System.out.println(new Cell(1,2).toString());
    }

    // Class Cell merged in this file
    public class Cell {
        int x,y;
        public Cell(int x, int y) {
            this.x = x;
            this.y = y;
        }
        public String toString() {
            return "["+x+","+y+"]";
        }
    }
}

What I achieved

I installed IntelliJ correctly, and downloaded the CHelper plugin. I installed the toolbar menu buttons linked to TopCoder (the site that this plugin is expressly made for), but the Launch TopCoder button throws a RuntimeException : cannot run program .../javaws no such file. With some tasks downloaded from TopCoder, I succeeded in merging 2 files into one : TaskA.java into Main.java (with templates downloaded)

What would be ideal

If an Eclipse plugin could work like what I want, I would be very happy to know of it. In fact, that was what I was looking for at the beginning of my search, and I only found some plugin for the IntelliJ IDE.

like image 499
OroshiX Avatar asked Jul 20 '16 22:07

OroshiX


Video Answer


1 Answers

So I finally found a way to do what I wanted: the guy who had done it shared me a link to the help I needed.

I am going to sum it up specifically for CodinGame here.

I- Toolbar buttons

The important buttons to add to the menu toolbar are

  • create new task
  • modify task
  • delete task
  • Edit project settings

Now, we have some buttons in the red rectangle : toolbar IDEA

II- Edit settings

Then we have to edit project settings :

  • set the default directory to your default package
  • output directory is for the generated source file

Editing project settings

III- Create task

Next thing, we have to create a new task (green "+" button) and set it up using the advanced option. We add the tests input and known output with the button Edit tests. We say we want the generated file to be called Solution.java, and the class where we are going to write is going to be called CGXFormatter.java

Creating a new task

We now have two files which have appeared in our package .../puzzle :

  • CGXFormatter.java with a method solve, which is where we are going to read the input and give our answer in the output
  • CGXFormatter.task, which contains the info on the test cases, etc. in order for the plugin to generate the source file

IV- Write your solution

For example, we are just going to print "This is the result" in our CGXFormatter class (but we could have created another class file and called it, it would have worked by copying the definition of the class in the generated solution class). Like this :

package com......puzzle;

import java.util.Scanner;
import java.io.PrintWriter;

public class CGXFormatter {
    public void solve(int testNumber, Scanner in, PrintWriter out) {
        out.println("This is the result");
    }
}

V- Generate the solution

Last step: click on run. Then we have the directory generated which is created, and in it, we have the Solution.java file newly generated. We can read this :

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author XXX
 */
public class Solution {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        CGXFormatter solver = new CGXFormatter();
        try {
            int testNumber = 1;
            while (true)
                solver.solve(testNumber++, in, out);
        } catch (UnknownError e) {
            out.close();
        }
    }

    static class CGXFormatter {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            out.println("This is the result");
        }

    }
}

VI- Last step

Okay, there still remains a little problem: in CodinGame, the solution class should not have public in front of it, so just put class Solution instead of public class Solution and you're done. If you want, you can also put it in a script to do it automatically with a multirun (plugin to install in IDEA, also).

That's it, you're done.

VII- Edit Octobre 2019

If the plugin complains about not finding a net.egork.... class, you can add these steps that I found here

Update Intellij IDEA to the latest version. Secondly, you go to File -> Settings... -> Plugins and search for the chelper plugin. It is required to run the task run configurations, and it supplies you with the buttons on the toolbar, too. After you have done that, you should be getting the error about impossibility to find and load class from net.egork... Now you go to the jetbrains plugin site, search for chelper plugin there, and download the latest zip archive. After unzipping it, go to File -> Project Structure... -> Libraries -> + -> Java, select recursively the folder you just unzipped until you get to a bunch of jars that contain that missing class in the error. After you have added those jars to your classpath, along with JDK, it should be enough


As a side note, I remarked that the out.println didn't work as I intended (I don't know why), so I replaced it by System.out.println instead of using the proposed out object in the solve method.

like image 54
OroshiX Avatar answered Sep 29 '22 11:09

OroshiX