Our own Robocode contest

Liviu Adrian Esanu
METRO SYSTEMS Romania
6 min readSep 18, 2017

Robocode is a game where you write the code for a tank like mini robot. Then your code gets to play against other people. Ten such robots are pitted against each other, and they gain points by shooting or ramming into other robots.

The Robocode project has an interesting history: it was created in 2000 by Mat Nelson, then IBM took interest in it in 2001. By 2005 the Robocode community was releasing newer versions of the game, and it was used in coding competitions and by students testing out their competitive algorithms.

The game rules are simple:

  • The game map is a rectangle and the robot can move anywhere on it:
  • Game time is referred to as ticks. Each robot will perform one turn each tick.
  • A robot is composed of three parts: the vehicle, the gun and the radar. These parts move independent of each other.

All this is done using the API offered by the Robocode platform. Here are some of its most useful methods:

  • turnRight(double degree) / turnLeft(double degree) = turns the robot
  • ahead(double distance) / back(double distance) = moves the robot
  • turnGunRight(double degree) / turnGunLeft(double degree) = turns the gunturnRadarRight(double degree) / turnRadarLeft(double degree) = turns the radar
  • getX() / getY() = get the coordinates of the robot
  • getHeading() / getGunHeading() / getRadarHeading()= get the angle of the vehicle / gun / radar
  • getBattleFieldWidth() / getBattleFieldHeight() = get dimensions of the map

This set of simple rules and API methods makes creation of a new robot to be easy and fast. Here is the code for a very simple robot (taken from Robocode wiki):

package man;
import robocode.*;

public class MyFirstRobot extends Robot {
public void run() {
while (true) {
ahead(100);
turnGunRight(360);
back(100);
turnGunRight(360);
}
}

public void onScannedRobot(ScannedRobotEvent e) {
fire(1);
}
}

Every turn, the commands issued by our robot above will be executed. He will move forth and back, and turn his gun and radar to search for enemies. Once an enemy is scanned, it fires a bullet. Since bullet power is between 0.1 and 3 and the speed of each bullet is 20 — (3 * bullet power), our bullet of power 1 is a pretty fast one, moving with a sped 17.

Our Robocode contest consisted of a 4 hour long marathon, where competitors wrote their robots and 2 rounds of battle: one intermediary round (after 2 hours of coding time) and one final round. The resulting robots had some very interesting and ingenious strategies. I will list below the key features of our most successful three robots.

RustyTrumpet:

RustyTrumpet

It is the yellow and orange colored bot, seen in the upper right corner in the above image.

- It keeps a list of all the other robots, and the distance to each of them

- Every time it scans a robot, it updates the distance to it, in the list above

- If the currently scanned enemy happens to be the robot closest to it, RustyTrumpet fires a bullet of maximum power (that is 3) but slow speed.

- The aiming strategy is highly efficient: it predicts where the target will be by looking at its current position, speed, orientation and distance. Thus, it computes the future position of the enemy, and fires at that location.

- Its movement pattern is semi random: it choose one direction and performs a large semicircle in that direction. After its completion, another direction is chosen and the next semicircle is performed.

PizzaPower:

PizzaPower

This bot won the second place, and can be seen in the upper right corner of the image above. Its strategy is the following:

- Movement pattern is similar to RustyTrumpet, namely it moves in random semicircles, except they are smaller and designed to keep him near on of the corners

- When there are only 2 opponent left, he suddenly changes his movement pattern: it start to oscillate in large random forward and backward semi circles.

- Aiming strategy is predictive, though a bit less refined than the previous bot. It computes the distance to the target enemy, then computes the time a bullet would take to get there, then approximates the location of that enemy after the computed time delay.

- It always fires at the closest enemy, and keeps track of its last scanned location., thus he can predict its movement.

- Its weakness is that it only stores the last position of the target. His aiming pattern makes computations based on that position and the current position of the target. More elaborate predictions could have been made if PizzaPower were to have stored more detailed information about enemies.

AdeBot:

AdeBot

The last, and most complex of the bots presented, is AdeBot, the red on seen in the upper left part of the image above. He won first place, and rightfully so:

- It keeps detailed data on all the enemies scanned. On each scan event it updated the scanned bot’s position.

- The movement strategy is highly advanced: it moves in short bursts, each time it computes a series of locations where he can easily get to (based on his current orientation) then choses the one where it is less likely to find another bot nearby (using all the data it stores about enemies). It takes into account the current position of enemy bots, their speed and orientations — so when deciding on a location to move to, it estimates how many opponents will be near that location, by considering their most likely trajectory.

- Targeting for AdeBot is similarly complex: for each enemy he computes the likely position where he will be. Once all these possible angles are computed, it will fire in the direction that will intersect the most of these enemy trajectories. Furthermore, it this direction should not need for the gun to be turned too much, as this extra time required will interfere with the computations.

- One extra advantage of AdeBot is its radar movement: while most bots had their radars continuously turning in a circle, some had left right radar sweeping motions, thus increasing their number of enemy scan events. AdeBot went one step further, and only scanned the minimum amount needed to get info about all the enemies, thus when all enemies were scanned the radar turned back in a sweeping motion.

Overall Robocode is a great example of how a set of simple rules and an easy to use API lead to emerging complexity, and it proved to be a fun and challenging contest. Its beauty lies in the fact that a simple bot can be programmed in minutes, but a good one is much harder to achieve.

Links and references:

1. http://robowiki.net/wiki/Robocode

2. http://robowiki.net/wiki/Robocode/My_First_Robot

3. http://mark.random-article.com/weber/java/robocode/lesson2.html

--

--

Liviu Adrian Esanu
METRO SYSTEMS Romania

Senior Java developer at METRO Systems Romania. 9+ years of Java experience. Founder of “Bucharest Competitive Programming” group.