Error output in java for CodeBusters

Hi everyone,

Here is how to reproduce a small bug with the CodeBusters game.

  • get the default code (in Java).
  • in the game loop just add any debug output in the first line (i.e. System.err.println("Debug messages...");)
  • run the game

In the first error stream you’ll see this line twice.
This is the easiest way to reproduce it. But when you have more outputs to debug your app it becomes the hell :confused:

I precise that wasn’t able to reproduce it in another game.

I don’t reproduce this behavior. Here’s the game loop:

    // game loop
    while (true) {
        int entities = in.nextInt(); // the number of busters and ghosts visible to you
        for (int i = 0; i < entities; i++) {
            int entityId = in.nextInt(); // buster id or ghost id
            int x = in.nextInt();
            int y = in.nextInt(); // position of this buster / ghost
            int entityType = in.nextInt(); // the team id if it is a buster, -1 if it is a ghost.
            int state = in.nextInt(); // For busters: 0=idle, 1=carrying a ghost. For ghosts: remaining stamina points.
            int value = in.nextInt(); // For busters: Ghost id being carried/busted or number of turns left when stunned. For ghosts: number of busters attempting to trap this ghost.
        }
        System.err.println("Debug messages...");
        for (int i = 0; i < bustersPerPlayer; i++) {
            // MOVE x y | BUST id | RELEASE | STUN id | RADAR | EJECT x y
            System.out.println("MOVE 8000 4500");
        }

I only see the debug line once per turn. Are you sure you didn’t put it into the for (int i = 0; i < bustersPerPlayer ... loop? That loop will get executed a minimum of 2 times per turn, depending on game parameters.

  • danBhentschel

You must write the debug line as the first line of the game loop and check at the first iteration of the game.

// game loop
while (true) {
    System.err.println("Debug messages...");
    int entities = in.nextInt(); // the number of busters and ghosts visible to you
    for (int i = 0; i < entities; i++) {
        int entityId = in.nextInt(); // buster id or ghost id
        int x = in.nextInt();
        [...]

Ah. This is because the reading of input really is the dividing line between turns, not the loop itself. Change the code to the following:

    // game loop
    while (true) {
        System.err.println("Here 1");
        int entities = in.nextInt(); // the number of busters and ghosts visible to you
        System.err.println("Here 2");
        for (int i = 0; i < entities; i++) {
            int entityId = in.nextInt(); // buster id or ghost id
            int x = in.nextInt();
            int y = in.nextInt(); // position of this buster / ghost
            int entityType = in.nextInt(); // the team id if it is a buster, -1 if it is a ghost.
            int state = in.nextInt(); // For busters: 0=idle, 1=carrying a ghost. For ghosts: remaining stamina points.
            int value = in.nextInt(); // For busters: Ghost id being carried/busted or number of turns left when stunned. For ghosts: number of busters attempting to trap this ghost.
        }
        System.err.println("Here 3");
        for (int i = 0; i < bustersPerPlayer; i++) {
            // MOVE x y | BUST id | RELEASE | STUN id | RADAR | EJECT x y
            System.out.println("MOVE 8000 4500");
        }
    }

And the first turn output shows:

Here 1
Here 2
Here 3
Here 1

Then the second (and all subsequent turns) looks like:

Here 2
Here 3
Here 1

Actually, I have found in the past that you can’t always count on stderr showing up on the turn that I expect it to. I suspect this is because of buffering. It will always be in the proper order, but the turn splitting and the order WRT stdout is not always consistent.

  • danBhentschel

The best way to synchronize stderr with the current turn is to use it after the first reading line and before the last output.

1 Like