-
Notifications
You must be signed in to change notification settings - Fork 0
/
AIDidicleta.cc
243 lines (226 loc) · 9.62 KB
/
AIDidicleta.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include "Player.hh"
/**
* Write the name of your player and save this file
* with the same name and .cc extension.
*/
#define PLAYER_NAME Didicleto
struct PLAYER_NAME : public Player {
/**
* Factory: returns a new instance of this class.
* Do not modify this function.
*/
static Player* factory () {
return new PLAYER_NAME;
}
/**
* Types and attributes for your player can be defined here.
*/
bool posicion_valida_dwarf(const Pos& newpos,const vector<vector<bool>>& visited){
return pos_ok(newpos) and not visited[newpos.i][newpos.j] and (cell(newpos).type==Cave or cell(newpos).type==Rock or cell(newpos).type==Outside);
}
bool posicion_valida_wizard(const Pos& newpos,const vector<vector<bool>>& visited){
return pos_ok(newpos) and not visited[newpos.i][newpos.j] and (cell(newpos).type==Cave or cell(newpos).type==Outside);
}
// bool posicion_valida_outside(const Pos& newpos,const vector<vector<bool>>& visited){
// return pos_ok(newpos) and not visited[newpos.i][newpos.j] and (cell(newpos).type==Cave or cell(newpos).type==Outside);
// }
void balrog_troll(vector<vector<bool>>& visited){
Pos bal_pos=unit(balrog_id()).pos;
for (int p=-3; p<=3;++p){
for (int k=-3; k<=3; ++k){
if (pos_ok((bal_pos.i)+p,(bal_pos.j)+k)) visited[(bal_pos.i)+p][(bal_pos.j)+k]=true;
}
}
vector<int> trolls_id=trolls();
int n=trolls_id.size();
for (int k=0; k<n; ++k){
for (int p=-2; p<=2;++p){
for (int u=-2; u<=2; ++u){
Pos troll_pos=unit(trolls_id[k]).pos;
if (pos_ok((troll_pos.i)+p,(troll_pos.j)+u)) visited[(troll_pos.i)+p][(troll_pos.j)+u]=true;
}
}
}
}
void move_dwarves(){
vector<int> D = dwarves(me());
int n=D.size();
for (int i=0; i<n; ++i){
int id=D[i];
Pos inicial=unit(id).pos;
vector<vector<bool>> visited(60,vector<bool>(60,false));
visited[inicial.i][inicial.j]=true;
balrog_troll(visited);
queue<pair<Pos,Dir>> qu;
Dir newdir=None;
qu.push(make_pair(inicial,newdir));
bool primera=true, found=false, enemy=false,team=false;
for (int j=0;j<8 and not enemy;++j){
if (pos_ok(inicial+Dir(j)) and cell(inicial+Dir(j)).id!=-1 and unit(cell(inicial+Dir(j)).id).player!=me()){
enemy=true;
newdir=Dir(j);
}
}
if (enemy){
command(id,newdir);
}
// else if (cell(inicial).type==Outside){
// while (not qu.empty() and not found){
// Pos act_pos=qu.front().first;
// Dir act_dir=qu.front().second; qu.pop();
// Cell c=cell(act_pos);
// newdir=act_dir;
// if (c.type==Cave){
// found=true;
// command(id, act_dir);
// }
// else { //mirar veins
// for (int p=0; p<8 and not found; ++p){
// Pos newpos;
// newpos=act_pos+Dir(p);
// if (posicion_valida_outside(newpos,visited)){
// if (primera) {
// if (p==0) newdir=Bottom;
// else if (p==1) newdir=BR;
// else if (p==2) newdir=Right;
// else if (p==3) newdir=RT;
// else if (p==4) newdir=Top;
// else if (p==5) newdir=TL;
// else if (p==6) newdir=Left;
// else if (p==7) newdir=LB;
// }
// visited[newpos.i][newpos.j]=true;
// qu.push(make_pair(newpos,newdir));
// }
// }
// primera=false;
// }
// }
// }
else{
while (not qu.empty() and not found){
team=false;
Pos act_pos=qu.front().first;
Dir act_dir=qu.front().second; qu.pop();
Cell c=cell(act_pos);
newdir=act_dir;
if (c.id!=-1 and unit(c.id).player!=me()) {
team=true;
}
if (c.id==-1 and not team and (c.treasure or (c.owner!=me() and c.type==Cave))){
found=true;
command(id, act_dir);
}
else { //mirar veins
for (int p=0; p<8 and not found; ++p){
Pos newpos;
newpos=act_pos+Dir(p);
if (posicion_valida_dwarf(newpos,visited)){
if (primera) {
if (p==0) newdir=Bottom;
else if (p==1) newdir=BR;
else if (p==2) newdir=Right;
else if (p==3) newdir=RT;
else if (p==4) newdir=Top;
else if (p==5) newdir=TL;
else if (p==6) newdir=Left;
else if (p==7) newdir=LB;
}
visited[newpos.i][newpos.j]=true;
qu.push(make_pair(newpos,newdir));
}
}
primera=false;
}
}
}
}
}
void move_wizards(){
vector<int> W = wizards(me());
int n=W.size();
for (int r=0; r<n; ++r){
int id=W[r];
Pos inicial=unit(id).pos;
vector<vector<bool>> visited(60,vector<bool>(60,false));
visited[inicial.i][inicial.j]=true;
balrog_troll(visited);
queue<pair<Pos,Dir>> qu;
Dir newdir=None;
bool primera=true;
qu.push(make_pair(inicial,newdir));
bool found=false;
// if (cell(inicial).type==Outside){
// while (not qu.empty() and not found){
// Pos act_pos=qu.front().first;
// Dir act_dir=qu.front().second; qu.pop();
// Cell c=cell(act_pos);
// newdir=act_dir;
// if (c.type==Cave) {
// found=true;
// command(id,newdir);
// }
// else { //mirar veins
// for (int p=0; p<4 and not found; ++p){
// Pos newpos;
// newpos=act_pos+Dir(p*2);
// if (posicion_valida_outside(newpos,visited)){
// if (primera) {
// if (p==0) newdir=Bottom;
// else if (p==1) newdir=Right;
// else if (p==2) newdir=Top;
// else if (p==3) newdir=Left;
// }
// visited[newpos.i][newpos.j]=true;
// qu.push(make_pair(newpos,newdir));
// }
// }
// primera=false;
// }
// }
// }
// else {
while (not qu.empty() and not found){
bool enemigo=false;
Pos act_pos=qu.front().first;
Dir act_dir=qu.front().second; qu.pop();
Cell c=cell(act_pos);
newdir=act_dir;
if (c.id!=-1 and (unit(c.id).player!=me() or (unit(c.id).player==me() and unit(c.id).type==Wizard))) enemigo=true;
if (c.id!=-1 and not enemigo and unit(c.id).player==me() and unit(c.id).type == Dwarf) {
found=true;
command(id,newdir);
}
else { //mirar veins
for (int p=0; p<4 and not found; ++p){
Pos newpos;
newpos=act_pos+Dir(p*2);
if (posicion_valida_wizard(newpos,visited)){
if (primera) {
if (p==0) newdir=Bottom;
else if (p==1) newdir=Right;
else if (p==2) newdir=Top;
else if (p==3) newdir=Left;
}
visited[newpos.i][newpos.j]=true;
qu.push(make_pair(newpos,newdir));
}
}
primera=false;
}
}
// }
}
}
/**
* Play method, invoked once per each round.
*/
virtual void play () {
move_dwarves();
move_wizards();
}
};
/**
* Do not modify the following line.
*/
RegisterPlayer(PLAYER_NAME);