Tuesday, September 4, 2007

Code Ruler Redux

Today, we have Code Ruler Redux. My ruler was reviewed by Randy Cox, and I have a revised ruler here.


Team Member

Brian Jaress

Style Changes

First, of course, I fixed the class coding standards violations that Randy found.

I also changed some of the code to make better use of Java 5 features. In a couple places I'd been looping over an array and adding every element to a Set, and I changed it to a one-liner using Arrays.asList() and addAll().

Strategy Changes

I also made some adjustments to the strategy, adding a target minimum number of peasants and always attacking enemy knights when there are no other enemy pieces. That took care of two edge cases that came up surprisingly often.

Here are the updated test results against built-in rulers:

Migratingjaress
1773
0781
0783
Gang-upjaress
176668
162701
80732
Split-upjaress
180654
148653
202610

Shaoxuan Zhang, whose ruler I reviewed, checked the code of successful rulers from another school and adapted them into something that sounds similar in spirit to my strategy: figure out what types of targets you want to attack and have each knight attack the closest one.

It's not quite the same, though. His ruler attacks castles first, then attacks knights and peasants once it has all castles. My ruler attacks castles, peasants, and weak knights first. When all of those are gone, it attacks strong knights.

Lessons Learned

Arrays.asList() is a very handy function static method, and Eclipse is a lot easier now that I've hooked up my mouse.

I also realized that my ruler is evil.

Of course, all the rulers try to kill off enemy pieces and squeeze out other rulers, and my ruler isn't the most aggressive or effective. If, however, you think of the strategy as representing the behavior of an actual person, my ruler is the worst thug.

The other rulers seem to have either sophisticated strategies that optimize the local behavior of their pieces (Ben Karsin's ruler is a good example) or simple strategies like "capture the castles" or "split up into groups." My ruler's simple strategies are "target the weak" and "don't let anyone have a bigger army." Even the peasant strategy, which I think is quite effective, leads to a creepy "peasant Matrix' effect.

No comments: