Tuesday, September 8, 2009

Robots are a Programmer's Best Friend

I recently downloaded a program called Robocode which allows a user to create the AI for software robots and have those robots fight against other robots. Having spent a fair bit of time this past week constructing simple robots, I am confident in my ability to start building a more complex one.

Before I talk about my experiences I should first explain a little about the battlefield and the robots. Each battle in robocode consists of a given number of robots in a 2d board. The view is top down and each robot has a gun and a certain number of life points. The robots battle until only one robot is remaining. Points are awarded not only on survivability but also damage dealt, and other factors. There are robocode leagues in which people create robots that need to satisfy certain requirements. For example in a Melee league no robots may fire guns and can only do damage by ramming other bots. There are also leagues based on the size of one's code so elegant bots written in efficient code don't challenge bots with complex algorithms.

Reach your dreams vicariously by destroying tiny robots

For this assignment we were instructed to create 13 simple robots that satisfied certain requirements. Six of them had movement requirements, three had tracking requirements and the other four were concerned with the process of firing the gun.

I believe I completed all of the simple robots correctly, but I may have misinterpreted some of the steps. For example I don't know the exact definition of a turn but I was too lazy to ask for clarification. For example I was not sure if Tracking03 was meant to move back 100 steps each round and never move again or if it was supposed to scan for the closest bot, move 100 steps away from it and repeat... In any case I just did what I interpreted as a correct implementation.

For the movement robots I completed the first three pretty quickly, but I had trouble on the Movement04 robot which needed to head towards the center of the map. I ended up writing on lots of scrap paper in order to do the trigonometric calculations. I did separate test cases based upon what quadrant the robot is in at the start relative to the center. For example if the robot starts above and to the left of the center then different calculations are needed compared to if the robot starts below and to the right of the center. I discovered my first problem was that the arc tangent function in Java returned the angle in radians while robocode functions return values in degrees. I also had some issues because robocode functions such as getBearing() return the bearing relative to the robots heading while other values like getHeading() return values in absolute terms (0 to 360 degrees). In addition the orientation for robocode is that 0 is facing north, 90 degrees is east, etc... which is different from the unit circle. Fortunately I am pretty good at Trigonometry and as a math major I was able to get a solution. I wrote a function which gets the robot to any x,y point and then I was able to do Movement05 and Movement06 easily.

The second issue I had with this assignment was getting Tracking03 to work. At first I thought I did it correctly by simply spinning the radar and moving away from the first robot it found. However I realized that this doesn't move the robot away from the closest robot. To identify the closest robot I read a tutorial from http://www.codepoet.org/~markw/weber/java/robocode/lesson3.html which suggested that one implement an enemy bot class to keep track of the distances of each bot. It also had a link to a lab assignment on implementing this class. So basically I did the lab assignment for some other class and came up with an enemy class that seemed to work. The robot scans for the nearest robot, turns toward that robot, then backs away. It then searches for the next closest robot and backs away.

While I did complete the assignments, I know I didn't do them in the most optimal way. For example although Firing04 does track a robot with its gun, it doesn't have a 100% lock on the target. I implemented it in a simple narrow lock method while I read about much better and more sophisticated locks. Also I didn't make the tracking turns efficiently, instead I just had the robot/gun/radar turn left or right until the condition was satisfied. I read about more efficient methods but I didn't have a chance to implement them because of all the grading I had to do this weekend.

After completing the simple robots, I tried to implement a robot with predictive targeting. I followed the tutorial from

http://www.codepoet.org/~markw/weber/java/robocode/lesson4.html

and implemented an AdvancedEnemyRobot but I was unable to get a robot working. I kept getting null pointer exceptions so I decided to delete it and start again when I have more time.

I learned from this assignment that there's a lot of trigonometry in robocode and even a good Java programmer may have trouble if she doesn't understand the usage of sin,cos, and tan functions. In fact I think that my math and statistical background will help me in developing algorithms in the future. Combine that with my experience of programming the AI in a connect four Java game and I should have a leg up on programmers without the math experience.

For a competitive robot I want to implement predictive targeting so I can beat a robot like walls. I'm not sure about movement but I like the idea of moving perpendicular to an enemy in a 1-1 setting. I also want to optimize my radar movement and implement a radar that locks onto an enemy bot and better than the narrow radar that I implemented.

To download the sample robots in java click here

Sunday, August 30, 2009

FizzBuzz: A Programmer's Beverage of Choice

I timed an implementation of a simple FizzBuzz Program and it took me about 9 minutes to get it done correctly from the moment I started Eclipse.

public class myFizzBuzz {

public static void main(String[] args) {
for (int i=1;i<101; i++) {
System.out.println(getFizzBuzz(i));
}
}

static String getFizzBuzz(int i) {
if (i%3==0 && i%5==0) return "FizzBuzz";
else if (i%3==0) return "Fizz";
else if (i%5==0) return "Buzz";
else return String.valueOf(i);
}

}


