Iceberg Races

Posted on 2010-04-12 22:03 in Blog • Tagged with competition, programming

On Friday night (April 9th), I held my second programming competition at NDSU. This year’s turnout was much better than last year.

The Game

Iceberg Races is you’re run of the mill racing game. Players control a car which they try to navigate through the race to complete laps. When the game starts, the players are sent a copy of the map, and during each turn, they are provided location and velocity details for all the vehicles on the track. The competitor’s goal was to write an AI to find the best path through the map to win the race.

Iceberg Races Screenshot

Participants

This year 22 people participated, including students from NDSU, MSUM, and Concordia. I believe all of the participants were undergraduates. All competitors came in blind. The only details that were released about the game ahead of time were clients were being provided in both Java and Python.

Problems

I was in contact with the NDSU ACM which arranged all the logistics of the actual event. They reserved rooms, provided advertising, and setup the competition computers. I had told them I needed Python and Pygame installed on the server machine. Upon launching the game, it was immediately discovered that I needed Python 2.6 and the server had 2.5.7. We spent the better part of half an hour trying to get 2.6 to install (it was not in the repositories for the distro). We eventually gave up and my laptop became the game server.

There were problems distributing the client source files. I had packaged Python and Mercurial binary libraries for use with Windows machine development (which I neglected to remove after learning the competition was being held in a Linux computer lab), this pushed the size of the source zip file from 500 Kb to 54MB. SimpleHTTPServer (Python library server) immediately crashed handling the sudden load from the downloads.

Competition

After five hours of development, all coding work came to a stop and the competition bracket was drawn up and the fierce battles began. Several clients performed excellently, some drove randomly, and a few simple spun in place. Overall, everyone had a great time watching the matches.

Winners

Place or Achievement Winner’s Name Prize Notes
1st Nick Laney $75 Best buy card
2nd Kevin Schroeder $50 Best buy card
3rd Ben Bechtold $25 Best buy card
Most Code Tom Gardiner Box of 20 Candy Bars >400 Lines
Least Code Sam Sussman RC Car 8 lines
Crash test dummy
(most wall impacts)
Andrew Dahl Text Book 408
Speed demon
(most time at full speed)
Ben Bechtold Text Book 638

Source Code

Full source code for the server and clients, as well as all game artwork can be found in the mercurial repository hosted on Assembla.

hg pull http://hg.assembla.com/icedberg_races

I’ve also included the source for the first and second place clients (both part of the Java client).

Usage:

Clients:

java Main <name> <server IP> <server port> [ai, first, second]
python Main.py <name> <server IP> <server port>

Server:

python Main.py <port>

Acknowledgements

Person Reason
Nick Laney (NDSU ACM Chair) Event running advertising and reserving the room.
Bethany Schlenvogt Vehicle graphics
All participants For showing up, the competition would not have been successfully without you.

Continue reading

T-minus two days

Posted on 2010-04-07 19:48 in Blog • Tagged with programming, competition

Two short days until competition day. I've spent the last several months developing an idea for a programming competition game, implementing the game, documenting, and organizing participants/ sponsors. It all comes to a head on Friday (April 9th).

