Class Condition

  • Direct Known Subclasses:
    NextTurnCondition

    public class Condition
    extends java.lang.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));
            ...
          }
        }
     
    • Constructor Summary

      Constructors 
      Constructor Description
      Condition()
      Constructor for initializing a new instance of the Condition class.
      Condition​(java.lang.String name)
      Constructor for initializing a new instance of the Condition class.
      Condition​(java.lang.String name, java.util.concurrent.Callable<java.lang.Boolean> callable)
      Constructor for initializing a new instance of the Condition class.
      Condition​(java.util.concurrent.Callable<java.lang.Boolean> callable)
      Constructor for initializing a new instance of the Condition class.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.String getName()
      Returns the name of this condition, if a name has been provided for it.
      boolean test()
      You can choose to override this method to let the game use it for testing your condition each turn.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Condition

        public Condition()
        Constructor for initializing a new instance of the Condition class.
      • Condition

        public Condition​(java.util.concurrent.Callable<java.lang.Boolean> callable)
        Constructor for initializing a new instance of the Condition class.
        Parameters:
        callable - is a callable containing a method returning true, if some condition is met, or false when the condition is not met.
      • Condition

        public Condition​(java.lang.String name,
                         java.util.concurrent.Callable<java.lang.Boolean> callable)
        Constructor for initializing a new instance of the Condition class.
        Parameters:
        name - is the name of the condition used for identifying a specific condition between multiple conditions with the IBaseBot.onCustomEvent(dev.robocode.tankroyale.botapi.events.CustomEvent) event handler.
        callable - is a callable containing a method returning true, if some condition is met, or false when the condition is not met.
    • Method Detail

      • test

        public boolean test()
        You can choose to override this method to let the game use it for testing your condition each turn. Alternatively, you can use the one of the constructors that take a Callable instead.
        Returns:
        true if the condition is met; false otherwise.