X-Git-Url: https://ruin.nu/git/?p=icfp05.git;a=blobdiff_plain;f=bot%2Fbot.cpp;h=6a486025c77ef869315e0e4f86e971e56e91bf9c;hp=8946d03f6feba2764407b3f0dedd7174a6c0acc6;hb=c2b823beca5d24afbac2c0489837eb7ec93bfbd3;hpb=1e91b29637d981acb253bc4be7d0eeab63602790 diff --git a/bot/bot.cpp b/bot/bot.cpp index 8946d03..6a48602 100644 --- a/bot/bot.cpp +++ b/bot/bot.cpp @@ -1,18 +1,66 @@ #include #include #include +#include +#include +#include #include "bot.h" using namespace std; using namespace __gnu_cxx; -Bot::Bot(string name, string type){ + +Bot::Bot(const string& name, PlayerType type){ _name = name; _type = type; + + _playerTypeNames[robber] = "robber"; + _playerTypeNames[cop_foot] = "cop-foot"; + _playerTypeNames[cop_car] = "cop-car"; + + _playerTypes["robber"] = robber; + _playerTypes["cop-foot"] = cop_foot; + _playerTypes["cop-car"] = cop_car; + /* + priority_queue, vector >, VectComp> pq; + + vector 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(){ - cout << "reg: " << _name << " " << _type << endl; + cout << "reg: " << _name << " " << _playerTypeNames[_type] << endl; string input; getline(cin, input); @@ -24,15 +72,15 @@ void Bot::play(){ //robber and 5 cops getline(cin, input); - _players[value(input)].type = "robber"; + _players[value(input)]; getline(cin, input); - _players[value(input)].type = "cop-foot"; + _players[value(input)]; getline(cin, input); - _players[value(input)].type = "cop-foot"; + _players[value(input)]; getline(cin, input); - _players[value(input)].type = "cop-foot"; + _players[value(input)]; getline(cin, input); - _players[value(input)].type = "cop-foot"; + _players[value(input)]; cerr << "Got players, building graph." << endl; getline(cin, input); @@ -47,13 +95,12 @@ void Bot::play(){ _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 )* @@ -77,7 +124,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; } @@ -98,10 +153,12 @@ void Bot::buildGraph(){ 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]; @@ -117,11 +174,11 @@ void Bot::updateWorld(){ string input; getline(cin,input); _world = value(input); - cerr << "World: " << _world << endl; + //cerr << "World: " << _world << endl; getline(cin,input); _robbed = value(input); - cerr << "Robbed: " << _robbed << endl; + //cerr << "Robbed: " << _robbed << endl; getline(cin,input); while (true){ @@ -133,7 +190,7 @@ void Bot::updateWorld(){ bank >> input; bank >> _banks[input]; } - cerr << "Number of banks: " << _banks.size() << endl; + //cerr << "Number of banks: " << _banks.size() << endl; getline(cin,input); while (true){ @@ -141,6 +198,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); @@ -154,16 +216,18 @@ void Bot::updateWorld(){ istringstream player(input); player >> input; player >> input; - cerr << "Player: " << input << endl; + //cerr << "Player: " << input << endl; Player& pl = _players[input]; player >> pl.location; - player >> pl.type; + player >> input; + pl.type = _playerTypes[input]; } - cerr << "Number of players: " << _players.size() << endl; + //cerr << "Number of players: " << _players.size() << endl; } void Bot::move(std::string location){ - cout << "mov: " << location << " " << _type << endl; + cerr << "Moving to: " << location << endl; + cout << "mov: " << location << " " << _playerTypeNames[_type] << endl; } template @@ -176,6 +240,82 @@ T value(std::string input){ } std::string Bot::turn(){ + cerr << "Using stupid stand still Bot::turn" << endl; return _location; } +struct SPInfoComp{ + bool operator()(const SPInfo* v1, const SPInfo* v2){ + return v1->cost > v2->cost; + } +}; + +std::list Bot::shortestPath(const std::string& from, PlayerType type, const SPGoal& goal){ + + //cerr << "New shortest path from: " << from << endl; + + priority_queue, SPInfoComp > pq; + hash_map nodes; + + SPInfo* node = &nodes[from]; + node->name = from; + node->settled = false; + node->parent = 0; + node->cost = 0; + pq.push(node); + + int g = 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(cerr, " : ")); + + g = goal(node); + if (g < 0) + break; + else if (g){ + list l; + front_insert_iterator > ii(l); + do{ + *ii++ = node->name; + node = node->parent; + }while (node != 0); + return l; + } + node->settled = true; + + Intersection& inter = _intersections[node->name]; + for (hash_map::const_iterator street = inter.connections.begin(); + street != inter.connections.end(); ++street){ + hash_map::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))){ + //cerr << "Adding street: " << street->first << endl; + SPInfo* n = 0; + if (newNode == nodes.end()){ + n = &nodes[street->first]; + n->name = street->first; + n->settled = false; + n->parent = 0; + n->cost = 10000; + }else if (newNode->second.settled) + continue; + else + n = &newNode->second; + + if (n->cost > node->cost + 1){ + n->cost = node->cost + 1; + n->parent = node; + pq.push(n); + } + } + + } + + } + + return list(); +}