Buckle up for 3 - Conditions & Logic π
π Comparing Values
When your bot checks numbers, it uses comparison operators:
>greater than<less than==equal to!=not equal to
These help answer questions like βIs the opponent close?β or βIs my energy low?β
π Logical Operators
Once you have comparisons, glue them together with logical operators:
&&means and β every part must be true||means or β any part can be true!means not β flips true to false and vice versa
Hereβs a quick example that uses ||:
boolean lowEnergy = getEnergy() < 20;
boolean gunHot = getGunHeat() > 0;
if (lowEnergy || gunHot) {
turnRight(90); // Take a breather if tired or the gun is hot
}π΅οΈ onScannedBot Example
The onScannedBot method runs when your radar spots another bot. We can combine several conditions to decide when to fire:
@Override
public void onScannedBot(ScannedBotEvent e) {
double distance = Math.hypot(e.getX() - getX(), e.getY() - getY());
boolean isClose = distance < 150;
boolean gunReady = getGunHeat() == 0;
boolean lowEnergy = getEnergy() < 20;
if (gunReady && !lowEnergy && isClose) {
fire(3); // Use strong firepower when close
} else if (gunReady && !lowEnergy && !isClose) {
fire(2);
}
}distanceusesMath.hypotto calculate how far the opponent is.isClosechecks if the opponent is within 150 units.gunReadyverifies the gun has cooled down.lowEnergymakes sure we save power when health is low.- The combined
ifuses&&and!to act only when itβs safe.
Navigation
β¬ οΈ Back: If Statements β‘οΈ Next: Loops