From 0fbd9bca7cd5cd937f306753f895cfe642dd9e6d Mon Sep 17 00:00:00 2001 From: Michael Andreen Date: Sun, 26 Jun 2005 17:07:40 +0000 Subject: [PATCH] tuning and restructuring --- bot/bot.cpp | 31 ++++++++++++++++++------------- bot/bot.h | 23 ++++++++++++----------- robber/robber.cpp | 33 ++++++++++++++++++++++++--------- 3 files changed, 54 insertions(+), 33 deletions(-) diff --git a/bot/bot.cpp b/bot/bot.cpp index 181b423..7605cf1 100644 --- a/bot/bot.cpp +++ b/bot/bot.cpp @@ -70,22 +70,11 @@ void Bot::play(){ _name = value(input); cerr << "Got name: " << _name << endl; - //robber and 5 cops - getline(cin, input); - _players[value(input)].type = robber; - getline(cin, input); - _players[value(input)].type = cop_foot; - getline(cin, input); - _players[value(input)].type = cop_foot; - getline(cin, input); - _players[value(input)].type = cop_foot; - getline(cin, input); - _players[value(input)].type = cop_foot; + getPlayers(); cerr << "Got players, building graph." << endl; getline(cin, input); buildGraph(); - getline(cin, input); while (true){ getline(cin, input); @@ -94,13 +83,27 @@ void Bot::play(){ updateWorld(); _type = _players[_name].type; _location = _players[_name].location; - getline(cin, input); cerr << "New turn" << endl; move(turn()); cerr << "Done with turn." << endl; } } +void Bot::getPlayers(){ + string input; + //robber and 5 cops + getline(cin, input); + _players[value(input)].type = robber; + getline(cin, input); + _players[value(input)].type = cop_foot; + getline(cin, input); + _players[value(input)].type = cop_foot; + getline(cin, input); + _players[value(input)].type = cop_foot; + getline(cin, input); + _players[value(input)].type = cop_foot; +} + /** nod\ eol ( nod: loc node-tag coordinate coordinate eol )* @@ -168,6 +171,7 @@ void Bot::buildGraph(){ _intersections[from].connections[to] = car; } cerr << "Number of streets: " << streets << endl; + getline(cin, input); } void Bot::updateWorld(){ @@ -223,6 +227,7 @@ void Bot::updateWorld(){ pl.type = _playerTypes[input]; } //cerr << "Number of players: " << _players.size() << endl; + getline(cin, input); } void Bot::move(std::string location){ diff --git a/bot/bot.h b/bot/bot.h index efe373f..d01ec11 100644 --- a/bot/bot.h +++ b/bot/bot.h @@ -70,17 +70,6 @@ struct SPGoal{ virtual int operator()(const SPInfo* node) const = 0; }; -struct SimpleSPGoal : public SPGoal{ - std::string _to; - SimpleSPGoal(std::string to):_to(to){}; - ~SimpleSPGoal(){} - int operator()(const SPInfo* node) const{ - if (node->name == _to) - return 1; - return 0; - } -}; - class Bot { public: Bot(const std::string& name, PlayerType type); @@ -93,6 +82,7 @@ class Bot { void updateWorld(); virtual std::string turn() = 0; void move(std::string location); + void getPlayers(); std::list shortestPath(const std::string& from, PlayerType type, const SPGoal& goal); __gnu_cxx::hash_map _intersections; @@ -109,4 +99,15 @@ class Bot { int _smell; }; +struct SimpleSPGoal : public SPGoal{ + std::string _to; + SimpleSPGoal(std::string to):_to(to){}; + ~SimpleSPGoal(){} + int operator()(const SPInfo* node) const{ + if (node->name == _to) + return 1; + return 0; + } +}; + #endif diff --git a/robber/robber.cpp b/robber/robber.cpp index 52e00de..8a11def 100644 --- a/robber/robber.cpp +++ b/robber/robber.cpp @@ -6,16 +6,17 @@ using namespace std; using namespace __gnu_cxx; -struct FindCop : SPGoal{ +struct FindPlayer : SPGoal{ int _limit; const hash_map& _players; - FindCop(const hash_map& players, int limit = 0) : _players(players), _limit(limit){} + PlayerType _type; + FindPlayer(const hash_map& players, PlayerType type, int limit = 0) : _players(players), _type(type), _limit(limit){} int 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 != robber && player->second.location == node->name){ + if (player->second.type == _type && player->second.location == node->name){ return 1; } } @@ -35,17 +36,31 @@ string Robber::turn(){ double goodness = 10; Intersection& conInter = _intersections[street->first]; - list l = shortestPath(street->first, _type, FindCop(_players, 5)); - if (l.size() > 0){ - cerr << "Cop " << l.size() << " intersections away." << endl; - if (l.size() < 3) + list closestFootCop = shortestPath(street->first, cop_foot, FindPlayer(_players, cop_foot, 5)); + list closestCarCop = shortestPath(street->first, cop_car, FindPlayer(_players, cop_car, 4)); + + unsigned int closestCop = 0; + bool copInCar = false; + + if (closestFootCop.size() < closestCarCop.size() && closestFootCop.size() > 0) + closestCop = closestFootCop.size(); + else { + closestCop = closestCarCop.size(); + copInCar = true; + } + + if (closestCop > 0){ + cerr << "Cop " << closestCop << " intersections away." << endl; + if (closestCop < 3) continue; - goodness *= 1 - 1/l.size(); + if (!copInCar) + --closestCop; + goodness *= 1 - 1/closestCop; } if (conInter.type == bank){ cerr << "FOUND A BANK" << endl; - if (l.size() > 0 && l.size() < 4) + if (closestCop > 0 && closestCop < 4) continue; else if (_banks[street->first] > 0){ cerr << "No cop close to bank" << endl; -- 2.39.2