Skip to content

Commit 50e3044

Browse files
Merge pull request #6 from robocode-dev/add-crazy-bot
Add Crazy bot (Java)
2 parents 6ec882b + 2a64814 commit 50e3044

4 files changed

Lines changed: 140 additions & 0 deletions

File tree

bots/java/Crazy/Crazy.cmd

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@echo off
2+
setlocal
3+
set BOT_API_VERSION=1.1.0
4+
if "%RUMBLE_JAVA_CACHE%"=="" set RUMBLE_JAVA_CACHE=%USERPROFILE%\.cache\rumble-bots-java
5+
if not exist "%RUMBLE_JAVA_CACHE%" mkdir "%RUMBLE_JAVA_CACHE%"
6+
set JAR=%RUMBLE_JAVA_CACHE%\robocode-tankroyale-bot-api-%BOT_API_VERSION%.jar
7+
if not exist "%JAR%" curl -fsSL -o "%JAR%" "https://repo1.maven.org/maven2/dev/robocode/tankroyale/robocode-tankroyale-bot-api/%BOT_API_VERSION%/robocode-tankroyale-bot-api-%BOT_API_VERSION%.jar"
8+
java -cp "%JAR%" "%~dp0src\Crazy.java" %*

bots/java/Crazy/Crazy.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "Crazy",
3+
"version": "1.0.0",
4+
"authors": ["Mathew Nelson", "Flemming N. Larsen"],
5+
"description": "Moves in a zigzag pattern, bouncing off walls, and fires at every bot it scans.",
6+
"platform": "JVM",
7+
"programmingLang": "Java 17",
8+
"gameTypes": ["1v1", "melee", "twinduel"],
9+
"license": "Apache-2.0"
10+
}

bots/java/Crazy/Crazy.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env sh
2+
set -eu
3+
SCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
4+
BOT_API_VERSION=1.1.0
5+
CACHE_DIR="${RUMBLE_JAVA_CACHE:-$HOME/.cache/rumble-bots-java}"
6+
JAR="$CACHE_DIR/robocode-tankroyale-bot-api-$BOT_API_VERSION.jar"
7+
if [ ! -f "$JAR" ]; then
8+
mkdir -p "$CACHE_DIR"
9+
curl -fsSL -o "$JAR.tmp" "https://repo1.maven.org/maven2/dev/robocode/tankroyale/robocode-tankroyale-bot-api/$BOT_API_VERSION/robocode-tankroyale-bot-api-$BOT_API_VERSION.jar"
10+
mv "$JAR.tmp" "$JAR"
11+
fi
12+
exec java -cp "$JAR" "$SCRIPT_DIR/src/Crazy.java" "$@"

bots/java/Crazy/src/Crazy.java

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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

Comments
 (0)