Tuesday, September 29, 2009

Ant Build Tool and QA Tools

I recently was given the task of learning and using the Ant Build tool and Quality Assurance (QA) tools. The Ant Build Tool is most useful when it is used with Ivy. Ivy imports a lib directory and is used in xml files to help The Ant Build tool is a great tool for working with big java group projects since it can automate the compilation and testing of java applications. This greatly saves time for the programmers since the compilation and testing could all be done from one command. Another great thing about the Ant Build tool is that if the build.xml file is setup properly, it could download and install all of the necessary applications for a project onto each member's computer. A good example is the build.xml file from the package that I used in my ICS 413 class. The build.xml file from that package downloaded all QA tools that I didn't have on my computer and downloaded it for me.

The QA tools that can be run using Ant+Ivy like checkstyle, findbugs, and pmd. Each of these programs check the java code for small errors like unused variables, inefficient code, and for correct documentation. Each of these small QA tools can be called in an Ant build file. This enables projects with large groups to all follow the same coding standards. This also allows everyone working with the project to be able to reach each others code easier since everyone needs to follow the same style of coding.

When I ran checkstyle, findbugs and pmd, I originally had no errors in pmd and findbugs. But I had a lot of checkstyle errors. I fixed all of the errors and ran findbugs and lost the checkstyle folder. So I am unable to paste any of those errors onto this blog.

The greatest part about Ant is that can be used on any type of platform. So the same build file could be run on a Mac as on a Windows machine.

CounterBot had some weaknesses in its design as it would always go the right wall first and go up and down that wall. Since my robot depends on taking a wall early, I changed my robot to go to the nearest wall, either the left or right wall and start moving up and down that wall. I also made my robot shoot the most powerful bullet when at a farther range. This allowed my robot to become a little stronger at close combat.

My updated CounterBot can be found here.
My distribution file can be found here.

Monday, September 21, 2009

Counter Robot

Trying to create a robot that is able to counter and beat a variety of other robots is a challenging task. I will try to create a robot that can beat Walls, RamFire, SpinBot, Crazy, Fire, Corners, Tracker, SittingDuck. My strategy for defeating each of these different robots is as follows:

Movement:
Robot will move up and down a wall, but if robot runs into an enemy robot, the robot turns and begins to move left and right across the map. This will help the robot evade attacks. The robot will also move the same way if it gets shot by another robot. The basic strategy for movement is to keep moving and not stay in one place.

Tracking:
Doesn't really utalize tracking capabilities. This robot will only pick up on enemy robots if the enemy is in front of the robot.

Firing:
Fires upon sight of the enemy.

Since Walls is one of the toughest robots to defeat, I decided to chose this design because it will defeat the Walls robot on average 80% of the time. After implementing this robot, I was able to defeat each of the robots on a consistant basis except the Tracking robot. I defeated all the other robots about 60% of the time, but I am 50-50 against the Tracking robot. I had a tough time against the Tracking robot because of it's aggressive nature and exploits the weak spots in my robot.

I have learned a lot about Robocode through this experience. It is very hard to get robots to do things by just using the basic Robot package. This restricts the robot's movements a lot. The robot only has a choice to do one command at any point. This meant that the robot was either evading or attacking and could not do both at the same time. If I were able to do this, I would have been able to build a much more balanced offensive and defensive robot.

My code can be downloaded here.

Tuesday, September 15, 2009

Goods and bad of sample robots

In this blog I review the different sample robots that are given in robocode. I will be judging each robot by the following criteria:
  1. Movement: How does the robot move? Does it have an avoidance or following strategy?
  2. Targeting: How does it find a target to fire?
  3. Firing: What is its criteria for firing?
For the Firing assessment, assume that all firing is called in the onScannedRobot() method call.

Walls
Movement: The walls robot moves exactly as the name implies: around the walls of the map. At the start of a game, the robot turns to a side of the and heads straight for it. Then turns parallel to the wall and moves in a clockwise fashion around the map. Before turning around the corner of the map, Walls checks if there is an enemy robot along that wall, and if there is, Walls fires at that robot until the robot is either not on the wall or dead. If Walls runs into another robot, Walls either backs up or moves ahead 100 pixels, depending on where the enemy robot it bumped into is. Walls then turns 90 degrees right moves across the screen. Thus, this robot impeliments an avoiding strategy since it is always trying to move away from an enemy.

