Over the past week or so, I've been creating my own competitive Robocode robot for an upcoming competition against other software development students. My previous blog posts have touched upon the basics of Robocode and build systems using Apache Ant, and the competitive robot is supposed to be a culmination of everything I've been learning about so far.
The bad news? I'm competing against grad students (shudders) as well as other undergrads, so my poor robot stands little chance of coming out of the competition in one piece. Especially if anyone adopts a strategy similar to the sample robot simply known as "Walls" (I never knew I could dislike a virtual robot as much as this one), who I am yet to beat with my robot that I've named "BattleBot."
With my robot, the first thing I tried to do was think about movement. I figured that sideways movement, or strafing, would be best for dodging enemy bullets. Thus, I programmed my robot to square off perpendicular to the other robot the entire time. Rather than moving each turn on its own, my robot only moves when it detects an "energy drop" (the enemy's energy level decreased from the previous turn), which allows it to dodge enemy bullets. The idea is that firing a bullet depletes a robot's energy, so we can use that information to guess that the enemy has fired a bullet.
In order to target the enemy robot, I simply built upon the movement strategy which I had already implemented. Because my robot is always squared off sideways to the enemy, I just turn the gun 90 degrees at the start, so that it's pointed at the enemy, and then lock the gun so it turns with the robot as it adjusts its position. That way the gun is always pointing at the enemy. This strategy works well against robots that don't use a lot of random movements. I tried to improve my strategy by using linear targeting to shoot moving targets, but my implementation was not accurate enough so I ultimately did not use the code.
Because my robot's gun is always pointed at the enemy, I decided to fire every turn if possible. My robot uses bullet power proportional to it's distance from the enemy in order to conserve energy (the farther the enemy, the less chance my robot has of hitting it). Also, my robot does not fire if its energy is below a certain level so that it doesn't run out of energy.
Of the sample robots included with Robocode, my robot can reliably beat all of them except for Walls. Against Crazy and SpinBot, my robot wins about eighty percent of the time (which I consider to be pretty reliable), and always beats RamFire, Fire, Corners, Tracker and SittingDuck. I think the reason my robot struggled against Walls was because it is always moving and my robot struggles with moving targets. The linear targeting strategy which I implemented improved my results against Walls but actually was worse against the more simple robots, so I eventually ditched it altogether.
In order to test my robot, I implemented six tests: two acceptance tests, one unit test, and three behavioral tests. The acceptance tests were used to verify that my robot could reliably beat some sample robots. This made the development process faster and easier since I could quickly test changes to my robot's behavior to see if they improved its performance against other robots. The unit test verified the output of one of the methods that my robot calls when it fires a bullet to ensure that it uses proportional power based on enemy distance.
The behavioral tests made sure that my robot behaved like I intended. For example, one behavioral test tested that the robot had moved when it detected an energy drop. Another tested the bullet power to verify that proportional power was being used in firing, and the last one made sure that my robot did not continually run into walls.
Overall, developing this competitive robot has really taught me a lot. I improved my skills in Java and Ant, became comfortable with build systems, wrote some tests for the first time, and become more familiar with the Eclipse IDE, among other things. This was a valuable learning experience, and I can't wait to see to what implementations my classmates have come up with for the Robocode competition.