]> ruin.nu Git - icfp05.git/blob - bot/bot.cpp
8946d03f6feba2764407b3f0dedd7174a6c0acc6
[icfp05.git] / bot / bot.cpp
1 #include <iostream>
2 #include <sstream>
3 #include <iterator>
4 #include "bot.h"
5
6 using namespace std;
7 using namespace __gnu_cxx;
8
9 Bot::Bot(string name, string type){
10         _name = name;
11         _type = type;
12 }
13
14 void Bot::play(){
15         cout << "reg: " << _name << " " << _type << endl;
16
17         string input;
18         getline(cin, input);
19         if (input != "wsk\\")
20                 return;
21         getline(cin, input);
22         _name = value<string>(input);
23         cerr << "Got name: " << _name << endl;
24
25         //robber and 5 cops
26         getline(cin, input);
27         _players[value<string>(input)].type = "robber";
28         getline(cin, input);
29         _players[value<string>(input)].type = "cop-foot";
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
37         cerr << "Got players, building graph." << endl;
38         getline(cin, input);
39         buildGraph();
40         getline(cin, input);
41
42         while (true){
43                 getline(cin, input);
44                 if (input == "game-over")
45                         return;
46                 updateWorld();
47                 _type = _players[_name].type;
48                 _location = _players[_name].location;
49                 getline(cin, input);
50                 move(turn());
51         }
52 }
53
54 Bot::~Bot(){
55 }
56
57 /**
58                                 nod\ eol        
59                                 ( nod: loc node-tag coordinate coordinate eol )*        
60                                 nod/ eol        
61                                 edg\ eol        
62                                 ( edg: loc loc edge-type eol )* 
63                                 edg/ eol        
64 */
65 void Bot::buildGraph(){
66         string input;
67         getline(cin, input);
68         if (input != "nod\\")
69                 return;
70
71         cerr << "Getting intersections" << endl;
72         while (true){
73                 getline(cin, input);
74                 if (input == "nod/")
75                         break;
76                 istringstream node(input);
77                 node >> input;
78                 node >> input;
79                 Intersection& inter = _intersections[input];
80                 node >> inter.type;
81                 node >> inter.x;
82                 node >> inter.y;
83         }
84
85         cerr << "Number of intersections: " << _intersections.size() << endl;
86
87         getline(cin, input);
88         if (input != "edg\\")
89                 return;
90
91         cerr << "Getting streets" << endl;
92         int streets = 0;
93         while (true){
94                 getline(cin, input);
95                 if (input == "edg/")
96                         break;
97                 ++streets;
98                 istringstream street(input);
99                 string from;
100                 street >> from;
101                 string to;
102                 street >> to;
103                 string type;
104                 street >> type;
105                 if (type == "foot"){
106                         _intersections[from].connections[to] = both;
107                         Intersection& inter = _intersections[to];
108                         if (inter.connections.find(from) == inter.connections.end())
109                                 inter.connections[from] = foot;
110                 }else
111                         _intersections[from].connections[to] = car;
112         }
113         cerr << "Number of streets: " << streets << endl;
114 }
115
116 void Bot::updateWorld(){
117         string input;
118         getline(cin,input);
119         _world = value<int>(input);
120         cerr << "World: " << _world << endl;
121
122         getline(cin,input);
123         _robbed = value<int>(input);
124         cerr << "Robbed: " << _robbed << endl;
125
126         getline(cin,input);
127         while (true){
128                 getline(cin, input);
129                 if (input == "bv/")
130                         break;
131                 istringstream bank(input);
132                 bank >> input;
133                 bank >> input;
134                 bank >> _banks[input];
135         }
136         cerr << "Number of banks: " << _banks.size() << endl;
137         
138         getline(cin,input);
139         while (true){
140                 getline(cin, input);
141                 if (input == "ev/")
142                         break;
143                 istringstream evidence(input);
144         }
145         
146         getline(cin,input);
147         _smell = value<int>(input);
148
149         getline(cin,input);
150         while (true){
151                 getline(cin, input);
152                 if (input == "pl/")
153                         break;
154                 istringstream player(input);
155                 player >> input;
156                 player >> input;
157                 cerr << "Player: " << input << endl;
158                 Player& pl = _players[input];
159                 player >> pl.location;
160                 player >> pl.type;
161         }
162         cerr << "Number of players: " << _players.size() << endl;
163 }
164
165 void Bot::move(std::string location){
166         cout << "mov: " << location << " " << _type << endl;
167 }
168
169 template<class T>
170 T value(std::string input){
171         istringstream istr(input);
172         istr >> input;
173         T s;
174         istr >> s;
175         return s;
176 }
177
178 std::string Bot::turn(){
179         return _location;
180 }
181