Tuesday, September 20, 2011

r..r..r...Robocode!

Ever wonder what it takes to become an avid programmer? If you have just started programming or have even been doing it for some amount of time, this is one question that will often appear from time to time in the depths of your subconscious (or in my case, usually while I'm programming!). There is no doubt that a study of the formal techniques of programming (which you can find in any book) will have an important role to play in any programming career. However, the most vital task in becoming an avid programmer, I believe, is practice (a task,sadly, we often don't have much time to do with our busy schedules).Practice, indeed, does makes perfect. In fact, it is often the detail that distinguishes great programmers from the not so great, since programmers who practice understand the need to challenge themselves by taking on tasks that take them beyond their usual comfort level.

And to both my surprise and satisfaction, I was able to get a good amount of practice in using Robocode. Robocode is an open source programming game that was originally developed by IBM, which was later picked up by Flemming N. Larsen and Parvel Savara. The goal of Robocode is two folded; Sticking to their motto, the first, "build the best to destroy the rest", alludes to the ability to create, manipulate, and destroy Robots in a no holds bard, fight to the death, battlefield. The second, which is my favorite, is to enhance the users knowledge of Java since the game involves writing code in Java and having the created robot(s) battle one other by moving around, shooting at enemies, or just following enemies around on the battlefield. Specifically, I worked with implementing 13 katas, or simply, 13 robots that that helped me to see where I am as a Java programmer. At first I thought with my previous experience in Java ( and Java robots) this project would be very easy, but as I soon found out, it was not as simple as I thought. Take a gander at the 13 katas:

Position01: The minimal robot. Does absolutely nothing at all.
Position02: Move forward a total of 100 pixels per turn. When you hit a wall, reverse direction.
Position03: Each turn, move forward a total of N pixels per turn, then turn right. N is initialized to 15, and increases by 15 per turn.
Position04: Move to the center of the playing field, spin around in a circle, and stop.
Position05: Move to the upper right corner. Then move to the lower left corner. Then move to the upper left corner. Then move to the lower right corner.
Position06: Move to the center, then move in a circle with a radius of approximately 100 pixels, ending up where you started.
Follow01: Pick one enemy and follow them.
Follow02: Pick one enemy and follow them, but stop if your robot gets within 50 pixels of them.
Follow03: Each turn, Find the closest enemy, and move in the opposite direction by 100 pixels, then stop.
Boom01: Sit still. Rotate gun. When it is pointing at an enemy, fire.
Boom02: Sit still. Pick one enemy. Only fire your gun when it is pointing at the chosen enemy.
Boom03: 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).
Boom04: 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).

While I was able to successfully implement all 13, by no means was it an easy feat. In fact, I had the help of two of my fellow classmates, Marifel Barbasa and Hansen Cheng. In the implementations, the most difficult portions (and I mean mind-numbingly difficult!) were the Positions 04,05, and 06. I was completely taken off by the amount of trigonometry needed (or the amount of trigonometry that I had forgotten) in these implementations in order for the robots to function as intended. I must say, if it wasn't for the help of my classmates, Java's helpful (and sometimes helpless) Math class, and the, what I thought were endless, hours of drawing out angles and directions on paper, then I probably would have not have finished these particular katas.

After having finished them however, I still feel that there is so much more for me to learn (and yes, trig is on the list!). The katas were great insightful tools that let me know where some of my comfort levels are and what I need to do in order to push past them. While I do have an idea about how to build a "good" competitive robot, in respect to positioning in a way to make escape easy and constant targeting a must, it is far from "great". I will still have to iron out some of the kinks.

No comments:

Post a Comment