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.*;
import java.io.*;

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(System.in)));

  // 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();
    else
      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 {
          k++;
        }
      }
    }
    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);
    System.out.print(mouse_x);
  }

  public void loop() {
    // Driver loop.
    readStartData();

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

      if (role == 'P') {
        copsMove();
      } else {
        mouseMove();
      }

      // Flush.
      System.out.println();
      System.out.flush();
    }
  }

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

Thanks for the help!

Answers

  • System.in 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 ✓

    System.in 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)
     * 
     * forum.processing.org/two/discussion/869
     * /check-array-contents-with-arraylist
     */
    
    import static javax.swing.JOptionPane.*;
    
    final StringList ids = new StringList( new String[] {
      "Eric", "Beth", "Katniss"
    } 
    );
    
    void draw() {
      println(ids);
    
      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!!!", 
        "Info", INFORMATION_MESSAGE);
        ids.append(id);
      }
    }
    
  • 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 theProgramAbove.java theProgramAbove.java 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.