Little Java Port [should be an easy solve]

Ok so i've coded very longly with processing and never felt the need of actually learning java. Now i also have very low knowledge on server stuffs. Of course i've run a server and opened a port, but that's not the point.

I know it's not to hard to port java code to processing since it's based on java and if someone could give a heads up, i'll be very grateful! So the following java code is not by me, but i couldn't port it to Processing:

/* SOI 2013 Creativity Task 'Cops and Robbers'
 * Sample bot in Java for the participants
 * by sebastian at soi dot ch

import java.util.*;
import java.lang.*;

class bot {

  // Game configuration.
  int C, R, N, M;
  char role;
  boolean G[][];

  // Players.
  int mouse_x, cops[];

  // IO.
  Scanner in = new Scanner(new BufferedReader(new InputStreamReader(;

  // Randomness source.
  Random random = new Random();

  private void readPos(boolean is_cops) {
    // Read current position(s) of player.
    if (is_cops)
      for (int c=0; c<C; c++)
        cops[c] = in.nextInt();
      mouse_x = in.nextInt();

  private void readStartData() {
    // Read start configuration.
    role = in.nextLine().charAt(0);

    // Read game configuration.
    C = in.nextInt(); R = in.nextInt(); N = in.nextInt(); M = in.nextInt();

    // Initialize.
    cops = new int[C]; G = new boolean[N+1][N+1];

    // Read streets.
    for (int edge = 0; edge < M; edge++) {
      int from = in.nextInt(), to = in.nextInt();
      G[from][to] = G[to][from] = true;

   // Read own start position.
   readPos(role == 'P');

  private int randomNeighbour(int a) {
    // Returns a randomly chosen neighbor of junction a.
    int neighbours = 0;
    for (int i = 1; i <= N; i++)
      if (G[a][i]) neighbours++;

    // Pick a random neighbour.
    int pick = random.nextInt(neighbours);
    for (int i = 1, k = 0; i <= N; i++) {
      if (G[a][i]) {
        if (pick == k) {
          return i;
        } else {
    return a;

  private void copsMove() {
    // Move the cops randomly.
    for (int c = 0; c < C; c++) {
      cops[c] = randomNeighbour(cops[c]);
      System.out.print(cops[c]+" ");

  private void mouseMove() {
    // Move the mouse randomly.
    mouse_x = randomNeighbour(mouse_x);

  public void loop() {
    // Driver loop.

    for (;;) {
      // Read position of adversary.
      readPos(role != 'P');

      if (role == 'P') {
      } else {

      // Flush.

  public static void main(String[] args) throws IOException {
    bot player = new bot();

Thanks for the help!


  • won't work, but if the purpose is to read key from the command line, you can replace the management of the in Scanner with keyPressed(), perhaps.

    Mmm, it is also used to read game data? This part can be handled with loadStrings(), I suppose.

    It is hard to adapt given that we don't know what it is supposed to do, to display, how to interact (if any) with the user, etc.

  • This program will be run by a game server. It has to read from stdin and has to output its moves to stdout. Thanks PhiLo

  • Answer ✓ only works when run from a console terminal AFAIK.
    Nonetheless, an input dialog box is a better substitute: =:)

     * No Repeat ID Input (v1.10)
     * by GoToLoop (2013/Nov)
     * /check-array-contents-with-arraylist
    import static javax.swing.JOptionPane.*;
    final StringList ids = new StringList( new String[] {
      "Eric", "Beth", "Katniss"
    void draw() {
      final String id = showInputDialog("Please enter new ID");
      if (id == null)   exit();
      else if ("".equals(id))
        showMessageDialog(null, "Empty ID Input!!!", 
        "Alert", ERROR_MESSAGE);
      else if (ids.hasValue(id))
        showMessageDialog(null, "ID \"" + id + "\" exists already!!!", 
        "Alert", ERROR_MESSAGE);
      else {
        showMessageDialog(null, "ID \"" + id + "\" successfully added!!!", 
  • This program will be run by a game server. It has to read from stdin and has to output its moves to stdout.

    Either you read those input data from a file or from execution passed arguments! >:)

  • edited November 2013

    Ok i'm sorry i didn't really explain it correctly the sample code can be run by a server that runs a cops and robbers program.

    In a terminal, i'd run a command like this:

    java -jar cops-and-robbers-server.jar mouseXCommand copsCommand C R mapPath startPosMouseX startPosCop1 startPosCop2 ...

    replacing the variables by actual values. Their meaning is:

    mouseXCommand: command to run the program playing Mouse X

    copsCommand: command to run the program playing the cops

    C: number of cops

    R: number of rounds played

    mapPath: path to the map (graph)

    startPosMouseX: number of the junction where Mouse X starts

    startPosCop1 startPosCop2 ... : numbers of the junctions where the cops start

    For Example:

    java -jar cops-and-robbers-server.jar 3 30 maps/scotyard30.txt 5 1 13 28

    I never done anything that way so let's call me a noob with this stuff. Helps very appreciated

Sign In or Register to comment.