]> ruin.nu Git - icfp05.git/blob - copsrc/cop.cpp
done, for now
[icfp05.git] / copsrc / cop.cpp
1 #include "cop.h"
2 #include <iostream>
3 #include <sstream>
4 #include <iterator>
5 #include <queue>
6 #include <cmath>
7
8 using namespace std;
9 using namespace __gnu_cxx;
10
11 string Cop::turn(){
12         //cerr << "New turn" << endl;
13         string input;
14
15         sendInformation();
16         //cerr << "Getting information " << endl;
17         getInformation();
18
19         sendPlan();
20         getPlans();
21
22         vote();
23
24         getline(cin,input);
25         if (input != "nowinner:"){
26                 string winner = value<string>(input);
27                 if (winner != _name)
28                         ++_winningPlans[winner];
29         }
30
31         return _location;
32 }
33
34 void Cop::preGamePreparations(){
35         //cerr << "Preparing.." << endl;
36         _copTargets[_name].first = _robberLocation;
37         _copTargets[_name].second = cop_foot;
38
39         priority_queue<pair<int,string> > banks;
40         //cerr << "Number of banks: " << endl;
41         for(hash_map<string,int>::const_iterator bank = _banks.begin();
42                                 bank != _banks.end(); ++bank){
43                 //cerr << "Finding bank: " << bank->first << endl;
44                 list<string> bankRoute  = shortestPath(_robberLocation, robber, SimpleSPGoal(bank->first));
45                 //cerr << "Pushing bank" << endl;
46                 banks.push(pair<int,string>(-bankRoute.size(), bank->first));
47         }
48         //cerr << "Done finding banks.." << endl;
49         priority_queue<pair<int,string> > banks2;
50         for (int i = 0; i < 4 && banks.size() > 0;++i){
51                 const pair<int,string>& bank = banks.top();
52                 //cerr << "Going to bank: " << bank.second << ", distance" << -bank.first << endl;
53                 list<string> bankRouteFoot = shortestPath(_copHq, cop_foot, SimpleSPGoal(bank.second));
54                 list<string> bankRouteCar = shortestPath(_copHq, cop_car, SimpleSPGoal(bank.second));
55                 banks2.push(pair<int,string>(bankRouteCar.size() - bankRouteFoot.size(), bank.second));
56                 banks.pop();
57         }
58         for (hash_map<string,Player>::const_iterator player = _players.begin();
59                         player != _players.end() && banks2.size() > 0; ++player){
60                 if (player->first == _name || player->second.type == robber)
61                         continue;
62                 const pair<int,string>& bank = banks2.top();
63                 _copTargets[player->first].first = bank.second;
64                 //cerr << "Sending: " << player->first << " to: " << bank.second;
65                 if (banks2.size() <= 2)
66                         _copTargets[player->first].second = cop_car;
67                 else
68                         _copTargets[player->first].second = cop_foot;
69                 //cerr << " on: " << _copTargets[player->first].second << endl;
70                 _winningPlans[player->first] = 0;
71                 banks2.pop();
72         }
73 }
74
75 void Cop::sendInformation(){
76         cout << "inf\\" << endl;
77         cout << "inf/" << endl;
78 }
79
80 void Cop::getInformation(){
81         string input;
82
83         if (false && _robberLocation.empty()){ //Disable for now, since it takes to long time, and does nothing 
84                 if (_smell > 0){
85                         //TODO: Calculate smell
86                 }
87                 while (true){
88                         getline(cin, input);
89                         if (input == "from/")
90                                 break;
91
92                         getline(cin, input);
93                         getline(cin, input);
94
95                         while (true){
96                                 getline(cin, input);
97                                 if (input == "inf/")
98                                         break;
99                                 istringstream inf(input);
100                                 inf >> input;
101                                 string bot;
102                                 inf >> bot;
103                                 string loc;
104                                 inf >> loc;
105                                 int world;
106                                 inf >> world;
107                                 if (world != _world)
108                                         continue;
109                                 int certainty;
110                                 inf >> certainty;
111
112                                 //TODO: Calculate possibilties
113                         }
114                 }
115         }else{
116                 do{
117                         getline(cin,input);
118                 }while(input != "from/");
119         }
120
121 }
122
123 void Cop::sendPlan(){
124         //cerr << "Planning" << endl;
125         if (_robberLocation.empty()){
126                 //Use information for plan.
127         }
128         else{
129                 //cerr << "Robber found at " << _robberLocation << endl;
130                 _copTargets[_name].first = _robberLocation;
131                 list<string> closestFootCop = shortestPath(_robberLocation, cop_foot, FindPlayer(_players, cop_foot), true);
132                 list<string> closestCarCop = shortestPath(_robberLocation, cop_car, FindPlayer(_players, cop_car, closestFootCop.size() - 1), true);
133                 PlayerType copType = cop_foot;
134                 string copLocation;
135                 if (closestCarCop.size() > 0){
136                         copType = cop_car;
137                         copLocation = closestCarCop.back();
138                 }else
139                         copLocation = closestFootCop.back();
140
141
142                 for (hash_map<string,Player>::const_iterator player = _players.begin();
143                                 player != _players.end(); ++player){
144                         if (player->first == _name || player->second.type == robber)
145                                 continue;
146                         if (player->second.type == copType && player->second.location == copLocation)
147                                 _copTargets[player->first].first = _robberLocation;
148                 }
149         }
150         //cerr << "Sending plan." << endl;
151         cout << "plan\\" << endl;
152         for(hash_map<string, pair<string, PlayerType> >::iterator player = _copTargets.begin();
153                         player != _copTargets.end(); ++player){
154                         Player& pl = _players[player->first];
155                 if (pl.type != player->second.second
156                                 && pl.location != _copHq)
157                         player->second.second = pl.type;
158                 list<string> playerRoute = shortestPath(pl.location, player->second.second, SimpleSPGoal(player->second.first));
159                 if (playerRoute.size() > 1){
160                         list<string>::iterator i = playerRoute.begin();
161                         ++i;
162                         if (*i == _robberLocation || !(playerRoute.size() == 2 && _intersections[*i].type == bank)){
163                                 if (player->first == _name)
164                                         _location = *i;
165                                 //cerr << "Sending " << player->first << " to " << *i << " by " << player->second.second << endl;
166                                 cout << "plan: " << player->first << " " << *i << " " << _playerTypeNames[player->second.second] << " " << _world+1 << endl;
167                         }
168                 }
169         }
170
171         cout << "plan/" << endl;
172 }
173
174 void Cop::getPlans(){
175         string input;
176
177
178         //ignore From-plan
179         do{
180                 getline(cin,input);
181         }while(input != "from/");
182
183
184 }
185
186 void Cop::vote(){
187         cout << "vote\\" << endl;
188         cout << "vote: " << _name << endl;
189         priority_queue<pair<int,string> > players;
190         for (hash_map<string,int>::const_iterator player = _winningPlans.begin();
191                         player != _winningPlans.end(); ++player){
192                 players.push(pair<int,string>(-player->second, player->first));
193         }
194         while (players.size() > 0){
195                 const pair<int,string>& player = players.top();
196                 cout << "vote: " << player.second << endl;
197                 //cerr << "voted for " << player.second << " with " << player.first << " previously won plans" << endl;
198                 players.pop();
199         }
200         cout << "vote/" << endl;
201 }
202
203 int main(){
204         Cop cop("harvCop");
205         cop.play();
206
207         return 0;
208 }