-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPacman.java
More file actions
1507 lines (1256 loc) · 58.9 KB
/
Copy pathPacman.java
File metadata and controls
1507 lines (1256 loc) · 58.9 KB
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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.stream.Stream;
import static java.lang.Thread.setDefaultUncaughtExceptionHandler;
class Reader {
int[] data;
public Reader(Scanner scanner) {
data = Arrays.stream(scanner.nextLine().split(" "))
.mapToInt(Integer::parseInt).toArray();
Scanner secondLine = new Scanner(scanner.nextLine());
int X = secondLine.nextInt();
int Y = secondLine.nextInt();
int pacmanCount = secondLine.nextInt();
int ghostCount = secondLine.nextInt();
if (secondLine.hasNextLine()) {
MainClass.log("\nGot message:" + secondLine.nextLine() + "\n");
}
char[][] fa = Stream.generate(() -> scanner.nextLine().substring(0, Y).toCharArray())
.limit(X).toArray(char[][]::new);
MainClass.fieldController = new FieldController(fa[0].length, fa.length);
for (int i = 0; i <= 10; i++){
System.err.println("Hello world!");
int a = 2 + 3;
double b
}
for (int i = 0; i < fa.length; i++) {
for (int j = 0; j < fa[i].length; j++) {
switch (fa[i][j]) {
case ' ':
MainClass.fieldController.fields[j][i] = Field.SPACE;
break;
case '+':
MainClass.fieldController.fields[j][i] = Field.ENERGIZER;
break;
case 'F':
MainClass.fieldController.fields[j][i] = Field.WALL;
break;
case '1':
MainClass.fieldController.fields[j][i] = Field.COIN;
break;
case 'G':
MainClass.fieldController.fields[j][i] = Field.GHOST_GATE;
break;
default:
MainClass.log("UNEXPECTED FIELD TYPE: " + fa[i][j]);
}
}
}
String[][] paa = Stream.generate(() -> scanner.nextLine().split(" "))
.limit(pacmanCount).toArray(String[][]::new);
MainClass.pacmanBots = new ArrayList<>();
for (String[] aPaa : paa) {
if (aPaa[1].equals("mlet")) {
MainClass.pacman = new Pacman(
aPaa[1],
new Position(
Integer.parseInt(aPaa[3]),
Integer.parseInt(aPaa[2])
),
Integer.parseInt(aPaa[4]),
Integer.parseInt(aPaa[5]),
aPaa[6],
false
);
} else {
MainClass.pacmanBots.add(
new Pacman(
aPaa[1],
new Position(
Integer.parseInt(aPaa[3]),
Integer.parseInt(aPaa[2])
),
Integer.parseInt(aPaa[4]),
Integer.parseInt(aPaa[5]),
aPaa[6],
false
)
);
}
}
String[][] ga = Stream.generate(() -> scanner.nextLine().split(" "))
.limit(ghostCount).toArray(String[][]::new);
MainClass.ghosts = new Ghost[ghostCount];
for (int i = 0; i < ghostCount; i++) {
MainClass.ghosts[i] = new Ghost(
ga[i][0].charAt(0),
new Position(
Integer.parseInt(ga[i][2]),
Integer.parseInt(ga[i][1])
),
Integer.parseInt(ga[i][3]), // Eatable until
Integer.parseInt(ga[i][4]) // Stopped until
);
}
}
}
class MainClass {
public static Pacman pacman;
public static ArrayList<Pacman> pacmanBots;
public static Ghost[] ghosts;
//public static Ghost[] previousGhosts;
public static FieldController fieldController;
public static int mennyiSzellemetEvettEbbenAGyorsitasban = 0;
public static boolean[][] badpositions;
private static long startTime;
static int tick = 0;
public static void main(String[] args) {
startTime = System.nanoTime();
setDefaultUncaughtExceptionHandler((t, e) -> log(t + " ERROR: " + Arrays.toString(e.getStackTrace())));
MainClass.log("******************************************************************************************");
MainClass.log("MEMORY: " + Math.round(getMemoryMB()) + "MB");
MainClass.log("TIME: " + getRunningTimeS() + "s");
//previousGhosts = new Ghost[0];
for (tick = 0; true; tick++) {
MainClass.log("TICK: " + tick);
MainClass.log("TIME: " + getRunningTimeS() + "s");
Reader read = new Reader(new Scanner(System.in));
if (read.data[2] == -1) {
break;
}
// GHOST-ok distance-einek kiszámítása
for (Ghost ghost : ghosts) {
ghost.calculateDistances();
}
// Konstruktorban nem lehet meghívni, mert akkor még nicsnenek ghostok
pacman.setupEverything();
//for (Pacman pacmanBot : pacmanBots) {
//pacmanBot.setupEverything();
//}
char dir = Directions.getBestDirection(Directions.getDirectionScores(tick, pacmanBots.toArray(new Pacman[0]), pacman, fieldController, ghosts));
/*previousGhosts = new Ghost[ghosts.length];
for (int i = 0; i < previousGhosts.length; i++) {
previousGhosts[i] = ghosts[i].copy();
}*/
System.err.print(dir);
if (fieldController.isThereEatableGhost(pacman, Directions.getPositionByDirection(pacman.position, dir))) {
mennyiSzellemetEvettEbbenAGyorsitasban++;
}
char dir2 = ' ';
if (pacman.isFast()) {
// INFÓK FRISSÍTÉSE ************************************************************************************
// Amin állt, ott már nincs coin/+
fieldController.setFieldAt(pacman.position, Field.SPACE);
/*
Ghost[] newGhosts = new Ghost[ghosts.length];
for (int i = 0; i < ghosts.length; i++) {
newGhosts[i] = ghosts[i].copy();
}*/
for (Ghost ghost : ghosts) {
if (ghost.isEatable()) {
ghost.position = Directions.getPositionByDirection(ghost.position, Directions.getOppositeDirection(fieldController.getDirectionByPositions(ghost.position, pacman.position)));
} else {
ghost.position = Directions.getPositionByDirection(ghost.position, fieldController.getDirectionByPositions(ghost.position, pacman.position));
}
ghost.calculateDistances();
}
// Pacman firssítése
pacman.position = Directions.getPositionByDirection(pacman.position, dir);
//MainClass.log("ppdg");
fieldController.setFieldAt(pacman.position, Field.SPACE);
pacman.fastUntil--;
/*if (previousGhosts != null) {
for (int i = 0; i < ghosts.length; i++) {
Ghost prevghost = MyMath.getGhostById(previousGhosts, ghosts[i].ghostId);
if (prevghost != null) {
Position prevPos = prevghost.position;
Position position = ghosts[i].position;
Position newPos = new Position(position.x + (position.x - prevPos.x), position.y + (position.y - prevPos.y));
if (fieldController.isThereWall(newPos)) {
newPos = position;
}
newGhosts[i].position = newPos;
newGhosts[i].calculateDistances();
}
}
}*/
pacman.setupEverything();
dir2 = Directions.getBestDirection(Directions.getDirectionScores(tick, pacmanBots.toArray(new Pacman[0]), pacman, fieldController, ghosts));
/*previousGhosts = new Ghost[ghosts.length];
for (int i = 0; i < previousGhosts.length; i++) {
previousGhosts[i] = ghosts[i].copy();
}*/
} else {
mennyiSzellemetEvettEbbenAGyorsitasban = 0;
}
// Megoldás kiírása:
System.out.println(String.format("%d %d %d %c %c", read.data[0], read.data[1], read.data[2], dir, dir2));
MainClass.log("");
}
}
public static void log(String text) {
/*try {
URL url = new URL("https://ambrusweb11.hu/pacman/log.php");
URLConnection con = url.openConnection();
HttpURLConnection http = (HttpURLConnection) con;
http.setRequestMethod("POST"); // PUT is another valid option
http.setDoOutput(true);
Map<String, String> arguments = new HashMap<>();
arguments.put("message", text);
StringJoiner sj = new StringJoiner("&");
for (Map.Entry<String, String> entry : arguments.entrySet())
sj.add(URLEncoder.encode(entry.getKey(), "UTF-8") + "="
+ URLEncoder.encode(entry.getValue(), "UTF-8"));
byte[] out = sj.toString().getBytes(StandardCharsets.UTF_8);
int length = out.length;
http.setFixedLengthStreamingMode(length);
http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
http.connect();
try (OutputStream os = http.getOutputStream()) {
os.write(out);
}
} catch (Exception ignored) {
}*/
}
public static double getRunningTimeS() {
return (System.nanoTime() - startTime) / 1000000000.0;
}
public static double getMemoryMB() {
return ((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1000000.0);
}
}
class Directions {
public static final char RIGHT = '>';
public static final char UP = '^';
public static final char LEFT = '<';
public static final char DOWN = 'v';
public static char[] directions = new char[]{'>', '^', '<', 'v'};
public static char getRandom() {
Random random = new Random();
return directions[random.nextInt(4)];
}
public static char getOppositeDirection(char direction) {
switch (direction) {
case RIGHT:
return Directions.LEFT;
case UP:
return Directions.DOWN;
case LEFT:
return Directions.RIGHT;
case DOWN:
return Directions.UP;
default:
MainClass.log("DEFAULT CASE! DIR: " + direction);
return Directions.RIGHT;
}
}
public static char getBestDirection(double[] directionScores) {
for (int i = 0; i < 4; i++) {
MainClass.log(Directions.get(i) + " -> " + directionScores[i]);
}
double bestScore = -10000000;
char bestDirection = '^';
for (int i = 0; i < 4; i++) {
if (directionScores[i] > bestScore) {
bestScore = directionScores[i];
bestDirection = Directions.get(i);
}
}
return bestDirection;
}
public static Position getDeltaPosByDirection(char direction) {
switch (direction) {
case RIGHT:
return new Position(1, 0);
case UP:
return new Position(0, -1);
case LEFT:
return new Position(-1, 0);
case DOWN:
return new Position(0, 1);
default:
MainClass.log("DEFAULT CASE! DIR: " + direction);
return new Position(0, -1);
}
}
public static Position getPositionByDirection(Position position, char direction) {
Position deltaPos = getDeltaPosByDirection(direction);
return MainClass.fieldController.checkTeleportPosition(new Position(position.x + deltaPos.x, position.y + deltaPos.y));
}
public static int getIndexByDirection(char dir) {
for (int i = 0; i < directions.length; i++) {
if (directions[i] == dir) return i;
}
return 0;
}
public static double[] getDirectionScores(int tick, Pacman[] pacmanBots, Pacman pacman, FieldController fieldController, Ghost[] ghosts) {
double[] directionScores = new double[]{0, 0, 0, 0};
// COINOK ÉRTÉKELÉSE *******************************************************************************************
for (int i = 0; i < pacman.coins.length; i++) {
int targetDir = getIndexByDirection(fieldController.getDirectionByPositions(pacman.position, pacman.coins[i]));
directionScores[targetDir] += 20.0 / pacman.getDistance(pacman.coins[i]);
}
// ENERGIZER ***************************************************************************************************
boolean isRemainingEnergizer = false;
ArrayList<Position> energizers = new ArrayList<>();
for (int j = 0; j < fieldController.getWidth(); j++) {
for (int k = 0; k < fieldController.getHeight(); k++) {
Position energizerPosition = new Position(j, k);
if (fieldController.isThereEnergizer(energizerPosition)) {
isRemainingEnergizer = true;
energizers.add(energizerPosition);
}
}
}
if (isRemainingEnergizer) {
int bestDirection = 0;
int bestDistance = 10000;
for (int i = 0; i < 4; i++) {
for (Position energizer : energizers) {
if (!fieldController.isThereWall(getPositionByDirection(pacman.position, Directions.directions[i])) && pacman.getDistance(energizer) < bestDistance) {
bestDistance = pacman.getDistance(energizer);
bestDirection = fieldController.getDirectionIndexByPositions(pacman.position, energizer);
}
}
}
directionScores[bestDirection] += 100;
}
// JUTALMAZÁS *********************************************************************************************
for (int i = 0; i < 4; i++) {
MainClass.log("irany " + Directions.get(i));
Position newPos;
newPos = Directions.getPositionByDirection(pacman.position, Directions.get(i));
Pacman newPacman = new Pacman(newPos, pacman.fastUntil <= 0 ? 0 : pacman.fastUntil - 1, pacman.currentScore, pacman.plus, false);
newPacman.calculateDistances();
// COINOK ÉRTÉKELÉSE
// Ha WALL-ra lépne ****************************************************************************************
if (fieldController.isThereWall(newPos) || fieldController.isThereGhostGate(newPos)) {
directionScores[i] -= 100000; // Nem jó
}
// FÉLÉS SZELLEMEKTŐL || SZELLEMEVÉS ***********************************************************************
if (pacman.isFast()) {
//MainClass.log("FAST");
for (Ghost ghost : ghosts) {
ghost.calculateDistances();
// Ha ehető a szellem:
if (ghost.eatableUntil >= ghost.getDistance(pacman.position)) {
if (pacman.getDistance(ghost.position) < 12) {
if (!fieldController.isThereWall(newPos) && !fieldController.isThereGhostGate(newPos)) {
if (newPacman.getDistance(ghost.position) < pacman.getDistance(ghost.position)) {
directionScores[i] += (1000 * Math.pow(2, MainClass.mennyiSzellemetEvettEbbenAGyorsitasban)) / newPacman.getDistance(ghost.position);
}
}
}
} else { // Ha nem ehető a szellem: (félünk!!)
if (ghost.getDistance(pacman.position) < 6) {
if (!fieldController.isThereWall(newPos) && !fieldController.isThereGhostGate(newPos)) {
if (ghost.getDistance(newPacman.position) < 2) {
// Ha életveszélybe kerülnénk:
directionScores[i] -= 1000000;
}
}
}
}
}
} else { // Ha lassúk vagyunk
for (Ghost ghost : ghosts) {
ghost.calculateDistances();
//MainClass.log(ghost.ghostId + " szellem tavolsaga: " + ghost.getDistance(pacman.position));
if (ghost.getDistance(pacman.position) < 6) {
if (!fieldController.isThereWall(newPos) && !fieldController.isThereGhostGate(newPos)) {
// Ha életveszélybe kerülnénk:
if (ghost.getDistance(newPacman.position) < 2) {
directionScores[i] -= 1000000;
//MainClass.log("eletveszely " + ghost.getDistance(newPacman.position));
}
}
}
}
}
for (Pacman pacmanBot : pacmanBots) {
if (pacmanBot.currentScore < pacman.currentScore) {
if (pacmanBot.isFast() && !pacman.isFast()) {
if (pacman.getDistance(pacmanBot.position) < 5) {
pacmanBot.calculateDistances();
if (pacmanBot.getDistance(newPos) < 4) {
directionScores[i] -= ((double) pacman.currentScore - (double) pacmanBot.currentScore) / 2.0;
}
}
} else {
if (pacman.getDistance(pacmanBot.position) < 3) {
pacmanBot.calculateDistances();
if (pacmanBot.getDistance(newPos) < 2) {
directionScores[i] -= ((double) pacman.currentScore - (double) pacmanBot.currentScore) / 2.0;
}
}
}
}
}
// TODO Nekünk kedves pacmanekhez menés
// ZSÁKUTCÁK ***********************************************************************************************
/*if (pacman.relativeDeadEnds[newPos.x][newPos.y]) {
directionScores[i] -= 1000;
}*/
if (pacman.obstacles[newPos.x][newPos.y]) {
directionScores[i] -= 10000;
}
if (pacman.dead_end_road[newPos.x][newPos.y]) {
directionScores[i] -= 10000;
}
/*int lolTick = 178;
if (tick == lolTick) {
MainClass.log("NEWPPACMAN---: " + newPacman.position);
MainClass.log("crosses: " + Arrays.toString(newPacman.crosses));
MainClass.log("---");
MainClass.log("G" + ghosts[0].ghostId + " distances: " + MyMath.matrixOut(ghosts[0].distances));
MainClass.log("G" + ghosts[1].ghostId + " distances: " + MyMath.matrixOut(ghosts[1].distances));
MainClass.log("WC");
for (int x = 0; x < fieldController.getWidth(); x++) {
for (int y = 0; y < fieldController.getHeight(); y++) {
System.err.print(Pacman.wallCount(new Position(x, y)) + " ");
}
System.err.print("\n");
}
MainClass.log("WCDER");
for (int x = 0; x < fieldController.getWidth(); x++) {
for (int y = 0; y < fieldController.getHeight(); y++) {
System.err.print(Pacman.wallCountWithDER(newPacman.dead_end_road, new Position(x, y)) + " ");
}
System.err.print("\n");
}
}*/
/*if (!fieldController.isThereWall(newPos)) {
if (Pacman.wallCount(newPacman.position) == 2) {
//MainClass.log("CROSSES: " + Arrays.toString(newPacman.crosses));
boolean isChecked = false;
for (Ghost ghost1 : ghosts) {
for (Ghost ghost2 : ghosts)
if (!isChecked && ghost1.ghostId != ghost2.ghostId) {
if (newPacman.crosses[0].crossType == CrossType.DEAD_END) {
if (!isChecked && newPacman.crosses[1].crossType == CrossType.DEAD_END) {
directionScores[i] -= 1001;
isChecked = true;
} else if (ghost1.getDistance(newPacman.crosses[1].position) < 2 + newPacman.getDistance(newPacman.crosses[1].position)) {
directionScores[i] -= 1002;
isChecked = true;
//MainClass.log("DEg");
}
} else if (newPacman.crosses[1].crossType == CrossType.DEAD_END) {
if (newPacman.crosses[0].crossType == CrossType.DEAD_END) {
directionScores[i] -= 1003;
isChecked = true;
//MainClass.log("dDE");
} else if (ghost1.getDistance(newPacman.crosses[0].position) < 2 + newPacman.getDistance(newPacman.crosses[0].position)) {
directionScores[i] -= 1004;
isChecked = true;
//MainClass.log("gDE");
}
} else {
if (//ghost1.eatableUntil == 0 && ghost2.eatableUntil == 0 &&
ghost1.getDistance(newPacman.crosses[1].position) < 2 + newPacman.getDistance(newPacman.crosses[1].position) &&
ghost2.getDistance(newPacman.crosses[0].position) < 2 + newPacman.getDistance(newPacman.crosses[0].position)
) {
directionScores[i] -= 1005;
isChecked = true;
}
}
}
}
} else if (Pacman.wallCount(newPacman.position) == 1 || Pacman.wallCount(newPacman.position) == 0) {
boolean[] jo_utak_e = new boolean[]{true, true, true, true};
for (int l = 0; l < 4; l++) {
Position newNewPos;
newNewPos = Directions.getPositionByDirection(newPacman.position, Directions.get(l));
Pacman newNewPacman = new Pacman(newPacman.id, newNewPos, newPacman.fastUntil, newPacman.currentScore, newPacman.plus);
if (!fieldController.isThereWall(newNewPos) && !fieldController.isThereGhostGate(newNewPos)) {
for (Ghost ghost1 : ghosts) {
for (Ghost ghost2 : ghosts)
if (ghost1.ghostId != ghost2.ghostId) {
if (newNewPacman.crosses[0].crossType == CrossType.DEAD_END) {
if (newNewPacman.crosses[1].crossType == CrossType.DEAD_END) {
jo_utak_e[l] = false;
} else if (ghost1.eatableUntil < pacman.fastUntil && ghost1.getDistance(newNewPacman.crosses[1].position) < 4 + newNewPacman.getDistance(newNewPacman.crosses[1].position)) {
jo_utak_e[l] = false;
}
} else if (newNewPacman.crosses[1].crossType == CrossType.DEAD_END) {
if (newNewPacman.crosses[0].crossType == CrossType.DEAD_END) {
jo_utak_e[l] = false;
} else if (ghost1.eatableUntil < pacman.fastUntil && ghost1.getDistance(newNewPacman.crosses[0].position) < 4 + newNewPacman.getDistance(newNewPacman.crosses[0].position)) {
jo_utak_e[l] = false;
}
} else {
if (ghost1.eatableUntil < pacman.fastUntil && ghost2.eatableUntil == 0 &&
ghost1.getDistance(newNewPacman.crosses[1].position) < 4 + newNewPacman.getDistance(newNewPacman.crosses[1].position) &&
ghost2.getDistance(newNewPacman.crosses[0].position) < 4 + newNewPacman.getDistance(newNewPacman.crosses[0].position)
) {
jo_utak_e[l] = false;
}
}
}
}
} else {
jo_utak_e[l] = false;
}
}
boolean van_e_jo_ut = false;
for (boolean jo_ut_e : jo_utak_e) {
if (jo_ut_e) {
van_e_jo_ut = true;
}
}
if (!van_e_jo_ut) {
directionScores[i] -= 1006;
}
}
// 1 lik
else if (Pacman.wallCount(newPacman.position) == 3) {
for (int l = 0; l < 4; l++) {
Position newNewPos;
newNewPos = Directions.getPositionByDirection(newPacman.position, Directions.get(l));
Pacman newNewPacman = new Pacman(newPacman.id, newNewPos, newPacman.fastUntil, newPacman.currentScore, newPacman.plus);
if (!fieldController.isThereWall(newNewPos) && !fieldController.isThereGhostGate(newNewPos)) {
for (Ghost ghost : ghosts) {
ghost.calculateDistances();
if (newNewPacman.crosses[0].crossType == CrossType.NORMAL_CROSS) {
if (newNewPacman.getDistance(newNewPacman.crosses[0].position) + 4 > ghost.getDistance(newNewPacman.crosses[0].position)) {
directionScores[i] -= 1007;
}
} else { // crosses 1 -> normal_cross
if (newNewPacman.getDistance(newNewPacman.crosses[1].position) + 4 > ghost.getDistance(newNewPacman.crosses[1].position)) {
directionScores[i] -= 1008;
}
}
}
}
}
}
}*/
}
MainClass.log("MEMORY: " + Math.round(MainClass.getMemoryMB()) + "MB");
return directionScores;
}
// Gets the nth direction
public static char get(int i) {
return directions[i];
}
}
class Pacman {
public String id;
public int fastUntil, currentScore;
public String plus;
public Position position;
public boolean[][] obstacles;
public boolean[][] dead_end_road;
public boolean[][] relativeDeadEnds;
int[][] distances;
Position[] coins;
Cross[] crosses;
Pacman(Position position, int fastUntil, Integer currentScore, String plus) {
this.position = position;
this.fastUntil = fastUntil;
this.currentScore = currentScore;
this.plus = plus;
calculateDistances();
calculateDER();
setCrosses();
setObstacles();
calculateRDEs();
}
Pacman(Position position, int fastUntil, Integer currentScore, String plus, boolean calculateThings) {
this.position = position;
this.fastUntil = fastUntil;
this.currentScore = currentScore;
this.plus = plus;
if (calculateThings) {
calculateDistances();
calculateDER();
setCrosses();
setObstacles();
calculateRDEs();
}
}
Pacman(String id, Position position, int fastUntil, int currentScore, String plus) {
this.id = id;
this.position = position;
this.fastUntil = fastUntil;
this.currentScore = currentScore;
this.plus = plus;
calculateDistances();
calculateDER();
setCrosses();
setObstacles();
calculateRDEs();
}
Pacman(String id, Position position, int fastUntil, int currentScore, String plus, boolean calculateThings) {
this.id = id;
this.position = position;
this.fastUntil = fastUntil;
this.currentScore = currentScore;
this.plus = plus;
if (calculateThings) {
calculateDistances();
calculateDER();
setCrosses();
setObstacles();
calculateRDEs();
}
}
public void setupEverything() {
calculateDistances();
calculateDER();
setCrosses();
setObstacles();
calculateRDEs();
}
public void setCrosses() {
crosses = new Cross[2];
for (int crossCount = 0; crossCount < crosses.length; crossCount++) {
Position currPos = this.position;
Position nextPos = Directions.getPositionByDirection(this.position, nthDirection(this.position, crossCount));
// Addig megyünk, amíg ki nem érünk a folyosóból egy kereszteződésbe
while ((/*dead_end_road[nextPos.x][nextPos.y] && */
wallCountWithDER(dead_end_road, nextPos) == 2 && !MainClass.fieldController.isThereEnergizer(nextPos)) /*||
(!dead_end_road[nextPos.x][nextPos.y] && wallCountWithDER(dead_end_road, nextPos) == 2 && !MainClass.fieldController.isThereEnergizer(nextPos))*/) {
//MainClass.log("("+currPos.x+","+currPos.y+") "+szomszedoszsakutca);
//Ha nem currPos-ból jött
if (Directions.getPositionByDirection(nextPos, nthDirection(nextPos, 0)).equals(currPos)) {
currPos = nextPos;
nextPos = Directions.getPositionByDirection(nextPos, nthDirection(nextPos, 1));
}
//Különben a másik járható irányba megy
else {
currPos = nextPos;
nextPos = Directions.getPositionByDirection(nextPos, nthDirection(nextPos, 0));
}
}
//Ha kilépett a folyósóból, ott a kereszteződés
if (MainClass.fieldController.isThereEnergizer(currPos)) {
crosses[crossCount] = new Cross(CrossType.NORMAL_CROSS, currPos);
} else if (wallCountWithDER(dead_end_road, currPos) == 3) {
//Zsákutca vége
crosses[crossCount] = new Cross(CrossType.DEAD_END, currPos);
} else {
//Normál kereszteződés
crosses[crossCount] = new Cross(CrossType.NORMAL_CROSS, currPos);
}
}
}
/**
* N-edik járható irány (0-tól számozva)
*/
public char nthDirection(Position position, int n) {
int i = 0;//jelenleg vizsgált irány
int n2 = n;//hártalévő irányok száma, amerre nincs fal
//Ha az i-edik irányba nincs fal, arra lehet menni
while (n2 >= 0 && i < 4) {
Position position1 = Directions.getPositionByDirection(position, Directions.get(i));
if (!MainClass.fieldController.isThereWall(position1) &&
!MainClass.fieldController.isThereGhostGate(position1) &&
!dead_end_road[position1.x][position1.y]) {
n2--;
}
i++;
}
return Directions.get(i - 1);
}
void calculateDistances() {
coins = new Position[4];
int coinCount = 0;
distances = new int[MainClass.fieldController.getWidth()][MainClass.fieldController.getHeight()];
ArrayList<Position> knownDistancePositions = new ArrayList<>();
for (int i = 0; i < MainClass.fieldController.getWidth(); i++) {
for (int j = 0; j < MainClass.fieldController.getHeight(); j++) {
distances[i][j] = -1;
}
}
distances[this.position.x][this.position.y] = 0;
knownDistancePositions.add(position);
int d = 1;
boolean allChanged = false;
while (!allChanged) {
allChanged = true;
ArrayList<Position> newKnownDistancePositions = new ArrayList<>();
for (Position position : knownDistancePositions) {
for (char direction : Directions.directions) {
Position newPosition = Directions.getPositionByDirection(position, direction);
newPosition = MainClass.fieldController.checkTeleportPosition(newPosition);
boolean wall = MainClass.fieldController.isThereWall(newPosition) || MainClass.fieldController.isThereGhostGate(newPosition);
if (!wall && distances[newPosition.x][newPosition.y] == -1) {
distances[newPosition.x][newPosition.y] = d;
if (MainClass.fieldController.isThereCoin(newPosition) && coinCount != 4) {
coins[coinCount] = newPosition;
coinCount++;
}
newKnownDistancePositions.add(newPosition);
allChanged = false;
}
}
}
knownDistancePositions = newKnownDistancePositions;
d++;
}
}
int getDistance(Position position) {
return distances[position.x][position.y];
}
public boolean isFast() {
return fastUntil > 0;
}
@Override
public String toString() {
return "Pacman{" +
" position: " + position.toString() +
", fastUntil=" + fastUntil +
", currentScore=" + currentScore +
", plus='" + plus + '\'' +
'}';
}
static int wallCount(Position position) {
if (MainClass.fieldController.isThereWall(position)) {
return -1;
}
int wallSum = 0;
for (int i = 0; i < 4; i++) {
Position pos = Directions.getPositionByDirection(position, Directions.get(i));
if (MainClass.fieldController.isThereWall(pos) ||
MainClass.fieldController.isThereGhostGate(pos)) {
wallSum++;
}
}
return wallSum;
}
static int wallCountWithDER(boolean[][] DER, Position position) {
if (MainClass.fieldController.isThereWall(position)) {
return -1;
}
int wallSum = 0;
for (int i = 0; i < 4; i++) {
Position pos = Directions.getPositionByDirection(position, Directions.get(i));
if (MainClass.fieldController.isThereWall(pos) ||
MainClass.fieldController.isThereGhostGate(pos) ||
DER[pos.x][pos.y]) {
wallSum++;
}
}
return wallSum;
}
void setObstacles() {
obstacles = new boolean[MainClass.fieldController.getWidth()][MainClass.fieldController.getHeight()];
for (int i = 0; i < MainClass.fieldController.getWidth(); i++) {
for (int j = 0; j < MainClass.fieldController.getHeight(); j++) {
obstacles[i][j] = false;
if (MainClass.fieldController.isThereWall(new Position(i, j))
|| dead_end_road[i][j]
|| MainClass.fieldController.isThereGhostGate(new Position(i, j))) {
obstacles[i][j] = true;
}
for (Ghost ghost : MainClass.ghosts) {
if (((ghost.getDistance(new Position(i, j)) < this.getDistance(new Position(i, j)) + 1 && ghost.getDistance(new Position(i, j)) >= 0))
&& ghost.eatableUntil <= (ghost.getDistance(new Position(i, j)))) {
obstacles[i][j] = true;
break;
}
}
}
}
}
boolean isThereObstacle(Position position) {
if (obstacles == null) {
setObstacles();
}
position = MainClass.fieldController.checkTeleportPosition(position);
return obstacles[position.x][position.y];
}
void calculateDER() {
dead_end_road = new boolean[MainClass.fieldController.getWidth()][MainClass.fieldController.getHeight()];
for (int i = 0; i < MainClass.fieldController.getWidth(); i++) {
for (int j = 0; j < MainClass.fieldController.getHeight(); j++) {
dead_end_road[i][j] = false;
}
}
boolean allChanged = false;
while (!allChanged) {
allChanged = true;
for (int i = 0; i < MainClass.fieldController.getWidth(); i++) {
for (int j = 0; j < MainClass.fieldController.getHeight(); j++) {
int wallCount;
Position newPos = new Position(i, j);
if (!MainClass.fieldController.isThereWall(newPos) && !MainClass.fieldController.isThereGhostGate(new Position(i, j)) && !MainClass.fieldController.isThereEnergizer(newPos)) {
wallCount = Pacman.wallCount(new Position(i, j));
} else {
wallCount = -1;
}
if (wallCount == 3 && !dead_end_road[i][j] && !MainClass.fieldController.isThereEnergizer(newPos)) {
dead_end_road[i][j] = true;
allChanged = false;
}
if (wallCount == 2 && !dead_end_road[i][j] && !MainClass.fieldController.isThereEnergizer(newPos)) {
for (char dir : Directions.directions) {
Position position1 = Directions.getPositionByDirection(newPos, dir);
if (dead_end_road[position1.x][position1.y]) {
dead_end_road[i][j] = true;
allChanged = false;
break;
}
}
}
if (wallCount == 1 && !dead_end_road[i][j] && !MainClass.fieldController.isThereEnergizer(newPos)) {
for (char dir1 : Directions.directions) {
for (char dir2 : Directions.directions) {
if (dir1 != dir2) {
Position position1 = Directions.getPositionByDirection(newPos, dir1);
Position position2 = Directions.getPositionByDirection(newPos, dir2);
if (dead_end_road[position1.x][position1.y] && dead_end_road[position2.x][position2.y]) {
dead_end_road[i][j] = true;
allChanged = false;
break;
}
}
}
}
}
}
}
}
}
void calculateRDEs() {
/*relativeDeadEnds = new boolean[MainClass.fieldController.getWidth()][MainClass.fieldController.getHeight()];
for (int i = 0; i < MainClass.fieldController.getWidth(); i++) {
for (int j = 0; j < MainClass.fieldController.getHeight(); j++) {
relativeDeadEnds[i][j] = false;
}
}
boolean allChanged = false;
while (!allChanged) {
allChanged = true;
for (int i = 0; i < MainClass.fieldController.getWidth(); i++) {
for (int j = 0; j < MainClass.fieldController.getHeight(); j++) {