-
Notifications
You must be signed in to change notification settings - Fork 1
/
GameSolver_uncompressed.js
4798 lines (4265 loc) · 218 KB
/
GameSolver_uncompressed.js
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
// ***************************************
// ********** GameSolver script **********
// ***************************************
"use strict";
if (typeof debug_game_state !== 'undefined') {
debug_game_state = 76;
}
// *************************************************************************
// *************************************************************************
// Global constants (shared between threads)
// *************************************************************************
// *************************************************************************
let emptyColor = 0; // (0 is also the Java default table init value)
let nbMinColors = 5;
let nbMaxColors = 10;
let nbMinColumns = 3;
let nbMaxColumns = 7; // (value hardcoded elsewhere: search "nbMaxColumns")
let overallNbMinAttempts = 4;
let overallNbMaxAttempts = 14;
let overallMaxDepth = 15;
let PerformanceNA = -3.00;
let PerformanceUNKNOWN = -2.00;
let PerformanceMinValidValue = -1.60; // (a valid relative performance can be < -1.00 in some extremely rare cases / Value observed: -1.35 for 5-colmuns game {1B4W 12345; 1B4W 51234; 45123} (SCODE: 25314))
let PerformanceMaxValidValue = +1.30; // (a valid relative performance can be > 0.00 in some rare (impossible code) cases / Some values observed: +0.94 for 5-colmuns game {4B0W 11223; 11456}, +1.04 for 6-colmuns game {5B0W 112234; 112567}
let PerformanceLOW = -0.25;
let PerformanceVERYLOW = -0.50;
// *************************************************************************
// *************************************************************************
// Global CodeHandler class (shared between threads)
// *************************************************************************
// *************************************************************************
class CodeHandler {
constructor(nbColumns_p, nbColors_p, nbMinColumns_p, nbMaxColumns_p, emptyColor_p, game_solver_call_p) {
if ((nbColumns_p == undefined) || !Number.isInteger(nbColumns_p)) {
throw new Error("CodeHandler: invalid nbColumns_p");
}
if ((nbColors_p == undefined) || !Number.isInteger(nbColors_p)) {
throw new Error("CodeHandler: invalid nbColors_p");
}
if ((nbMinColumns_p == undefined) || !Number.isInteger(nbMinColumns_p)) {
throw new Error("CodeHandler: invalid nbMinColumns_p");
}
if ((nbMaxColumns_p == undefined) || !Number.isInteger(nbMaxColumns_p)) {
throw new Error("CodeHandler: invalid nbMaxColumns_p");
}
if ((emptyColor_p == undefined) || !Number.isInteger(emptyColor_p)) {
throw new Error("CodeHandler: invalid emptyColor_p");
}
if ((game_solver_call_p == undefined) || ((game_solver_call_p != true) && (game_solver_call_p != false))) {
throw new Error("CodeHandler: invalid game_solver_call_p");
}
if ( (nbColumns_p < Math.max(nbMinColumns_p,3)) || (nbColumns_p > Math.min(nbMaxColumns_p,7)) /* 3 and 7 is hardcoded in some methods of this class for better performances */ ) {
throw new Error("CodeHandler: invalid nb of columns (" + nbColumns_p + ", " + nbMinColumns_p + "," + nbMaxColumns_p + ")");
}
if (nbColors_p < 0) {
throw new Error("CodeHandler: invalid nb of colors: (" + nbColors_p + ")");
}
this.nbColumns = nbColumns_p;
this.nbColors = nbColors_p;
this.nbMinColumns = nbMinColumns_p;
this.nbMaxColumns = nbMaxColumns_p;
this.emptyColor = emptyColor_p;
this.game_solver_call = game_solver_call_p;
this.code1_colors = new Array(this.nbMaxColumns);
this.code2_colors = new Array(this.nbMaxColumns);
this.colors_int = new Array(this.nbMaxColumns);
this.different_colors = new Array(this.nbColors+1);
this.different_colors_bis = new Array(this.nbColors+1);
// Attributes useful for getSMMCodeClassId() method:
this.complete_game = new Array(overallNbMaxAttempts+1);
this.different_game_colors_per_row = new Array(overallNbMaxAttempts+1);
for (let i = 0; i < overallNbMaxAttempts+1; i++) {
this.different_game_colors_per_row[i] = new Array(this.nbColors+1);
}
this.different_game_colors_per_column = new Array(this.nbColumns);
for (let i = 0; i < this.nbColumns; i++) {
this.different_game_colors_per_column[i] = new Array(this.nbColors+1);
}
this.color_correlation_matrix = new Array(this.nbColors+1);
for (let i = 0; i < this.nbColors+1; i++) {
this.color_correlation_matrix[i] = new Array(this.nbColors+1);
}
}
getNbColumns() {
return this.nbColumns;
}
getColor(code, column) {
switch (column) {
case 1:
return (code & 0x0000000F);
case 2:
return ((code >> 4) & 0x0000000F);
case 3:
return ((code >> 8) & 0x0000000F);
case 4:
return ((code >> 12) & 0x0000000F);
case 5:
return ((code >> 16) & 0x0000000F);
case 6:
return ((code >> 20) & 0x0000000F);
case 7:
return ((code >> 24) & 0x0000000F);
default:
throw new Error("CodeHandler: getColor (" + column + ")");
}
}
setColor(code, color, column) {
switch (column) {
case 1:
return ((code & 0xFFFFFFF0) | color);
case 2:
return ((code & 0xFFFFFF0F) | (color << 4));
case 3:
return ((code & 0xFFFFF0FF) | (color << 8));
case 4:
return ((code & 0xFFFF0FFF) | (color << 12));
case 5:
return ((code & 0xFFF0FFFF) | (color << 16));
case 6:
return ((code & 0xFF0FFFFF) | (color << 20));
case 7:
return ((code & 0xF0FFFFFF) | (color << 24));
default:
throw new Error("CodeHandler: setColor (" + column + ")");
}
}
setAllColors(color1, color2, color3, color4, color5, color6, color7) {
return color1
| (color2 << 4)
| (color3 << 8)
| (color4 << 12)
| (color5 << 16)
| (color6 << 20)
| (color7 << 24);
}
setAllColorsIdentical(color) {
let res_code = 0;
for (let col = 0; col < this.nbColumns; col++) {
res_code = this.setColor(res_code, color, col+1);
}
return res_code;
}
nbDifferentColors(code) {
let sum = 0;
this.different_colors.fill(0);
for (let col = 0; col < this.nbColumns; col++) {
let color = this.getColor(code, col+1);
if (this.different_colors[color] == 0) {
this.different_colors[color] = 1;
sum = sum + 1;
}
}
return sum;
}
nbDifferentColorsInListOfCodes(list_of_codes, nb_codes) {
let sum = 0;
this.different_colors.fill(0);
for (let i = 0; i < nb_codes; i++) {
for (let col = 0; col < this.nbColumns; col++) {
let color = this.getColor(list_of_codes[i], col+1);
if (this.different_colors[color] == 0) {
this.different_colors[color] = 1;
sum = sum + 1;
}
}
}
return sum;
}
sameColorsReused(code1, code2) {
for (let col2 = 0; col2 < this.nbColumns; col2++) {
let color2 = this.getColor(code2, col2+1);
let colorReused = false;
for (let col1 = 0; col1 < this.nbColumns; col1++) {
if (color2 == this.getColor(code1, col1+1)) {
colorReused = true;
break;
}
}
if (!colorReused) {
return false;
}
}
return true;
}
getSMMGameIdAfter2Attempts(code1, code2) {
// ***** CODE DUPLICATED IN extractPrecalculatedPerfs.java ******
if (this.nbColumns != 5) { // function only for Super Master Mind games
throw new Error("CodeHandler: getGameIdFrom2Codes (" + this.nbColumns + ")");
}
let nbBlacks = 0;
let nbWhites = 0;
let col, col1, col2;
this.colors_int[0] = true;
this.colors_int[1] = true;
this.colors_int[2] = true;
this.colors_int[3] = true;
this.colors_int[4] = true;
this.code1_colors[0] = (code1 & 0x0000000F);
this.code1_colors[1] = ((code1 >> 4) & 0x0000000F);
this.code1_colors[2] = ((code1 >> 8) & 0x0000000F);
this.code1_colors[3] = ((code1 >> 12) & 0x0000000F);
this.code1_colors[4] = ((code1 >> 16) & 0x0000000F);
this.code2_colors[0] = (code2 & 0x0000000F);
this.code2_colors[1] = ((code2 >> 4) & 0x0000000F);
this.code2_colors[2] = ((code2 >> 8) & 0x0000000F);
this.code2_colors[3] = ((code2 >> 12) & 0x0000000F);
this.code2_colors[4] = ((code2 >> 16) & 0x0000000F);
this.different_colors.fill(0);
for (let col = 0; col < this.nbColumns; col++) {
let color = this.code1_colors[col];
this.different_colors[color]++;
}
this.different_colors_bis.fill(0);
for (let col = 0; col < this.nbColumns; col++) {
let color = this.code2_colors[col];
this.different_colors_bis[color]++;
}
// 1) Mark
// (duplicated code from fillMark())
for (col1 = 0; col1 < this.nbColumns; col1++) {
if (this.code1_colors[col1] == this.code2_colors[col1]) {
nbBlacks++;
}
else {
for (col2 = 0; col2 < this.nbColumns; col2++) {
if ((this.code1_colors[col1] == this.code2_colors[col2]) && (this.code1_colors[col2] != this.code2_colors[col2]) && this.colors_int[col2]) {
this.colors_int[col2] = false;
nbWhites++;
break;
}
}
}
}
let res1 = nbBlacks * 10 + nbWhites;
// 2) Total number of colors
let totalnbcolors = 0;
for (let color = 1; color <= this.nbColors; color++) {
if ((this.different_colors[color] > 0) || (this.different_colors_bis[color] > 0)) {
totalnbcolors++;
}
}
// 3) Ponderated color correspondance (does not vary when permuting columns)
let res2 = 0;
for (col = 0; col < this.nbColumns; col++) {
let color1 = this.code1_colors[col];
let color2 = this.code2_colors[col];
let delta = this.different_colors[color1] * (this.different_colors_bis[color2] + 10)
* (this.different_colors[color2] + 100) * (this.different_colors_bis[color1] + 1000);
res2 = res2 + delta;
}
let final_res = totalnbcolors + res1 * 10 + res2 * 1000;
if (final_res <= 0) {
throw new Error("CodeHandler: getSMMGameIdAfter2Attempts - invalid final_res value: " + final_res);
}
return final_res;
}
getSMMCodeClassId(code, game = null, game_size = 0) {
if (this.nbColumns != 5) { // function only for Super Master Mind games
throw new Error("CodeHandler: getSMMCodeClassId (" + this.nbColumns + ")");
}
this.different_colors.fill(0);
for (let col = 0; col < this.nbColumns; col++) {
let color = this.getColor(code, col+1);
this.different_colors[color]++;
}
let extra_game_id = 0;
if ((game != null) && (game_size >= 1)) {
// Initializations
// ***************
let complete_game_size = game_size+1;
if (complete_game_size > overallNbMaxAttempts+1) {
throw new Error("CodeHandler: getSMMCodeClassId - internal error #1");
}
this.complete_game.fill(0);
for (let i = 0; i < game_size; i++) {
this.complete_game[i] = game[i];
}
this.complete_game[game_size] = code;
for (let row = 0; row < complete_game_size; row++) {
this.different_game_colors_per_row[row].fill(0);
for (let col = 0; col < this.nbColumns; col++) {
let color = this.getColor(this.complete_game[row], col+1);
this.different_game_colors_per_row[row][color]++;
}
}
for (let col = 0; col < this.nbColumns; col++) {
this.different_game_colors_per_column[col].fill(0);
for (let row = 0; row < complete_game_size; row++) {
let color = this.getColor(this.complete_game[row], col+1);
this.different_game_colors_per_column[col][color]++;
}
}
for (let i = 0; i < this.nbColors+1; i++) {
this.color_correlation_matrix[i].fill(0);
}
// Column-based color correlations
// *******************************
for (let col = 0; col < this.nbColumns; col++) {
for (let row1 = 0; row1 < complete_game_size; row1++) {
for (let row2 = 0; row2 < complete_game_size; row2++) {
if (row1 < row2) { // Go through all pairs of colors in current column
let color1 = this.getColor(this.complete_game[row1], col+1);
let color2 = this.getColor(this.complete_game[row2], col+1);
let color_min;
let color_max;
if (color1 <= color2) {
color_min = color1;
color_max = color2;
}
else {
color_min = color2;
color_max = color1;
}
let coef = ((row1+1) * 0xA26970) ^ ((row2+1) * 0xF14457) // (Rq: no permutations on rows)
^ (this.different_game_colors_per_row[row1][color1] * 0x749841) ^ (this.different_game_colors_per_row[row2][color2] * 0x369874)
^ (this.different_game_colors_per_row[row1][color2] * 0xB54796) ^ (this.different_game_colors_per_row[row2][color1] * 0x252241);
if (color_min == color_max) {
coef = coef ^ 0x5C1148;
}
this.color_correlation_matrix[color_min][color_max] = this.color_correlation_matrix[color_min][color_max] ^ coef;
}
}
}
}
// Row-based color correlations
// ****************************
for (let row = 0; row < complete_game_size; row++) {
for (let col1 = 0; col1 < this.nbColumns; col1++) {
for (let col2 = 0; col2 < this.nbColumns; col2++) {
if (col1 < col2) { // Go through all pairs of colors in current row
let color1 = this.getColor(this.complete_game[row], col1+1);
let color2 = this.getColor(this.complete_game[row], col2+1);
let color_min;
let color_max;
if (color1 <= color2) {
color_min = color1;
color_max = color2;
}
else {
color_min = color2;
color_max = color1;
}
let common_mask_1 = 0xA49875;
let common_mask_2 = 0xCE84F4;
let coef = ((row+1) * 0x2A3698) // (Rq: no permutations on rows)
^ (this.different_game_colors_per_column[col1][color1] * common_mask_1) ^ (this.different_game_colors_per_column[col2][color2] * common_mask_1)
^ (this.different_game_colors_per_column[col2][color1] * common_mask_2) ^ (this.different_game_colors_per_column[col1][color2] * common_mask_2);
if (color_min == color_max) {
coef = coef ^ 0x533E16;
}
this.color_correlation_matrix[color_min][color_max] = this.color_correlation_matrix[color_min][color_max] ^ coef;
}
}
}
}
// Color decorrelations
// ********************
for (let col1 = 0; col1 < this.nbColumns; col1++) {
for (let row1 = 0; row1 < complete_game_size; row1++) {
let color1 = this.getColor(this.complete_game[row1], col1+1);
for (let col2 = 0; col2 < this.nbColumns; col2++) {
if (col1 != col2) {
for (let row2 = 0; row2 < complete_game_size; row2++) {
if (row1 < row2) {
let color2 = this.getColor(this.complete_game[row2], col2+1);
if (color1 != color2) { // Go through all pairs of different colors not in current column and row
if ( (this.different_game_colors_per_row[row1][color2] == 0) && (this.different_game_colors_per_row[row2][color1] == 0)
&& (this.different_game_colors_per_column[col1][color2] == 0) && (this.different_game_colors_per_column[col2][color1] == 0) ) {
let color_min;
let color_max;
if (color1 <= color2) {
color_min = color1;
color_max = color2;
}
else {
color_min = color2;
color_max = color1;
}
let coef = ((row1+1) * 0xB48725) ^ ((row2+1) * 0x67F428); // (Rq: no permutations on rows)
this.color_correlation_matrix[color_min][color_max] = this.color_correlation_matrix[color_min][color_max] ^ coef;
}
}
}
}
}
}
}
}
// Unused colors
// *************
let nbUnusedColors = 0;
for (let color = 1; color <= this.nbColors; color++) {
let isColorUsedInCurrentGame = false;
for (let row = 0; row < complete_game_size; row++) {
for (let col = 0; col < this.nbColumns; col++) {
if (color == this.getColor(this.complete_game[row], col+1)) {
isColorUsedInCurrentGame = true;
break;
}
}
if (isColorUsedInCurrentGame) {
break;
}
}
if (!isColorUsedInCurrentGame) {
nbUnusedColors = nbUnusedColors + 1;
}
}
// Infer number from color correlation matrix
// ******************************************
for (let i = 0; i < this.nbColors+1; i++) {
for (let j = 0; j < this.nbColors+1; j++) {
extra_game_id = extra_game_id + this.color_correlation_matrix[i][j];
}
}
if (extra_game_id <= 0) {
throw new Error("CodeHandler: getSMMCodeClassId - internal error #2: " + extra_game_id);
}
extra_game_id = extra_game_id + nbUnusedColors * 444;
if (extra_game_id != Math.floor(extra_game_id)) {
throw new Error("CodeHandler: getSMMCodeClassId - internal error #3: " + extra_game_id);
}
}
// Basic SMM code class ids: {100: 11111, 200: 11112, 300: 11122, 400: 11123, 500: 11223, 600: 11234, 700: 12345}
let is_there_triple = false;
let nb_doubles = 0;
for (let color = 1; color <= this.nbColors; color++) {
let nb_different_colors = this.different_colors[color];
if (nb_different_colors == 2) {
nb_doubles++;
}
else if (nb_different_colors == 3) {
is_there_triple = true;
}
else if (nb_different_colors == 4) {
return 200 + extra_game_id;
}
else if (nb_different_colors == 5) {
return 100 + extra_game_id;
}
}
if (is_there_triple) {
if (nb_doubles == 0) {
return 400 + extra_game_id;
}
else if (nb_doubles == 1) {
return 300 + extra_game_id;
}
else {
throw new Error("CodeHandler: getSMMCodeClassId - internal error #4");
}
}
else {
if (nb_doubles == 0) {
return 700 + extra_game_id;
}
else if (nb_doubles == 1) {
return 600 + extra_game_id;
}
else if (nb_doubles == 2) {
return 500 + extra_game_id;
}
else {
throw new Error("CodeHandler: getSMMCodeClassId - internal error #5");
}
}
}
isVerySimple(code) {
this.different_colors.fill(0);
for (let col = 0; col < this.nbColumns; col++) {
let color = this.getColor(code, col+1);
this.different_colors[color]++;
}
for (let color = 0; color <= this.nbColors; color++) {
if (this.different_colors[color] == this.nbColumns) {
return true; // "111...1" like codes
}
else if (this.different_colors[color] == this.nbColumns - 1) {
return true; // "122...2" like codes
}
}
return false;
}
codeToString(code) {
let res = "[ ";
for (let col = 0; col < this.nbColumns; col++) {
let color = this.getColor(code, col+1);
res = res + color + " ";
}
res = res + "]";
return res;
}
compressCodeToString(code) {
let res = "";
for (let col = 0; col < this.nbColumns; col++) {
let color = this.getColor(code, col+1);
res = res + color.toString(16).toUpperCase(); // (hexa number used if >= 10)
}
return res;
}
uncompressStringToCode(str) {
let code = 0; // empty code
if (str.length != this.nbColumns) {
throw new Error("CodeHandler: uncompressStringToCode (1) (" + str + ")");
}
for (let col = 0; col < this.nbColumns; col++) {
let color = Number("0x" + str.substring(col, col+1)); // (hexa number parsing)
code = this.setColor(code, color, col+1);
}
if (!this.isFullAndValid(code)) {
throw new Error("CodeHandler: uncompressStringToCode (2) (" + str + ")");
}
return code;
}
isValid(code) {
for (let col = 0; col < this.nbColumns; col++) {
let color = this.getColor(code, col+1);
if ( ((color < 1) || (color > this.nbColors))
&& (color != this.emptyColor) ) {
return false;
}
}
for (let col = this.nbColumns+1; col <= this.nbMaxColumns; col++) {
let color = this.getColor(code, col);
if (color != this.emptyColor) {
return false;
}
}
return true;
}
isFullAndValid(code) {
for (let col = 0; col < this.nbColumns; col++) {
let color = this.getColor(code, col+1);
if ( (color < 1) || (color > this.nbColors)
|| (color == this.emptyColor) ) {
return false;
}
}
for (let col = this.nbColumns+1; col <= this.nbMaxColumns; col++) {
let color = this.getColor(code, col);
if (color != this.emptyColor) {
return false;
}
}
return true;
}
nbEmptyColors(code) {
let cnt = 0;
for (let col = 0; col < this.nbColumns; col++) {
if (this.getColor(code, col+1) == this.emptyColor) {
cnt++;
}
}
return cnt;
}
isEmpty(code) {
return (code == 0); // only emptyColor in the code
}
replaceEmptyColor(code, emptyColorIdx, code2) {
let cnt = 0;
for (let col = 0; col < this.nbColumns; col++) {
if (this.getColor(code, col+1) == this.emptyColor) {
if (cnt == emptyColorIdx) {
return this.setColor(code, this.getColor(code2, col+1), col+1);
}
cnt++;
}
}
return code;
}
// Get a mark between 2 codes
getMark(code1, code2) {
let mark = {nbBlacks:0, nbWhites:0};
this.fillMark(code1, code2, mark);
return mark;
}
// Fill a mark between 2 codes in a fast way
fillMark(code1, code2, mark) { // (duplicated code)
let nbBlacks = 0;
let nbWhites = 0;
let col1, col2;
// The below operations are unrolled for better performances (ruling out [5] and [6] index updates when this.nbColumns <= 5 does not bring measurable gains)
this.colors_int[0] = true;
this.colors_int[1] = true;
this.colors_int[2] = true;
this.colors_int[3] = true;
this.colors_int[4] = true;
this.colors_int[5] = true;
this.colors_int[6] = true;
this.code1_colors[0] = (code1 & 0x0000000F);
this.code1_colors[1] = ((code1 >> 4) & 0x0000000F);
this.code1_colors[2] = ((code1 >> 8) & 0x0000000F);
this.code1_colors[3] = ((code1 >> 12) & 0x0000000F);
this.code1_colors[4] = ((code1 >> 16) & 0x0000000F);
this.code1_colors[5] = ((code1 >> 20) & 0x0000000F);
this.code1_colors[6] = ((code1 >> 24) & 0x0000000F);
this.code2_colors[0] = (code2 & 0x0000000F);
this.code2_colors[1] = ((code2 >> 4) & 0x0000000F);
this.code2_colors[2] = ((code2 >> 8) & 0x0000000F);
this.code2_colors[3] = ((code2 >> 12) & 0x0000000F);
this.code2_colors[4] = ((code2 >> 16) & 0x0000000F);
this.code2_colors[5] = ((code2 >> 20) & 0x0000000F);
this.code2_colors[6] = ((code2 >> 24) & 0x0000000F);
for (col1 = 0; col1 < this.nbColumns; col1++) {
if (this.code1_colors[col1] == this.code2_colors[col1]) {
nbBlacks++;
}
else {
for (col2 = 0; col2 < this.nbColumns; col2++) {
if ((this.code1_colors[col1] == this.code2_colors[col2]) && (this.code1_colors[col2] != this.code2_colors[col2]) && this.colors_int[col2]) {
this.colors_int[col2] = false;
nbWhites++;
break;
}
}
}
}
mark.nbBlacks = nbBlacks;
mark.nbWhites = nbWhites;
}
marksEqual(mark1, mark2) {
return ( (mark1.nbBlacks == mark2.nbBlacks) && (mark1.nbWhites == mark2.nbWhites) );
}
isMarkValid(mark) {
if ( (mark.nbBlacks >= 0) && (mark.nbWhites >= 0) && (mark.nbBlacks + mark.nbWhites <= this.nbColumns)
&& !((mark.nbBlacks == this.nbColumns - 1) && (mark.nbWhites == 1)) ) {
return true;
}
return false;
}
markToString(mark) {
return mark.nbBlacks + "B" + mark.nbWhites + "W";
}
stringToMark(str, mark) {
if (str.length != 4) {
throw new Error("CodeHandler: stringToMark (1) (" + str + ")");
}
let index_blacks = str.indexOf("B");
if (index_blacks != 1) {
throw new Error("CodeHandler: stringToMark (2) (" + str + ")");
}
let index_whites = str.indexOf("W", index_blacks);
if (index_whites != 3) {
throw new Error("CodeHandler: stringToMark (3) (" + str + ")");
}
mark.nbBlacks = Number(str.substring(0,1));
mark.nbWhites = Number(str.substring(2,3));
if (!this.isMarkValid(mark)) {
throw new Error("CodeHandler: stringToMark (4) (" + str + ")");
}
}
convert(code) {
return ~code;
}
}
if (typeof debug_game_state !== 'undefined') {
debug_game_state = 76.1;
}
// ************************************************
// ************************************************
// ************************************************
let END_OF_COMMON_DEFINITIONS;
// ************************************************
// ************************************************
// ************************************************
if (typeof debug_game_state !== 'undefined') {
debug_game_state = 76.2;
}
// *************************************************************************
// *************************************************************************
// GsCodeHandler class
// *************************************************************************
// *************************************************************************
class GsCodeHandler extends CodeHandler {
constructor(nbColumns_p, nbColors_p, nbMinColumns_p, nbMaxColumns_p, emptyColor_p) {
super(nbColumns_p, nbColors_p, nbMinColumns_p, nbMaxColumns_p, emptyColor_p, true);
}
}
// *************************************************************************
// *************************************************************************
// GameSolver variables
// *************************************************************************
// *************************************************************************
let init_done = false;
let nbColumns = -1;
let nbColors = -1;
let nbMaxAttempts = -1;
let nbMaxPossibleCodesShown = -1;
let possibleCodesShown;
let globalPerformancesShown;
let game_id = -1;
let codesPlayed;
let marks;
let codeHandler;
let initialNbPossibleCodes = -1;
let previousNbOfPossibleCodes = -1;
let nextNbOfPossibleCodes = -1;
let colorsFoundCode = -1;
let minNbColorsTable;
let maxNbColorsTable;
let nbColorsTableForMinMaxNbColors;
let nbMaxMarks = -1;
let marksTable_MarkToNb;
let marksTable_NbToMark;
let best_mark_idx;
let worst_mark_idx;
let possibleCodesAfterNAttempts;
let curAttemptNumber = 0;
let nbMaxAttemptsForEndOfGame = -1;
let message_processing_ongoing = false;
let IAmAliveMessageSent = false;
let buffer_incoming_messages = false;
let nb_incoming_messages_buffered = 0;
let incoming_messages_table = new Array(3*overallMaxDepth);
// Performance-related variables
// *****************************
let maxPerformanceEvaluationTime = -1;
let appliedMaxPerformanceEvaluationTime = -1;
let extraTimeForSimplisticGames = 15000; // 15 seconds
let maxAllowedExtraTime = 35000; // 35 seconds
let factorForMaxPerformanceEvaluationTime = 1000; // 1000 for 1 second - shall be much higher in (precalculation mode)
let refNbOfCodesForSystematicEvaluation = 3200; // (high values may induce latencies)
let refNbOfCodesForSystematicEvaluation_AllCodesEvaluated = 3200; // (shall be <= refNbOfCodesForSystematicEvaluation - high values may induce latencies)
let nbOfCodesForSystematicEvaluation = -1;
let nbOfCodesForSystematicEvaluation_AllCodesEvaluated = -1;
let nbOfCodesForSystematicEvaluation_ForMemAlloc = -1;
let refNbCodesLimitForMarkOptimization = 1500;
let nbCodesLimitForMarkOptimization = -1;
let initialNbClasses = -1;
let curNbClasses = -1;
let possibleCodesForPerfEvaluation;
let possibleCodesForPerfEvaluation_lastIndexWritten = -1;
let possibleCodesForPerfEvaluation_InitialIndexes = null;
let possibleCodesForPerfEvaluation_InitialCodesPt = null;
let possibleCodesForPerfEvaluation_OptimizedCodes = null;
// let initialCodeListForPrecalculatedMode; // (precalculation mode)
let mem_reduc_factor = 0.90; // (too low values can lead to dynamic memory allocations)
let maxDepth = -1;
let maxDepthApplied = -1;
let listOfClassIds = null;
let performanceListsInitDone = false;
let performanceListsInitDoneForPrecalculatedGames = false;
let arraySizeAtInit = -1;
let listOfGlobalPerformances;
let listsOfPossibleCodeIndexes;
let nbOfPossibleCodes;
let listOfEquivalentCodesAndPerformances;
let marks_already_computed_table = null;
let nbCodesLimitForEquivalentCodesCheck = 40; // (value determined empirically)
let initialInitDone = false;
let curGame;
let curGameSize;
let marksIdxs;
let all_permutations_table_size;
let all_permutations_table;
let cur_permutations_table_size;
let cur_permutations_table;
// *************************************************************************
// *************************************************************************
// Game precalculation
// *************************************************************************
// *************************************************************************
let minNbCodesForPrecalculation = 270;
let nbCodesForPrecalculationThreshold = Math.max(refNbOfCodesForSystematicEvaluation, minNbCodesForPrecalculation); // (shall be in [minNbCodesForPrecalculation, refNbOfCodesForSystematicEvaluation])
let maxDepthForGamePrecalculation = -1; // (-1 or 3)
let maxDepthForGamePrecalculation_ForMemAlloc = 10;
let curGameForGamePrecalculation = new Array(maxDepthForGamePrecalculation_ForMemAlloc);
curGameForGamePrecalculation.fill(0); // empty code
let marksIdxsForGamePrecalculation = new Array(maxDepthForGamePrecalculation_ForMemAlloc);
marksIdxsForGamePrecalculation.fill(-1);
let lookForCodeInPrecalculatedGamesReuseTable = null;
let lookForCodeInPrecalculatedGamesClassIdsTable = null;
let lookForCodeInPrecalculatedGamesLastlineStr = null;
let precalculation_mode_mark = {nbBlacks:0, nbWhites:0};
let precalculation_mode_mark_first_2_codes_at_depth2 = {nbBlacks:0, nbWhites:0};
// ***************************************************************************************************************
// Precalculated table for 4 columns
// for minNbCodesForPrecalculation = 300, nbCodesForPrecalculationThreshold = 1296, precalculation_time >= 3.5 sec
// ***************************************************************************************************************
let precalculated_games_4columns =
"0||N:1296|1111:13C7,1112:11C8,1122:1168,1123:110C,1234:115F.";
// ***************************************************************************************************************
// Precalculated table for 5 columns
// ***************************************************************************************************************
let precalculated_games_5columns =
"0||N:32768|11111:28B03,11112:25A19,11122:24BF0,11123:24501,11223:23ED9,11234:23F55,12345:244BA."; // (precalculation mode: TBC with depth-2 or depth-3 precalculations)
// ***************************
// Look for precalculated game
// ***************************
let dotStr = ".";
let separatorStr = "|";
let separator2Str = ":";
let separator3Str = ",";
let nbCodesPrefixStr = "N:";
let precalculated_mark = {nbBlacks:0, nbWhites:0};
// Returned value:
// - > 0 if both game and code were precalculated
// - 0 if only game was precalculated
// - -1 if nothing was precalculated
function lookForCodeInPrecalculatedGames(code_p, cur_game_size, nb_possible_codes_p, reuse_mode) {
if (cur_game_size > maxDepthForGamePrecalculation) {
throw new Error("lookForCodeInPrecalculatedGames: invalid game size: " + cur_game_size);
}
if ((reuse_mode != 0) && (reuse_mode != 1) && (reuse_mode != 2)) {
throw new Error("lookForCodeInPrecalculatedGames: invalid reuse_mode: " + reuse_mode);
}
let precalculated_games;
switch (nbColumns) {
case 4:
precalculated_games = precalculated_games_4columns;
break;
case 5:
precalculated_games = precalculated_games_5columns;
break;
default:
throw new Error("lookForCodeInPrecalculatedGames: invalid nbColumns value: " + nbColumns);
}
let validLookForCodeInPrecalculatedGamesReuseTables = ((lookForCodeInPrecalculatedGamesReuseTable != null) && (lookForCodeInPrecalculatedGamesClassIdsTable != null));
if (validLookForCodeInPrecalculatedGamesReuseTables) {
if (reuse_mode == 1) {
lookForCodeInPrecalculatedGamesReuseTable.fill(0);
lookForCodeInPrecalculatedGamesClassIdsTable.fill(0);
lookForCodeInPrecalculatedGamesLastlineStr = null;
}
else if (reuse_mode == 2) {
if (lookForCodeInPrecalculatedGamesLastlineStr == null) {
throw new Error("lookForCodeInPrecalculatedGames: null lookForCodeInPrecalculatedGamesLastlineStr");
}
precalculated_games = lookForCodeInPrecalculatedGamesLastlineStr;
}
else {
lookForCodeInPrecalculatedGamesLastlineStr = null;
}
}
else {
lookForCodeInPrecalculatedGamesLastlineStr = null;
}
let dot_index = 0;
let last_dot_index = 0;
while ((dot_index = precalculated_games.indexOf(dotStr, last_dot_index)) != -1) {
let line_str = precalculated_games.substring(last_dot_index, dot_index+1);
let last_line_str_index = dot_index - last_dot_index;
// Parse precalculated depth
// *************************
let separator_index1 = line_str.indexOf(separatorStr);
let depth = Number(line_str.substring(0, separator_index1));
if ((separator_index1 == -1) || isNaN(depth) || (depth < 0) || (depth > maxDepthForGamePrecalculation)) {
throw new Error("lookForCodeInPrecalculatedGames: invalid depth: " + depth);
}
if (depth != cur_game_size) {
// End of loop processing
last_dot_index = dot_index+1;
continue;
}
// Parse precalculated game
// ************************
let last_separator_index = separator_index1+1;
if (cur_game_size == 0) {
last_separator_index++;
}
else {
for (let i = 0; i < cur_game_size; i++) {
// Precalculated code
let separator_index2 = line_str.indexOf(separator2Str, last_separator_index);
let code_str = line_str.substring(last_separator_index, separator_index2);
let code = codeHandler.uncompressStringToCode(code_str);
// Precalculated mark
let separator_index3 = line_str.indexOf(separatorStr, separator_index2+1);
let mark_str = line_str.substring(separator_index2+1, separator_index3);
codeHandler.stringToMark(mark_str, precalculated_mark);
curGameForGamePrecalculation[i] = code;
marksIdxsForGamePrecalculation[i] = marksTable_MarkToNb[precalculated_mark.nbBlacks][precalculated_mark.nbWhites];
last_separator_index = separator_index3+1;
}
}
// Check marks equivalence
// ***********************