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