Thursday, September 3, 2009

Robocode. Let the battle begin!


When was the last time you had some fun programming? Robocode is an open source software program that provides some fun and experience with writing code in java. You download a program that builds an environment in which you write code to create a robot that will follow your instructions ultimately to smash the other robots. This was an IBM program that was meant as an exercise in programming. But now 8 years after the release, the software is still strong. It's being maintained by volunteers and could very well go for another 8 years in my opinion. Why? Well I've had a taste of the software and how it works. After going through a short installation process, you'll be absorbed in programming your own robot for hours. It might be a guy's thing with building the most violent robot or just being competitive. You should also take a look at this wiki page volunteers have made for this program. They are a VERY dedicated bunch. (http://robowiki.net/wiki/Main_Page) Well, without sounding like I'm their advertising representative, let the battle begin!

This post will have a few stages to it. First I'll give a quick note about installation and go on to the things I've learned.

Installation:

Go to the link provided above to robowiki.net. It has a very thorough guide to installing robocode and programming it in the Eclipse IDE. Inside the basic tutorial page there will be five links for:
These are the five essential guides that will set you on the course to becoming a Robocoder. Follow through the instructions and you should be fine. They even got pictures and everything.

Basics functions:

I had twelve small programs to build. They were meant to challenge my knowledge and force me to learn the rudimentary skills of Robocode and coding in general. I won't bore you with a long explanation of what to do. Read it and I give a brief about it afterwards. My source code provided will give you an explanation of how I implemented.

1. Movement01. Compile the Robocode and check if you are able to run it.

2. Movement02. Move 50 pixels each turn and be able to turn around when the robot hits a wall.

3. Movement03. Move n pixels per turn and increment n by 10 each turn. n+=10.

4. Movement04. Move the robot to the center of the battle field.

5. Movement05. Move the robot in order from upper left corner, to lower right corner, to upper right corner and then to the lower left corner. You must move diagonally when you can.

6. Movement06. Move the robot to the center and then in a complete circle with the robot ending up in the same position from where it started from, the center.

7. Tracking01. Follow exactly one robot around.

8. Tracking02. Follow the robot and keep at least 20 pixels between it and your robot.

9. Tracking03. Each turn, determine the closest enemy around your robot and move 100 pixels away from it.

10. Firing01. Spin constantly and fire bullets at any of the targets when your robot detects them. The robot cannot move.

11. Firing02. Only target one enemy while spinning and fire on this target alone. The robot cannot move.

12. Firing03. Fire at any targets, but the fire power must be proportional to the distance from your robot. The farther away, the weaker the fire power. The robot cannot move.

13. Firing04. Track a target with your robots gun, always pointing at it. Although you don't actually fire.

I had a lot of problems with some of these programs. For example, movement05 required the robot to move diagonally rather than from wall to wall. Therefore, I had to find the angle to turn the robot and the length to move it. First I found the length using the pythagorean theorem, but finding the angle proved more difficult. In the end I found a compromise. I moved the robot to a certain point that was always 200 pixels away from the walls. That way I knew that the angle was always 45 degrees and the hypotenuse would be 282.

Fear not programmers, it may sound bleak but there are a lot of materials and samples you can rely upon to get you through this. The Robocode package itself has sample robots that you can try out and that have source codes to reference on. Look around and you'll find something to solve your problem. That's what I did.

Conclusion:

This particular exercise made me realize something that was inevitable. Mathematics is really important in programming. I need to know fundamental formulas in order to produce practical code. I actually look forward to studying math now, because I think it will be useful. These 13 programs were useful. Without it, there's not much you can do with Robocode if you don't know the basics.

Once you've gotten the basics out of the way, that's when you can actually built your own robot that will defeat the rest. Over the course of coding and testing these robots, I always had ideas coming up about how to change little details or how to use what already worked. Robocode is very exciting. (except at 1:00 in the morning and you're neck is aching from working on a bug all night)
Go to my UH website to download my source code for all thirteeen exercises. (http://www2.hawaii.edu/~wingma/)

No comments:

Post a Comment