]> ruin.nu Git - icfp05.git/commitdiff
done, for now
authorMichael Andreen <harv@ruin.nu>
Mon, 27 Jun 2005 13:29:33 +0000 (13:29 +0000)
committerMichael Andreen <harv@ruin.nu>
Mon, 27 Jun 2005 13:29:33 +0000 (13:29 +0000)
botsrc/bot.cpp
botsrc/bot.h
copsrc/cop.cpp
copsrc/cop.h
robbersrc/robber.cpp

index 203c96972673c13624667d6f996904d5cec6589b..7635f13c7894342ad6a60ba85bbb7a2c7ce7e5e7 100644 (file)
@@ -41,6 +41,8 @@ void Bot::play(){
        getline(cin, input);
        buildGraph();
 
+       preGamePreparations();
+
        while (true){
                getline(cin, input);
                if (input == "game-over")
@@ -90,15 +92,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;
@@ -177,6 +186,7 @@ void Bot::updateWorld(){
        getline(cin,input);
        _smell = value<int>(input);
 
+       _robberLocation = "";
        getline(cin,input);
        while (true){
                getline(cin, input);
@@ -190,6 +200,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);
index 2645ac0a8226d9bd8e8001abb5c0356929f28df6..306ec7ef52fb9c64f6ab52bf62f09c0a0bdd8030 100644 (file)
@@ -99,6 +99,8 @@ class Bot {
        int _world;
        int _robbed;
        int _smell;
+       std::string _robberLocation;
+       std::string _copHq;
 };
 
 class SimpleSPGoal : public SPGoal{
index f5a7f3f3e23f369c2291be78ee3042ea85a2678f..8874574ecb98df15039e34b3a356c167e622b80f 100644 (file)
@@ -1,15 +1,19 @@
 #include "cop.h"
 #include <iostream>
+#include <sstream>
 #include <iterator>
+#include <queue>
 #include <cmath>
 
 using namespace std;
 using namespace __gnu_cxx;
 
 string Cop::turn(){
+       //cerr << "New turn" << endl;
        string input;
 
        sendInformation();
+       //cerr << "Getting information " << endl;
        getInformation();
 
        sendPlan();
@@ -17,12 +21,57 @@ string Cop::turn(){
 
        vote();
 
-       //Ignore vote
        getline(cin,input);
+       if (input != "nowinner:"){
+               string winner = value<string>(input);
+               if (winner != _name)
+                       ++_winningPlans[winner];
+       }
 
        return _location;
 }
 
+void Cop::preGamePreparations(){
+       //cerr << "Preparing.." << endl;
+       _copTargets[_name].first = _robberLocation;
+       _copTargets[_name].second = cop_foot;
+
+       priority_queue<pair<int,string> > banks;
+       //cerr << "Number of banks: " << endl;
+       for(hash_map<string,int>::const_iterator bank = _banks.begin();
+                               bank != _banks.end(); ++bank){
+               //cerr << "Finding bank: " << bank->first << endl;
+               list<string> bankRoute  = shortestPath(_robberLocation, robber, SimpleSPGoal(bank->first));
+               //cerr << "Pushing bank" << endl;
+               banks.push(pair<int,string>(-bankRoute.size(), bank->first));
+       }
+       //cerr << "Done finding banks.." << endl;
+       priority_queue<pair<int,string> > banks2;
+       for (int i = 0; i < 4 && banks.size() > 0;++i){
+               const pair<int,string>& bank = banks.top();
+               //cerr << "Going to bank: " << bank.second << ", distance" << -bank.first << endl;
+               list<string> bankRouteFoot = shortestPath(_copHq, cop_foot, SimpleSPGoal(bank.second));
+               list<string> bankRouteCar = shortestPath(_copHq, cop_car, SimpleSPGoal(bank.second));
+               banks2.push(pair<int,string>(bankRouteCar.size() - bankRouteFoot.size(), bank.second));
+               banks.pop();
+       }
+       for (hash_map<string,Player>::const_iterator player = _players.begin();
+                       player != _players.end() && banks2.size() > 0; ++player){
+               if (player->first == _name || player->second.type == robber)
+                       continue;
+               const pair<int,string>& bank = banks2.top();
+               _copTargets[player->first].first = bank.second;
+               //cerr << "Sending: " << player->first << " to: " << bank.second;
+               if (banks2.size() <= 2)
+                       _copTargets[player->first].second = cop_car;
+               else
+                       _copTargets[player->first].second = cop_foot;
+               //cerr << " on: " << _copTargets[player->first].second << endl;
+               _winningPlans[player->first] = 0;
+               banks2.pop();
+       }
+}
+
 void Cop::sendInformation(){
        cout << "inf\\" << endl;
        cout << "inf/" << endl;
@@ -31,21 +80,101 @@ void Cop::sendInformation(){
 void Cop::getInformation(){
        string input;
 
-       //ignore From-inform
-       do{
-               getline(cin,input);
-       }while(input != "from/");
+       if (false && _robberLocation.empty()){ //Disable for now, since it takes to long time, and does nothing 
+               if (_smell > 0){
+                       //TODO: Calculate smell
+               }
+               while (true){
+                       getline(cin, input);
+                       if (input == "from/")
+                               break;
+
+                       getline(cin, input);
+                       getline(cin, input);
+
+                       while (true){
+                               getline(cin, input);
+                               if (input == "inf/")
+                                       break;
+                               istringstream inf(input);
+                               inf >> input;
+                               string bot;
+                               inf >> bot;
+                               string loc;
+                               inf >> loc;
+                               int world;
+                               inf >> world;
+                               if (world != _world)
+                                       continue;
+                               int certainty;
+                               inf >> certainty;
+
+                               //TODO: Calculate possibilties
+                       }
+               }
+       }else{
+               do{
+                       getline(cin,input);
+               }while(input != "from/");
+       }
+
 }
 
 void Cop::sendPlan(){
+       //cerr << "Planning" << endl;
+       if (_robberLocation.empty()){
+               //Use information for plan.
+       }
+       else{
+               //cerr << "Robber found at " << _robberLocation << endl;
+               _copTargets[_name].first = _robberLocation;
+               list<string> closestFootCop = shortestPath(_robberLocation, cop_foot, FindPlayer(_players, cop_foot), true);
+               list<string> closestCarCop = shortestPath(_robberLocation, cop_car, FindPlayer(_players, cop_car, closestFootCop.size() - 1), true);
+               PlayerType copType = cop_foot;
+               string copLocation;
+               if (closestCarCop.size() > 0){
+                       copType = cop_car;
+                       copLocation = closestCarCop.back();
+               }else
+                       copLocation = closestFootCop.back();
+
+
+               for (hash_map<string,Player>::const_iterator player = _players.begin();
+                               player != _players.end(); ++player){
+                       if (player->first == _name || player->second.type == robber)
+                               continue;
+                       if (player->second.type == copType && player->second.location == copLocation)
+                               _copTargets[player->first].first = _robberLocation;
+               }
+       }
+       //cerr << "Sending plan." << endl;
        cout << "plan\\" << endl;
-       //cout << "plan: " << _name << endl;
+       for(hash_map<string, pair<string, PlayerType> >::iterator player = _copTargets.begin();
+                       player != _copTargets.end(); ++player){
+                       Player& pl = _players[player->first];
+               if (pl.type != player->second.second
+                               && pl.location != _copHq)
+                       player->second.second = pl.type;
+               list<string> playerRoute = shortestPath(pl.location, player->second.second, SimpleSPGoal(player->second.first));
+               if (playerRoute.size() > 1){
+                       list<string>::iterator i = playerRoute.begin();
+                       ++i;
+                       if (*i == _robberLocation || !(playerRoute.size() == 2 && _intersections[*i].type == bank)){
+                               if (player->first == _name)
+                                       _location = *i;
+                               //cerr << "Sending " << player->first << " to " << *i << " by " << player->second.second << endl;
+                               cout << "plan: " << player->first << " " << *i << " " << _playerTypeNames[player->second.second] << " " << _world+1 << endl;
+                       }
+               }
+       }
+
        cout << "plan/" << endl;
 }
 
 void Cop::getPlans(){
        string input;
 
+
        //ignore From-plan
        do{
                getline(cin,input);
@@ -57,18 +186,22 @@ void Cop::getPlans(){
 void Cop::vote(){
        cout << "vote\\" << endl;
        cout << "vote: " << _name << endl;
-       for (hash_map<string,Player>::const_iterator player = _players.begin();
-                       player != _players.end(); ++player){
-               if (player->second.type != robber && player->first != _name){
-                       cout << "vote: " << player->first << endl;
-                       cerr << "voted for " << player->first << " of type: " << player->second.type << 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;
 }
 
 int main(){
-       Cop cop("cop");
+       Cop cop("harvCop");
        cop.play();
 
        return 0;
index 42cb7787d0d13e420be84f332354e2fb556698a4..0cc2a64e42827a90942827fb8a0399088af5565a 100644 (file)
@@ -2,6 +2,7 @@
 #define __COP_H__
 
 #include <bot.h>
+#include <utility>
 
 class Cop : public Bot {
        public:
@@ -9,10 +10,16 @@ class Cop : public Bot {
                
                std::string turn();
        protected:
+               virtual void preGamePreparations();
                void sendInformation();
                void getInformation();
                void sendPlan();
                void getPlans();
                void vote();
+
+               __gnu_cxx::hash_map<std::string, std::pair<std::string, PlayerType> > _copTargets;
+               std::vector<std::string> _noRobber;
+               __gnu_cxx::hash_map<std::string, int> _maybeRobber;
+               __gnu_cxx::hash_map<std::string, int> _winningPlans;
 };
 #endif
index a9ab618d90cd144e81a4365a0f4a8b1110d097dd..50db5100faf9b561a99a9f451ca319e4b71b9f05 100644 (file)
@@ -20,12 +20,12 @@ string Robber::turn(){
                Intersection& conInter = _intersections[street->first];
 
                list<string> closestFootCop = shortestPath(street->first, cop_foot, FindPlayer(_players, cop_foot, 6), true);
-               list<string> closestCarCop = shortestPath(street->first, cop_car, FindPlayer(_players, cop_car, 5), true);
+               list<string> closestCarCop = shortestPath(street->first, cop_car, FindPlayer(_players, cop_car, closestFootCop.size() - 1), true);
 
                unsigned int closestCop = 0;
                bool copInCar = false;
 
-               if (closestCarCop.size() < closestFootCop.size() && closestCarCop.size() > 0){
+               if (closestCarCop.size() > 0){
                        closestCop = closestCarCop.size();
                        copInCar = true;
                }else 
@@ -112,7 +112,7 @@ string Robber::turn(){
 }
 
 int main(){
-       Robber robber("robber");
+       Robber robber("harv-robber");
        robber.play();
 
        return 0;