-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhypersonic.cs
595 lines (524 loc) · 20.2 KB
/
hypersonic.cs
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
using System;
using System.Linq;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;
static class Global
{
public const int HEIGHT = 11;
public const int WIDTH = 13;
public const char EXPLOSION = 'H';
public const char REACHABLE = 'R';
public const char WALL = 'X';
public const char BOX0 = '0';
public const char BOX1 = '1';
public const char BOX2 = '2';
public const char EMPTY_CELL = '.';
public const char POWERUP_RANGE = '5';
public const char POWERUP_EXTRA = '6';
public const char BOMB = 'B';
}
class Game
{
static void Main(string[] args)
{
string[] inputs;
int turnCount = 0;
bool check = false;
Grid grid = new Grid();
Player myPlayer = new Player();
inputs = Console.ReadLine().Split(' ');
int width = int.Parse(inputs[0]);
int height = int.Parse(inputs[1]);
int myId = int.Parse(inputs[2]);
myPlayer.id = myId;
// game loop
while (true)
{
turnCount++;
check = false;
for (int i = 0; i < height; i++)
{
string row = Console.ReadLine();
grid.refreshGrid(row, i, "present");
grid.refreshGrid(row, i, "future");
grid.refreshGrid(row, i, "reachable");
}
int entities = int.Parse(Console.ReadLine());
for (int i = 0; i < entities; i++)
{
inputs = Console.ReadLine().Split(' ');
int entityType = int.Parse(inputs[0]);
int owner = int.Parse(inputs[1]);
int x = int.Parse(inputs[2]);
int y = int.Parse(inputs[3]);
int param1 = int.Parse(inputs[4]);
int param2 = int.Parse(inputs[5]);
switch ( entityType )
{
case 0 : // player
if (owner == myPlayer.id)
{
myPlayer.position.x = x;
myPlayer.position.y = y;
}
break;
case 1 : // bomb
grid.CalculateFutureExplosions(param2, x, y);
grid.changeGridValue(x, y, Global.BOMB);
if ( owner == myPlayer.id )
{
// ja bumba ir mana, tad atrod labāko vietu kur nostāties gaidot sprādzienu
// problēma - var likt tikai vienu bumbu. ja ir power-up, tad škrobīgi
grid.floodFill(myPlayer.position.x, myPlayer.position.y);
Coordinates coordsX = grid.getBestEscape(myPlayer);
Console.WriteLine("MOVE " + coordsX.x + " " + coordsX.y);
check = true;
}
else
{
// ja citu bumba - uzlikt kur nevar iet un no kā izvairīties
}
break;
case 2 : // items
if (param1 == 1)
{
grid.changeGridValue(x, y, Global.POWERUP_RANGE);
}
else if (param1 == 2)
{
grid.changeGridValue(x, y, Global.POWERUP_EXTRA);
}
break;
}
}
if (check) continue;
grid.fillAdjacentBoxesArray(2);
grid.printAdjacent();
grid.floodFill(myPlayer.position.x, myPlayer.position.y);
Coordinates coords = grid.getBestCoordinates(myPlayer); // dabū labākās un "safe" koord.
string nextCommand;
if ( coords.x == myPlayer.position.x && coords.y == myPlayer.position.y )
{
nextCommand = "BOMB " + coords.x + " " + coords.y;
}
else
{
Console.Error.WriteLine(" player x = " + myPlayer.position.x + " player y = " + myPlayer.position.y + " coords x : " + coords.x + " coords y : " + coords.y);
nextCommand = "MOVE " + coords.x + " " + coords.y;
}
Console.WriteLine(nextCommand);
}
}
public static void TestMethod()
{
Console.Error.WriteLine("TEST");
}
}
class Grid
{
private char[,] presentGrid = new char[Global.HEIGHT, Global.WIDTH];
private char[,] futureGrid = new char[Global.HEIGHT, Global.WIDTH];
private int[,] adjacentBoxesArray = new int[Global.HEIGHT, Global.WIDTH]; // cik katrām coord. ir kastes, ar attiecīgo bombRange
private char[,] reachableGrid = new char[Global.HEIGHT, Global.WIDTH];
private char[,] simulationGrid = new char[Global.HEIGHT, Global.WIDTH];
List<Coordinates> bombList = new List<Coordinates>();
Dictionary <string, char[,]> grids = new Dictionary <string, char[,]>();
public Grid()
{
grids.Add("present", presentGrid);
grids.Add("future", futureGrid);
grids.Add("reachable", reachableGrid);
grids.Add("simulation", simulationGrid);
for (int i = 0; i < Global.HEIGHT; i++)
{
for (int j = 0; j < Global.WIDTH; j++)
{
reachableGrid[i,j] = Global.EMPTY_CELL;
}
}
}
public void floodFill(int x, int y)
{
if ((x < 0) || (x >= Global.WIDTH)) return;
if ((y < 0) || (y >= Global.HEIGHT)) return;
if (reachableGrid[y,x].Equals(Global.EMPTY_CELL) || reachableGrid[y,x].Equals(Global.BOMB))
{
reachableGrid[y,x] = Global.REACHABLE;
floodFill(x+1, y);
floodFill(x, y+1);
floodFill(x-1, y);
floodFill(x, y-1);
}
}
public void refreshGrid(string row, int number, string gridName)
{
if (grids.ContainsKey(gridName))
{
for (int i = 0; i < Global.WIDTH; i++)
{
grids[gridName][number, i] = row[i];
// grids["reachable"][number,i] = (row[i].Equals(Global.EMPTY_CELL)) ? Global.BOX2 : Global.WALL; // fils reachable-array
}
}
else
{
Console.Error.WriteLine("Error, couldn't refresh grid");
}
}
public void copyArray(string from, string to)
{
for (int i = 0; i < Global.HEIGHT; i++)
{
for (int j = 0; j < Global.WIDTH; j++)
{
grids[to][i,j] = grids[from][i,j];
}
}
}
public void printGrid(string gridName)
{
Console.Error.WriteLine (gridName + " : ");
string row = String.Empty;
if (grids.ContainsKey(gridName))
{
for (int i = 0; i < Global.HEIGHT; i++)
{
for (int j = 0; j < Global.WIDTH; j++)
{
row = row + grids[gridName][i,j] + " ";
}
Console.Error.WriteLine(row);
row = String.Empty;
}
}
else
{
Console.Error.WriteLine("Couldn't print out the grid");
}
}
public void printAdjacent()
{
Console.Error.WriteLine("ADJACENT : ");
string row = String.Empty;
for (int i = 0; i < Global.HEIGHT; i++)
{
for (int j = 0; j < Global.WIDTH; j++)
{
row = row + adjacentBoxesArray[i,j] + " ";
}
Console.Error.WriteLine(row);
row = String.Empty;
}
}
public void simulateExplosion(int x, int y, int bombRange)
{
// fills the grid with expected bomb radius Global.EXPLOSION
int indexUp, indexDown, indexLeft, indexRight;
indexUp = ( x < bombRange ) ? x : bombRange;
indexLeft = ( y < bombRange ) ? y : bombRange;
indexDown = ( x > (10-bombRange) ) ? (10-x) : bombRange;
indexRight = ( y > (12-bombRange) ) ? (12-y) : bombRange;
for (int k = 1; k < indexUp+1; k++)
{
if (presentGrid[x-k, y].Equals(Global.WALL)) break; // uz attiecīgo pusi ir siena
simulationGrid[x-k,y] = Global.EXPLOSION;
}
for (int k = 1; k < indexDown+1; k++)
{
if (presentGrid[x+k, y].Equals(Global.WALL)) break; // uz attiecīgo pusi ir siena
simulationGrid[x+k,y] = Global.EXPLOSION ;
}
for (int k = 1; k < indexLeft+1; k++)
{
if (presentGrid[x, y-k].Equals(Global.WALL)) break; // uz attiecīgo pusi ir siena
simulationGrid[x,y-k] = Global.EXPLOSION ;
}
for (int k = 1; k < indexRight+1; k++)
{
if (presentGrid[x,y+k].Equals(Global.WALL)) break;
simulationGrid[x,y+k] = Global.EXPLOSION ;
}
simulationGrid[x,y] = Global.EXPLOSION;
}
public bool isInDanger(Coordinates playerCoordinates)
{
if (futureGrid[playerCoordinates.x, playerCoordinates.y].Equals(Global.EXPLOSION)) return true;
return false;
}
public bool isSafeToPutBomb(int x, int y, int range)
{
copyArray("reachable", "simulation");
simulateExplosion(x, y, range);
for (int i = 0; i < Global.HEIGHT; i++) // rindas
{
for (int j = 0; j < Global.WIDTH; j++) // kolonnas
{
if (simulationGrid[i,j].Equals(Global.REACHABLE))
{
resetArray("simulation");
return true; // ir kur aiziet
}
}
}
Console.Error.WriteLine("Simulations : ");
printGrid("simulation");
resetArray("simulation");
return false;
}
public void resetArray(string arrayName)
{
for (int i = 0; i < Global.HEIGHT; i++) // rindas
{
for (int j = 0; j < Global.WIDTH; j++) // kolonnas
{
grids[arrayName][i,j] = presentGrid[i,j];
}
}
}
public void changeGridValue(int x, int y, char value)
{
presentGrid[y,x] = value;
}
public void fillAdjacentBoxesArray(int bombRange) // cik katrai rūtiņai 'blakus' ir kastes
{
int indexUp, indexDown, indexLeft, indexRight; // par cik var iet uz attiecīgo pusi
int adjacentCount = 0;
int element;
printGrid("present");
for (int i = 0; i < Global.HEIGHT; i++) // rindas
{
for (int j = 0; j < Global.WIDTH; j++) // kolonnas
{
indexUp = ( i < bombRange ) ? i : bombRange;
indexLeft = ( j < bombRange ) ? j : bombRange;
indexDown = ( i > (10-bombRange) ) ? (10-i) : bombRange;
indexRight = ( j > (12-bombRange) ) ? (12-j) : bombRange;
// CHECK HOW MANY BOXES ARE ADJACENT
for (int k = 1; k < indexUp+1; k++)
{
element = presentGrid[i-k,j];
if(element.Equals(Global.WALL) || element.Equals(Global.POWERUP_EXTRA) || element.Equals(Global.POWERUP_RANGE)) break;
if(element.Equals(Global.BOX0) || element.Equals(Global.BOX1) || element.Equals(Global.BOX2))
{
adjacentCount++;
break;
}
}
for (int k = 1; k < indexDown+1; k++)
{
element = presentGrid[i+k,j];
if(element.Equals(Global.WALL) || element.Equals(Global.POWERUP_EXTRA) || element.Equals(Global.POWERUP_RANGE)) break;
if(element.Equals(Global.BOX0) || element.Equals(Global.BOX1) || element.Equals(Global.BOX2))
{
adjacentCount++;
break;
}
}
for (int k = 1; k < indexLeft+1; k++)
{
element = presentGrid[i,j-k];
if(element.Equals(Global.WALL) || element.Equals(Global.POWERUP_EXTRA) || element.Equals(Global.POWERUP_RANGE)) break;
if(element.Equals(Global.BOX0) || element.Equals(Global.BOX1) || element.Equals(Global.BOX2))
{
adjacentCount++;
break;
}
}
for (int k = 1; k < indexRight+1; k++)
{
element = presentGrid[i,j+k];
if(element.Equals(Global.WALL) || element.Equals(Global.POWERUP_EXTRA) || element.Equals(Global.POWERUP_RANGE)) break;
if(element.Equals(Global.BOX0) || element.Equals(Global.BOX1) || element.Equals(Global.BOX2))
{
adjacentCount++;
break;
}
}
//Console.Error.WriteLine("x = " + i + ", y = " + j + ", " + adjacentCount + " Up:" + indexUp + " Down:" + indexDown + " Left:" + indexLeft + " Right:" + indexRight);
adjacentBoxesArray[i,j] = adjacentCount;
adjacentCount = 0;
}
}
}
public void CalculateFutureExplosions(int bombRange, int x, int y) // iznuļļo nākotnes bumbas vietas
{
int indexUp, indexDown, indexLeft, indexRight; // par cik var iet uz attiecīgo pusi
// x - kolonna
// y - rinda
indexUp = ( y < bombRange ) ? y : bombRange;
indexLeft = ( x < bombRange ) ? x : bombRange;
indexDown = ( y > (10-bombRange) ) ? (10-y) : bombRange;
indexRight = ( x > (12-bombRange) ) ? (12-x) : bombRange;
Console.Error.WriteLine("Up:" + indexUp + " Left:" + indexLeft + " Down:" + indexDown + " Right:" + indexRight);
Console.Error.WriteLine("BombRange:" + bombRange + " x:" + x + " y:" + y);
// printGrid("present");
// CHECK HOW MANY BOXES ARE ADJACENT
for (int k = 1; k < indexUp+1; k++)
{
if (presentGrid[y-k, x].Equals(Global.WALL)) break; // uz attiecīgo pusi ir siena
if (!presentGrid[y-k, x].Equals(Global.EMPTY_CELL)) // uz attiecīgo pusi ir kaut kāds objekts
{
futureGrid[y-k,x] = Global.EXPLOSION;
break;
}
futureGrid[y-k,x] = Global.EXPLOSION;
}
for (int k = 1; k < indexDown+1; k++)
{
if (presentGrid[y+k, x].Equals(Global.WALL)) break; // uz attiecīgo pusi ir siena
if (!presentGrid[y+k, x].Equals(Global.EMPTY_CELL)) // uz attiecīgo pusi ir kaut kāds objekts
{
futureGrid[y+k,x] = Global.EXPLOSION;
break;
}
futureGrid[y+k,x] = Global.EXPLOSION;
}
for (int k = 1; k < indexLeft+1; k++)
{
if (presentGrid[y,x-k].Equals(Global.WALL)) break; // uz attiecīgo pusi ir siena
if (!presentGrid[y, x-k].Equals(Global.EMPTY_CELL)) // uz attiecīgo pusi ir kaut kāds objekts
{
futureGrid[y,x-k] = Global.EXPLOSION;
break;
}
futureGrid[y,x-k] = Global.EXPLOSION;
}
for (int k = 1; k < indexRight+1; k++)
{
if (presentGrid[y,x+k].Equals(Global.WALL)) break; // uz attiecīgo pusi ir siena
if (!presentGrid[y, x+k].Equals(Global.EMPTY_CELL)) // uz attiecīgo pusi ir kaut kāds objekts
{
futureGrid[y,x+k] = Global.EXPLOSION;
break;
}
futureGrid[y,x+k] = Global.EXPLOSION;
}
futureGrid[y,x] = Global.EXPLOSION;
}
public Coordinates getBestCoordinates(Player player)
{
int maxCount = 0;
List<Coordinates> list = new List<Coordinates>();
printGrid("reachable");
for (int i = 0; i < Global.HEIGHT; i++) // rindas
{
for (int j = 0; j < Global.WIDTH; j++) // kolonnas
{
if (reachableGrid[i,j].Equals(Global.REACHABLE) && !presentGrid[i,j].Equals(Global.BOMB))
{
if (adjacentBoxesArray[i,j] == maxCount)
{
if (!isSafeToPutBomb(i,j, 2)) continue; // ja nav safe, ignorē
Coordinates temp = new Coordinates(j,i);
list.Add(temp);
}
if (adjacentBoxesArray[i,j] > maxCount)
{
if (!isSafeToPutBomb(i,j, 2)) continue;
maxCount = adjacentBoxesArray[i,j];
list.Clear();
Coordinates temp = new Coordinates(j,i);
temp.surroundingBoxes = maxCount;
list.Add(temp);
}
}
}
}
// IR LISTS AR VISĀM "LABĀKAJĀM" KOORD.
// jāskatās, kurš punkts vistuvāk
Coordinates closest = new Coordinates();
int closestDistance = 0;
int tempDistance = 0;
closest.x = list[0].x;
closest.y = list[0].y;
closestDistance = player.distance(closest);
Console.Error.WriteLine("Player: " + player.position.x + " " + player.position.y);
Console.Error.WriteLine("Closest: " + closest.x + " " + closest.y);
Console.Error.WriteLine("Closest distance: " + closestDistance);
for (int i = 0; i < list.Count(); i++)
{
tempDistance = player.distance(list[i]);
if (tempDistance < closestDistance && reachableGrid[list[i].x , list[i].y].Equals('R'))
{
closest.x = list[i].x;
closest.y = list[i].y;
closestDistance = tempDistance;
//Console.Error.WriteLine("Error2: " + closest.x + " " + closest.y + " " + closestDistance);
}
}
//Console.Error.WriteLine("ErrorFinal: " + closest.x + " " + closest.y + " " + closestDistance);
return closest;
}
public Coordinates getBestEscape(Player player)
{
int maxCount = 0;
List<Coordinates> list = new List<Coordinates>();
printGrid("reachable");
printGrid("future");
for (int i = 0; i < Global.HEIGHT; i++) // rindas
{
for (int j = 0; j < Global.WIDTH; j++) // kolonnas
{
if (reachableGrid[i,j].Equals(Global.REACHABLE) && !futureGrid[i,j].Equals(Global.EXPLOSION))
{
// ja var aiziet, un ja nav bumba
Coordinates temp = new Coordinates(j,i);
list.Add(temp);
}
}
}
Coordinates closest = new Coordinates();
int closestDistance = 0;
int tempDistance = 0;
closest.x = list[0].x;
closest.y = list[0].y;
closestDistance = player.distance(closest);
for (int i = 0; i < list.Count(); i++)
{
tempDistance = player.distance(list[i]);
if (tempDistance < closestDistance && reachableGrid[list[i].y,list[i].x].Equals(Global.REACHABLE))
{
closest.x = list[i].x;
closest.y = list[i].y;
closestDistance = tempDistance;
//Console.Error.WriteLine("Error2: " + closest.x + " " + closest.y + " " + closestDistance);
}
}
//Console.Error.WriteLine("ErrorFinal: " + closest.x + " " + closest.y + " " + closestDistance);
return closest;
}
}
class Player
{
public int id;
public Coordinates position; // var taisīt kā private un ar getteriem/setteriem, bet ko no tā iegūs?
public char powerUp;
public Player (int x, int y)
{
position = new Coordinates(x, y);
}
public Player()
{
position = new Coordinates();
}
public int distance(Coordinates point) // finds what is the distance between two points
{
var dx = position.x - point.x;
var dy = position.y - point.y;
return Math.Abs(dx) + Math.Abs(dy);
}
}
class Coordinates
{
public int x;
public int y;
public int surroundingBoxes;
public Coordinates(){ }
public Coordinates(int X, int Y)
{
x = X;
y = Y;
}
}