import java io import java net import java util import java awt Point

  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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.Point;
/*****/
class Player
{
public int m_pid;
public Point m_pos;
public int m_bombs;
public int m_str;
public int m_speed;
public boolean m_infict;
public Player(int _pid, Point _pos, int _bombs, int _bombsStrength, int _speed, boolean _isInfected) {
m_pid = _pid; m_pos = _pos; m_bombs = _bombs; m_str = _bombsStrength; m_speed = _speed; m_infict = _isInfected;
}
public Player()
{}
}
/*****/
class Bomb
{
public Point m_pos;
public int m_radius;
public int m_timer;
public Bomb (Point _pos, int _damage, int _time) {
m_pos = _pos;
m_radius = _damage;
m_timer = _time;
}
}
/*****/
public class BotEnv
{
private SocketMaster m_SM;
private AlgoBrain m_AB;
private Parser m_P;
// connection parameters
private String m_host;
private int m_port;
// World's state
public char[][] m_map;
public Player[] m_enemies;
public Bomb[] m_bombs;
public int m_scale;
public int m_tick;
// Bot's state
Player m_bot;
public BotEnv(String[] _args) {
//m_host = _args[0];
//m_port = Integer.parseInt(_args[1]);
m_SM = new SocketMaster(this, "localhost", 20016);
m_P = new Parser(this);
m_AB = new AlgoBrain(this);
m_bot = new Player(-1, new Point(), 0, 0, 0, false);
}
// This function can be killed maybe if someone don't like it
public AlgoBrain getAlgoBrainInterface()
{
return m_AB;
}
public String proccessComand (String _cmd) {
m_P.parse(_cmd);
return m_AB.processTactics();
}
public static void main(String[] _args) {
new BotEnv(_args).m_SM.run();
}
}
/*****/
class SocketMaster
{
private BotEnv m_BE;
private Socket m_sock;
private Scanner m_in;
private PrintWriter m_out;
public SocketMaster (BotEnv _bptr, String _host, int _port) {
m_BE = _bptr;
try {
m_sock = new Socket(_host, _port);
m_in = new Scanner(m_sock.getInputStream());
m_out = new PrintWriter(m_sock.getOutputStream());
} catch (Exception _e) {
_e.printStackTrace();
}
}
public void run() {
m_out.println(INIT_STRING);
m_out.flush();
int idx;
boolean started = false;
String startLex = "PID";
// Here we going to get all fucking shit, from task details to input stream...
String get, cmd = "", put;
while (m_in.hasNext()) {
get = m_in.next();
if (!started) {
idx = get.indexOf(startLex);
if (idx != -1) {
cmd = get.substring(idx);
started = true;
}
}
else {
idx = get.indexOf(';');
if (idx != -1) {
cmd += get.substring(0, idx);
System.out.println(cmd);
// IMPORTANT! Here we will instantly give out solution response!!!
m_out.print(m_BE.proccessComand(cmd)+";");
m_out.flush();
cmd = get.substring(idx+1) + " ";
}
else cmd += get + " ";
}
}
}
final String INIT_STRING = "launch heratorz CFGSrUPQPKgtmDcKuOhgCSy#CFGwjQJiWQQUViJiymrQoGY#CFGUfimkSUQhcMgGMqlWQYs#CFGWYiZmCiYpYaYWZuBspkg#CFGAEeaubIIRkDOlIxGmyPM#CFGYLEQYicsFWmjwKmXaOmC#CFGufAMuyIOhKxKaSOxoxUN#CFGowmRWjWSWxcZiqlGeYkF#CFGUpogIViMtiOkulA@IYoZ#CFGQOwAkDwyVAZCoKTqFuyJ#CFGMSYfClQgVEaUuinytily#CFGyNQRgQmanQxKmUAXEepm;";
}
/*****/
class AlgoBrain
{
private BotEnv m_BE;
private Point m_way[];
private int m_pos = 0;//just for test
private boolean m_init = false; //just for test
public AlgoBrain(BotEnv _bptr) {
m_BE = _bptr;
m_way = new Point[20];
}
public void reInit()
{
m_init = false;
m_pos = 0;
}
public void init()
{
if (!m_init)
{
m_init = true;
m_way[0] = (Point) m_BE.m_bot.m_pos.clone();
int i = 1;
Random rand = new Random();
while(i<m_way.length)
{
// There is a dumb code here, cause we have not much time
int a = 2*Math.abs((rand.nextInt()%2));
m_way[i] =(Point) m_way[i-1].clone();
m_way[i].x = a;
m_way[i].y =Math.abs(a);
++i;
}
}
}
public String proccessWay()
{
String cmd = null;
if (m_pos < m_way.length-1)
{
++m_pos;
}
else
{
cmd = "s;";
return cmd;
}
if ((m_way[m_pos].x - m_way[m_pos-1].x)>0)
{
cmd = "r;";
return cmd;
}
else if ((m_way[m_pos].x - m_way[m_pos-1].x)<0)
{
cmd = "l;";
return cmd;
}
else if ((m_way[m_pos].y - m_way[m_pos-1].y)>0)
{
cmd = "d;";
return cmd;
}
else
{
cmd = "u;";
return cmd;
}
}
public String processTactics() {
/*
switch (new Random().nextInt() % 4) {
case 0 : return "l";
case 1 : return "r";
case 2 : return "u";
case 3 : return "d";
}*/
if (!m_init) //just for test
{
return "";
}
else
{
return proccessWay();
}
}
}
/*****/
class Parser
{
private BotEnv m_BE;
public Parser(BotEnv _bptr) {
m_BE = _bptr;
}
public void parse (String _cmd) {
switch(_cmd.charAt(0)) {
case 'P': playerIdComand(_cmd); break;
case 'S': startComand(_cmd); break;
case 'T': timerTick(_cmd); break;
case 'R': roundEnd(_cmd); break;
case 'G': gameEnd(_cmd); break;
}
}
private void playerIdComand (String _cmd) {
// At this stage map is ignored
int pid = 0, idx = 3;
while (_cmd.charAt(idx) >= '0' && _cmd.charAt(idx) <= '9')
pid = pid * 10 + _cmd.charAt(idx++) - '0';
m_BE.m_bot.m_pid = pid;
}
private void startComand (String _cmd) {
int idx = _cmd.indexOf('&') + 1;
int scale = 0;
while (_cmd.charAt(idx) >= '0' && _cmd.charAt(idx) <= '9')
scale = scale * 10 + _cmd.charAt(idx++) - '0';
String[] map = _cmd.substring(idx).trim().split(" ");
int h = map.length, w = map[0].length();
m_BE.m_map = new char[h][w];
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
m_BE.m_map[i][j] = map[i].charAt(j);
m_BE.m_scale = scale;
}
private void timerTick (String _cmd) {
String[] parts = _cmd.trim().split("&");
for (int p = 0; p < parts.length; p++)
if (p == 0) parseTickTimePart(parts[p]);
else if (p == 1) parseTickSapkaPart(parts[p]);
else if (p == 2) parseTickChangesPart(parts[p]);
}
private void parseTickTimePart (String _cmd) {
m_BE.m_tick = Integer.parseInt(_cmd.substring(1));
}
private void parseTickSapkaPart (String _cmd) {
int pid;//, x = -1, y = -1, bombs = -1, strength = -1, speed = -1;
boolean inficted = false;
Player link;
String[] pl = _cmd.trim().split(",");
String[] cur;
if (m_BE.m_enemies == null) {
m_BE.m_enemies = new Player[pl.length-1];
for (int i = 0; i < pl.length-1; i++)
m_BE.m_enemies[i] = new Player();
}
int k = 0;
for (int i = 0; i < pl.length; i++) {
cur = pl[i].trim().split(" ");
pid = Integer.parseInt(cur[0].substring(1));
link = (pid == m_BE.m_bot.m_pid) ? m_BE.m_bot : m_BE.m_enemies[k++];
if (cur.length == 2) link.m_pos = null;
else {
link.m_pos = new Point(Integer.parseInt(cur[1]), Integer.parseInt(cur[2]));
link.m_bombs = Integer.parseInt(cur[3]);
link.m_str = Integer.parseInt(cur[4]);
link.m_speed = Integer.parseInt(cur[5]);
link.m_infict = cur.length == 7;
}
}
m_BE.getAlgoBrainInterface().init(); // just for tests
}
private void parseTickChangesPart (String _cmd) {
if (_cmd.length() == 0) return;
String[] chp = _cmd.split(",");
String[] cur;
boolean add = false;
char type;
for (int i = 0; i < chp.length; i++) {
cur = chp[i].trim().split(" ");
for (int j = 0; j < cur.length; j++) {
add = cur[0].charAt(0) == '+';
type = cur[0].charAt(1);
switch (type) {
// Bomb
case '*':
{
} break;
// Explode
case '#':
{
} break;
// Destructable wall
case 'w':
{
} break;
// Indestructable wall
case 'X':
{
} break;
// Bonuses
default:
{
} break;
}
}
}
}
private void roundEnd(String _cmd) {
m_BE.m_enemies = null;
m_BE.m_bombs = null;
m_BE.getAlgoBrainInterface().reInit();
}
private void gameEnd(String _cmd) {
// Nothing to do...
}
}