Targeting: Walls finds a target around the map when an enemy passes in front of Walls and is picked up by the scanner. This robot does not lock on to any robot.

Firing:Fires on sight. As soon as Wall's radar identifies an enemy robot, it fires its weapon. It always fires a bullet with firepower 2.

RamFire
Movement: RamFire spins around in a circle until it finds and enemy. This robot utilizes a following strategy. When RamFire finds an enemy, it stops spinning, calculates the distance between it and the enemy and charges at the enemy full speed. If it gets to its destination without ramming into an enemy, RamFire starts spinning again until it finds another enemy and the process starts over again. If RamFire runs into another robot, it backs up and fires its weapon.

Targeting: This robot spins around 360 degrees until it picks up an enemy robot.

Firing: The amount of firepower that RamFire uses is proportional to the amount of energy the enemy robot has left. RamFire doesn't want to kill the enemy robot since it primarily wants to ram the other robot for extra bonus points. So the more energy the other robot has left the stronger the firepower up to firepower 3. The weakest bullet that RamFire uses is 0.1. Unlike other robots, this calls the fire() method in the onHitRobot() method instead of in onScannedRobot().

SpinBot
Movement: SpinBot moves around in a circular pattern throughout the entire game. SpinBot will turn if it runs into a wall. If SpinBot runs into another robot, SpinBot will fire upon that other robot if the enemy is in front of it, otherwise it will just turn right 10 degrees. This means that SpinBot uses an avoidance strategy, since it is always moving around.

Targeting: SpinBot's gun and radar never move around to track anything. SpinBot just scans the map as it moves around in its circular pattern.

Firing: Always fires a bullet with firepower 3 whenever it scans an enemy robot.

Crazy
Movement: Crazy moves around by turning and moving at the same time. This is accomplished by using the AdvancedRobot package and using the waitfor() method. The waitfor() method waits until the robot completes a turn while still allowing the robot to move forward. Thus, the method seems to allow the robot to move and turn at the same time. The robot keeps moving forward until it hits a wall or enemy robot and then moves in the opposite direction.

Targeting: The gun and radar do not move. So like previous robots explained, Crazy has no tracking system and just waits for a robot to be in front of it.

Firing: Fires when the robot scans an enemy robot in front of it with firepower 1.

Fire
Movement: Fire only moves after it got hit. When Fire gets hit, it turns at an angle relative to the angle that it was shot from and then moves 50 pixels forward. Fire only shows signs of evasive maneuvers after it gets shot.

Targeting: Fire targets other robots by spinning its gun around 360 degrees. After finding the first target it sees, Fire locks onto that target. This is the first robot that has tracking capabilities. Once it finds its target, Fire locks onto the target and keeps firing bullets until Fire recieves damage and then tries to use some evasive maneuvers.

Firing: After finding a target, Fire fires its strongest bullet if the enemy robot is within 50 pixels and if Fire has more than 50 energy left. Otherwise, Fire only fires a power level 1 bullet.
Sitting Duck
Movement: This robot has no movement, it just sits there and outputs some information on the console.

Targeting: None

Firing: None

Corners
Movement: When a match begins, Corners does exactly what the name implies and goes to a
corner that is specified in the code. Corners gets to a corner by first turning to one wall, moving
toward that one, then turning perpendicular to that wall until it gets into the corner of the map.

Targeting: After getting into a corner, the robot's gun turns its gun 360 degrees until it finds an enemy then tracks and fires at it.

Firing: Depending on its distance from an enemy, the firepower of the bullet changes. The closer the enemy the stronger the bullet.

Tracker
Movement: Tracker moves toward an enemy until the enemy is within 150 pixels of it. If the enemy is closer than 100 pixels away, then tracker moves back.

Targeting: Tracker initially spins its gun around and tracks the first enemy it finds. Tracker targets a single enemy until the enemy either dies or Tracker cannot find the enemy for 10 turns. If Tracker runs into another robot while moving, Tracker will change the robot it is tracking to the robot that it just bumped into and fire.

Firing: Tracker fires at the tracked enemy if the enemy is within 150 pixels with firepower 3.

Sunday, September 13, 2009

Java Coding Standards

