Class Condition
- java.lang.Object
-
- dev.robocode.tankroyale.botapi.events.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 theIBot.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 methodIBaseBot.addCustomEvent(dev.robocode.tankroyale.botapi.events.Condition)
that will triggerIBaseBot.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.
-
-
-
Constructor Detail
-
Condition
public Condition()
Constructor for initializing a new instance of the Condition class.
-
Condition
public Condition(java.lang.String name)
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 theIBaseBot.onCustomEvent(dev.robocode.tankroyale.botapi.events.CustomEvent)
event handler.
-
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 returningtrue
, if some condition is met, orfalse
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 theIBaseBot.onCustomEvent(dev.robocode.tankroyale.botapi.events.CustomEvent)
event handler.callable
- is a callable containing a method returningtrue
, if some condition is met, orfalse
when the condition is not met.
-
-
Method Detail
-
getName
public java.lang.String getName()
Returns the name of this condition, if a name has been provided for it.- Returns:
- The name of this condition or
null
if no name has been provided for it. - See Also:
IBaseBot.onCustomEvent(dev.robocode.tankroyale.botapi.events.CustomEvent)
-
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 aCallable
instead.- Returns:
true
if the condition is met;false
otherwise.
-
-