Monday, September 19, 2011

Kode Cut a Row Bow Code

     Who is Kode? Why did he cut a "row bow code"? What is this about? Say it slow: Kode-cut-a-row-bow-code. Now say it fast: Kodecutarobocode. Keep saying this phrase with practice and repetition then you'll get Code Kata: Robocode.
     For those who may know what this is about, did you see the analogy there? Well, basically what we did in this introduction exercise was to practice and repeat doing a task. In programming, a Code Kata is referred to an exercise which helps a programmer build their skills through practice and repetition. This is the first part of the topic. So this leaves Robocode as the second part.
     In short words, Robocode is "Robot Code". Robots programmed in Java compete with others through movement, radar and attack. There are many ways to design a robot, but how do you design the "best" robot? Well, first you need to… STOP! Before building the "best" robot, we need to start off small and deal with the basics first: code kata.

     The following list are simple robot exercises created to test out the basic robot functionalities:
  • 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).

     Now let's create them robots! Ready, set, GO!


     Okay, thanks for waiting while I was creating these robots. Some were easy to implement, while others took a lot more thinking to get it to run properly. Let me go over the robots I created in the three groups:

     Position
          This first group of robots practices with moving the robot. Position 01, 02 and 03 were straightforward using turns and moving the robot ahead. Then I got to writing out position 04. This robot could be implemented in… I would say 2 ways. The first is the simple method of moving the robot to the same X or Y coordinate as the center point then traversing the remaining axis to the center. I created this robot first but wanted to be adventurous and try the second method which involves a little trigonometry. a2 + b2 = c2? Right. This method calculates the distance to the center, rotates the robot to face the center, then moves it to the center. I drew it all on paper before attempting this method and it went well. The robots correctly moved to the center in every test. Here are some of the drawings I did for this algorithm:


These drawings may look confusing, but I totally understand it and it works. Trust me, it does.

          Now to position 05. This method builds off of position 04 to move to the corners instead of the center. Finally, position 06 is basically position 04 plus a circle. To create the circle, I figured that since the robot will need to travel the circumference and there are 360 degrees in a circle, the robot will need to travel (circumference / 360) and turn 1 degree at every turn. The outcome was that the robot did move in a circle with the given radius, but it was very slow because the robot moved forward and it turned after moving instead of doing these two methods simultaneously.

     Follow
          These robots introduced the radar functions of the robot. All three were very similar in design which involves using the radar and moving when an enemy is detected. For the first two follow 01 and 02, we needed to only had to follow one enemy, and to do that you just scan the robot and get its name. If the name of the enemy scanned is the same as the previous enemy, then move, otherwise continue scanning.

     Boom
          The final set of robots brings in the gun of the robot. The first two robots Boom 01 and 02 were the same and differed by one thing. Boom 02 only fired at the same enemy. With these two robots, you simply rotate the guns and when the radar detects an enemy, you fire the robot's gun. Boom 03 builds off Boom 01. Instead of firing the guns at the same power, we get the distance of the enemy and fire the guns at a power according to the distance the enemy is away. Finally Boom 04. This is the only robot in this group that doesn't actually boom (Fire its gun). The algorithm here is to track the same enemy as in Boom 02, but rotate the robot's gun to always point at the enemy.

     Overall by completing these code katas, I learned more about the robocode class. I guess if you practice robocode by doing these code katas you will learn more about the rodocode API, don't you agree? So how about getting to designing the "best" robot? Let's not get into too much detail, but for me, a "good" robot has a great combination of movement, radar and attack. A "good" robot should know when to move and when to stay in position. It should also have a good defense system that detects all obstacles and treats and decide how to overcome them. Lastly, the robot should also have good offense. It should know when to attack and decide if it will hit an enemy instead of missing all the time. That's what I think the "best" robot is, however, there is an advanceRobot class in the robocode API. This allows for more advanced control over the robot, but for this topic, let's just stick to the basics and create something advance with the tools we have.
     Generally these code katas make you think in different ways to solve a problem. Sometimes you can approach a problem in a simple matter whereas others can be more complex, just like how I approached the position 04 robot. There are many ways to do one thing and there is no wrong way to do it if it works correctly. It like every day life. Practice makes perfect… Well nothing is perfect, so let's rephrase, practice makes better! So what do you think of robocode now? Cool or cool??? Try it out, take on these code katas, practice, and you could possibly design the best and destroy the rest! Good luck to you!

Links:
Robocode Home: http://robocode.sourceforge.net/
Robocode API: http://robocode.sourceforge.net/docs/robocode/
RoboWiki: http://robowiki.net/wiki/Main_Page

No comments:

Post a Comment