Since we already did this exercise in class on the second day, I feel that it was much easier to code FizzBuzz quickly. The only error that puzzled me was returning the string form of i in the getFizzBuzz method. I remember the function to convert an int to a string was valueof but I wrote valueof in all lowercase instead of valueOf. Fortunately Eclipse found my error and suggested the function valueOf.

The other problem I encountered with Eclipse was not in writing the code but rather testing it using JUnit. I've only had minimal experience with Eclipse in ICS313 before this class, so I wasn't familar with the process of using JUnit. When I first learned Java I didn't use an IDE and I mostly used try/catch statements to check for errors. When I started with the JUnit I had problems running it because the Windows Firewall and Norton AntiVirus blocked the program from running. It took at least 15 minutes in class until I realized the error. I didn't realize the error until I quit Eclipse and found the firewall popup asking me whether or not to unblock the program.

I learned that even simple programs like FizzBuzz can be tricky when you're not familiar with the programming language and the developer environment. 16 lines of code in roughly 10 minutes is not a very good time especially since I already did this code in class before. In fact I read the FizzBuzz article on how programmers can't program this past summer before school started.

This reminds me of the time I started learning how to use Microsoft Visual Studio 2008 with Wes Peterson last year. I was taking his Data Security class but my laptop just broke so I was using my office computer. I decided to learn how to use Visual Studio because I wasn't allowed to download additional software to the office computer and visual studio was available. Before this I had only programmed in C on a Linux operating system with kate and gedit text editors and a gcc compiler.

In the beginning of the semester I asked Peterson for help with programming but he also was unfamilar with visual studio. Luckily his office was right across from mine and he was curious about how it worked as well. It took us 1/2 an hour to program hello world in C in visual studio just because we didn't know how each button worked. Considering that Wes Peterson was an amazingly talented guy, I realize that even talented programmers will take time to learn different environments even when they are skilled with the language. I approach this class with the expectation that I will probably struggle more than my classmates with these programming assignments because I was a math major as an undergraduate. Nevertheless, I'm a little more comfortable knowing that there's a lot of beginning programmers who struggle with creating simple programs like FizzBuzz.

Becoming the King of America: The FreeCol Experience

ICS 613 Blog Post 1, Assignment 02: OSS Experience

I’m a big fan of computer/video games and when I saw the FreeCol java game engine on SourceForge I decided to download it for this first assignment. I first became interested in programming because I met a friend in college who enjoyed programming computer games in his free time.

My friend John spent 40 hours a week programming video games in C++ for all four years I've known him at my undergraduate program in Iowa. On Friday nights when everyone else was getting drunk at some party, John would be working on his newest project. We would frequently download games to play and I used to give him feedback on the games he made. After we graduated from Grinnell, John started working for thatgamecompany which made games like Flower and Flow.

Getting back to the assignment, I downloaded FreeCol and found it pretty straightforward to use. It comes with a shortcut icon on my desktop and was easy to install.

Overview

FreeCol is the 11th most popular download from SourceForge as of August 29th, 2009. It's an open source version of the game Colonization. One can download the game from sourceforge at http://sourceforge.net/projects/freecol/.

I downloaded an executable file: freecol-0.8.4-installer.exe from http://sourceforge.net/projects/freecol/files/freecol/freecol-0.8.4/freecol-0.8.4-installer.exe/download

and a source code file freecol-0.8.4-src.zip from: http://sourceforge.net/projects/freecol/files/freecol/freecol-0.8.4/freecol-0.8.4-src.zip/download

On sourceforge there is a link to the official FreeCol website, http://www.freecol.org/, which contains additional information on the game including screenshots, news updates and a community forum. While I was unable to locate a stated objective or mission statement of the game on the website, I did find a blurb describing the gameplay:

FreeCol is a turn-based strategy game based on the old game Colonization, and similar to Civilization. The objective of the game is to create an independent nation. You start with only a few colonists defying the stormy seas in their search for new land. Will you guide them on the Colonization of a New World?


Three Prime Directives

Prime Directive 1

1. The system successfully accomplishes a useful task

The terms “useful task” and “computer games” are not often used in the same sentence. My mom would argue that most software games are not useful and instead they make her son lazy and obnoxious to be around. I prefer to view computer games as a source of entertainment and pleasure for a user. Since the word “useful” depends on the context of the situation, I chose to measure the usefullness of FreeCol in terms of the quality of the gaming experience. In order to determine whether this software satisfies the first prime directive I need to evaluate whether or not this game is fun to play. Since I have played many open source games, I based my evaluation of FreeCol relative to my experiences with similar types of games.

Who does this game appeal too?

FreeCol is an open source version of the game “Colonization” which I’ve never played before. It is very similar to a popular proprietary software game called Civilization. You choose a European Nation from the Dutch, English, French, Spanish, Portuguese, etc …. and attempt to develop a colony in the new world known as “America.” Each nation has a particular advantage such as the Dutch have a “trading advantage” the Spanish have a “conquest advantage.” Although the game rules and controls are well documented in a 94 page pdf, I found the gameplay confusing at first because of the many different controls. This game would appeal to people who know how to play the original Colonization and for those who enjoy time consuming, complex strategy games. This game would not appeal to people who like simple games like tic-tac-toe.

