Class Condition
java.lang.Object
dev.robocode.tankroyale.botapi.events.Condition
- Direct Known Subclasses:
NextTurnCondition
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
ConstructorDescriptionConstructor for initializing a new instance of the Condition class.Constructor for initializing a new instance of the Condition class.Constructor for initializing a new instance of the Condition class.Constructor for initializing a new instance of the Condition class. -
Method Summary
-
Constructor Details
-
Condition
public Condition()Constructor for initializing a new instance of the Condition class. -
Condition
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
-
Condition
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 Details
-
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:
-
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.
-