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.