]> ruin.nu Git - icfp05.git/blobdiff - bot/bot.cpp
fixed priority queue and started to implement the robber algorithm, it needs tuning...
[icfp05.git] / bot / bot.cpp
index c7af4a6f902f22e4736e5e8c8ee8d2465eb3ffe5..1c2b4dccf9adaf079abcdb2038aecf96ebd24020 100644 (file)
@@ -1,14 +1,58 @@
 #include <iostream>
 #include <sstream>
 #include <iterator>
+#include <queue>
+#include <utility>
 #include "bot.h"
 
 using namespace std;
 using namespace __gnu_cxx;
 
+struct VectComp{
+       bool operator()(const vector<string>& v1, const vector<string>& v2){
+               return v1.size() > v2.size();
+       }
+};
 Bot::Bot(string name, string type){
        _name = name;
        _type = type;
+
+       /*
+       priority_queue<vector<string>, vector<vector<string> >, VectComp> pq;
+
+       vector<string> v;
+       v.push_back("hej");
+       pq.push(v);
+       v.push_back("hå");
+       pq.push(v);
+       v.push_back("blaha");
+       pq.push(v);
+       v.clear();
+       v.push_back("hej hå");
+       pq.push(v);
+       v.clear();
+       v.push_back("hejsan");
+       pq.push(v);
+       v.push_back("what?");
+       pq.push(v);
+       v.push_back("blaha");
+       pq.push(v);
+
+       cerr << "Size: " << pq.top().size() << " " << pq.top()[0] << endl;
+       pq.pop();
+       cerr << "Size: " << pq.top().size() << " " << pq.top()[0] << endl;
+       pq.pop();
+       cerr << "Size: " << pq.top().size() << " " << pq.top()[0] << endl;
+       pq.pop();
+       cerr << "Size: " << pq.top().size() << " " << pq.top()[0] << endl;
+       pq.pop();
+       cerr << "Size: " << pq.top().size() << " " << pq.top()[0] << endl;
+       pq.pop();
+       cerr << "Size: " << pq.top().size() << " " << pq.top()[0] << endl;
+       pq.pop();
+
+*/
+
 }
 
 void Bot::play(){
@@ -19,20 +63,209 @@ void Bot::play(){
        if (input != "wsk\\")
                return;
        getline(cin, input);
-       _name = tokenizeString(input)[1];
+       _name = value<string>(input);
        cerr << "Got name: " << _name << endl;
+
+       //robber and 5 cops
+       getline(cin, input);
+       _players[value<string>(input)].type = "robber";
+       getline(cin, input);
+       _players[value<string>(input)].type = "cop-foot";
+       getline(cin, input);
+       _players[value<string>(input)].type = "cop-foot";
+       getline(cin, input);
+       _players[value<string>(input)].type = "cop-foot";
+       getline(cin, input);
+       _players[value<string>(input)].type = "cop-foot";
+
+       cerr << "Got players, building graph." << endl;
+       getline(cin, input);
        buildGraph();
-       updateWorld();
-       turn();
+       getline(cin, input);
+
+       while (true){
+               getline(cin, input);
+               if (input == "game-over")
+                       return;
+               updateWorld();
+               _type = _players[_name].type;
+               _location = _players[_name].location;
+               getline(cin, input);
+               cerr << "New turn" << endl;
+               move(turn());
+               cerr << "Done with turn." << endl;
+       }
+}
+
+Bot::~Bot(){
 }
 
+/**
+                               nod\ eol        
+                               ( nod: loc node-tag coordinate coordinate eol )*        
+                               nod/ eol        
+                               edg\ eol        
+                               ( edg: loc loc edge-type eol )* 
+                               edg/ eol        
+*/
 void Bot::buildGraph(){
+       string input;
+       getline(cin, input);
+       if (input != "nod\\")
+               return;
+
+       cerr << "Getting intersections" << endl;
+       while (true){
+               getline(cin, input);
+               if (input == "nod/")
+                       break;
+               istringstream node(input);
+               node >> input;
+               node >> input;
+               Intersection& inter = _intersections[input];
+               node >> inter.type;
+               node >> inter.x;
+               node >> inter.y;
+       }
 
+       cerr << "Number of intersections: " << _intersections.size() << endl;
+
+       getline(cin, input);
+       if (input != "edg\\")
+               return;
+
+       cerr << "Getting streets" << endl;
+       int streets = 0;
+       while (true){
+               getline(cin, input);
+               if (input == "edg/")
+                       break;
+               ++streets;
+               istringstream street(input);
+               string from;
+               street >> from;
+               street >> from;
+               string to;
+               street >> to;
+               string type;
+               street >> type;
+               cerr << "Street between: " << from << " and " << to << " of type: " << type << endl;
+               if (type == "foot"){
+                       _intersections[from].connections[to] = both;
+                       Intersection& inter = _intersections[to];
+                       if (inter.connections.find(from) == inter.connections.end())
+                               inter.connections[from] = foot;
+               }else
+                       _intersections[from].connections[to] = car;
+       }
+       cerr << "Number of streets: " << streets << endl;
+}
+
+void Bot::updateWorld(){
+       string input;
+       getline(cin,input);
+       _world = value<int>(input);
+       //cerr << "World: " << _world << endl;
+
+       getline(cin,input);
+       _robbed = value<int>(input);
+       //cerr << "Robbed: " << _robbed << endl;
+
+       getline(cin,input);
+       while (true){
+               getline(cin, input);
+               if (input == "bv/")
+                       break;
+               istringstream bank(input);
+               bank >> input;
+               bank >> input;
+               bank >> _banks[input];
+       }
+       //cerr << "Number of banks: " << _banks.size() << endl;
+       
+       getline(cin,input);
+       while (true){
+               getline(cin, input);
+               if (input == "ev/")
+                       break;
+               istringstream evidence(input);
+       }
+       
+       getline(cin,input);
+       _smell = value<int>(input);
+
+       getline(cin,input);
+       while (true){
+               getline(cin, input);
+               if (input == "pl/")
+                       break;
+               istringstream player(input);
+               player >> input;
+               player >> input;
+               //cerr << "Player: " << input << endl;
+               Player& pl = _players[input];
+               player >> pl.location;
+               player >> pl.type;
+       }
+       //cerr << "Number of players: " << _players.size() << endl;
 }
 
-std::vector<std::string> tokenizeString(std::string input){
+void Bot::move(std::string location){
+       cerr << "Moving to: " << location << endl;
+       cout << "mov: " << location << " " << _type << endl;
+}
+
+template<class T>
+T value(std::string input){
        istringstream istr(input);
-       vector<string> strings;
-       copy(istream_iterator<string>(istr), istream_iterator<string>(), back_inserter(strings));
-       return strings;
+       istr >> input;
+       T s;
+       istr >> s;
+       return s;
+}
+
+std::string Bot::turn(){
+       cerr << "Using stupid stand still Bot::turn" << endl;
+       return _location;
+}
+
+std::vector<std::string> Bot::shortestPath(std::string from, std::string to, std::string type){
+       
+       priority_queue<vector<string>, vector<vector<string> >, VectComp > pq;
+
+       vector<string> v;
+       v.push_back(from);
+
+       pq.push(v);
+       hash_map<string,bool> settled;
+
+       while(!pq.empty()){
+               const vector<string> w = pq.top();
+               pq.pop();
+               //cerr << "Vector with size: " << w.size() << endl;
+               //cerr << "Looking at: " << w.back() << endl;
+               //copy(w.begin(), w.end(), ostream_iterator<string>(cerr, " : "));
+               if (w.back() == to)
+                       return w;
+               settled[w.back()] = true;
+               Intersection& inter = _intersections[w.back()];
+               for (hash_map<string,StreetType>::const_iterator street = inter.connections.begin();
+                               street != inter.connections.end(); ++street){
+                       if (settled.find(street->first) != settled.end())
+                               continue;
+                       //cerr << "Adding: " << street->first << endl;
+                       bool hascar = type == "cop-car";
+                       if ( (hascar &&  (street->second == car || street->second == both)) ||
+                                       (!hascar && (street->second == foot || street->second == both))){
+                               
+                               v = w;
+                               v.push_back(street->first);
+                               pq.push(v);
+                       }
+
+               }
+               
+       }
+       
+       return vector<string>();
 }