Tuesday, September 8, 2009

Robocode fun!

For my ics 413 class, I am lucky enough to have Robocode programming as an assignment. Robocode is an open source program, written in Java, that allows a programmer to program a virtual robot to fight against other robots. At the very end of this assignment, the class will have a competition to see who will have the best combat robot. This is something that I think that I will enjoy working on, since I am a very competitive person.

The installation of Robocode went pretty smoothly. All I had to do was download the program from http://robocode.sourceforge.net/ and run the .jar file. It was very simple and only took a few minutes. However, getting the Robocode to work with eclipse was a little difficult to do. Luckily, there is an online step-by-step walkthrough of how to run Robocode from Eclipse and import your robot into Robocode to get the robot to run. I followed the instructions from this wiki.


To help us get acclimated with robocode, the class was assigned to create 12 different robots. The 12 different robots are :
  • Movement01: The minimal robot. Does absolutely nothing at all.
  • Movement02: Move forward a total of 50 pixels per turn. If you hit a wall, reverse direction.
  • Movement03: Each turn, move forward a total of N pixels per turn, then turn left. N is initialized to 10, and increases by 10 per turn.
  • Movement04: Move to the center of the playing field and stop.
  • Movement05: Move to the upper left corner. Then move to the lower right corner. Then move to the upper right corner. Then move to the lower left corner.
  • Movement06: Move to the center, then move in a circle, ending up where you started.
  • Tracking01: Pick one enemy and follow them.
  • Tracking02: Pick one enemy and follow them, but stop if your robot gets within 20 pixels of them.
  • Tracking03: Each turn, Find the closest enemy, and move in the opposite direction by 100 pixels, then stop.
  • Firing01: Sit still. Rotate gun. When it is pointing at an enemy, fire.
  • Firing02: Sit still. Pick one enemy. Only fire your gun when it is pointing at the chosen enemy.
  • Firing03: Sit still. Rotate gun. When it is pointing at an enemy, use bullet power proportional to the distance of the enemy from you. The farther away the enemy, the less power your bullet should use (since far targets increase the odds that the bullet will miss).
  • Firing04: Sit still. Pick one enemy and attempt to track it with your gun. In other words, try to have your gun always pointing at that enemy. Don't fire (you don't want to kill it).

After reading the methods from the robocode api, I thought I had a good idea of how to make my robot move around, but I realized that the movements were a lot trickier. Being able to move from one point to another precisely was very tricky since it required some geometry. A friend later pointed me in the to a website that contained a method that pointed the robot in the correct direction. I used the absoluteBearing method from the website. This helped my movements out greatly.

After figuring out the point to point movement of the robot, Movement04-Movement05 were relatively easy. But I ran into a few problems while on Movement06. I wasn't able to get a movement where a robot would move in a circle and stop when it returns to its starting point. I got the robot to move around in the circle, but the robot wouldn't stop when it return to the starting point.

The tracking robots were a bit tough to get working at first, but after reading the sample code and using a small portion of code to get the robot to lock onto a target, the tracking wasn't too bad. The firing robots were not very hard to get working. The only problem that I ran into was also getting the robot to track, but after I figured out the tracking, I was able to get the robot to track a single bot.

Samples of my code can be found here.

I learned that robots are really hard to program and need to be told to do almost every single action down to the smallest action. There is also a huge amount of math and physics that go into getting the robot to perform certain actions such as targeting and moving to the correct direction.

0 comments:

Post a Comment