Dedicated server stuff should now work
[progcomp2013.git] / agents / silverfish / agent.cpp
1 /**
2  * agent++ : A Sample agent for UCC::Progcomp2013
3  * @file agent.cpp
4  * @purpose Definition of Agent class
5  */
6
7 #include "agent.h"
8 #include <cassert> // for sanity checks
9
10 using namespace std;
11
12 /**
13  * @constructor Agent
14  * @param new_colour - colour of the Agent
15  */
16 Agent::Agent(const string & new_colour) : colour(Piece::str2colour(new_colour)), board(), selected(NULL)
17 {
18
19 }
20
21 /**
22  * @destructor ~Agent
23  */
24 Agent::~Agent()
25 {
26
27 }
28
29 /**
30  * @funct Select
31  * @purpose Selects a piece at random
32  * @returns Square containing the selected piece
33  */
34 Square & Agent::Select()
35 {
36         vector<Piece*> & v = board.pieces(colour); // get pieces
37         int choice = rand() % v.size(); // pick random index
38         Piece * p = v[choice]; // get piece at the index
39         assert(p->colour == colour);
40         selected = p; // update selected
41         //cerr << "Selected " << p->x << "," << p->y << " [" << p->types[0] << "," << p->types[1] << "]\n";
42         return board.square(p->x, p->y); // get Square from board
43 }
44
45 /**
46  * @funct Move
47  * @purpose Pick a square to move a selected piece into
48  * @returns Square to move last selected piece into
49  */
50 Square & Agent::Move()
51 {
52         assert(selected != NULL);
53         vector<Square*> moves; // all possible moves for selected piece
54         board.Get_moves(selected, moves); // populate possible moves
55         assert(moves.size() > 0); 
56         int choice = rand() % moves.size(); // pick random index
57         return *(moves[choice]); // return that move
58 }
59
60 /**
61  * @funct Run
62  * @purpose The "Game Loop" for the agent; read commands and call appropriate function to make responses
63  * @param in - Stream to read input from (use std::cin)
64  * @param out - Stream to write output to (use std::cout)
65  */
66 void Agent::Run(istream & in, ostream & out)
67 {
68         string cmd; // buffer for tokens
69         while (in.good())
70         {
71                 in >> cmd; // read first token only
72                 if (cmd == "QUIT")
73                 {
74                         break;
75                 }
76                 else if (cmd == "SELECTION?")
77                 {
78                         Square & s = Select(); // get selection
79                         out << s.x << " " << s.y  << "\n"; // return response through output
80                 }
81                 else if (cmd == "MOVE?")
82                 {
83                         Square & s = Move(); // get move
84                         out << s.x << " " << s.y << "\n"; // return response through output
85                 }
86                 else
87                 {
88                         // There were multiple tokens...
89                         stringstream s(cmd);
90                         int x; int y;
91                         s >> x; // Convert first token (in cmd) to an int
92                         in >> y; // Read second token from in
93                         
94                         in >> cmd; // Read third token
95                         
96                         if (cmd == "->") // Token indicates a move was made
97                         {
98                                 int x2; int y2; // remaining two tokens indicate destination
99                                 in >> x2; in >> y2;
100                                 board.Update_move(x, y, x2, y2); // update the board
101                         }
102                         else
103                         {
104                                 // Tokens are for a selection
105                                 int index; stringstream s2(cmd); 
106                                 s2 >> index; // convert third token to an index
107                                 in >> cmd; // Read fourth token - the new type of the piece
108                                 board.Update_select(x, y, index, cmd); // update the board
109                         }
110                         
111                 }
112         }
113 }

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