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 if (inter.connections.find(from) == inter.connections.end())
159 inter.connections[from] = foot;
161 _intersections[from].connections[to] = car;
163 //cerr << "Number of streets: " << streets << endl;
167 void Bot::updateWorld(){
168 cerr << "Entered updateWorld function" << endl;
171 cerr << input << endl;
172 _world = value<int>(input);
173 cerr << "World: " << _world << endl;
176 _robbed = value<int>(input);
177 //cerr << "Robbed: " << _robbed << endl;
184 istringstream dirtyCop(input);
192 istringstream controlledCop(input);
200 istringstream falseAccusation(input);
208 istringstream bank(input);
211 bank >> _banks[input];
213 //cerr << "Number of banks: " << _banks.size() << endl;
220 istringstream evidence(input);
225 _evidence[world] = input;
229 _smell = value<int>(input);
231 _robberLocation = "";
237 istringstream player(input);
240 //cerr << "Player: " << input << endl;
241 Player& pl = _players[input];
242 player >> pl.location;
244 pl.type = _playerTypes[input];
245 if (pl.type == robber)
246 _robberLocation = pl.location;
248 //cerr << "Number of players: " << _players.size() << endl;
252 void Bot::move(std::string location){
253 //cerr << "Moving to: " << location << endl;
254 cout << "mov: " << location << " " << _playerTypeNames[_type] << " " << _name << endl;
258 T value(std::string input){
259 istringstream istr(input);
266 std::string Bot::turn(){
267 cerr << "Using stupid stand still Bot::turn" << endl;
272 bool operator()(const SPInfo* v1, const SPInfo* v2){
273 return v1->cost > v2->cost;
277 std::list<std::string> Bot::shortestPath(const std::string& from, PlayerType type, const SPGoal& goal, bool reverse){
279 //cerr << "New shortest path from: " << from << endl;
281 priority_queue<SPInfo*, vector<SPInfo* >, SPInfoComp > pq;
282 hash_map<string,SPInfo> nodes;
284 SPInfo* node = &nodes[from];
286 node->settled = false;
292 bool hascar = type == cop_car;
304 front_insert_iterator<list<string> > ii(l);
311 node->settled = true;
313 hash_map<string,Intersection>::const_iterator intersection = _intersections.find(node->name);
314 if (intersection == _intersections.end()){ //Sanity check, should never be true..
315 cerr << "BUG: Could not find intersection: " << node->name << endl;
318 for (hash_map<string,StreetType>::const_iterator street = intersection->second.connections.begin();
319 street != intersection->second.connections.end(); ++street){
320 hash_map<string,SPInfo>::iterator newNode = nodes.find(street->first);
321 bool travelStreet = false;
324 if (street->second != foot){
325 hash_map<string,Intersection>::const_iterator newInter = _intersections.find(street->first);
326 if (newInter != _intersections.end()){
327 hash_map<string,StreetType>::const_iterator st = newInter->second.connections.find(node->name);
328 if (st != newInter->second.connections.end())
329 travelStreet = st->second != foot;
334 travelStreet = street->second != foot;
336 travelStreet = street->second != car;
339 //cerr << "Adding street: " << street->first << endl;
340 if (newNode == nodes.end()){
341 n = &nodes[street->first];
342 n->name = street->first;
346 }else if (newNode->second.settled)
349 n = &newNode->second;
351 if (n->cost > node->cost + 1){
352 n->cost = node->cost + 1;
362 return list<string>();
365 SimpleSPGoal::SimpleSPGoal(std::string to):_to(to){
368 int SimpleSPGoal::operator()(const SPInfo* node) const{
369 if (node->name == _to)
374 FindPlayer::FindPlayer(const hash_map<string, Player>& players, PlayerType type, int limit) : _players(players), _type(type), _limit(limit){
377 int FindPlayer::operator()(const SPInfo* node) const{
378 if (_limit > 0 && node->cost >= _limit)
380 for(hash_map<string, Player>::const_iterator player = _players.begin();
381 player != _players.end(); ++player){
382 if (player->second.type == _type && player->second.location == node->name){
389 void Bot::sendInformation(){
390 cout << "inf\\" << endl;
391 cout << "inf/" << endl;
394 void Bot::getInformation(){
398 }while(input != "from/");
401 void Bot::sendPlan(){
402 cout << "plan\\" << endl;
403 cout << "plan/" << endl;
406 void Bot::getPlans(){
411 }while(input != "from/");
415 cout << "vote\\" << endl;
416 cout << "vote: " << _name << endl;
417 priority_queue<pair<int,string> > players;
418 for (hash_map<string,int>::const_iterator player = _winningPlans.begin();
419 player != _winningPlans.end(); ++player){
420 players.push(pair<int,string>(-player->second, player->first));
422 while (players.size() > 0){
423 const pair<int,string>& player = players.top();
424 cout << "vote: " << player.second << endl;
425 //cerr << "voted for " << player.second << " with " << player.first << " previously won plans" << endl;
428 cout << "vote/" << endl;
431 void Bot::voteResult(){
434 if (input != "nowinner:"){
435 string winner = value<string>(input);
437 ++_winningPlans[winner];