Souce code – Documentation
For this assignment I had to write some basic rode which would interface with the Robocode battle interface. There were thirteen robots for this assignment which focused upon mastering the movement control for the robot’s core functions. More of the actual details of this assignment can be read here.
Through this assignment I learned much about the event driven object orient aspect of Java. At the Unversity of Hawaii’s beginning and intermediate Java course, there did not exist any assignments for which the students must program an object to interface with an already completed Java application. I had previously made a game before using the Java swing package to create a graphical user interface, but at the time I did not fully understand Java’s event driven model.
The Robot class inside the robocode package allows programmers to create a basic robot ready to compete within the robocode battlefield. Since robocode triggers every event or turn for the robot, all the programmer needs to override are those basic methods. For the most basic robot, only the ‘run’ method need be overrided. This basic run method implements the robot’s movement in accordance to its environment and other factors such as health or heading. On top of the run method, events such as “onScannedRobot” enables the robot to response to its environment such as hitting a wall or identifying an enemy.
On tricky portion of the robocode event model is that events are processed alongside of the run method. As such, the robot may still be moving while calculating the angle to the nearest enemy. This is where the problem arises. How are the events able to do calculations without interference from the run method. Thankfully the robocode development team had the insight to create a ‘stop’ and ‘resume’ method to handle this very case!
Previously, I had not much experience with Java’s double type. Since all the angles and coordinates are stored using double, there was a little problem with directly comparing two values. Since doubles hold decimal place numbers, a number such as 400.99969 is not equal to 400.99968. Direct comparison between these numbers would result in a false. In order to mitigate this problem, the subtraction results of the latter number must be compared to a threshold number such that 400.99969 – 400.99968 < 0.10.
This problem with double type appeared in Position06 where the robot must rotate in a circle of 100 pixels. If the threshold number was too high, the robot would not move since the coordinates around the starting point would fulfill the previously mentioned subtraction condition. In the end, I used a threshold of 0.01 to find the initial point.
Lastly, robocode tested my knowledge upon the application of basic trigonometry and graph theory. Postion04 of the assignment required the robot to move to the center of the battle field, spin around in one circle, then stop. Basically, the movement to the center of the batte field was the distance formula of sqrt( (x1 -x2)^2 + (y1- y2)^2 ). For me I had a problem with facing the robot to the center of the battlefield, but in the end I calculated the angle with respect the robot’s coordinates and center coordinates using a right triangle where the robot’s heading was the offset.
In hindsight my competitive robot would probably run close to the edge of the screen to maximize the distance between myself and the opponent. I would have to decrease the probability of tracking the robot’s movements by adding some kind of random movement. By staying close to the edge of the screen, the robot does not need to scan behind him. Thus, the focus will be on the center of the battle field.