-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathMSolver.java
1114 lines (867 loc) · 30.6 KB
/
MSolver.java
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.awt.*;
import java.awt.image.*;
import java.util.*;
/*
Initial setup to get working:
Change ScreenWidth, ScreenHeight
Make sure TOT_MINES is correct
Hopefully it works then
*/
public class MSolver{
static BufferedImage screenShotImage(){
try {
Rectangle captureSize = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
ScreenWidth = captureSize.width;
ScreenHeight = captureSize.height;
BufferedImage bufferedImage = robot.createScreenCapture(captureSize);
return bufferedImage;
}
catch(Exception e) { e.printStackTrace(); }
return null;
}
static int ScreenWidth = 1600;
static int ScreenHeight = 900;
static boolean isDark(int rgb){
int red = (rgb >> 16) & 0xFF;
int green = (rgb >> 8) & 0xFF;
int blue = rgb & 0xFF;
return red + green + blue < 120;
}
static int colorDifference(int r1, int g1, int b1, int r2, int g2, int b2){
return Math.abs(r1 - r2) + Math.abs(b1 - b2) + Math.abs(g1 - g2);
}
static int BoardWidth = 0;
static int BoardHeight = 1;
static double BoardPix = 0;
static int BoardTopW = 0;
static int BoardTopH = 0;
// Take a screenshot and try to figure out the board dimensions and stuff like that
static void calibrate(){
// Display this message
System.out.println("Calibrating Screen...");
BufferedImage bi = screenShotImage();
bi.createGraphics();
Graphics2D g = (Graphics2D)bi.getGraphics();
int hh = 0; // boardheight of previous column
int firh = 0; // position of first found
int firw = 0;
int lash = 0; // position of last found
int lasw = 0;
int tot = 0; // total number of crosses found
for(int w = 0; w<ScreenWidth; w++){
for(int h=0; h < ScreenHeight; h++){
int rgb = bi.getRGB(w,h);
if(isDark(rgb)){
if(w<10 || h<10 || w>ScreenWidth-10 || h> ScreenHeight-10)
continue;
// look for the cross shape to indicate position on board
// we consider it a cross if:
// - the square is dark
// - four selected pixels to the N,S,E,W are dark
// - four selected pixels to the NE, SE, NW, SW are not dark
if(isDark(bi.getRGB(w+7,h)))
if(isDark(bi.getRGB(w-7,h)))
if(isDark(bi.getRGB(w,h+7)))
if(isDark(bi.getRGB(w,h-7)))
if(isDark(bi.getRGB(w+3,h)))
if(isDark(bi.getRGB(w-3,h)))
if(isDark(bi.getRGB(w,h+3)))
if(isDark(bi.getRGB(w,h-3)))
if(!isDark(bi.getRGB(w-7,h-7)))
if(!isDark(bi.getRGB(w+7,h-7)))
if(!isDark(bi.getRGB(w-7,h+7)))
if(!isDark(bi.getRGB(w+7,h+7)))
if(!isDark(bi.getRGB(w-3,h-3)))
if(!isDark(bi.getRGB(w+3,h-3)))
if(!isDark(bi.getRGB(w-3,h+3)))
if(!isDark(bi.getRGB(w+3,h+3))){
g.setColor(Color.YELLOW); // for _calibrate.png
g.fillRect(w-3,h-3,7,7);
tot++;
BoardHeight++;
// Find the position of the first cross
if(firh == 0){
firh = h;
firw = w;
}
// Note position of the last cross
lash = h;
lasw = w;
}
}
}
if(BoardHeight > 1){
hh = BoardHeight;
BoardHeight = 1;
}
}
// Determine boardwidth from total and boardheight
BoardHeight = hh;
if(tot % (BoardHeight-1) == 0)
BoardWidth = tot / (BoardHeight-1) + 1;
else BoardWidth = 0;
// Determine BoardPix by taking an average
BoardPix = 0.5*((double)(lasw - firw) / (double)(BoardWidth-2))
+ 0.5*((double)(lash - firh) / (double)(BoardHeight-2));
// Determine first cell position (where to click)
int halfsiz = (int)BoardPix / 2;
BoardTopW = firw - halfsiz + 3;
BoardTopH = firh - halfsiz + 3;
System.out.printf("BoardWidth=%d, BoardHeight=%d, BoardPix=%f\n", BoardWidth, BoardHeight, BoardPix);
System.out.printf("BoardTopW=%d, BoardTopH=%d\n",BoardTopW, BoardTopH);
}
static int mouseLocX = ScreenWidth / 2;
static int mouseLocY = ScreenHeight / 2;
static void moveMouse(int mouseX, int mouseY) throws Throwable{
int distance = Math.max(Math.abs(mouseX-mouseLocX), Math.abs(mouseY-mouseLocY));
int DELAY = distance/4;
int numSteps = DELAY / 5;
double stepx = (double)(mouseX - mouseLocX) / (double)numSteps;
double stepy = (double)(mouseY - mouseLocY) / (double)numSteps;
for(int i=0; i<numSteps; i++){
robot.mouseMove(mouseLocX + (int)(i*stepx), mouseLocY + (int)(i*stepy));
Thread.sleep(5);
}
robot.mouseMove(mouseX, mouseY);
mouseLocX = mouseX;
mouseLocY = mouseY;
}
// Click on a given square, given i and j
static void clickOn(int i, int j) throws Throwable{
int mouseX = BoardTopW + (int)(j*BoardPix);
int mouseY = BoardTopH + (int)(i*BoardPix);
moveMouse(mouseX, mouseY);
robot.mousePress(16);
Thread.sleep(5);
robot.mouseRelease(16);
Thread.sleep(10);
}
// Manually flag some mine
static void flagOn(int i, int j) throws Throwable{
int mouseX = BoardTopW + (int)(j*BoardPix);
int mouseY = BoardTopH + (int)(i*BoardPix);
moveMouse(mouseX, mouseY);
robot.mousePress(4);
Thread.sleep(5);
robot.mouseRelease(4);
Thread.sleep(10);
}
// Click on it with both mouse buttons in order to "chord"
static void chordOn(int i, int j) throws Throwable{
int mouseX = BoardTopW + (int)(j*BoardPix);
int mouseY = BoardTopH + (int)(i*BoardPix);
moveMouse(mouseX, mouseY);
robot.mousePress(4);
robot.mousePress(16);
Thread.sleep(5);
robot.mouseRelease(4);
robot.mouseRelease(16);
Thread.sleep(10);
}
// Special method to try to separate 3 from 7
// which conveniently are the same color
static int detect_3_7(int[] areapix){
// Assume it's length 225 and dimensions 15x15.
// Classify each pixel as red or not.
// Since we don't have to deal with 5, we can take a greater liberty
// in deciding on red pixels.
boolean redx[][] = new boolean[15][15];
for(int k=0; k<225; k++){
int i = k % 15;
int j = k / 15;
int rgb = areapix[k];
int red = (rgb >> 16) & 0xFF;
int green = (rgb >> 8) & 0xFF;
int blue = rgb & 0xFF;
if(colorDifference(red, green, blue, 170, 0, 0) < 100)
redx[i][j] = true;
}
/*
for(int i=0; i<15; i++){
for(int j=0; j<15; j++){
if(redx[i][j]) System.out.print("x");
else System.out.print(" ");
}
System.out.println();
}
System.out.println();
*/
// Look for this pattern in the 3 but not 7:
// x x
// x .
/*
for(int i=0; i<14; i++){
for(int j=0; j<14; j++){
if(redx[i][j] && redx[i+1][j]
&& redx[i][j+1] && !redx[i+1][j+1])
return 3;
}
}
*/
// . . .
// x
for(int i=0; i<13; i++){
for(int j=0; j<13; j++){
if(!redx[i][j] && !redx[i][j+1] && !redx[i][j+2] && redx[i+1][j+1])
return 3;
}
}
return 7;
}
// Try to read what number's in this position
static int detect(BufferedImage bi, int i, int j){
int mouseX = BoardTopW + (int)(j*BoardPix);
int mouseY = BoardTopH + (int)(i*BoardPix);
// Don't take one pixel, take a 15x15 area of pixels
int areapix[] = new int[225];
int count = 0;
for(int ii = mouseX-7; ii <= mouseX+7; ii++)
for(int jj = mouseY-7; jj <= mouseY+7; jj++){
areapix[count] = bi.getRGB(ii,jj);
count++;
}
boolean hasColorOfOneSquare = false;
boolean hasColorOfBlank = false;
boolean isRelativelyHomogenous = true;
for(int rgb : areapix){
int red = (rgb >> 16) & 0xFF;
int green = (rgb >> 8) & 0xFF;
int blue = rgb & 0xFF;
// Detect death
if(colorDifference(red, green, blue, 110,110,110) < 20)
return -10;
// Detect flagging of any sort
if(colorDifference(red,green,blue,255,0,0) < 30)
return -3;
if(colorDifference(red, green, blue, 65,79,188) < 10){
hasColorOfOneSquare = true;
}
if(blue > red && blue > green &&
colorDifference(red, green, blue, 220,220,255) < 200){
hasColorOfBlank = true;
}
if(colorDifference(red, green, blue, 167,3,5) < 20)
return detect_3_7(areapix);
if(colorDifference(red, green, blue, 29,103,4) < 20) return 2;
if(colorDifference(red, green, blue, 0,0,138) < 20) return 4;
if(colorDifference(red, green, blue, 124,1,3) < 20) return 5;
if(colorDifference(red, green, blue, 7,122,131) < 20) return 6;
}
// Determine how 'same' the area is.
// This is to separate the empty areas which are relatively the same from
// the unexplored areas which have a gradient of some sort.
{
int rgb00 = areapix[0];
int red00 = (rgb00 >> 16) & 0xFF;
int green00 = (rgb00 >> 8) & 0xFF;
int blue00 = rgb00 & 0xFF;
for(int rgb : areapix){
int red = (rgb >> 16) & 0xFF;
int green = (rgb >> 8) & 0xFF;
int blue = rgb & 0xFF;
if(colorDifference(red, green, blue, red00, green00, blue00) > 60){
isRelativelyHomogenous = false;
break;
}
}
}
if(hasColorOfOneSquare && hasColorOfBlank)
return 1;
if(hasColorOfBlank && isRelativelyHomogenous)
return 0;
return -1;
}
static Scanner scanner = new Scanner(System.in);
static Robot robot;
static Random rand = new Random();
// Internal representation of the board state as displayed on the screen.
// 1-8 means that the square there is that number
// 0 means that it's actually empty
// -1 means it's not opened yet
// -2 means it's outside the boundries of the board
// -3 means a mine
// -10 means that something went wrong and we should exit the program
static int[][] onScreen = null;
// List of squares in which we know there are mines
static boolean[][] flags = null;
static int numMines = 0;
static int TOT_MINES = 99;
// Remove the need for edge detection every fricking time
static int onScreen(int i, int j){
if(i < 0 || j < 0 || i > BoardHeight-1 || j > BoardWidth-1)
return -10;
return onScreen[i][j];
}
// take a screenshot and update the onScreen array
static int updateOnScreen(){
BufferedImage bi = screenShotImage();
int numMines_t = 0;
for(int i = 0; i < BoardHeight; i++){
for(int j=0; j < BoardWidth; j++){
int d = detect(bi,i,j);
if(d == -10) return d; // death
onScreen[i][j] = d;
// Special case for flags
if(d == -3 || flags[i][j]){
onScreen[i][j] = -1;
flags[i][j] = true;
}
if(d == -1){
flags[i][j] = false;
}
// Update mines count
if(flags[i][j]){
numMines_t++;
}
}
}
//if(numMines_t < numMines - 2) exit();
numMines = numMines_t;
return 0;
}
// tries to detect problems with screenshotting
static boolean checkConsistency(){
for(int i=0; i<BoardHeight; i++){
for(int j=0; j<BoardWidth; j++){
int freeSquares = countFreeSquaresAround(onScreen, i, j);
int numFlags = countFlagsAround(flags, i, j);
if(onScreen(i,j) == 0 && freeSquares > 0){
return false;
}
if((onScreen(i,j) - numFlags) > 0 && freeSquares == 0){
return false;
}
}
}
return true;
}
// Handle clicking the first square
static void firstSquare() throws Throwable{
// Check that it's indeed the first square
robot.mouseMove(0,0);
Thread.sleep(20);
updateOnScreen();
robot.mouseMove(mouseLocX,mouseLocY);
boolean isUntouched = true;
for(int i=0; i<BoardHeight; i++){
for(int j=0; j<BoardWidth; j++){
if(onScreen(i,j) != -1)
isUntouched = false;
}
}
if(!isUntouched){
return;
}
// Click the middle
clickOn(BoardHeight/2-1, BoardWidth/2-1);
clickOn(BoardHeight/2-1, BoardWidth/2-1);
Thread.sleep(200);
}
// How many flags exist around this square?
static int countFlagsAround(boolean[][] array, int i, int j){
int mines = 0;
// See if we're on the edge of the board
boolean oU = false, oD = false, oL = false, oR = false;
if(i == 0) oU = true;
if(j == 0) oL = true;
if(i == BoardHeight-1) oD = true;
if(j == BoardWidth-1) oR = true;
if(!oU && array[i-1][j]) mines++;
if(!oL && array[i][j-1]) mines++;
if(!oD && array[i+1][j]) mines++;
if(!oR && array[i][j+1]) mines++;
if(!oU && !oL && array[i-1][j-1]) mines++;
if(!oU && !oR && array[i-1][j+1]) mines++;
if(!oD && !oL && array[i+1][j-1]) mines++;
if(!oD && !oR && array[i+1][j+1]) mines++;
return mines;
}
// How many unopened squares around this square?
static int countFreeSquaresAround(int[][] board, int i, int j){
int freeSquares = 0;
if(onScreen(i-1,j)==-1) freeSquares++;
if(onScreen(i+1,j)==-1) freeSquares++;
if(onScreen(i,j-1)==-1) freeSquares++;
if(onScreen(i,j+1)==-1) freeSquares++;
if(onScreen(i-1,j-1)==-1) freeSquares++;
if(onScreen(i-1,j+1)==-1) freeSquares++;
if(onScreen(i+1,j-1)==-1) freeSquares++;
if(onScreen(i+1,j+1)==-1) freeSquares++;
return freeSquares;
}
// A boundry square is an unopened square with opened squares near it.
static boolean isBoundry(int[][] board, int i, int j){
if(board[i][j] != -1) return false;
boolean oU = false, oD = false, oL = false, oR = false;
if(i == 0) oU = true;
if(j == 0) oL = true;
if(i == BoardHeight-1) oD = true;
if(j == BoardWidth-1) oR = true;
boolean isBoundry = false;
if(!oU && board[i-1][j]>=0) isBoundry = true;
if(!oL && board[i][j-1]>=0) isBoundry = true;
if(!oD && board[i+1][j]>=0) isBoundry = true;
if(!oR && board[i][j+1]>=0) isBoundry = true;
if(!oU && !oL && board[i-1][j-1]>=0) isBoundry = true;
if(!oU && !oR && board[i-1][j+1]>=0) isBoundry = true;
if(!oD && !oL && board[i+1][j-1]>=0) isBoundry = true;
if(!oD && !oR && board[i+1][j+1]>=0) isBoundry = true;
return isBoundry;
}
// Attempt to deduce squares that we know have mines
// More specifically if number of squares around it = its number
static void attemptFlagMine() throws Throwable{
for(int i=0; i<BoardHeight; i++){
for(int j=0; j<BoardWidth; j++){
if(onScreen(i,j) >= 1){
int curNum = onScreen(i,j);
// Flag necessary squares
if(curNum == countFreeSquaresAround(onScreen,i,j)){
for(int ii=0; ii<BoardHeight; ii++){
for(int jj=0; jj<BoardWidth; jj++){
if(Math.abs(ii-i)<=1 && Math.abs(jj-j)<=1){
if(onScreen(ii,jj) == -1 && !flags[ii][jj]){
flags[ii][jj] = true;
flagOn(ii,jj);
}
}
}
}
}
}
}
}
}
// Attempt to deduce a spot that should be free and click it
// More specifically:
// Find a square where the number of flags around it is the same as it
// Then click every empty square around it
static void attemptMove() throws Throwable{
boolean success = false;
for(int i=0; i<BoardHeight; i++){
for(int j=0; j<BoardWidth; j++){
if(onScreen(i,j) >= 1){
// Count how many mines around it
int curNum = onScreen[i][j];
int mines = countFlagsAround(flags,i,j);
int freeSquares = countFreeSquaresAround(onScreen,i,j);
// Click on the deduced non-mine squares
if(curNum == mines && freeSquares > mines){
success = true;
// Use the chord or the classical algorithm
if(freeSquares - mines > 1){
chordOn(i,j);
onScreen[i][j] = 0; // hack to make it not overclick a square
continue;
}
// Old algorithm: don't chord
for(int ii=0; ii<BoardHeight; ii++){
for(int jj=0; jj<BoardWidth; jj++){
if(Math.abs(ii-i)<=1 && Math.abs(jj-j)<=1){
if(onScreen(ii,jj) == -1 && !flags[ii][jj]){
clickOn(ii,jj);
onScreen[ii][jj] = 0;
}
}
}
}
}
}
}
}
if(success) return;
// Bring in the big guns
tankSolver();
}
// Exactly what it says on the tin
static void guessRandomly() throws Throwable{
System.out.println("Attempting to guess randomly");
while(true){
int k = rand.nextInt(BoardHeight*BoardWidth);
int i = k / BoardWidth;
int j = k % BoardWidth;
if(onScreen(i,j) == -1 && !flags[i][j]){
clickOn(i,j);
return;
}
}
}
// TANK solver: slow and heavyweight backtrack solver designed to
// solve any conceivable position! (in development)
static void tankSolver() throws Throwable{
// Be extra sure it's consistent
Thread.sleep(100);
robot.mouseMove(0,0);
Thread.sleep(20);
updateOnScreen();
robot.mouseMove(mouseLocX,mouseLocY);
//dumpPosition();
if(!checkConsistency()) return;
// Timing
long tankTime = System.currentTimeMillis();
ArrayList<Pair> borderTiles = new ArrayList<Pair>();
ArrayList<Pair> allEmptyTiles = new ArrayList<Pair>();
// Endgame case: if there are few enough tiles, don't bother with border tiles.
borderOptimization = false;
for(int i=0; i<BoardHeight; i++)
for(int j=0; j<BoardWidth; j++)
if(onScreen(i,j) == -1 && !flags[i][j])
allEmptyTiles.add(new Pair(i,j));
// Determine all border tiles
for(int i=0; i<BoardHeight; i++)
for(int j=0; j<BoardWidth; j++)
if(isBoundry(onScreen,i,j) && !flags[i][j])
borderTiles.add(new Pair(i,j));
// Count how many squares outside the knowable range
int numOutSquares = allEmptyTiles.size() - borderTiles.size();
if(numOutSquares > BF_LIMIT){
borderOptimization = true;
}
else borderTiles = allEmptyTiles;
// Something went wrong
if(borderTiles.size() == 0)
return;
// Run the segregation routine before recursing one by one
// Don't bother if it's endgame as doing so might make it miss some cases
ArrayList<ArrayList<Pair>> segregated;
if(!borderOptimization){
segregated = new ArrayList<ArrayList<Pair>>();
segregated.add(borderTiles);
}
else segregated = tankSegregate(borderTiles);
int totalMultCases = 1;
boolean success = false;
double prob_best = 0; // Store information about the best probability
int prob_besttile = -1;
int prob_best_s = -1;
for(int s = 0; s < segregated.size(); s++){
// Copy everything into temporary constructs
tank_solutions = new ArrayList<boolean[]>();
tank_board = onScreen.clone();
knownMine = flags.clone();
knownEmpty = new boolean[BoardHeight][BoardWidth];
for(int i=0; i<BoardHeight; i++)
for(int j=0; j<BoardWidth; j++)
if(tank_board[i][j] >= 0)
knownEmpty[i][j] = true;
else knownEmpty[i][j] = false;
// Compute solutions -- here's the time consuming step
tankRecurse(segregated.get(s),0);
// Something screwed up
if(tank_solutions.size() == 0) return;
// Check for solved squares
for(int i=0; i<segregated.get(s).size(); i++){
boolean allMine = true;
boolean allEmpty = true;
for(boolean[] sln : tank_solutions){
if(!sln[i]) allMine = false;
if(sln[i]) allEmpty = false;
}
Pair<Integer,Integer> q = segregated.get(s).get(i);
int qi = q.getFirst();
int qj = q.getSecond();
// Muahaha
if(allMine){
flags[qi][qj] = true;
flagOn(qi,qj);
}
if(allEmpty){
success = true;
clickOn(qi,qj);
}
}
totalMultCases *= tank_solutions.size();
// Calculate probabilities, in case we need it
if(success) continue;
int maxEmpty = -10000;
int iEmpty = -1;
for(int i=0; i<segregated.get(s).size(); i++){
int nEmpty = 0;
for(boolean[] sln : tank_solutions){
if(!sln[i]) nEmpty++;
}
if(nEmpty > maxEmpty){
maxEmpty = nEmpty;
iEmpty = i;
}
}
double probability = (double)maxEmpty / (double)tank_solutions.size();
if(probability > prob_best){
prob_best = probability;
prob_besttile = iEmpty;
prob_best_s = s;
}
}
// But wait! If there's any hope, bruteforce harder (by a factor of 32x)!
if(BF_LIMIT == 8 && numOutSquares > 8 && numOutSquares <= 13){
System.out.println("Extending bruteforce horizon...");
BF_LIMIT = 13;
tankSolver();
BF_LIMIT = 8;
return;
}
tankTime = System.currentTimeMillis() - tankTime;
if(success){
System.out.printf(
"TANK Solver successfully invoked at step %d (%dms, %d cases)%s\n",
numMines, tankTime, totalMultCases, (borderOptimization?"":"*"));
return;
}
// Take the guess, since we can't deduce anything useful
System.out.printf(
"TANK Solver guessing with probability %1.2f at step %d (%dms, %d cases)%s\n",
prob_best, numMines, tankTime, totalMultCases,
(borderOptimization?"":"*"));
Pair<Integer,Integer> q = segregated.get(prob_best_s).get(prob_besttile);
int qi = q.getFirst();
int qj = q.getSecond();
clickOn(qi,qj);
}
// Segregation routine: if two regions are independant then consider
// them as separate regions
static ArrayList<ArrayList<Pair>>
tankSegregate(ArrayList<Pair> borderTiles){
ArrayList<ArrayList<Pair>> allRegions = new ArrayList<ArrayList<Pair>>();
ArrayList<Pair> covered = new ArrayList<Pair>();
while(true){
LinkedList<Pair> queue = new LinkedList<Pair>();
ArrayList<Pair> finishedRegion = new ArrayList<Pair>();
// Find a suitable starting point
for(Pair firstT : borderTiles){
if(!covered.contains(firstT)){
queue.add(firstT);
break;
}
}
if(queue.isEmpty())
break;
while(!queue.isEmpty()){
Pair<Integer,Integer> curTile = queue.poll();
int ci = curTile.getFirst();
int cj = curTile.getSecond();
finishedRegion.add(curTile);
covered.add(curTile);
// Find all connecting tiles
for(Pair<Integer,Integer> tile : borderTiles){
int ti = tile.getFirst();
int tj = tile.getSecond();
boolean isConnected = false;
if(finishedRegion.contains(tile))
continue;
if(Math.abs(ci-ti)>2 || Math.abs(cj-tj) > 2)
isConnected = false;
else{
// Perform a search on all the tiles
tilesearch:
for(int i=0; i<BoardHeight; i++){
for(int j=0; j<BoardWidth; j++){
if(onScreen(i,j) > 0){
if(Math.abs(ci-i) <= 1 && Math.abs(cj-j) <= 1 &&
Math.abs(ti-i) <= 1 && Math.abs(tj-j) <= 1){
isConnected = true;
break tilesearch;
}
}
}
}
}
if(!isConnected) continue;
if(!queue.contains(tile))
queue.add(tile);
}
}
allRegions.add(finishedRegion);
}
return allRegions;
}
static int[][] tank_board = null;
static boolean[][] knownMine = null;
static boolean[][] knownEmpty = null;
static ArrayList<boolean[]> tank_solutions;
// Should be true -- if false, we're bruteforcing the endgame
static boolean borderOptimization;
static int BF_LIMIT = 8;
// Recurse from depth k (0 is root)
// Assumes the tank variables are already set; puts solutions in
// the static arraylist.
static void tankRecurse(ArrayList<Pair> borderTiles, int k){
// Return if at this point, it's already inconsistent
int flagCount = 0;
for(int i=0; i<BoardHeight; i++)
for(int j=0; j<BoardWidth; j++){
// Count flags for endgame cases
if(knownMine[i][j])
flagCount++;
int num = tank_board[i][j];
if(num < 0) continue;
// Total bordering squares
int surround = 0;
if((i==0&&j==0) || (i==BoardHeight-1 && j==BoardWidth-1))
surround = 3;
else if(i==0 || j==0 || i==BoardHeight-1 || j==BoardWidth-1)
surround = 5;
else surround = 8;
int numFlags = countFlagsAround(knownMine, i,j);
int numFree = countFlagsAround(knownEmpty, i,j);
// Scenario 1: too many mines
if(numFlags > num) return;
// Scenario 2: too many empty
if(surround - numFree < num) return;
}
// We have too many flags
if(flagCount > TOT_MINES)
return;
// Solution found!
if(k == borderTiles.size()){
// We don't have the exact mine count, so no
if(!borderOptimization && flagCount < TOT_MINES)
return;
boolean[] solution = new boolean[borderTiles.size()];
for(int i=0; i<borderTiles.size(); i++){
Pair<Integer,Integer> s = borderTiles.get(i);
int si = s.getFirst();
int sj = s.getSecond();
solution[i] = knownMine[si][sj];
}
tank_solutions.add(solution);
return;
}
Pair<Integer,Integer> q = borderTiles.get(k);
int qi = q.getFirst();
int qj = q.getSecond();
// Recurse two positions: mine and no mine
knownMine[qi][qj] = true;
tankRecurse(borderTiles, k+1);
knownMine[qi][qj] = false;
knownEmpty[qi][qj] = true;
tankRecurse(borderTiles, k+1);
knownEmpty[qi][qj] = false;
}
/*
todo -
read / write calibration settings
Minor defects / bugs:
1) Calibration routine kind of sucks, can't calibrate at
small resolutions and can't calibrate non-empty board
2) Death / win detection kind of sucks, can't distinguish
win from loss and sometimes fails to detect either
3) Clicking order is highly non-human
4) Endgame solver is inefficient: we could make it kick in earlier if
it was more efficient
Known but non-fixable defects:
1) Cannot automatically detect number of mines
*/
public static void main(String[] args) throws Throwable {
Thread.sleep(2000);
robot = new Robot();
// Keep these as these are the most common settings
BoardWidth = 30;
BoardHeight = 16;
/*
BoardPix = 35.267857;
BoardTopW = 175;
BoardTopH = 120;
*/
BoardPix = 42.035714;
BoardTopW = 193;
BoardTopH = 134;
// Determine board height and width and position
calibrate();
if(BoardWidth < 9 || BoardHeight < 9 || BoardWidth > 30 || BoardWidth > 30){
System.out.println("Calibration Failed.");
return;
}
// Initialize internal constructs
onScreen = new int[BoardHeight][BoardWidth];
flags = new boolean[BoardHeight][BoardWidth];