Dedicated server stuff should now work
[progcomp2013.git] / agents / java / Agent.java
1 /**
2  * @class Agent
3  * @purpose Class that represents an agent which will play qchess
4  */
5
6 import java.util.Random;
7 import java.util.Vector;
8
9 class Agent
10 {
11         public Agent(String colour) throws Exception
12         {
13                 this.colour = Piece.str2colour(colour);
14
15                 this.board = new Board();
16                 this.selected = null;
17                 this.rand = new Random();
18         }
19
20         public static void main(String [] args)
21         {
22                 String colour = Reader.readLine();
23                 
24                 try
25                 {
26                         Agent agent = new Agent(colour);
27                         agent.Run();
28                 }
29                 catch (Exception e)
30                 {
31                         //System.err.println("EXCEPTION: agent.Run - "+e); 
32                         e.printStackTrace();
33                 }
34         }
35
36         public void Run() throws Exception
37         {
38                 while (true)
39                 {
40                         Vector<String> v = Reader.readTokens();
41                         
42                         String cmd = v.get(0);
43                         
44                         if (cmd.compareTo("QUIT") == 0)
45                                 break;
46                         else if (cmd.compareTo("SELECTION?") == 0)
47                         {
48                                 Square s = select();
49                                 System.out.println(Integer.toString(s.x) + " " + Integer.toString(s.y));
50                         }
51                         else if (cmd.compareTo("MOVE?") == 0)
52                         {
53                                 Square s = move();
54                                 System.out.println(Integer.toString(s.x) + " " + Integer.toString(s.y));
55                         }
56                         else
57                         {
58                                 int x = Integer.parseInt(v.get(0));
59                                 int y = Integer.parseInt(v.get(1));
60                                 if (v.get(2).compareTo("->") == 0)
61                                 {
62                                         int x2 = Integer.parseInt(v.get(3));
63                                         int y2 = Integer.parseInt(v.get(4));
64                                         board.Update_move(x, y, x2, y2);
65                                 }
66                                 else
67                                 {
68                                         int index = Integer.parseInt(v.get(2));
69                                         String type = v.get(3);
70                                         board.Update_select(x, y, index, type);
71                                 }
72                         }
73                 }
74         }
75                 
76         public Square select() throws Exception
77         {
78                 Vector<Piece> p = board.pieces(colour);
79                 int choice = rand.nextInt(p.size());
80                 Square s = board.Get_square(p.get(choice).x, p.get(choice).y);
81                 if (s.piece == null)
82                         throw new Exception("ARGH");
83                 selected = s.piece;
84                 return s;
85         }
86
87         public Square move()
88         {
89                 Vector<Square> v = new Vector<Square>();
90                 board.Get_moves(selected, v);
91                 return v.get(rand.nextInt(v.size()));
92         }
93         
94         private final Piece.Colour colour; // colour of the agent; do not change it
95         private Board board;
96         private Piece selected; 
97         private Random rand;
98                 
99 };

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