Other JVM Languages
Jump to navigation
Jump to search
Robocode is not limited to Java robots! Since Robocode allows you to load classes on the CLASSPATH, you can code robots in any language that can produce compiled CLASS files. Support for languages which don't have a compiler will require changes to Robocode which nobody has yet volunteered to implement.
The easiest way to add a language's runtime to Robocode's CLASSPATH is to edit the starter script.
Scala
You need to add scala-library.jar to Robocode's CLASSPATH.
java -Xmx512M -Dsun.io.useCanonCaches=false -cp libs/robocode.jar;_SCALA_HOME_/lib/scala-library.jar robocode.Robocode %*
Once that is done, you can code robots in Scala just as you would in Java:
// ScalaDemo.scala
package wiki
import robocode._
import robocode.util.Utils
/*
A small demo to test robot coding in Scala.
*/
class ScalaDemo extends AdvancedRobot {
// Main thread of robot
override def run {
setAdjustGunForRobotTurn(true)
while (true) {
// If we're not going anywhere, move randomly around battlefield
if (Math.abs(getDistanceRemaining) < Rules.MAX_VELOCITY
&& Math.abs(getTurnRemaining) < Rules.MAX_TURN_RATE)
{
setAhead((Math.random*2-1)*100)
setTurnRight(Math.random*90-45)
}
// Tell RADAR to spin
if (getRadarTurnRemaining == 0)
setTurnRadarRightRadians(4)
// Carry out pending actions
execute
}
}
// Picked up a robot on RADAR
override def onScannedRobot(e : ScannedRobotEvent) {
// Absolute bearing to detected robot
val absBearing = e.getBearingRadians + getHeadingRadians
// Tell RADAR to paint detected robot
setTurnRadarRightRadians(3*Utils.normalRelativeAngle(
absBearing - getRadarHeadingRadians ))
// Tell gun to point at detected robot
setTurnGunRightRadians(Utils.normalRelativeAngle(
absBearing - getGunHeadingRadians ))
// Tell robot to shoot with power 2
setFire(2)
}
// Robot ran into a wall
override def onHitWall(e : HitWallEvent) {
// Turn in opposite direction to wall
setTurnRightRadians(Utils.normalRelativeAngle(
e.getBearingRadians + Math.Pi))
}
}