}
}
-Bot::~Bot(){
-}
-
/**
nod\ eol
( nod: loc node-tag coordinate coordinate eol )*
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;
}
return _location;
}
-struct SPInfo{
- string name;
- bool settled;
- SPInfo* parent;
- int cost;
-};
-
struct SPInfoComp{
bool operator()(const SPInfo* v1, const SPInfo* v2){
return v1->cost > v2->cost;
}
};
-std::list<std::string> Bot::shortestPath(const std::string& from, const std::string& to, PlayerType type){
+std::list<std::string> Bot::shortestPath(const std::string& from, PlayerType type, const SPGoal& goal){
+
+ //cerr << "New shortest path from: " << from << endl;
priority_queue<SPInfo*, vector<SPInfo* >, SPInfoComp > pq;
hash_map<string,SPInfo> nodes;
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: " << w.back() << endl;
+ //cerr << "Looking at: " << node->name << endl;
//copy(w.begin(), w.end(), ostream_iterator<string>(cerr, " : "));
- if (node->name == to){
+
+ g = goal(node);
+ if (g < 0)
+ break;
+ else if (g){
list<string> l;
front_insert_iterator<list<string> > ii(l);
do{
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];
struct Intersection{
__gnu_cxx::hash_map<std::string,StreetType> connections;
- std::string type;
+ IntersectionType type;
int x;
int y;
};
template<class T>
T value(std::string input);
+struct SPInfo{
+ std::string name;
+ bool settled;
+ SPInfo* parent;
+ int cost;
+};
+
+
+struct SPGoal{
+ virtual ~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);
- virtual ~Bot();
+ virtual ~Bot(){};
virtual void play();
protected:
void buildGraph();
void updateWorld();
- virtual std::string turn();
+ virtual std::string turn() = 0;
void move(std::string location);
- std::list<std::string> shortestPath(const std::string& from, const std::string& to, PlayerType type);
+ std::list<std::string> shortestPath(const std::string& from, PlayerType type, const SPGoal& goal);
__gnu_cxx::hash_map<std::string, Intersection> _intersections;
__gnu_cxx::hash_map<std::string, Player> _players;
int _smell;
};
-
#endif
using namespace std;
using namespace __gnu_cxx;
+struct FindCop : SPGoal{
+ int _limit;
+ const hash_map<string, Player>& _players;
+ FindCop(const hash_map<string, Player>& players, int limit = 0) : _players(players), _limit(limit){}
+ int operator()(const SPInfo* node) const{
+ if (_limit > 0 && node->cost > _limit)
+ return -1;
+ for(hash_map<string, Player>::const_iterator player = _players.begin();
+ player != _players.end(); ++player){
+ if (player->second.type != robber && player->second.location == node->name){
+ return 1;
+ }
+ }
+ return 0;
+ }
+};
+
string Robber::turn(){
hash_map<string,double> streets;
Intersection& inter = _intersections[_location];
for (hash_map<string,StreetType>::const_iterator street = inter.connections.begin();
street != inter.connections.end(); ++street){
+ if (street->second == car){
+ cerr << "Discarding: " << street->first << " since car is needed" << endl;
+ continue;
+ }
double goodness = 10;
- int curx = _intersections[_location].x;
- int cury = _intersections[_location].y;
+ Intersection& conInter = _intersections[street->first];
+
+ if (conInter.type == bank){
+ cerr << "FOUND A BANK" << endl;
+ list<string> l = shortestPath(_location, _type, FindCop(_players, 3));
+ if (l.size() > 0){
+ cerr << "Cop " << l.size() << " intersections away." << endl;
+ goodness = 0;
+ }else if (_banks[street->first] > 0){
+ cerr << "No cop close to bank" << endl;
+ return street->first;
+ }
+ }
+ int curx = inter.x;
+ int cury = inter.y;
- int newx = _intersections[street->first].x;
- int newy = _intersections[street->first].y;
+ int newx = conInter.x;
+ int newy = conInter.y;
for (hash_map<string, Player>::const_iterator player = _players.begin();
player != _players.end(); ++player){
if (player->first == _name)
bank != _banks.end(); ++bank){
//cerr << "Handling bank at: " << bank->first << endl;
if (bank->second > 0){
- list<string> l = shortestPath(_location, bank->first,_type);
- if (l.size() < 1)
+ list<string> l = shortestPath(_location, _type, SimpleSPGoal(bank->first));
+ if (l.size() < 2)
continue;
list<string>::iterator i = l.begin();
- streets[*++i] += bank->second/(pow(5.0,double(l.size())));
+ if (*++i == "53-and-kimbark")
+ cerr << "We should NOT find 53-and-kimbark here" << endl;
+ streets[*i] += bank->second/(pow(5.0,double(l.size())));
}
}
#ifndef __ROBBER_H__
#define __ROBBER_H__
-#include <bot.h>
+#include "bot.h"
class Robber : public Bot {
public:
# Input
-HEADERS += robber.h
+HEADERS += robber.h ../bot/bot.h
SOURCES += robber.cpp ../bot/bot.cpp