From a46f782eda088f2229c2c3375aad92e4e4d16421 Mon Sep 17 00:00:00 2001 From: Sam Moore Date: Fri, 16 Mar 2012 12:06:50 +0800 Subject: [PATCH] Fixed error in Java AI I was using Vector.remove(Object), but I realised that this doesn't compare references, it calls the "compareTo" method. See KillUnit Java AI is still using Buffered IO and so hasn't been tested with the manager program yet. --- agents/basic_java/Makefile | 13 ++++++++----- agents/basic_java/basic.java | 19 +++++++++++++++---- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/agents/basic_java/Makefile b/agents/basic_java/Makefile index b058cdf..a904ebd 100644 --- a/agents/basic_java/Makefile +++ b/agents/basic_java/Makefile @@ -2,12 +2,15 @@ # Sample C++ Stratego AI # UCC Programming Competition 2012 -BasicAI.class : basic.java Reader.java piece.java - javac Reader.java - javac piece.java - javac basic.java +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 BasicAI diff --git a/agents/basic_java/basic.java b/agents/basic_java/basic.java index 242e6cb..72b2b90 100644 --- a/agents/basic_java/basic.java +++ b/agents/basic_java/basic.java @@ -256,18 +256,29 @@ class BasicAI */ private void KillUnit(Piece kill) throws Exception { + Vector 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.remove(ii); + return; + } + } + throw new Exception("BasicAI.KillUnit - Couldn't find unit in unit list."); } /** -- 2.20.1