From 7ea4f9f073009ba9a6bbf2fec398b0742f798658 Mon Sep 17 00:00:00 2001 From: Michael Andreen Date: Sat, 9 Jul 2005 22:48:11 +0000 Subject: [PATCH] added limit to SimpleSPGoal and ensured that two additional cops chases the smell --- botsrc/bot.cpp | 4 +++- botsrc/bot.h | 3 ++- copsrc/cop.cpp | 48 +++++++++++++++++++++++++++--------------------- 3 files changed, 32 insertions(+), 23 deletions(-) diff --git a/botsrc/bot.cpp b/botsrc/bot.cpp index a98a42b..411d933 100644 --- a/botsrc/bot.cpp +++ b/botsrc/bot.cpp @@ -369,10 +369,12 @@ std::list Bot::shortestPath(const std::string& from, PlayerType typ return list(); } -SimpleSPGoal::SimpleSPGoal(std::string to):_to(to){ +SimpleSPGoal::SimpleSPGoal(const std::string& to, int limit):_to(to), _limit(limit){ } int SimpleSPGoal::operator()(const SPInfo* node) const{ + if (_limit > 0 && node->cost >= _limit) + return -1; if (node->name == _to) return 1; return 0; diff --git a/botsrc/bot.h b/botsrc/bot.h index 84f9de5..aab4e83 100644 --- a/botsrc/bot.h +++ b/botsrc/bot.h @@ -112,8 +112,9 @@ class Bot { class SimpleSPGoal : public SPGoal{ std::string _to; + int _limit; public: - SimpleSPGoal(std::string to); + SimpleSPGoal(const std::string& to, int limit = 0); int operator()(const SPInfo* node) const; }; diff --git a/copsrc/cop.cpp b/copsrc/cop.cpp index dd0b1d1..2a80022 100644 --- a/copsrc/cop.cpp +++ b/copsrc/cop.cpp @@ -205,7 +205,7 @@ int Cop::maybeAtNeighbor(const Intersection& intersection){ void Cop::sendPlan(){ //cerr << "Planning" << endl; - string secondLocation = _robberLocation; + list locations; if (_robberLocation.empty()){ priority_queue > positions; for (hash_map::iterator location = _maybeRobber.front().begin(); @@ -218,35 +218,41 @@ void Cop::sendPlan(){ _robberLocation = positions.top().second; cerr << "Sending myself to: " << _robberLocation << ", probability of robber: " << positions.top().first << endl; } - if (positions.size() > 1){ + for (int i = 0; i < 2 && positions.size() > 1; ++i ){ positions.pop(); - secondLocation = positions.top().second; - cerr << "Sending another cop to: " << secondLocation << ", probability of robber: " << positions.top().first << endl; + locations.push_back(positions.top().second); + cerr << "Sending another cop to: " << locations.back() << ", probability of robber: " << positions.top().first << endl; } + }else{ + locations.push_back(_robberLocation); } if (!_robberLocation.empty()){ //cerr << "Robber found at " << _robberLocation << endl; _copTargets[_name].first = _robberLocation; - - if (!secondLocation.empty()){ - list closestFootCop = shortestPath(_robberLocation, cop_foot, FindPlayer(_players, cop_foot), true); - list 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(); - - + + vector busyCops; + busyCops.push_back(_name); + while (locations.size() > 0){ + string cop; + int dist = 1000000; for (hash_map::const_iterator player = _players.begin(); - player != _players.end(); ++player){ - if (player->first == _name || player->second.type == robber) + player != _players.end();++player){ + if (player->second.type == robber || find(busyCops.begin(), busyCops.end(), player->first) != busyCops.end()) continue; - if (player->second.type == copType && player->second.location == copLocation) - _copTargets[player->first].first = _robberLocation; + list l = shortestPath(player->second.location, player->second.type,SimpleSPGoal(locations.front(), dist-1)); + if (l.size() > 0 && l.size() < dist){ + dist = l.size(); + cop = player->first; + if (dist == 1) + break; + } + } + if (!cop.empty()){ + cerr << "Sending " << cop << " to " << locations.front() << endl; + _copTargets[cop].first = locations.front(); + busyCops.push_back(cop); } + locations.pop_front(); } } //cerr << "Sending plan." << endl; -- 2.39.2