Fixed bug in Java AI
authorSam Moore <[email protected]>
Fri, 16 Mar 2012 04:16:51 +0000 (12:16 +0800)
committerSam Moore <[email protected]>
Fri, 16 Mar 2012 04:16:51 +0000 (12:16 +0800)
In KillUnit:
I was using Vector.remove(Object), but I realised that this doesn't compare
references, instead it uses "compareTo". Which meant the AI deleted every unit
as soon as the first one died. Fun.

I discovered gcj, which lets you compile a Java program.
This is much easier than trying to convince the java interpreter to run.

agents/basic_java/Makefile
agents/basic_java/basic.java

index b058cdf..5088d15 100644 (file)
@@ -2,12 +2,16 @@
 # Sample C++ Stratego AI
 # UCC Programming Competition 2012 
 
+BasicAI : basic.java Reader.java piece.java
+       gcj -o BasicAI --main=BasicAI basic.java Reader.java piece.java
+
 BasicAI.class : basic.java Reader.java piece.java
        javac Reader.java
        javac piece.java
        javac basic.java
 
 clean : 
-       rm *.class
+       rm -f BasicAI
+       rm -f *.class
 
 
index 242e6cb..e9db507 100644 (file)
@@ -256,18 +256,29 @@ class BasicAI
         */
        private void KillUnit(Piece kill) throws Exception
        {
+               Vector<Piece> removeFrom = null;
                if (kill.colour.compareTo(colour) == 0)
                {
                        totalAllies[Piece.Index(kill.rank)] -= 1;
-                       if (units.remove(kill) == false)
-                               throw new Exception("BasicAI.KillUnit - Couldn't remove allied Piece from units Vector!");
+                       removeFrom = units;
                }
                else if (kill.colour.compareTo(OppositeColour(colour)) == 0)
                {
                        totalEnemies[Piece.Index(kill.rank)] -= 1;
-                       if (enemyUnits.remove(kill) == false)
-                               throw new Exception("BasicAI.KillUnit - Couldn't remove enemy Piece from enemyUnits Vector!");
+                       removeFrom = enemyUnits;
                }
+               if (removeFrom == null)
+                       throw new Exception("BasicAI.KillUnit - Can't identify unit with colour " + kill.colour + "!");
+
+               for (int ii=0; ii < removeFrom.size(); ++ii)
+               {
+                       if (removeFrom.elementAt(ii) == kill)
+                       {
+                               removeFrom.eraseAt(ii);
+                               return;
+                       }                               
+               }
+               throw new Exception("BasicAI.KillUnit - Couldn't find unit in unit list.");
        }
 
        /**

UCC git Repository :: git.ucc.asn.au