Keeping track of resource prices is one of the complicated features of FreeCol

What are some strengths/problems with the gameplay?

Although the gameplay was smooth, one bug I discovered was that the game would sometimes freeze during the turn of one of the AI European nations or Indian Tribes. If I saved the game and reloaded it from that point the problem would be resolved. This happened to me three times after playing for roughly 5 hours. Another missing feature in the game was the lack of a game tutorial on strategy. I read about the various options in the game but I had no clue as to which of the hundreds of different tasks I should perform to help me win.

One of the strengths of the gameplay in FreeCol is the many different ways to proceed through the game and the incredible functionality of the system. The user can create a new game, load a saved game, create your own map terrain, adjust gameplay settings and controls, etc… and that’s only in the first menu. A user can choose to play a game on a hosted server against other users or play against AI opponents with 5 different difficulty settings. A user can choose the type of map and number of players. When the actual game starts a player controls a certain number of units, each having different functionality. Ships can move around the seas, while land units can establish colonies on the new world and perform different tasks such as farming, building, exploring, etc…

FreeCol world map image taken from www.freecol.com

The gameplay is centered of buying, trading and selling goods with Indian tribes and other European nations to make money and establish prosperity in the new world. There are other ways to succeed in the game such as building up military units and taking over other towns by force. You win the game by defeating all other European Nations or buy getting independence in the New World. There are so many different options in the game that covering them all would make this blog even longer than the 94 page instruction manual. These features help to simulate the European Colonization experience in America from 1400-1800. Dealing with taxation from the Home nation, managing resources for trade and creating armed forces to protect against or attack other groups help to make the game more realistic. These as well as many others are the useful functionality of the game

Overall I say that FreeCol does accomplish a useful task because it does provide entertainment to the user. The useful functionality of the game is related to the complex strategy and gameplay.

Prime Directive 2

2. An external user can successfully install and use the system

A user doesn’t need to know anything about programming or Java to successfully install and use the system. A user also doesn’t need to use any Java compiler or IDE to run the software. In contrast I’ve seen many projects that require a user to run the Java code in order to use the program. The download of FreeCol from sourceforge or the FreeCol website come in several different formats including executable only files, source code files and different zipped formats. If you click on the giant “download the game” button you download the executable form which gives the option of creating a shortcut on your desktop or in the start menu for the windows operating system. You can simply double click the icon and start playing.

The game interface was straightforward and easy to use although the game objective and gameplay strategy were quite complex. Documentation of the system is one of the strengths of FreeCol. In addition to the lengthy instruction manual, the game has a built in “Colopedia” which is a wiki on all the different units and featurs.

Colopedia provides in game information on every resource in FreeCol

A user can look up a particular type of good, unit, terrain feature or almost anything else from an easily accessible in game menu. I only played against the AI so I don’t know about servers and handling multiple players, but the single player mode didn’t have any lag. The units could be moved by clicking on the mouse or by using the arrow keys on the keyboard. The system also had intuitive shortcuts such as pressing “b” to build a colony on the main map.

Prime Directive 3

3. An external developer can successfully understand and enhance the system


At first I was confused because I couldn’t find the sourcecode for freecol. Then I realized that I downloaded the executable file so I went back and downloaded freecol-0.8.4-src. The documentation that came with the download from sourceforge redirected me to the official FreeCol website http://www.freecol.org/documentation/ which contains user documentation, developer documentation, and code documentation. The code documentation portion contains the javadoc which is certainly useful in keeping track of all the classes. Due to the large size of the project I didn’t have a chance to peruse all of the documentation, but the naming conventions and the functionality of the code were pretty straightforward.

Screenshot of the sourcecode for your viewing pleasure

The development documentation contained a Subversion Repository Layout which describes the types of files within each directory. For example the “graphics” file has the description:

“The source files for the graphics are contained within this directory. We also keep certain rendered images as well - especially if the images are currently not in use.”

The development documentation also contains information on how to update a bug tracker item and information on how the developers of FreeCol use subversion to store the source code in a centralized location.

The documentation lacks visual diagrams of the relationships between classes, objects or packages. Some of it is explained in the javadoc and there is a diagram of development branches, but overall the visual documentation of the project is poor. To be fair, visually documenting the relationships in an object or class diagram would take a very long time for a project this size. It may not even be feasible since there are so many different objects for all the individual units and components. Nevertheless I think such documentation could have been done for major components of the world map, server classes, etc…

I’m not an experienced Java programmer so I don’t feel confident in my ability to enhance the FreeCol project. I feel that the project does satisfy prime directive 3 but not to the extent that it does for the previous two directives. The documentation is not set up for a novice programmer like me to add additional features. Nevertheless I feel that a program with a few years of experience could understand and enhance the system.