10 using namespace __gnu_cxx;
13 Bot::Bot(const string& name, PlayerType type){
17 _playerTypeNames[robber] = "robber";
18 _playerTypeNames[cop_foot] = "cop-foot";
19 _playerTypeNames[cop_car] = "cop-car";
21 _playerTypes["robber"] = robber;
22 _playerTypes["cop-foot"] = cop_foot;
23 _playerTypes["cop-car"] = cop_car;
28 cout << "reg: " << _name << " " << _playerTypeNames[_type] << endl;
35 _name = value<string>(input);
36 //cerr << "Got name: " << _name << endl;
40 //cerr << "Got players, building graph." << endl;
44 preGamePreparations();
48 if (input == "game-over")
50 //cerr << input << endl;
51 //cerr << "Updating world" << endl;
53 //cerr << "Done updating world" << endl;
54 _type = _players[_name].type;
55 _location = _players[_name].location;
56 //cerr << "New turn" << endl;
61 //cerr << "Getting information " << endl;
71 //cerr << "Done with turn." << endl;
75 void Bot::getPlayers(){
79 _players[value<string>(input)].type = robber;
81 _players[value<string>(input)].type = cop_foot;
83 _players[value<string>(input)].type = cop_foot;
85 _players[value<string>(input)].type = cop_foot;
87 _players[value<string>(input)].type = cop_foot;
92 ( nod: loc node-tag coordinate coordinate eol )*
95 ( edg: loc loc edge-type eol )*
98 void Bot::buildGraph(){
101 if (input != "nod\\")
104 //cerr << "Getting intersections" << endl;
109 istringstream node(input);
113 Intersection& inter = _intersections[name];
115 if (input == "bank"){
119 else if (input == "hq"){
123 else if (input == "robber-start"){
124 inter.type = robber_start;
125 _robberLocation = name;
128 inter.type = ordinary;
133 //cerr << "Number of intersections: " << _intersections.size() << endl;
136 if (input != "edg\\")
139 //cerr << "Getting streets" << endl;
146 istringstream street(input);
154 //cerr << "Street between: " << from << " and " << to << " of type: " << type << endl;
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;
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;
172 //cerr << "Number of streets: " << streets << endl;
176 void Bot::updateWorld(){
179 _world = value<int>(input);
180 //cerr << "World: " << _world << endl;
183 _robbed = value<int>(input);
184 //cerr << "Robbed: " << _robbed << endl;
191 istringstream dirtyCop(input);
199 istringstream controlledCop(input);
207 istringstream falseAccusation(input);
215 istringstream bank(input);
218 bank >> _banks[input];
220 //cerr << "Number of banks: " << _banks.size() << endl;
227 istringstream evidence(input);
232 _evidence[world] = input;
236 _smell = value<int>(input);
238 _robberLocation = "";
244 istringstream player(input);
247 //cerr << "Player: " << input << endl;
248 Player& pl = _players[input];
249 player >> pl.location;
251 pl.type = _playerTypes[input];
252 if (pl.type == robber)
253 _robberLocation = pl.location;
255 //cerr << "Number of players: " << _players.size() << endl;
259 void Bot::move(std::string location){
260 //cerr << "Moving to: " << location << endl;
261 cout << "mov: " << location << " " << _playerTypeNames[_type] << " " << _name << endl;
265 T value(std::string input){
266 istringstream istr(input);
273 std::string Bot::turn(){
274 //cerr << "Using stupid stand still Bot::turn" << endl;
279 bool operator()(const SPInfo* v1, const SPInfo* v2){
280 return v1->cost > v2->cost;
284 std::list<std::string> Bot::shortestPath(const std::string& from, PlayerType type, const SPGoal& goal, bool reverse){
286 //cerr << "New shortest path from: " << from << endl;
288 priority_queue<SPInfo*, vector<SPInfo* >, SPInfoComp > pq;
289 hash_map<string,SPInfo> nodes;
291 SPInfo* node = &nodes[from];
293 node->settled = false;
299 bool hascar = type == cop_car;
311 front_insert_iterator<list<string> > ii(l);
318 node->settled = true;
320 hash_map<string,Intersection>::const_iterator intersection = _intersections.find(node->name);
321 if (intersection == _intersections.end()){ //Sanity check, should never be true..
322 cerr << "BUG: Could not find intersection: " << node->name << endl;
325 for (hash_map<string,StreetType>::const_iterator street = intersection->second.connections.begin();
326 street != intersection->second.connections.end(); ++street){
327 hash_map<string,SPInfo>::iterator newNode = nodes.find(street->first);
328 bool travelStreet = false;
331 if (street->second != foot){
332 hash_map<string,Intersection>::const_iterator newInter = _intersections.find(street->first);
333 if (newInter != _intersections.end()){
334 hash_map<string,StreetType>::const_iterator st = newInter->second.connections.find(node->name);
335 if (st != newInter->second.connections.end())
336 travelStreet = st->second != foot;
341 travelStreet = street->second != foot;
343 travelStreet = street->second != car;
346 //cerr << "Adding street: " << street->first << endl;
347 if (newNode == nodes.end()){
348 n = &nodes[street->first];
349 n->name = street->first;
353 }else if (newNode->second.settled)
356 n = &newNode->second;
358 if (n->cost > node->cost + 1){
359 n->cost = node->cost + 1;
369 return list<string>();
372 SimpleSPGoal::SimpleSPGoal(const std::string& to, int limit):_to(to), _limit(limit){
375 int SimpleSPGoal::operator()(const SPInfo* node) const{
376 if (_limit > 0 && node->cost >= _limit)
378 if (node->name == _to)
383 FindPlayer::FindPlayer(const hash_map<string, Player>& players, PlayerType type, int limit) : _players(players), _type(type), _limit(limit){
386 int FindPlayer::operator()(const SPInfo* node) const{
387 if (_limit > 0 && node->cost >= _limit)
389 for(hash_map<string, Player>::const_iterator player = _players.begin();
390 player != _players.end(); ++player){
391 if (player->second.type == _type && player->second.location == node->name){
398 void Bot::sendInformation(){
399 cout << "inf\\" << endl;
400 cout << "inf/" << endl;
403 void Bot::getInformation(){
407 }while(input != "from/");
410 void Bot::sendPlan(){
411 cout << "plan\\" << endl;
412 cout << "plan/" << endl;
415 void Bot::getPlans(){
420 }while(input != "from/");
424 cout << "vote\\" << endl;
425 cout << "vote: " << _name << endl;
426 priority_queue<pair<int,string> > players;
427 for (hash_map<string,int>::const_iterator player = _winningPlans.begin();
428 player != _winningPlans.end(); ++player){
429 players.push(pair<int,string>(-player->second, player->first));
431 while (players.size() > 0){
432 const pair<int,string>& player = players.top();
433 cout << "vote: " << player.second << endl;
434 //cerr << "voted for " << player.second << " with " << player.first << " previously won plans" << endl;
437 cout << "vote/" << endl;
440 void Bot::voteResult(){
443 if (input != "nowinner:"){
444 string winner = value<string>(input);
446 ++_winningPlans[winner];