]> ruin.nu Git - icfp05.git/blob - botsrc/bot.h
initial implementation of new robber
[icfp05.git] / botsrc / bot.h
1 #ifndef __BOT_H__
2 #define __BOT_H__
3
4 #include <ext/hash_map>
5 #include <list>
6 #include <string>
7 #include <locale>
8 #include <map>
9 // These are needed to be able to use std::string as key in a hash_map.
10 namespace __gnu_cxx {
11         template< typename CharT, typename Traits, typename Alloc >
12                 struct hash< std::basic_string<CharT, Traits, Alloc> > {
13                         size_t operator()(const std::basic_string<CharT, Traits, Alloc>& s) const {
14
15                                 const std::collate<CharT>& c = std::use_facet< std::collate<CharT> >(std::locale());
16
17                                 return c.hash(s.c_str(), s.c_str() + s.size());
18
19                         }
20
21                 };
22
23         template< typename CharT, typename Traits, typename Alloc >
24                 struct hash< const std::basic_string<CharT, Traits, Alloc> > { //yes you need this version aswell!
25
26                         size_t operator()(const std::basic_string<CharT, Traits, Alloc>& s) const {
27
28                                 const std::collate<CharT>& c = std::use_facet< std::collate<CharT> >(std::locale());
29
30                                 return c.hash(s.c_str(), s.c_str() + s.size());
31                         }
32
33                 };
34 };
35
36 enum StreetType{foot, car, both};
37 enum PlayerType{robber, cop_foot, cop_car};
38 enum IntersectionType{hq, bank, robber_start, ordinary};
39
40 struct Intersection{
41         __gnu_cxx::hash_map<std::string,StreetType> connections;
42         IntersectionType type;
43         int x;
44         int y;
45 };
46
47 struct Player{
48         PlayerType type;
49         std::string location;
50 };
51
52 struct Bank{
53         std::string location;
54         int value;
55 };
56
57 template<class T>
58 T value(std::string input);
59
60 struct SPInfo{
61         std::string name;
62         bool settled;
63         SPInfo* parent;
64         int cost;
65 };
66
67 class Bot {
68         public:
69                 Bot(const std::string& name, PlayerType type);
70                 virtual ~Bot(){};
71
72                 virtual void play();
73
74         protected:
75                 void buildGraph();
76                 void updateWorld();
77                 virtual std::string turn();
78                 virtual void preGamePreparations(){};
79                 virtual void move(std::string location);
80                 virtual void sendInformation();
81                 virtual void getInformation();
82                 virtual void sendPlan();
83                 virtual void getPlans();
84                 virtual void vote();
85                 virtual void voteResult();
86                 void getPlayers();
87                 template<class Goal, class Cost>
88                 std::list<std::string> shortestPath(const std::string& from, PlayerType type, const Goal& goal, const Cost& cost, bool reverse = false) const;
89                 template<class Goal>
90                 std::list<std::string> shortestPath(const std::string& from, PlayerType type, const Goal& goal, bool reverse = false) const;
91
92                 __gnu_cxx::hash_map<std::string, Intersection> _intersections;
93                 __gnu_cxx::hash_map<std::string, Player> _players;
94                 __gnu_cxx::hash_map<std::string, int> _banks;
95                 std::map<int, std::string> _evidence;
96                 std::map<PlayerType, std::string> _playerTypeNames;
97                 std::map<std::string, PlayerType> _playerTypes;
98                 std::string _name;
99                 PlayerType _type;
100                 std::string _location;
101                 int _world;
102                 int _robbed;
103                 int _smell;
104                 std::string _robberLocation;
105                 std::string _copHq;
106                 __gnu_cxx::hash_map<std::string, int> _winningPlans;
107 };
108
109
110 class SimpleSPGoal{
111         std::string _to;
112         int _limit;
113         public:
114         SimpleSPGoal(const std::string& to, int limit = 0):_to(to), _limit(limit){
115         }
116         inline int operator()(const SPInfo* node) const{
117                 if (_limit > 0 && node->cost >= _limit)
118                         return -1;
119                 if (node->name == _to)
120                         return 1;
121                 return 0;
122         }
123
124 };
125
126 class FindPlayer{
127         int _limit;
128         const __gnu_cxx::hash_map<std::string, Player>& _players;
129         PlayerType _type;
130         public:
131         FindPlayer(const __gnu_cxx::hash_map<std::string, Player>& players, PlayerType type, int limit = 0): _players(players), _type(type), _limit(limit){
132         }
133         inline int operator()(const SPInfo* node) const{
134                 if (_limit > 0 && node->cost >= _limit)
135                         return -1;
136                 for(__gnu_cxx::hash_map<std::string, Player>::const_iterator player = _players.begin();
137                                 player != _players.end(); ++player){
138                         if (player->second.type == _type && player->second.location == node->name){
139                                 return 1;
140                         }
141                 }
142                 return 0;
143         }
144
145 };
146
147 struct SPInfoComp{
148         inline bool operator()(const SPInfo* v1, const SPInfo* v2){
149                 return v1->cost > v2->cost;
150         }
151 };
152
153
154 #endif