Class Condition

java.lang.Object
dev.robocode.tankroyale.botapi.events.Condition
Direct Known Subclasses:
NextTurnCondition

public class Condition extends Object
The Condition class is used for testing if a specific condition is met. For example, program execution can be blocked by using the IBot.waitFor(dev.robocode.tankroyale.botapi.events.Condition) method, which will wait until a condition is met. A condition can also be used to trigger a custom event by adding a custom event handler using the method IBaseBot.addCustomEvent(dev.robocode.tankroyale.botapi.events.Condition) that will trigger IBaseBot.onCustomEvent(dev.robocode.tankroyale.botapi.events.CustomEvent) when the condition is fulfilled.

Here is an example of how to use the condition:


  public class MyBot extends Bot {
    public void run() {
      while (isRunning()) {
        ...
        setTurnRight(90);
        waitFor(new TurnCompleteCondition(this));
        ...
      }
    }

    public class TurnCompleteCondition extends Condition {
      private final Bot bot;

      public TurnCompleteCondition(Bot bot) {
        this.bot = bot;
      }

      public boolean test() {
        return bot.getTurnRemaining() == 0;
      }
    }
  }
 

Here is another example using the same condition using a lambda expression instead of a (reusable) class:


  public class MyBot extends Bot {
    public void run() {
      while (isRunning()) {
        ...
        setTurnRight(90);
        waitFor(new Condition(() -> getTurnRemaining() == 0));
        ...
      }
    }