X-Git-Url: https://ruin.nu/git/?p=icfp05.git;a=blobdiff_plain;f=botsrc%2Fbot.cpp;h=168a0a904ee553dbacdd44ddbb4dbe9bc770ddf7;hp=28e8004a2167997f2b5a8bbe629a486ac03f6711;hb=78f8a847a81dc52d71a44833bc177bd156f62c4c;hpb=e80a1fceda25988c576631be47a9b122126d8f53 diff --git a/botsrc/bot.cpp b/botsrc/bot.cpp index 28e8004..168a0a9 100644 --- a/botsrc/bot.cpp +++ b/botsrc/bot.cpp @@ -33,24 +33,42 @@ void Bot::play(){ return; getline(cin, input); _name = value(input); - cerr << "Got name: " << _name << endl; + //cerr << "Got name: " << _name << endl; getPlayers(); - cerr << "Got players, building graph." << endl; + //cerr << "Got players, building graph." << endl; 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; + //cerr << "New turn" << endl; + // + string input; + + sendInformation(); + //cerr << "Getting information " << endl; + getInformation(); + + sendPlan(); + getPlans(); + + vote(); + voteResult(); + move(turn()); - cerr << "Done with turn." << endl; + //cerr << "Done with turn." << endl; } } @@ -83,35 +101,42 @@ void Bot::buildGraph(){ if (input != "nod\\") return; - cerr << "Getting intersections" << endl; + //cerr << "Getting intersections" << endl; while (true){ getline(cin, input); if (input == "nod/") 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; node >> inter.y; } - cerr << "Number of intersections: " << _intersections.size() << endl; + //cerr << "Number of intersections: " << _intersections.size() << endl; getline(cin, input); if (input != "edg\\") return; - cerr << "Getting streets" << endl; + //cerr << "Getting streets" << endl; int streets = 0; while (true){ getline(cin, input); @@ -130,12 +155,21 @@ 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; + //cerr << "Number of streets: " << streets << endl; getline(cin, input); } @@ -148,6 +182,30 @@ void Bot::updateWorld(){ getline(cin,input); _robbed = value(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(input); + _robberLocation = ""; getline(cin,input); while (true){ getline(cin, input); @@ -190,14 +249,16 @@ 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); } void Bot::move(std::string location){ - cerr << "Moving to: " << location << endl; - cout << "mov: " << location << " " << _playerTypeNames[_type] << endl; + //cerr << "Moving to: " << location << endl; + cout << "mov: " << location << " " << _playerTypeNames[_type] << " " << _name << endl; } template @@ -210,121 +271,61 @@ 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; - 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 travelStreet = false; - if (hascar){ - if (reverse){ - if (street->second != foot){ - hash_map::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]; - 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); - } - } +void Bot::sendInformation(){ + cout << "inf\\" << endl; + cout << "inf/" << endl; +} - } - - } - - return list(); +void Bot::getInformation(){ + string input; + do{ + getline(cin,input); + }while(input != "from/"); } -SimpleSPGoal::SimpleSPGoal(std::string to):_to(to){ +void Bot::sendPlan(){ + cout << "plan\\" << endl; + cout << "plan/" << endl; } -int SimpleSPGoal::operator()(const SPInfo* node) const{ - if (node->name == _to) - return 1; - return 0; +void Bot::getPlans(){ + string input; + //ignore From-plan + do{ + getline(cin,input); + }while(input != "from/"); } -FindPlayer::FindPlayer(const hash_map& players, PlayerType type, int limit) : _players(players), _type(type), _limit(limit){ +void Bot::vote(){ + cout << "vote\\" << endl; + cout << "vote: " << _name << endl; + priority_queue > players; + for (hash_map::const_iterator player = _winningPlans.begin(); + player != _winningPlans.end(); ++player){ + players.push(pair(-player->second, player->first)); + } + while (players.size() > 0){ + const pair& 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 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; - } +void Bot::voteResult(){ + string input; + getline(cin,input); + if (input != "nowinner:"){ + string winner = value(input); + if (winner != _name) + ++_winningPlans[winner]; } - return 0; } + +#include "shortestPath.cpp"