]> ruin.nu Git - icfp05.git/blobdiff - bot/bot.cpp
some restructuring
[icfp05.git] / bot / bot.cpp
index 8450a6321c0c462f99d4b142be4de931a27d78e5..28e8004a2167997f2b5a8bbe629a486ac03f6711 100644 (file)
@@ -21,41 +21,6 @@ Bot::Bot(const string& name, PlayerType type){
        _playerTypes["robber"] = robber;
        _playerTypes["cop-foot"] = cop_foot;
        _playerTypes["cop-car"] = cop_car;
-       /*
-       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();
-
-*/
 
 }
 
@@ -70,22 +35,11 @@ void Bot::play(){
        _name = value<string>(input);
        cerr << "Got name: " << _name << endl;
 
-       //robber and 5 cops
-       getline(cin, input);
-       _players[value<string>(input)];
-       getline(cin, input);
-       _players[value<string>(input)];
-       getline(cin, input);
-       _players[value<string>(input)];
-       getline(cin, input);
-       _players[value<string>(input)];
-       getline(cin, input);
-       _players[value<string>(input)];
+       getPlayers();
 
        cerr << "Got players, building graph." << endl;
        getline(cin, input);
        buildGraph();
-       getline(cin, input);
 
        while (true){
                getline(cin, input);
@@ -94,14 +48,25 @@ void Bot::play(){
                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(){
+void Bot::getPlayers(){
+       string input;
+       //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;
 }
 
 /**
@@ -127,7 +92,15 @@ void Bot::buildGraph(){
                node >> input;
                node >> input;
                Intersection& inter = _intersections[input];
-               node >> inter.type;
+               node >> input;
+               if (input == "bank")
+                       inter.type = bank;
+               else if (input == "hq")
+                       inter.type = hq;
+               else if (input == "robber-start")
+                       inter.type = robber_start;
+               else
+                       inter.type = ordinary;
                node >> inter.x;
                node >> inter.y;
        }
@@ -163,6 +136,7 @@ void Bot::buildGraph(){
                        _intersections[from].connections[to] = car;
        }
        cerr << "Number of streets: " << streets << endl;
+       getline(cin, input);
 }
 
 void Bot::updateWorld(){
@@ -193,6 +167,11 @@ void Bot::updateWorld(){
                if (input == "ev/")
                        break;
                istringstream evidence(input);
+               int world;
+               evidence >> input;
+               evidence >> input;
+               evidence >> world;
+               _evidence[world] = input;
        }
        
        getline(cin,input);
@@ -213,6 +192,7 @@ void Bot::updateWorld(){
                pl.type = _playerTypes[input];
        }
        //cerr << "Number of players: " << _players.size() << endl;
+       getline(cin, input);
 }
 
 void Bot::move(std::string location){
@@ -234,20 +214,15 @@ std::string Bot::turn(){
        return _location;
 }
 
-struct SPInfo{
-       string name;
-       bool settled;
-       SPInfo* parent;
-       int cost;
-};
-
 struct SPInfoComp{
        bool operator()(const SPInfo* v1, const SPInfo* v2){
                return v1->cost > v2->cost;
        }
 };
 
-std::list<std::string> Bot::shortestPath(const std::string& from, const std::string& to, PlayerType type){
+std::list<std::string> Bot::shortestPath(const std::string& from, PlayerType type, const SPGoal& goal, bool reverse){
+
+       //cerr << "New shortest path from: " << from << endl;
        
        priority_queue<SPInfo*, vector<SPInfo* >, SPInfoComp > pq;
        hash_map<string,SPInfo> nodes;
@@ -259,13 +234,19 @@ std::list<std::string> Bot::shortestPath(const std::string& from, const std::str
        node->cost = 0;
        pq.push(node);
 
+       int g = 0;
+       bool hascar = type == cop_car;
        while(!pq.empty()){
                node = pq.top();
                pq.pop();
                //cerr << "Vector with size: " << w.size() << endl;
-               //cerr << "Looking at: " << w.back() << endl;
+               //cerr << "Looking at: " << node->name << endl;
                //copy(w.begin(), w.end(), ostream_iterator<string>(cerr, " : "));
-               if (node->name == to){
+
+               g = goal(node);
+               if (g < 0)
+                       break;
+               else if (g){
                        list<string> l;
                        front_insert_iterator<list<string> > ii(l);
                        do{
@@ -280,9 +261,24 @@ std::list<std::string> Bot::shortestPath(const std::string& from, const std::str
                for (hash_map<string,StreetType>::const_iterator street = inter.connections.begin();
                                street != inter.connections.end(); ++street){
                        hash_map<string,SPInfo>::iterator newNode = nodes.find(street->first);
-                       bool hascar = type == cop_car;
-                       if ( (hascar &&  (street->second == car || street->second == both)) ||
-                                       (!hascar && (street->second == foot || street->second == both))){
+                       bool travelStreet = false;
+                       if (hascar){
+                               if (reverse){
+                                       if (street->second != foot){
+                                               hash_map<string,StreetType>::const_iterator st = _intersections[street->first].connections.find(node->name);
+                                               if (st != _intersections[street->first].connections.end())
+                                                       travelStreet = st->second != foot;
+                                               else
+                                                       travelStreet = false;
+                                       }else
+                                               travelStreet = true;
+                               }else
+                                       travelStreet = street->second != foot;
+                       }else{
+                               travelStreet = street->second != car;
+                       }
+                       if (travelStreet){
+                               //cerr << "Adding street: " << street->first << endl;
                                SPInfo* n = 0;
                                if (newNode == nodes.end()){
                                        n = &nodes[street->first];
@@ -308,3 +304,27 @@ std::list<std::string> Bot::shortestPath(const std::string& from, const std::str
        
        return list<string>();
 }
+
+SimpleSPGoal::SimpleSPGoal(std::string to):_to(to){
+}
+
+int SimpleSPGoal::operator()(const SPInfo* node) const{
+       if (node->name == _to)
+               return 1;
+       return 0;
+}
+
+FindPlayer::FindPlayer(const hash_map<string, Player>& players, PlayerType type, int limit) : _players(players), _type(type), _limit(limit){
+}
+
+int FindPlayer::operator()(const SPInfo* node) const{
+       if (_limit > 0 && node->cost >= _limit)
+               return -1;
+       for(hash_map<string, Player>::const_iterator player = _players.begin();
+                       player != _players.end(); ++player){
+               if (player->second.type == _type && player->second.location == node->name){
+                       return 1;
+               }
+       }
+       return 0;
+}