Author Topic: Version 1.3 uploaded  (Read 3407 times)


  • Guest
Version 1.3 uploaded
« on: January 09, 2009, 12:22:36 AM »
I have uploaded version 1.3 of Hill 218.  Through the magic of Java Web Start, it should automatically be downloaded to your computer the next time you launch Hill 218, without you needing to do anything special.

For those (few) who might be interested, an explanation of the 2 bug fixes, along with musings about the root causes of the original bugs and what could have been done to prevent them:

Bug 1: Computer refusing to play the last turn if game will be a draw
Possible variation of Bug 1 (unconfirmed): Computer claiming it won when result should have been a draw.

I'm pretty confident I fixed the bug, I'm less confident I fixed the variation, but since I can't repro it and the person that reported it can no longer repro it with the new version, I can't be certain one way or the other.

The problem here was a line of code missing in the AI algorithm.  There was a "while" loop that checked the value of a counter, but the counter was never being incremented.  This meant that the program went into an infinite loop and never played the final card.

The bug was in the function that handled the case of the computer needing to play a single card, and specifically in the code that did the targeting.  Thus, it never showed up when the computer needed to play a single card to start the game, since there was nothing to target.

This bug clearly did not exist for the vast majority of the life of the code base.  Chad and I played hundreds of games, and achieved many draws, without hitting this.  The line must have been inadvertantly deleted while making one of the last few changes.  This makes a little bit of sense, because the last changes I made were to the AI functions, to implement the "easy" opponent.

What could have been done to prevent this:

(1) Final validation testing.  Before uploading the program to the world, I should have validated all the obvious conditions-- blue player winning, green player winning, computer winning by attrition, computer winning by capturing base, human winning by attriting, human winning by capturing base, and draw, at a minimum.  The fact that these had all been tested a gazillion times during development shouldn't have mattered, it should have been done once I was sure the code was complete.

(2) Source code control.  Because this project started out as a small throwaway project to teach myself Java, I didn't bother finding, learning, and installing a Source Control system that worked with Java.  If I had done so, I would have noticed when the line got deleted when I ran the diffs between version n and version n+1.  And if I hadn't, at least now I could go back and figure out what changes I had been making when it got deleted, which might prove useful.

Bug 2: Sometimes the first player gets to play 2 cards instead of 1 on turn 1.

The problem here was a race condition.  The global variable that keeps track of whether the current player is supposed to play 1 card or 2 cards was being set 1 line of code after the function call that told the AI thread it was time to play; in almost all cases, this didn't matter, because by the time the AI thread had started up and ran to the point it was ready to play, the original thread had gotten to the next line of code and set the variable to indicate only 1 card should be played.  But on certain hardware/OS combinations, the timing was different: the AI thread got started and ran for a while before context switching back the the original thread, so it used the default value of the single card variable, which was uninitialized, and hence 0, which is false-- meaning "no, don't play a single card."

What could have been done to prevent this:

(1) The variable should have been initialized.  The first player always only plays a single card, so there's no reason not to set the variable to 'true' when it's declared.  This is good defensive programming-- initialize your variables!

(2) Be more careful about the names I give my functions.  In this case, the function that woke up the AI thread had a misleading name, which made it sound as if it was just setting a variable value (which is probably what the function originally did).  So, looking at the source code, it looked like the lines of code could be written in any order-- it's just setting a bunch of variable values.  But if the function had been called something like "SetXandKickOffAI" (or had been broken down into two appropriately named functions), then it would have been obvious at a glance that I needed to be careful about what the values of the variables were before I called the function.




  • Guest
Re: Version 1.3 uploaded
« Reply #1 on: January 15, 2009, 02:25:27 PM »
Great work!

Thanks a lot!!!



  • Hero Member
  • *****
  • Posts: 637
Re: Version 1.3 uploaded
« Reply #2 on: January 19, 2009, 04:18:01 PM »
I have played around 100 games against the computer after this update and I can say everything apears to be working and I see no traces of the old bugs.  That being said even the hard AI does make a game losing mistake in very rare cases but they have so far been near impossible to reproduce.  All in all great work and very addicting game that has wasted many a hour i should have been working.  Speaking of which I need to get back to that now.
Quote from: Chad_YMG
Cards are definitely good to have, but I like punching my opponent in the face, too!