I just got word from the chair of the NDSU ACM (organization that I'm hosting the competition through) that we have 16 registered competitors from two different local colleges. Quite a step up from last year where only 8 competitors arrived. I'm beginning to worry that my game is not going to be complex enough for the people who arrive.

Last year, the number one complaint was the game was too complex. They said five hours was not enough time to learn how to play the game to build a functional AI. So, for this year, I drastically simplified the exposed surface area of the game (details to be announced after the competition).


Continue reading

Problems with Java'’s parseInt

Posted on 2010-04-01 21:41 in Blog • Tagged with java, programming

Background

I work in an industry that generates a lot of binary encode data. Primarily from the lab at my office, but occasionally we get data that was collected at a government test range. In order to understand the data, it goes through a process called, “Data Reduction”, which converts the binary/ hex encoded data (test data) into simple decimal numbers (engineering data) that can get used by excel. The data fields vary in size, from a byte up to a long, and it is usually unsigned data. A tool was written last spring (2009) with the help of a regional university to automate the data conversion process and it is currently being extended (2010).

Problem

The students discovered that negative numbers were not being parsed correctly. Sometimes giving an invalid value (byte: 0x80), other times they caused a NumberFormatException (int: 0xFFFFFFFF). I told them I would look into the problem. It quickly became apparent what the problem was; Integer.parseInt doesn’t under two’s complement values.

parseInt takes a hex encoded string and returns an int. In the case of negative numbers, parseInt looks for a negative sign at the beginning of the string. It does not examine the hex code within the string. When a negative byte or short was parsed, the method treated it as a positive int, and parsed accordingly. When a negative int was provided, the method threw an exception.

Cause of the Exception

The Java int has “two” fields: 31 bits of data, and 1 bit of sign. When a negative hex string was provided, parseInt saw 32 bits of data, determined it was an overflow condition, and threw the exception.

Solution

With the root cause of the problem in hand, I was quickly able to write a solution. I first wrote a routine that determined if the proved string was negative, based on its type (or size). If it was negative, I replace the original data string with its two’s complement and added a negative sign to the front. Now, parseInt returns the expected results.

Code

/** Test if string is negative  
 *  
 * @param number - hex string containing data  
 * @param bytes - number of bytes in data type (8 bit vs 16 bit, etc)  
 *  
 * @ return True means the number is negative  
 */  
public static boolean isNumberNegative(String number, int bytes){  
    if((number.length() / 2) < bytes ){ // check if top bit even has a value in it  
        return false;  
    }

    // extract first two characters into number  
    int bit = Integer.parseInt( number.substring(0,2), 16);  
    bit >>= 7; // down shift seven bits

    return (bit == 1); // top bit set == negative  
}

/**  
 * Make the hex string negative by performing two's complement  
 * @param hex the string to make negative  
 * @return input string after 2's complement reversal  
 */  
public static String makeNegativeInt(String hex){  
    String finalString = "";  
    int[] list = new int[ hex.length() / 2 ];

    // copy hex string into array  
    for(int i = 0; i < hex.length() / 2; i++){  
        list[i] = Integer.parseInt( hex.substring(i*2, i*2 +2), 16);  
    }

    // make each block negative  
    int add = 1;  
    int topBitOld = 0;  
    int topBitNew = 0;

    for(int i = list.length -1 ; i &gt;= 0; i--){  
        list[i] ^= 0xFF; // flip bits  
        topBitOld = (list[i] >> 7) & 1;             // save old top bit  
        list[i] += add;                             // possibly add bit  
        topBitNew = (list[i] >> 7) & 1;             // save new top bit  
        if((topBitOld == 1) && (topBitNew == 0)){   // check if overflow occurred  
            add = 1;  
        } else {  
            add = 0;  
        }  
        list[i] &= 0xFF;  
    }

    // rebuild hex string  
    for(int i = 0; i &lt; list.length; i++){  
        finalString += padHex(list[i], 2);  
    }

    return finalString;  
}

/**  
 * This pads a number with zeros, after it has been converted to  
 * hexidecimal format, sans 0x.  
 *  
 * @param number Number to be converted to hexidecimal  
 * @param length Final length of padded string  
 * @return String after padding  
 */  
public static String padHex(int number, int length){  
    char padding = '0';  
    String s = Integer.toHexString(number);  
    while(s.length() < length){  
        s = padding + s;  
    }  
    return s;  
}  

Continue reading

XmlHttpRequest

Posted on 2010-03-31 23:10 in Blog • Tagged with javascript, programming

I was reading about Websockets and their usefulness the other day when I realized how little of their precursor, the XmlHttpRequest, I actually knew. I performed some research and code a few quick and dirty example pages and discovered a new realm of capabilities that I was previously ignorant about.

For years, I had assumed AJAX technology was only useful if there were server side scripts interpreting the requests, gathering the needed data, and returning well formatted responses. These responses were then used to update the page content.  While coding one of the examples, I had an ‘Ah-ah moment’.  AJAX capabilities could be utilized to load static content when updating the page.  Instead of having to address PHP or ASP pages, plain old HTML files could be the target.

Within minutes, I had seamlessly written a basic homepage, consisting of several sections that loaded all the content without a single page refresh. This game me a liberating feeling. Now I can update the content without losing my JavaScript variable values.

Quick Example

The simplest way to dynamically load content is to specify

/* Create the request object */  
if (window.XMLHttpRequest) {        // Modern browsers  
    xhttp = new XMLHttpRequest();  
} else {                            // Old Internet Explorer  
    xhttp = new ActiveXObject("Microsoft.XMLHTTP");  
}
/* Perform the request */  
xhttp.open("GET","external\_file.txt",false);  
xhttp.send("");  
destinationElement.innerHTML = xhttp.responseText;

This creates a new XmlHttpRequest object across multiple browser types.  The ‘open’ command defines how the file will be retrieved (GET vs. POST) and the URL for the file.  Send performs the actual communication with the server.  After the communication is complete, responseText is extracted and displayed to the user.

This block of code is very straightforward and very misleading.  It performs no error handling (file not found) and does not take into account the amount of time needed to retrieve file (server load).

Better Example

A better approach would be to use a JavaScript library that is designed for this type work, JQuery.

$(".destinationElement").load("external_file.txt");

This single line of JavaScript performs the same function as the block of code above. It says load the contents of the files, external_file.txt, into the DOM element whose title is ‘destinationElement’.

Again, this over looks the same errors the first block contained, errors while loading the file and what not.  Luckily, JQuery has built in error handling events. As a programmer, we can define what action should be taken when a query fails.

$(window).error(function(msg, url, line){  
    // handle the error  
});  

Quick note: The above block will catch all errors that occur, not just errors AJAX errors.


Continue reading

Mercurial

Posted on 2010-03-31 22:54 in Blog • Tagged with programming, competition, version control

For the last three weeks, development on my news program, Iceberg Races, has been in high gear. Each night before bed, I simply zipped up my work and archived it away. This approach was fine at first, as I was hammering out the design as I coded, which meant I took a lot of dead end paths. Large chunks of code and entire files were created and then deleted the next day (as I discovered better implementation methods.) It’s true that the best way to learn a language is to dive right in by writing ‘useful’ program.

On Wednesday, I realized that my code structure had stabilized a week prior and I was overdue for tracking my code in a source revision tool. Luckily, I ran across an amazing Mercurial tutorial and decided to give it a try.

So far I’m loving it. The commands are all straight forward. It was extremely quick to setup, and has perform admirably for the past two three days that I’ve been using it. I’ve summarized the most useful commands below (this is not an exhaustive list):

Command Description
hg * All commands start with hg followed by…
init Create a new repository in current directory.
add Add new files to repository
remove Remove file from repository
status View the status of the files in the repository.
diff View the differences between two different versions of a file.
commit Save the current state of the files to the repository.
rollback Undo the last one commit.
merge Resolve conflicts in a file.
log View the revision history.
update Check out files to a specified revision
push Copy changes in local repository to a remote repository.
pull Copy changes in a remote repository and bring them into your local repository.

I’ve bolded the commands that come in the handiest. This includes the operations to save the current state of the repository and how to restore a past state.


Continue reading