|
| 1 | +import dev.robocode.tankroyale.botapi.*; |
| 2 | +import dev.robocode.tankroyale.botapi.events.*; |
| 3 | +import dev.robocode.tankroyale.botapi.graphics.Color; |
| 4 | + |
| 5 | +// ------------------------------------------------------------------ |
| 6 | +// Crazy |
| 7 | +// ------------------------------------------------------------------ |
| 8 | +// A sample bot originally made for Robocode by Mathew Nelson. |
| 9 | +// |
| 10 | +// This robot moves in a zigzag pattern while firing at enemies. |
| 11 | +// ------------------------------------------------------------------ |
| 12 | +public class Crazy extends Bot { |
| 13 | + |
| 14 | + boolean movingForward; |
| 15 | + |
| 16 | + // The main method starts our bot |
| 17 | + public static void main(String[] args) { |
| 18 | + if ("1".equals(System.getenv("RUMBLE_SMOKE"))) { |
| 19 | + System.out.println("RUMBLE_SMOKE_READY Crazy"); |
| 20 | + return; |
| 21 | + } |
| 22 | + new Crazy().start(); |
| 23 | + } |
| 24 | + |
| 25 | + // Called when a new round is started -> initialize and do some movement |
| 26 | + public void run() { |
| 27 | + // Set colors |
| 28 | + setBodyColor(Color.fromRgb(0x00, 0xC8, 0x00)); // lime |
| 29 | + setTurretColor(Color.fromRgb(0x00, 0x96, 0x32)); // green |
| 30 | + setRadarColor(Color.fromRgb(0x00, 0x64, 0x64)); // dark cyan |
| 31 | + setBulletColor(Color.fromRgb(0xFF, 0xFF, 0x64)); // yellow |
| 32 | + setScanColor(Color.fromRgb(0xFF, 0xC8, 0xC8)); // light red |
| 33 | + |
| 34 | + // Loop while as long as the bot is running |
| 35 | + while (isRunning()) { |
| 36 | + // Tell the game we will want to move ahead 40000 -- some large number |
| 37 | + setForward(40000); |
| 38 | + movingForward = true; |
| 39 | + // Tell the game we will want to turn right 90 |
| 40 | + setTurnLeft(90); |
| 41 | + // At this point, we have indicated to the game that *when we do something*, |
| 42 | + // we will want to move ahead and turn right. That's what "set" means. |
| 43 | + // It is important to realize we have not done anything yet! |
| 44 | + // In order to actually move, we'll want to call a method that takes real time, such as |
| 45 | + // waitFor. |
| 46 | + // waitFor actually starts the action -- we start moving and turning. |
| 47 | + // It will not return until we have finished turning. |
| 48 | + waitFor(new TurnCompleteCondition(this)); |
| 49 | + // Note: We are still moving ahead now, but the turn is complete. |
| 50 | + // Now we'll turn the other way... |
| 51 | + setTurnRight(180); |
| 52 | + // ... and wait for the turn to finish ... |
| 53 | + waitFor(new TurnCompleteCondition(this)); |
| 54 | + // ... then the other way ... |
| 55 | + setTurnLeft(180); |
| 56 | + // ... and wait for that turn to finish. |
| 57 | + waitFor(new TurnCompleteCondition(this)); |
| 58 | + // then back to the top to do it all again. |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + // We collided with a wall -> reverse the direction |
| 63 | + @Override |
| 64 | + public void onHitWall(HitWallEvent e) { |
| 65 | + // Bounce off! |
| 66 | + reverseDirection(); |
| 67 | + } |
| 68 | + |
| 69 | + // ReverseDirection: Switch from ahead to back & vice versa |
| 70 | + public void reverseDirection() { |
| 71 | + if (movingForward) { |
| 72 | + setBack(40000); |
| 73 | + movingForward = false; |
| 74 | + } else { |
| 75 | + setForward(40000); |
| 76 | + movingForward = true; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + // We scanned another bot -> fire! |
| 81 | + @Override |
| 82 | + public void onScannedBot(ScannedBotEvent e) { |
| 83 | + fire(1); |
| 84 | + } |
| 85 | + |
| 86 | + // We hit another bot -> back up! |
| 87 | + @Override |
| 88 | + public void onHitBot(HitBotEvent e) { |
| 89 | + // If we're moving into the other bot, reverse! |
| 90 | + if (e.isRammed()) { |
| 91 | + reverseDirection(); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + // Condition that is triggered when the turning is complete |
| 96 | + public static class TurnCompleteCondition extends Condition { |
| 97 | + |
| 98 | + private final IBot bot; |
| 99 | + |
| 100 | + public TurnCompleteCondition(IBot bot) { |
| 101 | + this.bot = bot; |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public boolean test() { |
| 106 | + // turn is complete when the remainder of the turn is zero |
| 107 | + return bot.getTurnRemaining() == 0; |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments