Let’s jump into 1 - System.out.println for Debugging 😎
Using System.out.println
Robocode runs your robot inside a Java VM, so the standard way to print messages is with System.out.println
. These messages show up in the Robocode console window while your robot is running. This is a very useful feature for debugging and understanding what your robot is doing at any moment.
System.out.println("Scanning...");
📘 For full documentation of the
System
class, includingprintln
, see the Java System class documentation.
Why Use println?
Using System.out.println
helps you debug by letting you track what your robot is doing at key points. For example:
- When the robot starts or stops
- When it sees another robot
- When it hits a wall
- When it gets hit by a bullet
This lets you check what your robot is “thinking” and if it’s doing what you intended.
A Full Use Case Example
Let’s say you want your robot to introduce itself and report key events.
Step 1: Create a Name Field
First, define a name in your robot class:
public class DebugBot extends Robot {
String name = "Mr Robot";
Step 2: Add println Messages in Key Methods
public void run() {
System.out.println("Hello! My name is " + name);
while (true) {
System.out.println("Turning right and scanning...");
turnRight(45);
forward(100);
}
}
public void onScannedRobot(ScannedRobotEvent e) {
System.out.println(name + " saw a robot");
fire(1);
}
public void onHitByBullet(HitByBulletEvent e) {
System.out.println(name + " was hit by a bullet from direction: " + e.getBearing());
back(50);
}
public void onHitWall(HitWallEvent e) {
System.out.println(name + " hit a wall at bearing: " + e.getBearing());
back(100);
}
Output Example
In the Robocode console, you’ll see messages like:
Hello! My name is Mr Robot
Turning right and scanning...
Mr Robot saw a robot
Mr Robot was hit by a bullet from direction: 45.0
Mr Robot hit a wall at bearing: 90.0
This gives you a real-time log of what’s going on during battle.
Tips for Effective Debugging
- Be specific in your messages (e.g., include robot name, event type, and values).
- Use clear and consistent formatting.
- Temporarily comment out prints if they’re overwhelming.
- Print variables you think might be causing issues.
Challenge
Try adding System.out.println
to other events like onBulletHit
, onBulletMissed
, and onRobotDeath
. See if you can log all actions your robot takes and adjust behavior based on what you learn.