Wednesday, September 9, 2009

Robocode

Over the past week, I have been working with the Robocode project, a Java-based project available on SourceForge.net that allows users to create and battle robots. I downloaded Robocode from SourceForge.net, installed it on my computer, and then used the Eclipse IDE to do all of my programming. I only used the basic Robot, although there are also the AdvancedRobot and JuniorRobot.

I encountered this project as part of my software engineering class, but I didn't immediately begin building the robots that we needed to for class. Instead, I began experimenting with Robocode by going through the wiki for the project. It had a very useful tutorial on integrating Robocode with Eclipse, as well as a walkthrough on creating a baisc bot. The first robot that I created was this sample, which essentially moved along the walls of the field, firing when an enemy was in sight. The sample provided me with a basic skeleton for the code for all of my subsequent robots.

After getting a feel for Robocode, I moved on the the actual assignment. We were given descriptions of 13 basic robots that we were to implement within a week. The robots were categorized by their overall goals: movement, tracking, and firing. The process of building these robots allowed me to get used to programming in Robocode, from finding functions in the Robocode API, to understanding all of the robot's functionalities. Another resource that I occasionally turned to was this class website. Although I wasn't able to access the sample robots, there were code snippets that helped to point me in the right direction for some of the robots for my assignment.

The movement robots introduced me to the robots' coordinate system and movement options. Some of the counter-intuitive things about Robocode is that its coordinate system has the origin in the lower left corner, and degrees are measured clockwise, from the top of the screen. The origin was counter-intuitive to my experience with Java applets, which have it located in the top left corner, and I expected the degrees measured according to the unit circle, counter-clockwise from the right side of the screen. The basic Robot class can only use one move at a time, so turning or moving ahead, but not both. Thus, my general strategy was to orient the robot in the correct direction, and then move ahead the appropriate amount. I was able to implement all of the moves relatively quickly, with room for improvement as I describe later.

The tracking robots were much more challenging for me. Despite all of the practice at having my robot moving in very rigid patterns, I found it hard to get my robot to track another moving robot. Eventually, I used the Tracker sample robot provided with the download as a basis for several of my tracking robots. I had to eliminate a lot of code that was based on firing at targets, but digging through the sample code gave me a lot of experience at seeing the Robocode functions and getting a feel for what is involved in finding and tracking a robot.

Finally, with the firing robots, I moved on to having my robot shooting at other robots. Again, I found the biggest obstacle in tracking a single opponent with my gun. I ended up adapting the code from one of my tracking robots to complete this task. Firing is easy to do - you use the function fire(), which takes a single parameter on how much power to use - the tricky part is in finding your target. You can also be smart about how much power you use, so as not to waste power on targets that are very far away.

Overall, it seems like coding robots to track well is the biggest challenge we were assigned. Given more time, I would have liked to play around with my tracking code further, to be able to understand the reason for picking the intervals at which to move the robot ahead the full distance, or nearly full distance. I did end up changing the numbers for my purposes, but I don't know what would have been optimal.

I was able to successfully implement all 13 robots. However, I would be interested in optimizing some of my movement robots. Several of them move in a stair-case motion, going up, then across, or vice-versa, instead of moving in a diagonal line. I implemented the robots this way after running into some difficulty trying to get them to complete moves with a single straight line and opting to code in a way that I knew worked and that would save me time coding.

My implementation of all 13 basic robots for class are contained in this zip file.


Important links referenced in this post:

No comments:

Post a Comment