]> ruin.nu Git - icfp05.git/blob - bot/bot.h
some restructuring
[icfp05.git] / bot / 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
68 struct SPGoal{
69         virtual ~SPGoal(){}
70         virtual int operator()(const SPInfo* node) const = 0;
71 };
72
73 class Bot {
74         public:
75                 Bot(const std::string& name, PlayerType type);
76                 virtual ~Bot(){};
77
78                 virtual void play();
79
80         protected:
81                 void buildGraph();
82                 void updateWorld();
83                 virtual std::string turn() = 0;
84                 void move(std::string location);
85                 void getPlayers();
86                 std::list<std::string> shortestPath(const std::string& from, PlayerType type, const SPGoal& goal, bool reverse = false);
87
88         __gnu_cxx::hash_map<std::string, Intersection> _intersections;
89         __gnu_cxx::hash_map<std::string, Player> _players;
90         __gnu_cxx::hash_map<std::string, int> _banks;
91         std::map<int, std::string> _evidence;
92         std::map<PlayerType, std::string> _playerTypeNames;
93         std::map<std::string, PlayerType> _playerTypes;
94         std::string _name;
95         PlayerType _type;
96         std::string _location;
97         int _world;
98         int _robbed;
99         int _smell;
100 };
101
102 struct SimpleSPGoal : public SPGoal{
103         std::string _to;
104         SimpleSPGoal(std::string to);
105         int operator()(const SPInfo* node) const;
106 };
107
108 struct FindPlayer : SPGoal{
109         int _limit;
110         const __gnu_cxx::hash_map<std::string, Player>& _players;
111         PlayerType _type;
112         FindPlayer(const __gnu_cxx::hash_map<std::string, Player>& players, PlayerType type, int limit = 0);
113         int operator()(const SPInfo* node) const;
114 };
115
116 #endif