Added Java sample AI
[progcomp2012.git] / agents / basic_java / basic.java
1 /**
2  * Class to manage a Stratego playing AI in Java
3  * @author Sam Moore for the UCC::Progcomp 2012
4  * @website http://progcomp.ucc.asn.au
5  */
6
7 import java.lang.Exception;
8 import java.util.Vector;
9 import java.util.Random;
10
11
12
13 class BasicAI
14 {
15
16
17         
18         /**
19          * Moves a point in a direction, returns new point
20          * @param x x coord
21          * @param y y coord
22          * @param direction Indicates direction. Must be "LEFT", "RIGHT", "UP", "DOWN"
23          * @param multiplier Spaces to move
24          * @returns An array of length 2, containing the new x and y coords
25          * @throws Exception on unrecognised direction
26          */
27         public static int[] Move(int x, int y, String direction, int multiplier) throws Exception
28         {
29                 //NOTE: The board is indexed so that the top left corner is x = 0, y = 0
30                 //      Does not check that coordiantes would be valid in the board.
31
32                 if (direction.compareTo("DOWN") == 0)
33                         y += multiplier; //Moving down increases y
34                 else if (direction.compareTo("UP") == 0)
35                         y -= multiplier; //Moving up decreases y
36                 else if (direction.compareTo("LEFT") == 0)
37                         x -= multiplier; //Moving left decreases x
38                 else if (direction.compareTo("RIGHT") == 0)
39                         x += multiplier;
40                 else
41                 {
42                         throw new Exception("BasicAI.Move - Unrecognised direction " + direction);
43                 }
44
45                 int result[] = new int[2];
46                 result[0] = x; result[1] = y;
47                 return result;
48         }
49
50         /**
51          * Returns the "opposite" colour to that given
52          * @param colour Must be "RED" or "BLUE"
53          * @returns The alternate String to colour
54          * @throws Exception if colour is not "RED" or "BLUE"
55          */
56         public static String OppositeColour(String colour) throws Exception
57         {
58                 if (colour.compareTo("BLUE") == 0)
59                         return "RED";
60                 else if (colour.compareTo("RED") == 0)
61                         return "BLUE";
62                 else
63                         throw new Exception("BasicAI.OppositeColour - Unrecognised colour " + colour);
64         }
65
66         /**
67          * Tests if a value is an integer
68          * I cry at using exceptions for this
69          */
70         public static boolean IsInteger(String str)
71         {
72                 try
73                 {
74                         Integer.parseInt(str);
75                 }
76                 catch (NumberFormatException e)
77                 {
78                         return false;
79                 }
80                 return true;
81         }
82
83         private int turn; //The turn number of the game
84         private Piece board[][]; //The board
85         private Vector<Piece> units; //All units
86         private Vector<Piece> enemyUnits; //All enemy units
87         private Piece lastMoved; //Last moved piece
88         private String colour; //Colour of the AI
89         private String opponentName; //Name of the AI's opponent
90         private int width; //Width of the board (NOTE: Should always be 10)
91         private int height; //Height of the board (NOTE: Should always be 10)
92
93         private static int totalAllies[] = {6,1,1,2,3,4,4,4,5,8,1,1}; //Numbers of allies, B -> F
94         private static int totalEnemies[] = {6,1,1,2,3,4,4,4,5,8,1,1}; //Numbers of enemies, B -> F
95         private static int hiddenEnemies[] = {6,1,1,2,3,4,4,4,5,8,1,1}; //Number of hidden enemies of each type
96         private static int hiddenAllies[] = {6,1,1,2,3,4,4,4,5,8,1,1}; //Number of hidden allies of each type
97
98         private static String directions[] = {"UP", "DOWN", "LEFT", "RIGHT"}; //All available directions
99         
100         private static Random rand = new Random(); //A random number generator
101         
102         /**
103          * Constructor
104          * Sets up a board, prepares to play
105          */
106         public BasicAI()
107         {
108                 turn = 0;
109                 board = null;
110                 units = new Vector<Piece>();
111                 enemyUnits = new Vector<Piece>();
112
113                 lastMoved = null;
114                 colour = null;
115                 opponentName = null;
116
117                 //HACK to get rid of stupid Javac warnings
118                 if (lastMoved == null && opponentName == null);
119         }
120
121         /**
122          * Implements Setup phase of protocol described in manager program man page
123          * Always uses the same setup. Override to create custom setups.
124          */
125         public void Setup() throws Exception
126         {
127                 Vector<String> setup = Reader.readTokens(); //Wierd java way of doing input from stdin, see Reader.java
128                 if (setup.size() != 4)
129                 {
130                         throw new Exception("BasicAI.Setup - Expected 4 tokens, got " + setup.size());
131                 }       
132                 colour = setup.elementAt(0);
133                 opponentName = setup.elementAt(1);
134                 width = Integer.parseInt(setup.elementAt(2));
135                 height = Integer.parseInt(setup.elementAt(3));
136
137                 if (width != 10 || height != 10)
138                         throw new Exception("BasicAI.Setup - Expected width and height of 10, got " + width + " and " + height);
139
140                 board = new Piece[width][height];
141                 for (int x=0; x < board.length; ++x)
142                 {
143                         for (int y = 0; y < board[x].length; ++y)
144                                 board[x][y] = null;
145                 }
146
147                 //TODO: Modify this setup
148                 if (colour.compareTo("RED") == 0)
149                         System.out.println("FB8sB479B8\nBB31555583\n6724898974\n967B669999");
150                 else if (colour.compareTo("BLUE") == 0)
151                         System.out.println("967B669999\n6724898974\nBB31555583\nFB8sB479B8");
152                 else
153                         throw new Exception("BasicAI.Setup - Unrecognised colour of " + colour);
154
155         }
156
157         /**
158          * Cycles a move
159          */
160         public void MoveCycle() throws Exception
161         {
162                 InterpretResult();
163                 ReadBoard();
164                 MakeMove();
165                 InterpretResult();
166         }
167
168         /**
169          * Makes a move
170          * TODO: Rewrite move algorithm (currently uses random)
171          */
172         public void MakeMove() throws Exception
173         {
174                 if (units.size() <= 0)
175                         throw new Exception("BasicAI.MakeMove - No units left!");
176
177                 int index = rand.nextInt(units.size() - 1); //Pick index of unit to move
178                 int startIndex = index; //Remember it
179                 
180                 while (true) //Don't worry, there is a break
181                 {
182                         Piece piece = units.elementAt(index);
183                         if (piece == null)
184                                 throw new Exception("BasicAI.MakeMove - null unit ???");
185         
186                         if (piece.Mobile())
187                         {
188                                 int dirIndex = rand.nextInt(directions.length); //Pick a random direction index
189                                 int startDirIndex = dirIndex; //Remember
190                                 while (true)
191                                 {
192                                         int p[] = Move(piece.x, piece.y, directions[dirIndex], 1);
193                                         if (p[0] >= 0 && p[0] < width && p[1] >= 0 && p[1] < height)
194                                         {
195                                                 Piece target = board[p[0]][p[1]];
196                                                 if (target == null || (target.colour != piece.colour && target.colour != "NONE" && target.colour != "BOTH"))
197                                                 {
198                                                         System.out.println(""+piece.x + " " + piece.y + " " + directions[dirIndex]);
199                                                         return;
200                                                 }
201                                                 
202                                         }
203                                         dirIndex = (dirIndex + 1) % directions.length;
204                                         if (dirIndex == startDirIndex)
205                                                 break;
206                                 }
207                         }
208                         index = (index + 1) % units.size();
209                         if (index == startIndex)
210                         {
211                                 System.out.println("NO_MOVE");
212                                 break;
213                         }
214                 }
215                 
216         }
217         
218         /**
219          * Reads the board
220          */
221         public void ReadBoard() throws Exception
222         {
223                 for (int y = 0; y < height; ++y)
224                 {
225                         String row = Reader.readLine();
226                         if (row.length() != width)
227                                 throw new Exception("BasicAI.ReadBoard - Row " + y + " has width " + row.length() + " instead of " + width);
228                         for (int x = 0; x < width; ++x)
229                         {
230                                 if (turn == 0)
231                                 {
232                                         switch (row.charAt(x))
233                                         {
234                                                 case '.':
235                                                         break;
236                                                 case '#':
237                                                         board[x][y] = new Piece(OppositeColour(colour), '?', x, y);
238                                                         enemyUnits.add(board[x][y]);
239                                                         break;
240                                                 case '+':
241                                                         board[x][y] = new Piece("NONE", '+', x, y);
242                                                         break;
243                                                 default:
244                                                         board[x][y] = new Piece(colour, row.charAt(x), x, y);
245                                                         units.add(board[x][y]);
246                                                         break;  
247                                         }
248                                                 
249                                 }
250                         }
251                 }
252         }
253
254         /**
255          * Removes a unit from the game
256          */
257         private void KillUnit(Piece kill) throws Exception
258         {
259                 if (kill.colour.compareTo(colour) == 0)
260                 {
261                         totalAllies[Piece.Index(kill.rank)] -= 1;
262                         if (units.remove(kill) == false)
263                                 throw new Exception("BasicAI.KillUnit - Couldn't remove allied Piece from units Vector!");
264                 }
265                 else if (kill.colour.compareTo(OppositeColour(colour)) == 0)
266                 {
267                         totalEnemies[Piece.Index(kill.rank)] -= 1;
268                         if (enemyUnits.remove(kill) == false)
269                                 throw new Exception("BasicAI.KillUnit - Couldn't remove enemy Piece from enemyUnits Vector!");
270                 }
271         }
272
273         /**
274          * Interprets the result of a move, updates all relevant variables
275          */
276         public void InterpretResult() throws Exception
277         {
278                 Vector<String> result = Reader.readTokens();
279                 if (turn == 0)
280                         return;
281                 if (result.elementAt(0).compareTo("QUIT") == 0)
282                         System.exit(0);
283                 if (result.elementAt(0).compareTo("NO_MOVE") == 0)
284                         return;
285
286                 if (result.size() < 4)
287                 {
288                         throw new Exception("BasicAI.InterpretResult - Expect at least 4 tokens, got " + result.size());
289                 }
290
291                 int x = Integer.parseInt(result.elementAt(0));
292                 int y = Integer.parseInt(result.elementAt(1));
293                 String direction = result.elementAt(2);
294
295                 int multiplier = 1;
296                 String outcome = result.elementAt(3);
297                 int outIndex = 3;
298                 if (IsInteger(outcome))
299                 {
300                         multiplier = Integer.parseInt(outcome);
301                         outcome = result.elementAt(4);
302                         outIndex = 4;
303                 }
304                 int p[] = Move(x,y,direction, multiplier);
305
306                 Piece attacker = board[x][y];
307                 board[x][y] = null;
308                 if (attacker == null)
309                         throw new Exception("BasicAI.InterpretResult - Couldn't find a piece to move at (" + x +"," + y+")");
310
311                 lastMoved = attacker;
312
313                 Piece defender = board[p[0]][p[1]];
314                 
315
316                 attacker.x = p[0]; attacker.y = p[1];
317                 attacker.positions.add(0, p);
318
319                 if (result.size() >= outIndex + 3)
320                 {
321                         if (defender == null)
322                                 throw new Exception("BasicAI.InterpretResult - Result suggests a defender at ("+p[0]+","+p[1]+"), but none found");
323                         attacker.rank = result.elementAt(outIndex+1).charAt(0); //ranks are 1 char long
324                         if (attacker.beenRevealed == false)
325                         {
326                                 if (attacker.colour.compareTo(colour) == 0)
327                                         hiddenAllies[Piece.Index(attacker.rank)] -= 1;
328                                 else if (attacker.colour.compareTo(OppositeColour(colour)) == 0)
329                                         hiddenEnemies[Piece.Index(attacker.rank)] -= 1;
330                                 else
331                                         throw new Exception("BasicAI.InterpretResult - Colour " + attacker.colour + " for moving piece makes no sense.");
332                         }
333                         attacker.beenRevealed = true;
334                         defender.rank = result.elementAt(outIndex+2).charAt(0); //ranks are 1 char long
335                         if (defender.beenRevealed == false)
336                         {
337                                 if (defender.colour.compareTo(colour) == 0)
338                                         hiddenAllies[Piece.Index(defender.rank)] -= 1;
339                                 else if (attacker.colour.compareTo(OppositeColour(colour)) == 0)
340                                         hiddenEnemies[Piece.Index(defender.rank)] -= 1;
341                                 else
342                                         throw new Exception("BasicAI.InterpretResult - Colour " + attacker.colour + " for defending piece makes no sense.");
343                         }
344                         defender.beenRevealed = true;
345                         
346                 }
347                 if (outcome.compareTo("OK") == 0)
348                         board[p[0]][p[1]] = attacker;
349                 else if (outcome.compareTo("KILLS") == 0)
350                 {
351                         board[p[0]][p[1]] = attacker;
352                         KillUnit(defender);
353                 }
354                 else if (outcome.compareTo("DIES") == 0)
355                 {
356                         KillUnit(attacker);
357                 }
358                 else if (outcome.compareTo("BOTHDIE") == 0)
359                 {
360                         board[p[0]][p[1]] = null;
361                         KillUnit(attacker);
362                         KillUnit(defender);
363                 }
364                 else
365                 {
366                         System.exit(0); //Game over
367                 }
368                 
369
370         }
371
372         /**
373          * The main function!
374          */
375         public static void main(String [] args)
376         {
377                 try
378                 {
379                         BasicAI theAI = new BasicAI();
380                         theAI.Setup();
381                         while (true)
382                                 theAI.MoveCycle();
383                 }
384                 catch (Exception e)
385                 {
386                         System.out.println("EXCEPTION: " + e.getMessage());
387                 }
388         }
389
390 }

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