]> ruin.nu Git - icfp05.git/blob - botsrc/bot.cpp
templates
[icfp05.git] / botsrc / bot.cpp
1 #include <iostream>
2 #include <sstream>
3 #include <iterator>
4 #include <queue>
5 #include <vector>
6 #include <utility>
7 #include "bot.h"
8
9 using namespace std;
10 using namespace __gnu_cxx;
11
12
13 Bot::Bot(const string& name, PlayerType type){
14         _name = name;
15         _type = type;
16
17         _playerTypeNames[robber] = "robber";
18         _playerTypeNames[cop_foot] = "cop-foot";
19         _playerTypeNames[cop_car] = "cop-car";
20
21         _playerTypes["robber"] = robber;
22         _playerTypes["cop-foot"] = cop_foot;
23         _playerTypes["cop-car"] = cop_car;
24         //shortestPath("",robber,SimpleSPGoal(""));
25         //shortestPath("",robber,FindPlayer(hash_map<string,Player>(),robber));
26
27 }
28
29 void Bot::play(){
30         cout << "reg: " << _name << " " << _playerTypeNames[_type] << endl;
31
32         string input;
33         getline(cin, input);
34         if (input != "wsk\\")
35                 return;
36         getline(cin, input);
37         _name = value<string>(input);
38         //cerr << "Got name: " << _name << endl;
39
40         getPlayers();
41
42         //cerr << "Got players, building graph." << endl;
43         getline(cin, input);
44         buildGraph();
45
46         preGamePreparations();
47
48         while (true){
49                 getline(cin, input);
50                 if (input == "game-over")
51                         return;
52                 //cerr << input << endl;
53                 //cerr << "Updating world" << endl;
54                 updateWorld();
55                 //cerr << "Done updating world" << endl;
56                 _type = _players[_name].type;
57                 _location = _players[_name].location;
58                 //cerr << "New turn" << endl;
59                 //
60                 string input;
61
62                 sendInformation();
63                 //cerr << "Getting information " << endl;
64                 getInformation();
65
66                 sendPlan();
67                 getPlans();
68
69                 vote();
70                 voteResult();
71
72                 move(turn());
73                 //cerr << "Done with turn." << endl;
74         }
75 }
76
77 void Bot::getPlayers(){
78         string input;
79         //robber and 5 cops
80         getline(cin, input);
81         _players[value<string>(input)].type = robber;
82         getline(cin, input);
83         _players[value<string>(input)].type = cop_foot;
84         getline(cin, input);
85         _players[value<string>(input)].type = cop_foot;
86         getline(cin, input);
87         _players[value<string>(input)].type = cop_foot;
88         getline(cin, input);
89         _players[value<string>(input)].type = cop_foot;
90 }
91
92 /**
93                                 nod\ eol        
94                                 ( nod: loc node-tag coordinate coordinate eol )*        
95                                 nod/ eol        
96                                 edg\ eol        
97                                 ( edg: loc loc edge-type eol )* 
98                                 edg/ eol        
99 */
100 void Bot::buildGraph(){
101         string input;
102         getline(cin, input);
103         if (input != "nod\\")
104                 return;
105
106         //cerr << "Getting intersections" << endl;
107         while (true){
108                 getline(cin, input);
109                 if (input == "nod/")
110                         break;
111                 istringstream node(input);
112                 node >> input;
113                 string name;
114                 node >> name;
115                 Intersection& inter = _intersections[name];
116                 node >> input;
117                 if (input == "bank"){
118                         inter.type = bank;
119                         _banks[name] = 0;
120                 }
121                 else if (input == "hq"){
122                         inter.type = hq;
123                         _copHq = name;
124                 }
125                 else if (input == "robber-start"){
126                         inter.type = robber_start;
127                         _robberLocation = name;
128                 }
129                 else
130                         inter.type = ordinary;
131                 node >> inter.x;
132                 node >> inter.y;
133         }
134
135         //cerr << "Number of intersections: " << _intersections.size() << endl;
136
137         getline(cin, input);
138         if (input != "edg\\")
139                 return;
140
141         //cerr << "Getting streets" << endl;
142         int streets = 0;
143         while (true){
144                 getline(cin, input);
145                 if (input == "edg/")
146                         break;
147                 ++streets;
148                 istringstream street(input);
149                 string from;
150                 street >> from;
151                 street >> from;
152                 string to;
153                 street >> to;
154                 string type;
155                 street >> type;
156                 //cerr << "Street between: " << from << " and " << to << " of type: " << type << endl;
157                 if (type == "foot"){
158                         _intersections[from].connections[to] = both;
159                         Intersection& inter = _intersections[to];
160                         hash_map<string,StreetType>::iterator conn = inter.connections.find(from);
161                         if (conn == inter.connections.end())
162                                 inter.connections[from] = foot;
163                         else if (conn->second == car)
164                                 inter.connections[from] = both;
165                 }else{
166                         Intersection& inter = _intersections[from];
167                         hash_map<string,StreetType>::iterator conn = inter.connections.find(to);
168                         if (conn == inter.connections.end())
169                                 inter.connections[from] = car;
170                         else if (conn->second == foot)
171                                 inter.connections[from] = both;
172                 }
173         }
174         //cerr << "Number of streets: " << streets << endl;
175         getline(cin, input);
176 }
177
178 void Bot::updateWorld(){
179         string input;
180         getline(cin,input);
181         _world = value<int>(input);
182         //cerr << "World: " << _world << endl;
183
184         getline(cin,input);
185         _robbed = value<int>(input);
186         //cerr << "Robbed: " << _robbed << endl;
187         //
188         getline(cin,input);
189         while (true){
190                 getline(cin, input);
191                 if (input == "dc/")
192                         break;
193                 istringstream dirtyCop(input);
194         }
195
196         getline(cin,input);
197         while (true){
198                 getline(cin, input);
199                 if (input == "sc/")
200                         break;
201                 istringstream controlledCop(input);
202         }
203
204         getline(cin,input);
205         while (true){
206                 getline(cin, input);
207                 if (input == "fac/")
208                         break;
209                 istringstream falseAccusation(input);
210         }
211
212         getline(cin,input);
213         while (true){
214                 getline(cin, input);
215                 if (input == "bv/")
216                         break;
217                 istringstream bank(input);
218                 bank >> input;
219                 bank >> input;
220                 bank >> _banks[input];
221         }
222         //cerr << "Number of banks: " << _banks.size() << endl;
223         
224         getline(cin,input);
225         while (true){
226                 getline(cin, input);
227                 if (input == "ev/")
228                         break;
229                 istringstream evidence(input);
230                 int world;
231                 evidence >> input;
232                 evidence >> input;
233                 evidence >> world;
234                 _evidence[world] = input;
235         }
236         
237         getline(cin,input);
238         _smell = value<int>(input);
239
240         _robberLocation = "";
241         getline(cin,input);
242         while (true){
243                 getline(cin, input);
244                 if (input == "pl/")
245                         break;
246                 istringstream player(input);
247                 player >> input;
248                 player >> input;
249                 //cerr << "Player: " << input << endl;
250                 Player& pl = _players[input];
251                 player >> pl.location;
252                 player >> input;
253                 pl.type = _playerTypes[input];
254                 if (pl.type == robber)
255                         _robberLocation = pl.location;
256         }
257         //cerr << "Number of players: " << _players.size() << endl;
258         getline(cin, input);
259 }
260
261 void Bot::move(std::string location){
262         //cerr << "Moving to: " << location << endl;
263         cout << "mov: " << location << " " << _playerTypeNames[_type] << " " << _name << endl;
264 }
265
266 template<class T>
267 T value(std::string input){
268         istringstream istr(input);
269         istr >> input;
270         T s;
271         istr >> s;
272         return s;
273 }
274
275 std::string Bot::turn(){
276         //cerr << "Using stupid stand still Bot::turn" << endl;
277         return _location;
278 }
279
280
281 void Bot::sendInformation(){
282         cout << "inf\\" << endl;
283         cout << "inf/" << endl;
284 }
285
286 void Bot::getInformation(){
287         string input;
288         do{
289                 getline(cin,input);
290         }while(input != "from/");
291 }
292
293 void Bot::sendPlan(){
294         cout << "plan\\" << endl;
295         cout << "plan/" << endl;
296 }
297
298 void Bot::getPlans(){
299         string input;
300         //ignore From-plan
301         do{
302                 getline(cin,input);
303         }while(input != "from/");
304 }
305
306 void Bot::vote(){
307         cout << "vote\\" << endl;
308         cout << "vote: " << _name << endl;
309         priority_queue<pair<int,string> > players;
310         for (hash_map<string,int>::const_iterator player = _winningPlans.begin();
311                         player != _winningPlans.end(); ++player){
312                 players.push(pair<int,string>(-player->second, player->first));
313         }
314         while (players.size() > 0){
315                 const pair<int,string>& player = players.top();
316                 cout << "vote: " << player.second << endl;
317                 //cerr << "voted for " << player.second << " with " << player.first << " previously won plans" << endl;
318                 players.pop();
319         }
320         cout << "vote/" << endl;
321 }
322
323 void Bot::voteResult(){
324         string input;
325         getline(cin,input);
326         if (input != "nowinner:"){
327                 string winner = value<string>(input);
328                 if (winner != _name)
329                         ++_winningPlans[winner];
330         }
331 }
332
333 #include "shortestPath.cpp"