Tuesday, September 8, 2009

My First Robot Assignment

Click to download LNP package

Robocode is a Java-based game, created by Mathew Nelson. Digest through all the robocode resource, I found that it's very interesting that the initial purpose of the this project was to prove that we can program game in Java.

Include in my package are the following movements:

  • Movement01: The robot does absolutely nothing.
  • Movement02: The robot move forward a total of 50 pixels per turn and it reverse direction if it hits a wall.
  • Movement03: The robot move forward a total of N pixels per turn, then turn left. N is initialized to 10 and increases by 10 per turn.
  • Movement04: The robot move to the center of the battlefield and stop.
  • Movement05: The robot move to the upper left corner, then to the lower right corner, then to the upper right corner, and to the lower left corner.
  • Movement06: The robot move to the center, then move in a circle, and ending up where it started.
  • Tracking01: The robot pick one enemy to follow them.
  • Tracking02: The robot pick one enemy and follow them, but stop when it gets within 20 pixels of them.
  • Tracking03: The robot find the closest enemy each turn and move in the opposite direction by 100 pixels, then stop.
  • Firing01: The robot sit still, rotate gun, and fire when it is pointing at an enemy.
  • Firing02: The robot sit still, pick one enemy, and only fire when it is pointing at the chosen enemy.
  • Firing03: The robot sit still, rotate gun, and fire the bullet power proportionally to the distance of the enemy.
  • Firing04: The robot sit still, pick one enemy, and attempt to point the gun at the enemy all the time but does not fire.
  • It's very obvious that one of my problem was to install the Robocode. Due to the default program for any *.jar file on my computer was Eclips, the installation of robocode did not start automatically. It tooks me a while to figure out and change the default setting of *.jar file.

    After I successfully installed the Robocode, first I took a few hours to get my feet wet with all the classes, functions, movements, and events. I've read each link in 06.Robocode of this class as well as did my searching, and digging through the sample robot's code.

    I did not have any problem with movement 01 to 03 since they are just to understand the basic steps of a robot. However, I've spent a lot of time on movement 04. I tried to create a shared function for movement 04, 05, and 06 and I first started with two steps to get to a new position. It seemed to work well for movement 04; however, it got confused when the robot has to go to multiple postions in movement 05. Instead, I rewrote the function by applying some trignometry function to measure the angle between two. Below, I attached my code to calculate the angle between two points:

        public double getDegree(double x1, double y1, double x2, double y2){
            double dx = Math.abs(x1-x2);
            double dy = Math.abs(y1-y2);
            double t = dy/dx;    //tan = opp/adj
            double r = Math.atan(t);     //angle = inverse tan (in radian)
            return Math.abs(r * (180/Math.PI));     // deg = rad * 180/pi
        }
    
    

    Thus, to simply move to a new position, my robot has to:

  • turn to face the 0 or 180 (this help me adjust the robot's heading in the next step)
  • calculate the distant and angle between both positions
  • move to the new position the approximate distant

    For tracking and firing robots, the most challenging issue is to keep the gun tracking the enemy (and not turning away). For my tracking01-bot, it will follow a new enemy when it collide with a new enemy. After digesting throught some sample robots, I used the approach from TrackFire-bot to track the enemy. This approach work perfectly with all of my tracking and firing robots.

    However, I could not follow the specification for Tracking03 and Firing02. For Tracking03, I could not define "the closest enemy". I assume that the closest enemy for each turn should be the closest one in the radar angle. For Firing02, the robot sit still but the gun will follow the chosen target and keep firing until the target die. I first tried with the gun also sit still but it will take too long waiting for the target to come back again (which probably not in most cases).

    Beside the technical issue, I also had problem with the unclear specification of each movement. For example, the turn left angle for Movement03, the started point in Movement06, and rotating gun in Firing02.

    After spent most of the long weekends with this robocode assignment, I've learnt that to build a competitive robot, the following factors are very important:

  • smart fire: use the bullet power proportionally to the distant of the enermy
  • try to kill the enermy fast in each turn
  • destroy the most threaten enermy first

    I've also got some ideas from the sample robots TrackFire and RamFire which can put together to build a competitive robot.

  • No comments:

    Post a Comment