]> ruin.nu Git - icfp05.git/blob - bot/bot.cpp
fixed priority queue and started to implement the robber algorithm, it needs tuning...
[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 struct VectComp{
12         bool operator()(const vector<string>& v1, const vector<string>& v2){
13                 return v1.size() > v2.size();
14         }
15 };
16 Bot::Bot(string name, string type){
17         _name = name;
18         _type = type;
19
20         /*
21         priority_queue<vector<string>, vector<vector<string> >, VectComp> pq;
22
23         vector<string> v;
24         v.push_back("hej");
25         pq.push(v);
26         v.push_back("hå");
27         pq.push(v);
28         v.push_back("blaha");
29         pq.push(v);
30         v.clear();
31         v.push_back("hej hå");
32         pq.push(v);
33         v.clear();
34         v.push_back("hejsan");
35         pq.push(v);
36         v.push_back("what?");
37         pq.push(v);
38         v.push_back("blaha");
39         pq.push(v);
40
41         cerr << "Size: " << pq.top().size() << " " << pq.top()[0] << endl;
42         pq.pop();
43         cerr << "Size: " << pq.top().size() << " " << pq.top()[0] << endl;
44         pq.pop();
45         cerr << "Size: " << pq.top().size() << " " << pq.top()[0] << endl;
46         pq.pop();
47         cerr << "Size: " << pq.top().size() << " " << pq.top()[0] << endl;
48         pq.pop();
49         cerr << "Size: " << pq.top().size() << " " << pq.top()[0] << endl;
50         pq.pop();
51         cerr << "Size: " << pq.top().size() << " " << pq.top()[0] << endl;
52         pq.pop();
53
54 */
55
56 }
57
58 void Bot::play(){
59         cout << "reg: " << _name << " " << _type << endl;
60
61         string input;
62         getline(cin, input);
63         if (input != "wsk\\")
64                 return;
65         getline(cin, input);
66         _name = value<string>(input);
67         cerr << "Got name: " << _name << endl;
68
69         //robber and 5 cops
70         getline(cin, input);
71         _players[value<string>(input)].type = "robber";
72         getline(cin, input);
73         _players[value<string>(input)].type = "cop-foot";
74         getline(cin, input);
75         _players[value<string>(input)].type = "cop-foot";
76         getline(cin, input);
77         _players[value<string>(input)].type = "cop-foot";
78         getline(cin, input);
79         _players[value<string>(input)].type = "cop-foot";
80
81         cerr << "Got players, building graph." << endl;
82         getline(cin, input);
83         buildGraph();
84         getline(cin, input);
85
86         while (true){
87                 getline(cin, input);
88                 if (input == "game-over")
89                         return;
90                 updateWorld();
91                 _type = _players[_name].type;
92                 _location = _players[_name].location;
93                 getline(cin, input);
94                 cerr << "New turn" << endl;
95                 move(turn());
96                 cerr << "Done with turn." << endl;
97         }
98 }
99
100 Bot::~Bot(){
101 }
102
103 /**
104                                 nod\ eol        
105                                 ( nod: loc node-tag coordinate coordinate eol )*        
106                                 nod/ eol        
107                                 edg\ eol        
108                                 ( edg: loc loc edge-type eol )* 
109                                 edg/ eol        
110 */
111 void Bot::buildGraph(){
112         string input;
113         getline(cin, input);
114         if (input != "nod\\")
115                 return;
116
117         cerr << "Getting intersections" << endl;
118         while (true){
119                 getline(cin, input);
120                 if (input == "nod/")
121                         break;
122                 istringstream node(input);
123                 node >> input;
124                 node >> input;
125                 Intersection& inter = _intersections[input];
126                 node >> inter.type;
127                 node >> inter.x;
128                 node >> inter.y;
129         }
130
131         cerr << "Number of intersections: " << _intersections.size() << endl;
132
133         getline(cin, input);
134         if (input != "edg\\")
135                 return;
136
137         cerr << "Getting streets" << endl;
138         int streets = 0;
139         while (true){
140                 getline(cin, input);
141                 if (input == "edg/")
142                         break;
143                 ++streets;
144                 istringstream street(input);
145                 string from;
146                 street >> from;
147                 street >> from;
148                 string to;
149                 street >> to;
150                 string type;
151                 street >> type;
152                 cerr << "Street between: " << from << " and " << to << " of type: " << type << endl;
153                 if (type == "foot"){
154                         _intersections[from].connections[to] = both;
155                         Intersection& inter = _intersections[to];
156                         if (inter.connections.find(from) == inter.connections.end())
157                                 inter.connections[from] = foot;
158                 }else
159                         _intersections[from].connections[to] = car;
160         }
161         cerr << "Number of streets: " << streets << endl;
162 }
163
164 void Bot::updateWorld(){
165         string input;
166         getline(cin,input);
167         _world = value<int>(input);
168         //cerr << "World: " << _world << endl;
169
170         getline(cin,input);
171         _robbed = value<int>(input);
172         //cerr << "Robbed: " << _robbed << endl;
173
174         getline(cin,input);
175         while (true){
176                 getline(cin, input);
177                 if (input == "bv/")
178                         break;
179                 istringstream bank(input);
180                 bank >> input;
181                 bank >> input;
182                 bank >> _banks[input];
183         }
184         //cerr << "Number of banks: " << _banks.size() << endl;
185         
186         getline(cin,input);
187         while (true){
188                 getline(cin, input);
189                 if (input == "ev/")
190                         break;
191                 istringstream evidence(input);
192         }
193         
194         getline(cin,input);
195         _smell = value<int>(input);
196
197         getline(cin,input);
198         while (true){
199                 getline(cin, input);
200                 if (input == "pl/")
201                         break;
202                 istringstream player(input);
203                 player >> input;
204                 player >> input;
205                 //cerr << "Player: " << input << endl;
206                 Player& pl = _players[input];
207                 player >> pl.location;
208                 player >> pl.type;
209         }
210         //cerr << "Number of players: " << _players.size() << endl;
211 }
212
213 void Bot::move(std::string location){
214         cerr << "Moving to: " << location << endl;
215         cout << "mov: " << location << " " << _type << endl;
216 }
217
218 template<class T>
219 T value(std::string input){
220         istringstream istr(input);
221         istr >> input;
222         T s;
223         istr >> s;
224         return s;
225 }
226
227 std::string Bot::turn(){
228         cerr << "Using stupid stand still Bot::turn" << endl;
229         return _location;
230 }
231
232 std::vector<std::string> Bot::shortestPath(std::string from, std::string to, std::string type){
233         
234         priority_queue<vector<string>, vector<vector<string> >, VectComp > pq;
235
236         vector<string> v;
237         v.push_back(from);
238
239         pq.push(v);
240         hash_map<string,bool> settled;
241
242         while(!pq.empty()){
243                 const vector<string> w = pq.top();
244                 pq.pop();
245                 //cerr << "Vector with size: " << w.size() << endl;
246                 //cerr << "Looking at: " << w.back() << endl;
247                 //copy(w.begin(), w.end(), ostream_iterator<string>(cerr, " : "));
248                 if (w.back() == to)
249                         return w;
250                 settled[w.back()] = true;
251                 Intersection& inter = _intersections[w.back()];
252                 for (hash_map<string,StreetType>::const_iterator street = inter.connections.begin();
253                                 street != inter.connections.end(); ++street){
254                         if (settled.find(street->first) != settled.end())
255                                 continue;
256                         //cerr << "Adding: " << street->first << endl;
257                         bool hascar = type == "cop-car";
258                         if ( (hascar &&  (street->second == car || street->second == both)) ||
259                                         (!hascar && (street->second == foot || street->second == both))){
260                                 
261                                 v = w;
262                                 v.push_back(street->first);
263                                 pq.push(v);
264                         }
265
266                 }
267                 
268         }
269         
270         return vector<string>();
271 }