]> ruin.nu Git - icfp05.git/blobdiff - botsrc/bot.cpp
fixed bug in the graph-building
[icfp05.git] / botsrc / bot.cpp
index 203c96972673c13624667d6f996904d5cec6589b..a98a42bfb8fc5978a41c7ddcd14ad2b1cd6e0489 100644 (file)
@@ -41,14 +41,32 @@ void Bot::play(){
        getline(cin, input);
        buildGraph();
 
+       preGamePreparations();
+
        while (true){
                getline(cin, input);
                if (input == "game-over")
                        return;
+               //cerr << input << endl;
+               //cerr << "Updating world" << endl;
                updateWorld();
+               //cerr << "Done updating world" << endl;
                _type = _players[_name].type;
                _location = _players[_name].location;
                //cerr << "New turn" << endl;
+               //
+               string input;
+
+               sendInformation();
+               //cerr << "Getting information " << endl;
+               getInformation();
+
+               sendPlan();
+               getPlans();
+
+               vote();
+               voteResult();
+
                move(turn());
                //cerr << "Done with turn." << endl;
        }
@@ -90,15 +108,22 @@ void Bot::buildGraph(){
                        break;
                istringstream node(input);
                node >> input;
+               string name;
+               node >> name;
+               Intersection& inter = _intersections[name];
                node >> input;
-               Intersection& inter = _intersections[input];
-               node >> input;
-               if (input == "bank")
+               if (input == "bank"){
                        inter.type = bank;
-               else if (input == "hq")
+                       _banks[name] = 0;
+               }
+               else if (input == "hq"){
                        inter.type = hq;
-               else if (input == "robber-start")
+                       _copHq = name;
+               }
+               else if (input == "robber-start"){
                        inter.type = robber_start;
+                       _robberLocation = name;
+               }
                else
                        inter.type = ordinary;
                node >> inter.x;
@@ -130,10 +155,19 @@ void Bot::buildGraph(){
                if (type == "foot"){
                        _intersections[from].connections[to] = both;
                        Intersection& inter = _intersections[to];
-                       if (inter.connections.find(from) == inter.connections.end())
+                       hash_map<string,StreetType>::iterator conn = inter.connections.find(from);
+                       if (conn == inter.connections.end())
                                inter.connections[from] = foot;
-               }else
-                       _intersections[from].connections[to] = car;
+                       else if (conn->second == car)
+                               inter.connections[from] = both;
+               }else{
+                       Intersection& inter = _intersections[from];
+                       hash_map<string,StreetType>::iterator conn = inter.connections.find(to);
+                       if (conn == inter.connections.end())
+                               inter.connections[from] = car;
+                       else if (conn->second == foot)
+                               inter.connections[from] = both;
+               }
        }
        //cerr << "Number of streets: " << streets << endl;
        getline(cin, input);
@@ -148,6 +182,30 @@ void Bot::updateWorld(){
        getline(cin,input);
        _robbed = value<int>(input);
        //cerr << "Robbed: " << _robbed << endl;
+       //
+       getline(cin,input);
+       while (true){
+               getline(cin, input);
+               if (input == "dc/")
+                       break;
+               istringstream dirtyCop(input);
+       }
+
+       getline(cin,input);
+       while (true){
+               getline(cin, input);
+               if (input == "sc/")
+                       break;
+               istringstream controlledCop(input);
+       }
+
+       getline(cin,input);
+       while (true){
+               getline(cin, input);
+               if (input == "fac/")
+                       break;
+               istringstream falseAccusation(input);
+       }
 
        getline(cin,input);
        while (true){
@@ -177,6 +235,7 @@ void Bot::updateWorld(){
        getline(cin,input);
        _smell = value<int>(input);
 
+       _robberLocation = "";
        getline(cin,input);
        while (true){
                getline(cin, input);
@@ -190,6 +249,8 @@ void Bot::updateWorld(){
                player >> pl.location;
                player >> input;
                pl.type = _playerTypes[input];
+               if (pl.type == robber)
+                       _robberLocation = pl.location;
        }
        //cerr << "Number of players: " << _players.size() << endl;
        getline(cin, input);
@@ -197,7 +258,7 @@ void Bot::updateWorld(){
 
 void Bot::move(std::string location){
        //cerr << "Moving to: " << location << endl;
-       cout << "mov: " << location << " " << _playerTypeNames[_type] << endl;
+       cout << "mov: " << location << " " << _playerTypeNames[_type] << " " << _name << endl;
 }
 
 template<class T>
@@ -210,7 +271,7 @@ T value(std::string input){
 }
 
 std::string Bot::turn(){
-       cerr << "Using stupid stand still Bot::turn" << endl;
+       //cerr << "Using stupid stand still Bot::turn" << endl;
        return _location;
 }
 
@@ -236,12 +297,11 @@ std::list<std::string> Bot::shortestPath(const std::string& from, PlayerType typ
 
        int g = 0;
        bool hascar = type == cop_car;
+       ;
+       SPInfo* n = 0;
        while(!pq.empty()){
                node = pq.top();
                pq.pop();
-               //cerr << "Vector with size: " << w.size() << endl;
-               //cerr << "Looking at: " << node->name << endl;
-               //copy(w.begin(), w.end(), ostream_iterator<string>(cerr, " : "));
 
                g = goal(node);
                if (g < 0)
@@ -257,19 +317,24 @@ std::list<std::string> Bot::shortestPath(const std::string& from, PlayerType typ
                }
                node->settled = true;
 
-               Intersection& inter = _intersections[node->name];
-               for (hash_map<string,StreetType>::const_iterator street = inter.connections.begin();
-                               street != inter.connections.end(); ++street){
+               hash_map<string,Intersection>::const_iterator intersection = _intersections.find(node->name);
+               if (intersection == _intersections.end()){ //Sanity check, should never be true..
+                       cerr << "BUG: Could not find intersection: " << node->name << endl;
+                       continue;
+               }
+               for (hash_map<string,StreetType>::const_iterator street = intersection->second.connections.begin();
+                               street != intersection->second.connections.end(); ++street){
                        hash_map<string,SPInfo>::iterator newNode = nodes.find(street->first);
                        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;
+                                               hash_map<string,Intersection>::const_iterator newInter = _intersections.find(street->first);
+                                               if (newInter != _intersections.end()){
+                                                       hash_map<string,StreetType>::const_iterator st = newInter->second.connections.find(node->name);
+                                                       if (st != newInter->second.connections.end())
+                                                               travelStreet = st->second != foot;
+                                               }
                                        }else
                                                travelStreet = true;
                                }else
@@ -279,7 +344,6 @@ std::list<std::string> Bot::shortestPath(const std::string& from, PlayerType typ
                        }
                        if (travelStreet){
                                //cerr << "Adding street: " << street->first << endl;
-                               SPInfo* n = 0;
                                if (newNode == nodes.end()){
                                        n = &nodes[street->first];
                                        n->name = street->first;
@@ -328,3 +392,55 @@ int FindPlayer::operator()(const SPInfo* node) const{
        }
        return 0;
 }
+
+void Bot::sendInformation(){
+       cout << "inf\\" << endl;
+       cout << "inf/" << endl;
+}
+
+void Bot::getInformation(){
+       string input;
+       do{
+               getline(cin,input);
+       }while(input != "from/");
+}
+
+void Bot::sendPlan(){
+       cout << "plan\\" << endl;
+       cout << "plan/" << endl;
+}
+
+void Bot::getPlans(){
+       string input;
+       //ignore From-plan
+       do{
+               getline(cin,input);
+       }while(input != "from/");
+}
+
+void Bot::vote(){
+       cout << "vote\\" << endl;
+       cout << "vote: " << _name << endl;
+       priority_queue<pair<int,string> > players;
+       for (hash_map<string,int>::const_iterator player = _winningPlans.begin();
+                       player != _winningPlans.end(); ++player){
+               players.push(pair<int,string>(-player->second, player->first));
+       }
+       while (players.size() > 0){
+               const pair<int,string>& player = players.top();
+               cout << "vote: " << player.second << endl;
+               //cerr << "voted for " << player.second << " with " << player.first << " previously won plans" << endl;
+               players.pop();
+       }
+       cout << "vote/" << endl;
+}
+
+void Bot::voteResult(){
+       string input;
+       getline(cin,input);
+       if (input != "nowinner:"){
+               string winner = value<string>(input);
+               if (winner != _name)
+                       ++_winningPlans[winner];
+       }
+}