views:

159

answers:

2

IBM has (had) a free learn-Java program called RoboCode, in which custom robots could be written that would then do battle in a 2D space. I would like to write the environment that supports such robots, but don't know what pattern or design to use. Each robot is a thread. Each thread is given a certain (indeterminate) amount of run-time by the JRE. A robot world engine would start by building a list of the players/robots, then enter a loop that allows each player in turn to do whatever it likes: move forward, turn left, fire toward the northwest corner, whatever. Each robot is also informed of events of interest: an enemy or several enemies are within range, it has been hit by a bullet, etc.

The combination of handling threads that are also event listeners (are they also producers?) confuses me; it seems that there's more to the game engine. Even some general ideas would help. TIA.

+1  A: 

Robocode is now open source. It's a fairly elaborate project but easy enough to follow. Download the source to see the gory details.

Corbin March
A: 

Hi Bob, I'm Robocode engine developer. I joined 2 years ago and spent most of the time by refactoring Robocode engine. I wrote couple of articles during the time as my understanding grown. Not all of them are up to date, thought.

There are several difficult parts about it:

1) synchronization: we solved it by 'sending' messages between robot and battle. Updating the UI and other components is producer/consumer with non-mutable messages about each new state (game tick)

2) sandboxing malicious code (go and learn from our code)

3) design of API and rules to be constant for long years, because engine evolves, old robots stay same, but they must run and score same way as before. We hit several troubles here, for example people use the classes from API in their own logic. This wasn't intention. We had to make the classes final and with protected constructors. But now we can't change it to not broke existing robots.

For your question about events in Robocode. They all come each tick, we populate EventManager (priority queue) and then robot pulls one by one. Call is dispatched back to robot's implementation for each event. The subscription is automatic, implementation is always present in base class and you could override it in robot class. There is single thread for robot.

BTW: Next big thing seems to be extension of Robocode to allow for custom/plugable rules. It's design challenge. We are looking for smart, hard working people to join Robocode project to help us. Interested ?

Pavel Savara