X-Git-Url: https://ruin.nu/git/?p=icfp05.git;a=blobdiff_plain;f=botsrc%2Fbot.cpp;h=168a0a904ee553dbacdd44ddbb4dbe9bc770ddf7;hp=10fb5447952d3e4bd58c21e80af169cb2fbf7e6e;hb=78f8a847a81dc52d71a44833bc177bd156f62c4c;hpb=f0bca5d0e69c0d1035c6b32b7d2c27e671f7060b diff --git a/botsrc/bot.cpp b/botsrc/bot.cpp index 10fb544..168a0a9 100644 --- a/botsrc/bot.cpp +++ b/botsrc/bot.cpp @@ -47,10 +47,10 @@ void Bot::play(){ getline(cin, input); if (input == "game-over") return; - cerr << input << endl; - cerr << "Updating world" << endl; + //cerr << input << endl; + //cerr << "Updating world" << endl; updateWorld(); - cerr << "Done updating world" << endl; + //cerr << "Done updating world" << endl; _type = _players[_name].type; _location = _players[_name].location; //cerr << "New turn" << endl; @@ -155,22 +155,29 @@ 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::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::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); } void Bot::updateWorld(){ - cerr << "Entered updateWorld function" << endl; string input; getline(cin,input); - cerr << input << endl; _world = value(input); - cerr << "World: " << _world << endl; + //cerr << "World: " << _world << endl; getline(cin,input); _robbed = value(input); @@ -264,127 +271,10 @@ 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; } -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, bool reverse){ - - //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; - bool hascar = type == cop_car; - ; - SPInfo* n = 0; - while(!pq.empty()){ - node = pq.top(); - pq.pop(); - - 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; - - hash_map::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::const_iterator street = intersection->second.connections.begin(); - street != intersection->second.connections.end(); ++street){ - hash_map::iterator newNode = nodes.find(street->first); - bool travelStreet = false; - if (hascar){ - if (reverse){ - if (street->second != foot){ - hash_map::const_iterator newInter = _intersections.find(street->first); - if (newInter != _intersections.end()){ - hash_map::const_iterator st = newInter->second.connections.find(node->name); - if (st != newInter->second.connections.end()) - travelStreet = st->second != foot; - } - }else - travelStreet = true; - }else - travelStreet = street->second != foot; - }else{ - travelStreet = street->second != car; - } - if (travelStreet){ - //cerr << "Adding street: " << street->first << endl; - 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(); -} - -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& 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::const_iterator player = _players.begin(); - player != _players.end(); ++player){ - if (player->second.type == _type && player->second.location == node->name){ - return 1; - } - } - return 0; -} void Bot::sendInformation(){ cout << "inf\\" << endl; @@ -437,3 +327,5 @@ void Bot::voteResult(){ ++_winningPlans[winner]; } } + +#include "shortestPath.cpp"