Search code examples
javarobocode

How can I get a RobotState of my robot?


Do you know, how can I get a RobotState of my robot in Robocode? I would like to get an information for example, if my robot is active, alive, dead etc. In debug mode is it possible to see that information, but how can I get this in my code?

enter image description here


Solution

  • The enum RobotState is only used in the core of RoboCode. There is no getState() method for your robot. (i digged through the source code)

    However: you can get all theses state in other ways:

    If your robot is hitting a wall, a HitWall event is fired. you can handle those events by overriding a method in your robot class:

    @Override
    public void onHitWall(HitWallEvent e)
    {
        //your code
    }
    

    same with hitting a robot:

    @Override
    public void onHitRobot(HitRobotEvent e)
    {
        //your code
    }
    

    if your robot dies:

    @Override
    public void onRobotDeath(RobotDeathEvent event) 
    {
        //salute your warrior
        //your code
    }
    

    and if it's alive it should still loop in your runs method:

    @Override
    public void run() {
        do {
             //here you do the normal stuff a robot does
        } while(true);
    }
    

    You can look at even more event methods in this interfaces: