-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathisepic-chess.js
4211 lines (3354 loc) · 112 KB
/
isepic-chess.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
/** Copyright (c) 2025 Ajax Isepic (ajax333221) Licensed MIT */
/* jshint undef:true, unused:true, jquery:false, curly:true, latedef:nofunc, bitwise:false, eqeqeq:true, esversion:9 */
/* globals exports, define */
(function (windw, expts, defin) {
var Ic = (function (_WIN) {
var _VERSION = '8.7.1';
var _SILENT_MODE = true;
var _BOARDS = {};
var _EMPTY_SQR = 0;
var _PAWN = 1;
var _KNIGHT = 2;
var _BISHOP = 3;
var _ROOK = 4;
var _QUEEN = 5;
var _KING = 6;
var _DIRECTION_TOP = 1;
var _DIRECTION_TOP_RIGHT = 2;
var _DIRECTION_RIGHT = 3;
var _DIRECTION_BOTTOM_RIGHT = 4;
var _DIRECTION_BOTTOM = 5;
var _DIRECTION_BOTTOM_LEFT = 6;
var _DIRECTION_LEFT = 7;
var _DIRECTION_TOP_LEFT = 8;
var _SHORT_CASTLE = 1;
var _LONG_CASTLE = 2;
var _RESULT_ONGOING = '*';
var _RESULT_W_WINS = '1-0';
var _RESULT_B_WINS = '0-1';
var _RESULT_DRAW = '1/2-1/2';
var _DEFAULT_FEN = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1';
var _ALERT_LIGHT = 'light';
var _ALERT_DARK = 'dark';
var _ALERT_SUCCESS = 'success';
var _ALERT_WARNING = 'warning';
var _ALERT_ERROR = 'error';
var _MUTABLE_KEYS = [
'w',
'b',
'activeColor',
'nonActiveColor',
'fen',
'enPassantBos',
'halfMove',
'fullMove',
'moveList',
'currentMove',
'isRotated',
'isPuzzleMode',
'checks',
'isCheck',
'isCheckmate',
'isStalemate',
'isThreefold',
'isInsufficientMaterial',
'isFiftyMove',
'inDraw',
'promoteTo',
'manualResult',
'isHidden',
'legalUci',
'legalUciTree',
'legalRevTree',
'squares',
];
//---------------- helpers
function _promoteValHelper(qal) {
return _toInt(toAbsVal(qal) || _QUEEN, _KNIGHT, _QUEEN);
}
function _pgnResultHelper(str) {
var rtn;
rtn = '';
str = String(str).replace(/\s/g, '').replace(/o/gi, '0').replace(/½/g, '1/2');
if (str === _RESULT_ONGOING || str === _RESULT_W_WINS || str === _RESULT_B_WINS || str === _RESULT_DRAW) {
rtn = str;
}
return rtn;
}
function _strToValHelper(str) {
var temp, pc_exec, rtn;
rtn = 0;
block: {
if (!str) {
break block;
}
if (!Number.isNaN(str * 1) && _isIntOrStrInt(str)) {
rtn = _toInt(str, -_KING, _KING);
break block;
}
str = _trimSpaces(str);
if (/^[pnbrqk]$/i.test(str)) {
temp = str.toLowerCase();
rtn = ('pnbrqk'.indexOf(temp) + 1) * getSign(str === temp);
break block;
}
pc_exec = /^([wb])([pnbrqk])$/.exec(str.toLowerCase());
if (pc_exec) {
rtn = ('pnbrqk'.indexOf(pc_exec[2]) + 1) * getSign(pc_exec[1] === 'b');
break block;
}
}
return rtn;
}
function _strToBosHelper(str) {
var rtn;
rtn = null;
str = _trimSpaces(str);
if (str && /^[a-h][1-8]$/i.test(str)) {
rtn = str.toLowerCase();
}
return rtn;
}
function _arrToPosHelper(arr) {
var rank_pos, file_pos, rtn;
rtn = null;
if (_isArray(arr) && arr.length === 2) {
rank_pos = _toInt(arr[0]);
file_pos = _toInt(arr[1]);
if (rank_pos <= 7 && rank_pos >= 0 && file_pos <= 7 && file_pos >= 0) {
rtn = [rank_pos, file_pos];
}
}
return rtn;
}
function _pgnParserHelper(str) {
var g, temp, rgxp, mtch, meta_tags, move_list, game_result, last_index, rtn;
rtn = null;
block: {
if (!_isNonBlankStr(str)) {
break block;
}
meta_tags = {};
last_index = -1;
rgxp = /\[\s*(\w+)\s+\"([^\"]*)\"\s*\]/g;
str = str.replace(/“|”/g, '"');
while ((mtch = rgxp.exec(str))) {
last_index = rgxp.lastIndex;
meta_tags[_trimSpaces(mtch[1])] = _trimSpaces(mtch[2]);
}
if (last_index === -1) {
last_index = 0;
}
g = ' ' + _cleanSan(str.slice(last_index));
move_list = [];
last_index = -1;
rgxp = /\s+([1-9][0-9]*)*\s*\.*\s*\.*\s*([^\s]+)/g;
while ((mtch = rgxp.exec(g))) {
last_index = rgxp.lastIndex;
temp = mtch[0];
move_list.push(mtch[2]);
}
if (last_index === -1) {
break block;
}
game_result = _RESULT_ONGOING;
temp = _pgnResultHelper(temp);
if (temp) {
move_list.pop();
game_result = temp;
}
if (meta_tags.Result) {
temp = _pgnResultHelper(meta_tags.Result);
if (temp) {
meta_tags.Result = temp;
game_result = temp;
}
}
rtn = {
tags: meta_tags,
sanMoves: move_list,
result: game_result,
};
}
return rtn;
}
function _uciParserHelper(str) {
var rtn;
rtn = null;
block: {
if (!_isNonBlankStr(str)) {
break block;
}
str = _trimSpaces(str)
.replace(/[^a-h1-8 nrq]/gi, '')
.toLowerCase();
if (!str) {
break block;
}
rtn = str.split(' ');
}
return rtn;
}
function _uciWrapmoveHelper(mov) {
var temp, possible_promote, rtn;
rtn = null;
block: {
if (!_isNonBlankStr(mov)) {
break block;
}
mov = _trimSpaces(mov);
if (mov.length !== 4 && mov.length !== 5) {
break block;
}
temp = [_strToBosHelper(mov.slice(0, 2)), _strToBosHelper(mov.slice(2, 4))];
if (temp[0] === null || temp[1] === null) {
break block;
}
possible_promote = mov.charAt(4) || '';
rtn = [temp, possible_promote];
}
return rtn;
}
//p = {delimiter}
function _joinedWrapmoveHelper(mov, p) {
var temp, rtn;
rtn = null;
p = _unreferenceP(p);
block: {
p.delimiter = _isNonEmptyStr(p.delimiter) ? p.delimiter.charAt(0) : '-';
if (!_isNonBlankStr(mov)) {
break block;
}
mov = _trimSpaces(mov);
if (mov.length !== 5 || mov.charAt(2) !== p.delimiter) {
break block;
}
temp = mov.split(p.delimiter);
temp = [_strToBosHelper(temp[0]), _strToBosHelper(temp[1])];
if (temp[0] === null || temp[1] === null) {
break block;
}
rtn = temp;
}
return rtn;
}
function _fromToWrapmoveHelper(mov) {
var rtn;
rtn = null;
block: {
if (!_isArray(mov) || mov.length !== 2) {
break block;
}
if (!isInsideBoard(mov[0]) || !isInsideBoard(mov[1])) {
break block;
}
rtn = [toBos(mov[0]), toBos(mov[1])];
}
return rtn;
}
function _moveWrapmoveHelper(mov) {
var possible_promote, rtn;
rtn = null;
block: {
if (!_isMove(mov)) {
break block;
}
possible_promote = mov.promotion || '';
rtn = [[mov.fromBos, mov.toBos], possible_promote];
}
return rtn;
}
function _unreferencedMoveHelper(obj) {
var rtn;
rtn = {};
rtn.colorMoved = obj.colorMoved;
rtn.colorToPlay = obj.colorToPlay;
rtn.fen = obj.fen;
rtn.san = obj.san;
rtn.uci = obj.uci;
rtn.fromBos = obj.fromBos;
rtn.toBos = obj.toBos;
rtn.enPassantBos = obj.enPassantBos;
rtn.piece = obj.piece;
rtn.captured = obj.captured;
rtn.promotion = obj.promotion;
rtn.comment = obj.comment;
rtn.moveResult = obj.moveResult;
rtn.canDraw = obj.canDraw;
rtn.isEnPassantCapture = obj.isEnPassantCapture;
return rtn;
}
function _nullboardHelper(board_name) {
var i, j, temp, current_pos, current_bos, target;
target = getBoard(board_name);
if (target === null) {
_BOARDS[board_name] = {
boardName: board_name,
getSquare: _getSquare,
setSquare: _setSquare,
attackersFromActive: _attackersFromActive,
attackersFromNonActive: _attackersFromNonActive,
toggleActiveNonActive: _toggleActiveNonActive,
toggleIsRotated: _toggleIsRotated,
setPromoteTo: _setPromoteTo,
silentlyResetOptions: _silentlyResetOptions,
silentlyResetManualResult: _silentlyResetManualResult,
setManualResult: _setManualResult,
setCurrentMove: _setCurrentMove,
loadFen: _loadFen,
loadValidatedFen: _loadValidatedFen,
getClocklessFenHelper: _getClocklessFenHelper,
updateFenAndMisc: _updateFenAndMisc,
refinedFenTest: _refinedFenTest,
testCollision: _testCollision,
isLegalMove: _isLegalMove,
legalMovesHelper: _legalMovesHelper,
legalMoves: _legalMoves,
legalFenMoves: _legalFenMoves,
legalSanMoves: _legalSanMoves,
legalUciMoves: _legalUciMoves,
getCheckmateMoves: _getCheckmateMoves,
getDrawMoves: _getDrawMoves,
fenHistoryExport: _fenHistoryExport,
pgnExport: _pgnExport,
uciExport: _uciExport,
ascii: _ascii,
boardHash: _boardHash,
isEqualBoard: _isEqualBoard,
cloneBoardFrom: _cloneBoardFrom,
cloneBoardTo: _cloneBoardTo,
reset: _reset,
undoMove: _undoMove,
undoMoves: _undoMoves,
countLightDarkBishops: _countLightDarkBishops,
updateHelper: _updateHelper,
fenWrapmoveHelper: _fenWrapmoveHelper,
sanWrapmoveHelper: _sanWrapmoveHelper,
getWrappedMove: _getWrappedMove,
draftMove: _draftMove,
playMove: _playMove,
playMoves: _playMoves,
playRandomMove: _playRandomMove,
navFirst: _navFirst,
navPrevious: _navPrevious,
navNext: _navNext,
navLast: _navLast,
navLinkMove: _navLinkMove,
refreshUi: _refreshUi,
};
target = _BOARDS[board_name];
}
target.w = {
//static
isBlack: false,
sign: 1,
firstRankPos: 7,
secondRankPos: 6,
lastRankPos: 0,
singlePawnRankShift: -1,
pawn: _PAWN,
knight: _KNIGHT,
bishop: _BISHOP,
rook: _ROOK,
queen: _QUEEN,
king: _KING,
//mutable
kingBos: null,
castling: null,
materialDiff: null,
};
target.b = {
//static
isBlack: true,
sign: -1,
firstRankPos: 0,
secondRankPos: 1,
lastRankPos: 7,
singlePawnRankShift: 1,
pawn: -_PAWN,
knight: -_KNIGHT,
bishop: -_BISHOP,
rook: -_ROOK,
queen: -_QUEEN,
king: -_KING,
//mutable
kingBos: null,
castling: null,
materialDiff: null,
};
target.activeColor = null;
target.nonActiveColor = null;
target.fen = null;
target.enPassantBos = null;
target.halfMove = null;
target.fullMove = null;
target.moveList = null;
target.currentMove = null;
target.isRotated = null;
target.isPuzzleMode = null;
target.checks = null;
target.isCheck = null;
target.isCheckmate = null;
target.isStalemate = null;
target.isThreefold = null;
target.isInsufficientMaterial = null;
target.isFiftyMove = null;
target.inDraw = null;
target.promoteTo = null;
target.manualResult = null;
target.isHidden = null;
target.legalUci = null;
target.legalUciTree = null;
target.legalRevTree = null;
target.squares = {};
for (i = 0; i < 8; i++) {
//0...7
for (j = 0; j < 8; j++) {
//0...7
current_pos = [i, j];
current_bos = toBos(current_pos);
target.squares[current_bos] = {};
temp = target.squares[current_bos];
//static
temp.pos = current_pos;
temp.bos = current_bos;
temp.rankPos = getRankPos(current_pos);
temp.filePos = getFilePos(current_pos);
temp.rankBos = getRankBos(current_pos);
temp.fileBos = getFileBos(current_pos);
//mutable
temp.bal = null;
temp.absBal = null;
temp.val = null;
temp.absVal = null;
temp.className = null;
temp.sign = null;
temp.isEmptySquare = null;
temp.isPawn = null;
temp.isKnight = null;
temp.isBishop = null;
temp.isRook = null;
temp.isQueen = null;
temp.isKing = null;
}
}
return target;
}
//---------------- utilities
function _consoleLog(msg, alert_type) {
var rtn;
rtn = false;
if (!_SILENT_MODE) {
rtn = true;
switch (alert_type) {
case _ALERT_LIGHT:
console.log(msg);
break;
case _ALERT_DARK:
console.log(msg);
break;
case _ALERT_SUCCESS:
console.log(msg);
break;
case _ALERT_WARNING:
console.warn(msg);
break;
case _ALERT_ERROR:
console.error(msg);
break;
default:
console.log(msg);
alert_type = _ALERT_LIGHT;
}
if (_WIN && _WIN.IcUi && _WIN.IcUi.pushAlert) {
_WIN.IcUi.pushAlert.apply(null, [msg, alert_type]);
}
}
return rtn;
}
function _isObject(obj) {
return typeof obj === 'object' && obj !== null && !_isArray(obj);
}
function _isArray(arr) {
return Object.prototype.toString.call(arr) === '[object Array]';
}
function _isSquare(obj) {
return _isObject(obj) && typeof obj.bos === 'string';
}
function _isBoard(obj) {
return _isObject(obj) && typeof obj.boardName === 'string';
}
function _isMove(obj) {
return _isObject(obj) && typeof obj.fromBos === 'string' && typeof obj.toBos === 'string';
}
function _trimSpaces(str) {
return String(str)
.replace(/^\s+|\s+$/g, '')
.replace(/\s\s+/g, ' ');
}
function _formatName(str) {
return _trimSpaces(str)
.replace(/[^a-z0-9]/gi, '_')
.replace(/__+/g, '_');
}
function _strContains(str, str_to_find) {
return String(str).indexOf(str_to_find) !== -1;
}
function _occurrences(str, str_rgxp) {
var rtn;
rtn = 0;
if (_isNonEmptyStr(str) && _isNonEmptyStr(str_rgxp)) {
rtn = (str.match(RegExp(str_rgxp, 'g')) || []).length;
}
return rtn;
}
function _toInt(num, min_val, max_val) {
num = num * 1 || 0;
num = num < 0 ? Math.ceil(num) : Math.floor(num);
min_val *= 1;
max_val *= 1;
/*NO remove default 0, (-0 || 0) = 0*/
min_val = (Number.isNaN(min_val) ? -Infinity : min_val) || 0;
max_val = (Number.isNaN(max_val) ? Infinity : max_val) || 0;
return Math.min(Math.max(num, min_val), max_val);
}
function _isIntOrStrInt(num) {
return String(_toInt(num)) === String(num);
}
function _isNonEmptyStr(val) {
return !!(typeof val === 'string' && val);
}
function _isNonBlankStr(val) {
return !!(typeof val === 'string' && _trimSpaces(val));
}
function _hashCode(val) {
var i, len, hash;
hash = 0;
val = _isNonEmptyStr(val) ? val : '';
for (i = 0, len = val.length; i < len; i++) {
//0<len
hash = (hash << 5) - hash + val.charCodeAt(i);
hash |= 0; //to 32bit integer
}
return hash;
}
function _castlingChars(num) {
return ['', 'k', 'q', 'kq'][_toInt(num, 0, 3)];
}
function _unreferenceP(p, changes) {
var i, len, rtn;
rtn = _isObject(p) ? { ...p } : {};
if (_isArray(changes)) {
for (i = 0, len = changes.length; i < len; i++) {
//0<len
if (!_isArray(changes[i]) || changes[i].length !== 2 || !_isNonBlankStr(changes[i][0])) {
_consoleLog('[_unreferenceP]: unexpected format', _ALERT_ERROR);
continue;
}
rtn[_trimSpaces(changes[i][0])] = changes[i][1];
}
}
return rtn;
}
function _cleanSan(rtn) {
rtn = _isNonBlankStr(rtn) ? rtn : '';
if (rtn) {
while (rtn !== (rtn = rtn.replace(/\{[^{}]*\}/g, '\n'))); /*TODO: keep comment*/
while (rtn !== (rtn = rtn.replace(/\([^()]*\)/g, '\n')));
while (rtn !== (rtn = rtn.replace(/\<[^<>]*\>/g, '\n')));
rtn = rtn.replace(/(\t)|(\r?\n)|(\r\n?)/g, '\n');
rtn = rtn.replace(/;+[^\n]*(\n|$)/g, '\n'); /*TODO: keep comment*/
rtn = rtn
.replace(/^%.*\n?/gm, '')
.replace(/^\n+|\n+$/g, '')
.replace(/\n/g, ' ');
rtn = rtn.replace(/\$\d+/g, ' '); /*TODO: keep NAG*/
rtn = rtn.replace(/[^a-h0-9nrqkxo /½=-]/gi, ''); //no planned support for P and e.p.
rtn = rtn.replace(/\s*\-+\s*/g, '-');
rtn = rtn.replace(/0-0-0/g, 'w').replace(/0-0/g, 'v');
rtn = rtn.replace(/o-o-o/gi, 'w').replace(/o-o/gi, 'v');
rtn = rtn.replace(/o/gi, '0').replace(/½/g, '1/2');
rtn = rtn
.replace(/1\-0/g, ' i ')
.replace(/0\-1/g, ' j ')
.replace(/1\/2\-1\/2/g, ' z ');
rtn = rtn.replace(/\-/g, ' ');
rtn = rtn.replace(/w/g, 'O-O-O').replace(/v/g, 'O-O');
rtn = rtn.replace(/i/g, _RESULT_W_WINS).replace(/j/g, _RESULT_B_WINS).replace(/z/g, _RESULT_DRAW);
rtn = _trimSpaces(rtn);
}
return rtn;
}
function _cloneBoardToObj(to_obj = {}, from_woard) {
var i,
j,
k,
len,
len2,
len3,
current_key,
to_prop,
from_prop,
sub_current_key,
sub_from_prop,
sub_to_prop,
sub_sub_current_key,
sub_sub_from_prop,
//sub_sub_to_prop,
sub_keys,
sub_sub_keys,
from_board;
block: {
if (!_isObject(to_obj)) {
_consoleLog('[_cloneBoardToObj]: to_obj must be Object type', _ALERT_ERROR);
break block;
}
from_board = getBoard(from_woard);
if (from_board === null) {
_consoleLog("[_cloneBoardToObj]: from_woard doesn't exist", _ALERT_ERROR);
break block;
}
if (to_obj === from_board) {
_consoleLog('[_cloneBoardToObj]: trying to self clone', _ALERT_ERROR);
break block;
}
to_obj.moveList = [];
to_obj.legalUci = [];
to_obj.legalUciTree = {};
to_obj.legalRevTree = {};
for (i = 0, len = _MUTABLE_KEYS.length; i < len; i++) {
//0<len
current_key = _MUTABLE_KEYS[i];
to_prop = to_obj[current_key];
from_prop = from_board[current_key];
if (!to_prop && (current_key === 'w' || current_key === 'b' || current_key === 'squares')) {
to_obj[current_key] = {};
to_prop = to_obj[current_key];
}
//primitive data type
if (!_isObject(from_prop) && !_isArray(from_prop)) {
to_obj[current_key] = from_prop; //can't use to_prop, it's not a reference here
continue;
}
if (current_key === 'legalUci') {
to_obj.legalUci = from_board.legalUci.slice(0);
continue;
}
if (current_key === 'w' || current_key === 'b') {
//["w" | "b"] object of (12 static + 3 mutables = 15) Note: materialDiff is array
//object or array data type
to_prop.materialDiff = from_prop.materialDiff.slice(0); //mutables
//primitive data type
to_prop.isBlack = from_prop.isBlack; //static
to_prop.sign = from_prop.sign; //static
to_prop.firstRankPos = from_prop.firstRankPos; //static
to_prop.secondRankPos = from_prop.secondRankPos; //static
to_prop.lastRankPos = from_prop.lastRankPos; //static
to_prop.singlePawnRankShift = from_prop.singlePawnRankShift; //static
to_prop.pawn = from_prop.pawn; //static
to_prop.knight = from_prop.knight; //static
to_prop.bishop = from_prop.bishop; //static
to_prop.rook = from_prop.rook; //static
to_prop.queen = from_prop.queen; //static
to_prop.king = from_prop.king; //static
to_prop.kingBos = from_prop.kingBos; //mutables
to_prop.castling = from_prop.castling; //mutables
continue;
}
sub_keys = Object.keys(from_prop);
for (j = 0, len2 = sub_keys.length; j < len2; j++) {
//0<len2
sub_current_key = sub_keys[j];
sub_to_prop = to_prop[sub_current_key];
sub_from_prop = from_prop[sub_current_key];
if (!sub_to_prop && current_key === 'squares') {
to_prop[sub_current_key] = {};
sub_to_prop = to_prop[sub_current_key];
}
//primitive data type
if (!_isObject(sub_from_prop) && !_isArray(sub_from_prop)) {
_consoleLog('[_cloneBoardToObj]: unexpected primitive data type', _ALERT_ERROR);
continue;
}
if (current_key === 'legalUciTree') {
//["legalUciTree"] object of (0-64), array of (0-N)
to_prop[sub_current_key] = sub_from_prop.slice(0); //can't use sub_to_prop, it's not a reference here
continue;
}
if (current_key === 'squares') {
//["squares"] object of (64), object of (6 static + 13 mutables = 19) Note: pos is array
//object or array data type
sub_to_prop.pos = sub_from_prop.pos.slice(0); //static
//primitive data type
sub_to_prop.bos = sub_from_prop.bos; //static
sub_to_prop.rankPos = sub_from_prop.rankPos; //static
sub_to_prop.filePos = sub_from_prop.filePos; //static
sub_to_prop.rankBos = sub_from_prop.rankBos; //static
sub_to_prop.fileBos = sub_from_prop.fileBos; //static
sub_to_prop.bal = sub_from_prop.bal; //mutables
sub_to_prop.absBal = sub_from_prop.absBal; //mutables
sub_to_prop.val = sub_from_prop.val; //mutables
sub_to_prop.absVal = sub_from_prop.absVal; //mutables
sub_to_prop.className = sub_from_prop.className; //mutables
sub_to_prop.sign = sub_from_prop.sign; //mutables
sub_to_prop.isEmptySquare = sub_from_prop.isEmptySquare; //mutables
sub_to_prop.isPawn = sub_from_prop.isPawn; //mutables
sub_to_prop.isKnight = sub_from_prop.isKnight; //mutables
sub_to_prop.isBishop = sub_from_prop.isBishop; //mutables
sub_to_prop.isRook = sub_from_prop.isRook; //mutables
sub_to_prop.isQueen = sub_from_prop.isQueen; //mutables
sub_to_prop.isKing = sub_from_prop.isKing; //mutables
continue;
}
sub_sub_keys = Object.keys(sub_from_prop);
if (current_key === 'moveList' || current_key === 'legalRevTree') {
to_prop[sub_current_key] = {};
sub_to_prop = to_prop[sub_current_key];
/*NO put a "continue" in here*/
}
for (k = 0, len3 = sub_sub_keys.length; k < len3; k++) {
//0<len3
sub_sub_current_key = sub_sub_keys[k];
//sub_sub_to_prop = sub_to_prop[sub_sub_current_key];
sub_sub_from_prop = sub_from_prop[sub_sub_current_key];
if (current_key === 'legalRevTree') {
sub_to_prop[sub_sub_current_key] = sub_sub_from_prop.slice(0); //can't use sub_sub_to_prop, it's not a reference here
continue;
}
//object or array data type
if (_isObject(sub_sub_from_prop) || _isArray(sub_sub_from_prop)) {
_consoleLog('[_cloneBoardToObj]: unexpected type in key "' + sub_sub_current_key + '"', _ALERT_ERROR);
continue;
}
//primitive data type
sub_to_prop[sub_sub_current_key] = sub_sub_from_prop; //can't use sub_sub_to_prop, it's not a reference here
}
}
}
}
return to_obj;
}
function _basicFenTest(fen) {
var i,
j,
len,
temp,
optional_clocks,
last_is_num,
current_is_num,
fen_board,
fen_board_arr,
total_files_in_current_rank,
rtn_msg;
rtn_msg = '';
block: {
fen = String(fen);
if (fen.length < 20) {
rtn_msg = 'Error [0] fen is too short';
break block;
}
fen = _trimSpaces(fen);
optional_clocks = fen.replace(
/^([rnbqkRNBQK1-8]+\/)([rnbqkpRNBQKP1-8]+\/){6}([rnbqkRNBQK1-8]+)\s[bw]\s(-|K?Q?k?q?)\s(-|[a-h][36])($|\s)/,
''
);
if (fen.length === optional_clocks.length) {
rtn_msg = 'Error [1] invalid fen structure';
break block;
}
if (optional_clocks.length) {
if (!/^(0|[1-9][0-9]*)\s([1-9][0-9]*)$/.test(optional_clocks)) {
rtn_msg = 'Error [2] invalid half/full move';
break block;
}
}
fen_board = fen.split(' ')[0];
fen_board_arr = fen_board.split('/');
for (i = 0; i < 8; i++) {
//0...7
total_files_in_current_rank = 0;
last_is_num = false;
for (j = 0, len = fen_board_arr[i].length; j < len; j++) {
//0<len
temp = fen_board_arr[i].charAt(j) * 1;
current_is_num = !!temp;
if (last_is_num && current_is_num) {
rtn_msg = 'Error [3] two consecutive numeric values';
break block;
}
last_is_num = current_is_num;
total_files_in_current_rank += temp || 1;
}
if (total_files_in_current_rank !== 8) {
rtn_msg = 'Error [4] rank without exactly 8 columns';
break block;
}
}
temp = fen_board.indexOf('K');
if (temp === -1 || fen_board.lastIndexOf('K') !== temp) {
rtn_msg = 'Error [5] board without exactly one white king';
break block;
}
temp = fen_board.indexOf('k');
if (temp === -1 || fen_board.lastIndexOf('k') !== temp) {
rtn_msg = 'Error [6] board without exactly one black king';
break block;
}
}
return rtn_msg;
}
function _perft(woard, depth, specific_uci) {
var i, len, board, count, rtn;
rtn = 1;
block: {
if (depth < 1) {
break block;
}
board = getBoard(woard);