Wednesday, September 9, 2009

Java Style: Robocode No Jutsu!

Programming robots...that is pretty much the highlight of my week. Of course, it may not be what you'll initially think. Just those two words alone makes me think of movies such as iRobot and Terminator. But alas, the types of robots I programmed were on a much smaller, more virtual scale.

This past week, and future weeks to come, I am programming in Robocode, a java based, open source game. Robocode's motto alone "Build the best, destroy the rest" (which appears on the splash page every time Robocode is opened) will put a smile on your face as you code and watch your robot take action on the battlefield.

However, I am just a beginner, so much of the enjoyment will come when I have fully grasped the ways of Robocode. For starters I have developed and completed 12 simple robots that full requirements assigned by Prof. Johnson's - Software Engineering class. These simple robots covered tracking, movement, and firing. Since Robocode is Java based, I had no problem with syntax and using Robocode's API, as it is well laid out and mirrors Java's very own API. The only difficulty is getting to know the different functions each robot has, and any Utility functions that are used to calculate headings/bearings/distances.

My completed robots can be downloaded here

Upon installing Robocode, it comes with a variety of sample robots, each complete with source code so you'll know exactly how they work. I think including sample robots AND source code is an excellent way to dissect and reverse engineer these robots. It also makes learning how to code much more easier and smoother.

Some of the difficulties I encountered was the coordinate system of the battlefield. I'm used to coding Java Applets, so the coordinate system for those start in the upper left, but the coordinate system for Robocode starts as if it's in the First Quadrant, with the origin in the lower left. This was a disorienting fact as my robots would turn in the opposite direction I wanted.

Also was how Robocode handles degrees. When I started to code for turning, I assumed Robocode used the Unit Circle, with 0 degrees pointing to the right. This proved wrong, and later realized that it works just like a clock, where 0 degrees is at the top and moves clock-wise around 360. Calculating bearing and headings and knowing the difference between these two concepts was a bit difficult. Since you can't explicitly tell your robot to move to a specific degree, you have to subtract or add degrees to get it there. Luckily, the sample robots show code on how to reposition your robot given a target.

There is one of my robots that I am concerned about, and that is my Movement05 robot, where it moves to the 4 corners of the battlefield. I managed to use some basic trigonometry to calculate the angle to turn towards each corner. However, going from the upper left to lower right corner doesn't work too well if the battlefield is not a square. The angles become too steep and the robot hits a wall about 20 pixels short of the corner.

Coding in Robocode this week alone, I learned that programming for these robots is just like programming anything else in general. You need to be very specific in your coding. Using robots you can physically see where your code is acting "funny" without using a debugger that would just tell you a very technical wording error, accompanied by the line that cause it. There are also some technical things such as getting a robot to move to a specific place on the grid, whether that be a point such as the center or a corner, or a roaming target which you have to track.

No comments:

Post a Comment