]> ruin.nu Git - icfp05.git/blob - bot/bot.cpp
2829030b3dfe4b26cc2652ffb667759892d7131e
[icfp05.git] / bot / bot.cpp
1 #include <iostream>
2 #include <sstream>
3 #include <iterator>
4 #include <queue>
5 #include <utility>
6 #include "bot.h"
7
8 using namespace std;
9 using namespace __gnu_cxx;
10
11 Bot::Bot(string name, string type){
12         _name = name;
13         _type = type;
14 }
15
16 void Bot::play(){
17         cout << "reg: " << _name << " " << _type << endl;
18
19         string input;
20         getline(cin, input);
21         if (input != "wsk\\")
22                 return;
23         getline(cin, input);
24         _name = value<string>(input);
25         cerr << "Got name: " << _name << endl;
26
27         //robber and 5 cops
28         getline(cin, input);
29         _players[value<string>(input)].type = "robber";
30         getline(cin, input);
31         _players[value<string>(input)].type = "cop-foot";
32         getline(cin, input);
33         _players[value<string>(input)].type = "cop-foot";
34         getline(cin, input);
35         _players[value<string>(input)].type = "cop-foot";
36         getline(cin, input);
37         _players[value<string>(input)].type = "cop-foot";
38
39         cerr << "Got players, building graph." << endl;
40         getline(cin, input);
41         buildGraph();
42         getline(cin, input);
43
44         while (true){
45                 getline(cin, input);
46                 if (input == "game-over")
47                         return;
48                 updateWorld();
49                 _type = _players[_name].type;
50                 _location = _players[_name].location;
51                 getline(cin, input);
52                 cerr << "New turn" << endl;
53                 move(turn());
54                 cerr << "Done with turn." << endl;
55         }
56 }
57
58 Bot::~Bot(){
59 }
60
61 /**
62                                 nod\ eol        
63                                 ( nod: loc node-tag coordinate coordinate eol )*        
64                                 nod/ eol        
65                                 edg\ eol        
66                                 ( edg: loc loc edge-type eol )* 
67                                 edg/ eol        
68 */
69 void Bot::buildGraph(){
70         string input;
71         getline(cin, input);
72         if (input != "nod\\")
73                 return;
74
75         cerr << "Getting intersections" << endl;
76         while (true){
77                 getline(cin, input);
78                 if (input == "nod/")
79                         break;
80                 istringstream node(input);
81                 node >> input;
82                 node >> input;
83                 Intersection& inter = _intersections[input];
84                 node >> inter.type;
85                 node >> inter.x;
86                 node >> inter.y;
87         }
88
89         cerr << "Number of intersections: " << _intersections.size() << endl;
90
91         getline(cin, input);
92         if (input != "edg\\")
93                 return;
94
95         cerr << "Getting streets" << endl;
96         int streets = 0;
97         while (true){
98                 getline(cin, input);
99                 if (input == "edg/")
100                         break;
101                 ++streets;
102                 istringstream street(input);
103                 string from;
104                 street >> from;
105                 street >> from;
106                 string to;
107                 street >> to;
108                 string type;
109                 street >> type;
110                 cerr << "Street between: " << from << " and " << to << " of type: " << type << endl;
111                 if (type == "foot"){
112                         _intersections[from].connections[to] = both;
113                         Intersection& inter = _intersections[to];
114                         if (inter.connections.find(from) == inter.connections.end())
115                                 inter.connections[from] = foot;
116                 }else
117                         _intersections[from].connections[to] = car;
118         }
119         cerr << "Number of streets: " << streets << endl;
120 }
121
122 void Bot::updateWorld(){
123         string input;
124         getline(cin,input);
125         _world = value<int>(input);
126         cerr << "World: " << _world << endl;
127
128         getline(cin,input);
129         _robbed = value<int>(input);
130         cerr << "Robbed: " << _robbed << endl;
131
132         getline(cin,input);
133         while (true){
134                 getline(cin, input);
135                 if (input == "bv/")
136                         break;
137                 istringstream bank(input);
138                 bank >> input;
139                 bank >> input;
140                 bank >> _banks[input];
141         }
142         cerr << "Number of banks: " << _banks.size() << endl;
143         
144         getline(cin,input);
145         while (true){
146                 getline(cin, input);
147                 if (input == "ev/")
148                         break;
149                 istringstream evidence(input);
150         }
151         
152         getline(cin,input);
153         _smell = value<int>(input);
154
155         getline(cin,input);
156         while (true){
157                 getline(cin, input);
158                 if (input == "pl/")
159                         break;
160                 istringstream player(input);
161                 player >> input;
162                 player >> input;
163                 cerr << "Player: " << input << endl;
164                 Player& pl = _players[input];
165                 player >> pl.location;
166                 player >> pl.type;
167         }
168         cerr << "Number of players: " << _players.size() << endl;
169 }
170
171 void Bot::move(std::string location){
172         cerr << "Moving to: " << location << endl;
173         cout << "mov: " << location << " " << _type << endl;
174 }
175
176 template<class T>
177 T value(std::string input){
178         istringstream istr(input);
179         istr >> input;
180         T s;
181         istr >> s;
182         return s;
183 }
184
185 std::string Bot::turn(){
186         cerr << "Using stupid stand still Bot::turn" << endl;
187         return _location;
188 }
189
190 std::vector<std::string> Bot::shortestPath(std::string from, std::string to, std::string type){
191         
192         priority_queue<vector<string>, vector<vector<string> >, greater<vector<string> > > pq;
193
194         vector<string> v;
195         v.push_back(from);
196
197         pq.push(v);
198         hash_map<string,bool> settled;
199
200         while(!pq.empty()){
201                 const vector<string>& w = pq.top();
202                 if (w.back() == to)
203                         return w;
204                 settled[w.back()] = true;
205                 Intersection& inter = _intersections[w.back()];
206                 for (hash_map<string,StreetType>::const_iterator street = inter.connections.begin();
207                                 street != inter.connections.end(); ++street){
208                         if (settled.find(street->first) == settled.end())
209                                 continue;
210                         bool car = type == "cop-car";
211                         if ( (car &&  (street->second == car || street->second == both)) ||
212                                         (!car && (street->second == foot || street->second == both))){
213                                 v = w;
214                                 v.push_back(street->first);
215                                 pq.push(v);
216                         }
217                 }
218                 
219                 pq.pop();
220         }
221         
222         return vector<string>();
223 }