In all the java coding that I have read so far in my short career, I have seen some very complex java code that was very hard to understand. This was due to the fact that the code wasn't well documented, which made it very hard to follow. This made me waste a lot of time just trying to figure out exactly what certain methods and classes did within the program. This has taught me that proper documentation saves the person reading code a lot of time.

Not only will following proper coding standards reduce the reading and understanding time of another person's code, but it will also increase it's reuse ability because of it. Many times, java programmers finish a project and move onto other things, then other programmers may want to improve on the code. In these cases, having the proper java coding standards are most important. Since it saves the programmers that take over and improve the code a lot of time understanding exactly how the code works.

To help myself have better coding standards, I have read a book called The Elements of Java Style. I also follow the java coding standards of my software engineering class found here. After reading those coding standards, I started fixing my code from my previous robocode assignment. I, like many programmers, did not follow all of the coding standards. I had to fix a lot of my documentation because I didn't explain why a method was there but rather what the method does. This my biggest problem because I realized that I have always been breaking coding standard.

While fixing my code to conform to the java coding standards, I also had a chance to fix up some of my codes that were not able to work properly by looking at how some other people in my class did it. I ended up adopting code from Kimberly Heu for my Tracking03 robot. Her code was very easy to understand since she docuemented it well, and her robot worked perfect also.

My new updated code can be found here.

Tuesday, September 8, 2009

Robocode fun!

For my ics 413 class, I am lucky enough to have Robocode programming as an assignment. Robocode is an open source program, written in Java, that allows a programmer to program a virtual robot to fight against other robots. At the very end of this assignment, the class will have a competition to see who will have the best combat robot. This is something that I think that I will enjoy working on, since I am a very competitive person.

The installation of Robocode went pretty smoothly. All I had to do was download the program from http://robocode.sourceforge.net/ and run the .jar file. It was very simple and only took a few minutes. However, getting the Robocode to work with eclipse was a little difficult to do. Luckily, there is an online step-by-step walkthrough of how to run Robocode from Eclipse and import your robot into Robocode to get the robot to run. I followed the instructions from this wiki.


To help us get acclimated with robocode, the class was assigned to create 12 different robots. The 12 different robots are :
  • Movement01: The minimal robot. Does absolutely nothing at all.
  • Movement02: Move forward a total of 50 pixels per turn. If you hit a wall, reverse direction.
  • Movement03: Each turn, move forward a total of N pixels per turn, then turn left. N is initialized to 10, and increases by 10 per turn.
  • Movement04: Move to the center of the playing field and stop.
  • Movement05: Move to the upper left corner. Then move to the lower right corner. Then move to the upper right corner. Then move to the lower left corner.
  • Movement06: Move to the center, then move in a circle, ending up where you started.
  • Tracking01: Pick one enemy and follow them.
  • Tracking02: Pick one enemy and follow them, but stop if your robot gets within 20 pixels of them.
  • Tracking03: Each turn, Find the closest enemy, and move in the opposite direction by 100 pixels, then stop.
  • Firing01: Sit still. Rotate gun. When it is pointing at an enemy, fire.
  • Firing02: Sit still. Pick one enemy. Only fire your gun when it is pointing at the chosen enemy.
  • Firing03: 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).
  • Firing04: 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).

After reading the methods from the robocode api, I thought I had a good idea of how to make my robot move around, but I realized that the movements were a lot trickier. Being able to move from one point to another precisely was very tricky since it required some geometry. A friend later pointed me in the to a website that contained a method that pointed the robot in the correct direction. I used the absoluteBearing method from the website. This helped my movements out greatly.

After figuring out the point to point movement of the robot, Movement04-Movement05 were relatively easy. But I ran into a few problems while on Movement06. I wasn't able to get a movement where a robot would move in a circle and stop when it returns to its starting point. I got the robot to move around in the circle, but the robot wouldn't stop when it return to the starting point.

The tracking robots were a bit tough to get working at first, but after reading the sample code and using a small portion of code to get the robot to lock onto a target, the tracking wasn't too bad. The firing robots were not very hard to get working. The only problem that I ran into was also getting the robot to track, but after I figured out the tracking, I was able to get the robot to track a single bot.

Samples of my code can be found here.

I learned that robots are really hard to program and need to be told to do almost every single action down to the smallest action. There is also a huge amount of math and physics that go into getting the robot to perform certain actions such as targeting and moving to the correct direction.