Tuesday, September 8, 2009

Can your code beat a robot?

Robocode is a educational game which helps people practice their programming in Java. Competitors write code that builds robots and then "releases" them into the battlefield to compete against other robots.

I wouldn't say that it "teaches" you to programming in the way that the game itself teaches the language. Instead, it provides a challenging atmosphere that requires people to use their knowledge in programming and encourages the people themselves to improve their coding skills in order to build better robots and win more games. It is definitely more fun to write a program where you can actually see the results, instead of printing things to the console. In addition, the competitiveness of trying to build the best robot makes you want to improve your code.

I created 13 different robots that fell into one of three categories: movement, tracking, or firing. The movement robots had abilities ranging from sitting in one place to moving to the center of the battle field and moving in a circle. The tracking robots ranged from just following a target to following a target and reversing if it got to close to an enemy. Finally, the firing robots ranged from firing when its rotating gun came across an enemy to shooting bullets of size inversely proportional to the distance of the enemy.

All of my robots accomplished most of their tasks, although I would have liked to fix some details on some of my robots. For my Movement05 robot, it is supposed to move to each corner which requires it to move diagonally to get to some corners. However, if you move diagonally to a corner the robot cannot get all the way in the corner because of the size of the robot. The corner of the robot will hit the wall before it reaches the absolute corner. Therefore, if you try to move to an adjacent corner the robot will not be flush up against the wall.

One thing that bothered me about this Movement05 robot is that it was cluttered with trigonometry equations to calculate angles. I decided to put that function into its own method getAngle(). I also would store the heading of the robot into the variable heading everytime I wanted to use it. I removed the variable and just used the getHeading() call whenever I needed it.

/**
    * Computes the angle between the width and hypotenuse of the playing field
    * cut diagonally
    *
    * @param height
    *            height of the battle field
    * @param width
    *            width of the battle field
    * @return angle returns the angle between the width and the hypotenuse in
    *         degrees
    */
  
public double getAngle(double height, double width) {
      
double angle = Math.atan(height / width);
      
angle = Math.toDegrees(angle);
      
return angle;
  
}



Another problem I had was that in Tracking03 which is supposed to find the closest enemy and move in the opposite direction by 100 pixels, then stop. My robot does not stop, it continues to scan and move backwards.

Producing small simple robots like these were the right way to go in that you can focus on perfecting small tasks then adding more complex abilities. If I had tried to make a powerful and complex robot at once it would have been overwhelming. Overall I really enjoyed making these robots and I look forward to making an awesome robot and challenging others with it.

Learn More
Here is a link to my robots: link
Robocode Site
Robocode Wiki

0 comments:

Post a Comment