-
Notifications
You must be signed in to change notification settings - Fork 1
/
chess001.html
2854 lines (2417 loc) · 88.4 KB
/
chess001.html
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
<body>
<script>
var startingFen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
var each = function (o, cb) {
if (o instanceof Array) {
for (var i = 0; i < o.length; i++) {
if (cb(o[i], i, o) == false)
return false
}
} else {
for (var k in o) {
if (o.hasOwnProperty(k)) {
if (cb(o[k], k, o) == false)
return false
}
}
}
return true
}
function getPos(obj) {
var x = y = 0
do {
x += obj.offsetLeft
y += obj.offsetTop
} while (obj = obj.offsetParent)
return {x : x, y : y}
}
function makeDraggable(d, onDrop) {
d.onmousedown = function (e) {
e.preventDefault()
var pos = getPos(d.offsetParent)
var offset = {
x : e.pageX - pos.x,
y : e.pageY - pos.y
}
var oldZ = d.style.zIndex
var oldMove = document.onmousemove
var oldUp = document.onmouseup
d.style.zIndex = 1000000
document.onmousemove = function (e) {
d.style.left = e.pageX - offset.x - pos.x
d.style.top = e.pageY - offset.y - pos.y
}
document.onmouseup = function (e) {
document.onmousemove = oldMove
document.onmouseup = oldUp
d.style.zIndex = oldZ
onDrop(e.pageX, e.pageY)
}
}
d.ontouchstart = function (e) {
e.preventDefault()
var pos = getPos(d.offsetParent)
var last_pageX = e.touches[0].pageX
var last_pageY = e.touches[0].pageY
var offset = {
x : last_pageX - pos.x,
y : last_pageY - pos.y
}
var oldZ = d.style.zIndex
var oldMove = document.ontouchmove
var oldEnd = document.ontouchend
var oldCancel = document.ontouchcancel
d.style.zIndex = 1000000
document.ontouchmove = function (e) {
last_pageX = e.touches[0].pageX
last_pageY = e.touches[0].pageY
d.style.left = last_pageX - offset.x - pos.x
d.style.top = last_pageY - offset.y - pos.y
}
document.ontouchcancel = function (e) {
document.ontouchmove = oldMove
document.ontouchend = oldEnd
document.ontouchcancel = oldCancel
d.style.zIndex = oldZ
}
document.ontouchend = function (e) {
document.ontouchcancel(e)
onDrop(last_pageX, last_pageY)
}
}
}
function drawChessBoard(fen, size, onMove, suggestedMove) {
fen = fen || "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w"
var s = fen.split(/ /)
var whitesMove = s[1] == 'w'
fen = s[0]
fen = fen.replace(/\d/g, function (x) { return new Array(1 * x + 1).join(' ') }).replace(/\//g, '')
size = size || 400
var squareSize = size / 8
var blackSquareColor = 'rgb(230,230,255)' // 'rgb(230,230,255)'
var whiteSquareColor = 'rgb(230,230,255)' // 'rgb(230,255,255)'
var blackPieceColor = 'black'
var whitePieceColorBottom = 'white'
var whitePieceColorTop = 'black'
var d = document.createElement('div')
d.style.position = 'relative'
d.style.width = size
d.style.height = size
var c = document.createElement('canvas')
c.width = size
c.height = size
c.style.position = 'absolute'
c.style.left = 0
c.style.top = 0
var g = c.getContext('2d')
g.beginPath()
for (var y = 0; y <= 8; y++) {
g.moveTo(0.5, y * squareSize + (y < 8 ? 0.5 : -0.5))
g.lineTo(size + 0.5, y * squareSize + (y < 8 ? 0.5 : -0.5))
}
for (var x = 0; x <= 8; x++) {
g.moveTo(x * squareSize + (x < 8 ? 0.5 : -1.5), 0.5)
g.lineTo(x * squareSize + (x < 8 ? 0.5 : -1.5), size + 0.5)
}
g.lineWidth = 1
g.strokeStyle = 'black'
g.stroke()
d.appendChild(c)
function makeSquare() {
var d = document.createElement('div')
d.style.width = squareSize
d.style.height = squareSize
d.style.textAlign = 'center'
d.style.verticalAlign = 'top'
// d.style.fontFamily = '"cases"'
// d.style.fontSize = squareSize * .9
d.style.font = squareSize * .5 + 'px "cases", serif'
d.style.cursor = 'default'
return d
}
for (var yy = 0; yy < 8; yy++) {
var y = whitesMove ? yy : 7 - yy
for (var xx = 0; xx < 8; xx++) {
var x = whitesMove ? xx : 7 - xx
var square = makeSquare()
square.style.float = 'left'
square.style.position = 'relative'
//square.style.background = (x + y) % 2 == 0 ? whiteSquareColor : blackSquareColor
// if (suggestedMove && suggestedMove.indexOf(String.fromCharCode('a'.charCodeAt(0) + x) + (8 - y)) >= 0) {
// square.css({ background : 'red' })
// }
var piece = get_fresh_piece(fen[y * 8 + x])
if (piece) {
var pieceDiv = makeSquare()
pieceDiv.style.position = 'relative'
pieceDiv.appendChild(piece)
;(function () {
function getBoardPos(x, y) {
if (x < 0 || x > 7 || y < 0 || y > 7)
return 'offboard'
return String.fromCharCode('a'.charCodeAt(0) + x) + (8 - y)
}
var from = getBoardPos(x, y)
makeDraggable(pieceDiv, function (x, y) {
var pos = getPos(d)
x = Math.floor((x - pos.x) / squareSize)
y = Math.floor((y - pos.y) / squareSize)
if (!whitesMove) {
x = 7 - x
y = 7 - y
}
if (onMove)
onMove(from + getBoardPos(x, y))
})
})();
square.appendChild(pieceDiv)
}
d.appendChild(square)
var loc = document.createElement('div')
loc.style.position = 'absolute'
loc.style.bottom = '0px'
loc.style.left = '0px'
loc.style.color = 'black'
loc.style.fontSize = 'small'
loc.innerHTML = ' ' + String.fromCharCode('a'.charCodeAt(0) + x) + ((7 - y) + 1)
square.appendChild(loc)
}
}
return d
}
function drawChessGame(fen) {
var d = document.createElement('div')
var fens = [fen]
function redrawBoard() {
while (d.firstChild) { d.removeChild(d.firstChild) }
d.appendChild(drawChessBoard(fens.slice(-1)[0], window.innerWidth, function (move) {
var newFen = applyMove(fens.slice(-1)[0], move)
if (newFen) {
fens.push(newFen)
}
redrawBoard()
}))
var b = document.createElement('div')
b.style.background = 'rgb(255, 255, 200)'
b.style.height = window.innerWidth / 4
b.innerHTML = '<'
var f = function () {
if (fens.length > 1)
fens.pop()
redrawBoard()
}
if (b.ontouchstart != undefined)
b.ontouchstart = f
else
b.onmousedown = f
d.appendChild(b)
}
redrawBoard()
return d
}
var piece_pics = {}
function get_fresh_piece(x) {
var old_c = piece_pics[x]
if (!old_c) { return }
var c = document.createElement('canvas')
c.width = old_c.width
c.height = old_c.height
var g = c.getContext('2d')
g.drawImage(old_c, 0, 0)
return c
}
function main() {
var w = window.innerWidth
var h = window.innerHeight
var size = Math.min(w, h)
var peice_size = Math.floor(size / 8)
console.log('stuff:')
console.log(size)
console.log(peice_size)
var pieces = {P: 'wp', R: 'wr', N: 'wn', B: 'wb', K: 'wk', Q: 'wq', p: 'bp', r: 'br', n: 'bn', b: 'bb', k: 'bk', q: 'bq'}
var count = 0
each(pieces, function (p, pp) {
count++
var img = new Image()
img.onload = function () {
var c = document.createElement('canvas')
c.width = peice_size
c.height = peice_size
var g = c.getContext('2d')
g.drawImage(img, 0, 0, peice_size, peice_size)
piece_pics[pp] = c
next235()
}
img.src = 'https://dglittle.github.io/cdn/' + p + '001.png'
})
var done_count = 0
function next235() {
done_count++
if (done_count == count) {
document.body.style.padding = 0
document.body.style.margin = 0
document.body.appendChild(drawChessGame(startingFen))
}
}
}
// adapted from https://raw.github.com/glinscott/Garbochess-JS/master/js/garbochess.js
var myMap = function (o, func) {
if (o instanceof Array) {
var accum = []
for (var i = 0; i < o.length; i++)
accum[i] = func(o[i], i, o)
return accum
} else {
var accum = {}
for (var k in o)
if (o.hasOwnProperty(k))
accum[k] = func(o[k], k, o)
return accum
}
}
var mySort = function (a, func, desc) {
return myMap(myMap(a, function (e, i) {
return {
v : e,
f : func(e, i)
}
}).sort(function (a, b) {
if (a.f < b.f) return desc ? 1 : -1
if (a.f > b.f) return desc ? -1 : 1
return 0
}), function (e) { return e.v })
}
var unPairs = function (a, b) {
var accum = {}
if (b) {
each(a, function (k, i) {
accum[k] = b[i]
})
} else {
each(a, function (e) {
accum[e[0]] = e[1]
})
}
return accum
}
function applyMove(fen, move) {
ResetGame()
InitializeFromFen(fen)
var moves = unPairs(myMap(GenerateValidMoves(), function (m) { return [FormatMove(m), m] }))
move = moves[move] || moves[move + 'q']
if (move) {
MakeMove(move)
return GetFen()
}
}
function suggestMoves(fen, time) {
ResetGame()
InitializeFromFen(fen)
g_timeout = time || 40
return Search(function (bestMove, score) {
console.log("bestMove: " + FormatMove(bestMove))
console.log("score: " + score)
}, 99, function () {})
}
function GetFen(){
var result = "";
for (var row = 0; row < 8; row++) {
if (row != 0)
result += '/';
var empty = 0;
for (var col = 0; col < 8; col++) {
var piece = g_board[((row + 2) << 4) + col + 4];
if (piece == 0) {
empty++;
}
else {
if (empty != 0)
result += empty;
empty = 0;
var pieceChar = [" ", "p", "n", "b", "r", "q", "k", " "][(piece & 0x7)];
result += ((piece & colorWhite) != 0) ? pieceChar.toUpperCase() : pieceChar;
}
}
if (empty != 0) {
result += empty;
}
}
result += g_toMove == colorWhite ? " w" : " b";
result += " ";
if (g_castleRights == 0) {
result += "-";
}
else {
if ((g_castleRights & 1) != 0)
result += "K";
if ((g_castleRights & 2) != 0)
result += "Q";
if ((g_castleRights & 4) != 0)
result += "k";
if ((g_castleRights & 8) != 0)
result += "q";
}
result += " ";
if (g_enPassentSquare == -1) {
result += '-';
}
else {
result += FormatSquare(g_enPassentSquare);
}
return result;
}
function GetMoveSAN(move, validMoves) {
var from = move & 0xFF;
var to = (move >> 8) & 0xFF;
if (move & moveflagCastleKing) return "O-O";
if (move & moveflagCastleQueen) return "O-O-O";
var pieceType = g_board[from] & 0x7;
var result = ["", "", "N", "B", "R", "Q", "K", ""][pieceType];
var dupe = false, rowDiff = true, colDiff = true;
if (validMoves == null) {
validMoves = GenerateValidMoves();
}
for (var i = 0; i < validMoves.length; i++) {
var moveFrom = validMoves[i] & 0xFF;
var moveTo = (validMoves[i] >> 8) & 0xFF;
if (moveFrom != from &&
moveTo == to &&
(g_board[moveFrom] & 0x7) == pieceType) {
dupe = true;
if ((moveFrom & 0xF0) == (from & 0xF0)) {
rowDiff = false;
}
if ((moveFrom & 0x0F) == (from & 0x0F)) {
colDiff = false;
}
}
}
if (dupe) {
if (colDiff) {
result += FormatSquare(from).charAt(0);
} else if (rowDiff) {
result += FormatSquare(from).charAt(1);
} else {
result += FormatSquare(from);
}
} else if (pieceType == piecePawn && (g_board[to] != 0 || (move & moveflagEPC))) {
result += FormatSquare(from).charAt(0);
}
if (g_board[to] != 0 || (move & moveflagEPC)) {
result += "x";
}
result += FormatSquare(to);
if (move & moveflagPromotion) {
if (move & moveflagPromoteBishop) result += "=B";
else if (move & moveflagPromoteKnight) result += "=N";
else if (move & moveflagPromoteQueen) result += "=Q";
else result += "=R";
}
MakeMove(move);
if (g_inCheck) {
result += GenerateValidMoves().length == 0 ? "#" : "+";
}
UnmakeMove(move);
return result;
}
function FormatSquare(square) {
var letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
return letters[(square & 0xF) - 4] + ((9 - (square >> 4)) + 1);
}
function FormatMove(move) {
var result = FormatSquare(move & 0xFF) + FormatSquare((move >> 8) & 0xFF);
if (move & moveflagPromotion) {
if (move & moveflagPromoteBishop) result += "b";
else if (move & moveflagPromoteKnight) result += "n";
else if (move & moveflagPromoteQueen) result += "q";
else result += "r";
}
return result;
}
function GetMoveFromString(moveString) {
var moves = GenerateValidMoves();
for (var i = 0; i < moves.length; i++) {
if (FormatMove(moves[i]) == moveString) {
return moves[i];
}
}
alert("busted! ->" + moveString + " fen:" + GetFen());
}
function PVFromHash(move, ply) {
if (ply == 0)
return "";
if (move == 0) {
if (g_inCheck) return "checkmate";
return "stalemate";
}
var pvString = " " + GetMoveSAN(move);
MakeMove(move);
var hashNode = g_hashTable[g_hashKeyLow & g_hashMask];
if (hashNode != null && hashNode.lock == g_hashKeyHigh && hashNode.bestMove != null) {
pvString += PVFromHash(hashNode.bestMove, ply - 1);
}
UnmakeMove(move);
return pvString;
}
//
// Searching code
//
var g_startTime;
var g_nodeCount;
var g_qNodeCount;
var g_searchValid;
var g_globalPly = 0;
function Search(finishMoveCallback, maxPly, finishPlyCallback) {
var lastEval;
var alpha = minEval;
var beta = maxEval;
g_globalPly++;
g_nodeCount = 0;
g_qNodeCount = 0;
g_searchValid = true;
var bestMove = 0;
var value;
g_startTime = (new Date()).getTime();
var i;
var moveScores
for (i = 1; i <= maxPly && g_searchValid; i++) {
var newMoveScores = []
var tmp = AlphaBeta(i, 0, alpha, beta, newMoveScores);
if (!g_searchValid) break;
moveScores = newMoveScores
value = tmp;
if (value > alpha && value < beta) {
alpha = value - 500;
beta = value + 500;
if (alpha < minEval) alpha = minEval;
if (beta > maxEval) beta = maxEval;
} else if (alpha != minEval) {
alpha = minEval;
beta = maxEval;
i--;
}
if (g_hashTable[g_hashKeyLow & g_hashMask] != null) {
bestMove = g_hashTable[g_hashKeyLow & g_hashMask].bestMove;
}
if (finishPlyCallback != null) {
finishPlyCallback(bestMove, value, (new Date()).getTime() - g_startTime, i);
}
}
if (finishMoveCallback != null) {
finishMoveCallback(bestMove, value, (new Date()).getTime() - g_startTime, i - 1);
}
return mySort(moveScores, function (e) { return -e.score })
}
var minEval = -2000000;
var maxEval = +2000000;
var minMateBuffer = minEval + 2000;
var maxMateBuffer = maxEval - 2000;
var materialTable = [0, 800, 3350, 3450, 5000, 9750, 600000];
var pawnAdj =
[
0, 0, 0, 0, 0, 0, 0, 0,
-25, 105, 135, 270, 270, 135, 105, -25,
-80, 0, 30, 176, 176, 30, 0, -80,
-85, -5, 25, 175, 175, 25, -5, -85,
-90, -10, 20, 125, 125, 20, -10, -90,
-95, -15, 15, 75, 75, 15, -15, -95,
-100, -20, 10, 70, 70, 10, -20, -100,
0, 0, 0, 0, 0, 0, 0, 0
];
var knightAdj =
[-200, -100, -50, -50, -50, -50, -100, -200,
-100, 0, 0, 0, 0, 0, 0, -100,
-50, 0, 60, 60, 60, 60, 0, -50,
-50, 0, 30, 60, 60, 30, 0, -50,
-50, 0, 30, 60, 60, 30, 0, -50,
-50, 0, 30, 30, 30, 30, 0, -50,
-100, 0, 0, 0, 0, 0, 0, -100,
-200, -50, -25, -25, -25, -25, -50, -200
];
var bishopAdj =
[ -50,-50,-25,-10,-10,-25,-50,-50,
-50,-25,-10, 0, 0,-10,-25,-50,
-25,-10, 0, 25, 25, 0,-10,-25,
-10, 0, 25, 40, 40, 25, 0,-10,
-10, 0, 25, 40, 40, 25, 0,-10,
-25,-10, 0, 25, 25, 0,-10,-25,
-50,-25,-10, 0, 0,-10,-25,-50,
-50,-50,-25,-10,-10,-25,-50,-50
];
var rookAdj =
[ -60, -30, -10, 20, 20, -10, -30, -60,
40, 70, 90,120,120, 90, 70, 40,
-60, -30, -10, 20, 20, -10, -30, -60,
-60, -30, -10, 20, 20, -10, -30, -60,
-60, -30, -10, 20, 20, -10, -30, -60,
-60, -30, -10, 20, 20, -10, -30, -60,
-60, -30, -10, 20, 20, -10, -30, -60,
-60, -30, -10, 20, 20, -10, -30, -60
];
var kingAdj =
[ 50, 150, -25, -125, -125, -25, 150, 50,
50, 150, -25, -125, -125, -25, 150, 50,
50, 150, -25, -125, -125, -25, 150, 50,
50, 150, -25, -125, -125, -25, 150, 50,
50, 150, -25, -125, -125, -25, 150, 50,
50, 150, -25, -125, -125, -25, 150, 50,
50, 150, -25, -125, -125, -25, 150, 50,
150, 250, 75, -25, -25, 75, 250, 150
];
var emptyAdj =
[0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
];
var pieceSquareAdj = new Array(8);
// Returns the square flipped
var flipTable = new Array(256);
function PawnEval(color) {
var pieceIdx = (color | 1) << 4;
var from = g_pieceList[pieceIdx++];
while (from != 0) {
from = g_pieceList[pieceIdx++];
}
}
function Mobility(color) {
var result = 0;
var from, to, mob, pieceIdx;
var enemy = color == 8 ? 0x10 : 0x8
var mobUnit = color == 8 ? g_mobUnit[0] : g_mobUnit[1];
// Knight mobility
mob = -3;
pieceIdx = (color | 2) << 4;
from = g_pieceList[pieceIdx++];
while (from != 0) {
mob += mobUnit[g_board[from + 31]];
mob += mobUnit[g_board[from + 33]];
mob += mobUnit[g_board[from + 14]];
mob += mobUnit[g_board[from - 14]];
mob += mobUnit[g_board[from - 31]];
mob += mobUnit[g_board[from - 33]];
mob += mobUnit[g_board[from + 18]];
mob += mobUnit[g_board[from - 18]];
from = g_pieceList[pieceIdx++];
}
result += 65 * mob;
// Bishop mobility
mob = -4;
pieceIdx = (color | 3) << 4;
from = g_pieceList[pieceIdx++];
while (from != 0) {
to = from - 15; while (g_board[to] == 0) { to -= 15; mob++; }
if (g_board[to] & enemy) {
mob++;
if (!(g_board[to] & piecePawn)) {
to -= 15; while (g_board[to] == 0) to -= 15;
mob += mobUnit[g_board[to]] << 2;
}
}
to = from - 17; while (g_board[to] == 0) { to -= 17; mob++; }
if (g_board[to] & enemy) {
mob++;
if (!(g_board[to] & piecePawn)) {
to -= 17; while (g_board[to] == 0) to -= 17;
mob += mobUnit[g_board[to]] << 2;
}
}
to = from + 15; while (g_board[to] == 0) { to += 15; mob++; }
if (g_board[to] & enemy) {
mob++;
if (!(g_board[to] & piecePawn)) {
to += 15; while (g_board[to] == 0) to += 15;
mob += mobUnit[g_board[to]] << 2;
}
}
to = from + 17; while (g_board[to] == 0) { to += 17; mob++; }
if (g_board[to] & enemy) {
mob++;
if (!(g_board[to] & piecePawn)) {
to += 17; while (g_board[to] == 0) to += 17;
mob += mobUnit[g_board[to]] << 2;
}
}
from = g_pieceList[pieceIdx++];
}
result += 44 * mob;
// Rook mobility
mob = -4;
pieceIdx = (color | 4) << 4;
from = g_pieceList[pieceIdx++];
while (from != 0) {
to = from - 1; while (g_board[to] == 0) { to--; mob++;} if (g_board[to] & enemy) mob++;
to = from + 1; while (g_board[to] == 0) { to++; mob++; } if (g_board[to] & enemy) mob++;
to = from + 16; while (g_board[to] == 0) { to += 16; mob++; } if (g_board[to] & enemy) mob++;
to = from - 16; while (g_board[to] == 0) { to -= 16; mob++; } if (g_board[to] & enemy) mob++;
from = g_pieceList[pieceIdx++];
}
result += 25 * mob;
// Queen mobility
mob = -2;
pieceIdx = (color | 5) << 4;
from = g_pieceList[pieceIdx++];
while (from != 0) {
to = from - 15; while (g_board[to] == 0) { to -= 15; mob++; } if (g_board[to] & enemy) mob++;
to = from - 17; while (g_board[to] == 0) { to -= 17; mob++; } if (g_board[to] & enemy) mob++;
to = from + 15; while (g_board[to] == 0) { to += 15; mob++; } if (g_board[to] & enemy) mob++;
to = from + 17; while (g_board[to] == 0) { to += 17; mob++; } if (g_board[to] & enemy) mob++;
to = from - 1; while (g_board[to] == 0) { to--; mob++; } if (g_board[to] & enemy) mob++;
to = from + 1; while (g_board[to] == 0) { to++; mob++; } if (g_board[to] & enemy) mob++;
to = from + 16; while (g_board[to] == 0) { to += 16; mob++; } if (g_board[to] & enemy) mob++;
to = from - 16; while (g_board[to] == 0) { to -= 16; mob++; } if (g_board[to] & enemy) mob++;
from = g_pieceList[pieceIdx++];
}
result += 22 * mob;
return result;
}
function Evaluate() {
var curEval = g_baseEval;
var evalAdjust = 0;
// Black queen gone, then cancel white's penalty for king movement
if (g_pieceList[pieceQueen << 4] == 0)
evalAdjust -= pieceSquareAdj[pieceKing][g_pieceList[(colorWhite | pieceKing) << 4]];
// White queen gone, then cancel black's penalty for king movement
if (g_pieceList[(colorWhite | pieceQueen) << 4] == 0)
evalAdjust += pieceSquareAdj[pieceKing][flipTable[g_pieceList[pieceKing << 4]]];
// Black bishop pair
if (g_pieceCount[pieceBishop] >= 2)
evalAdjust -= 500;
// White bishop pair
if (g_pieceCount[pieceBishop | colorWhite] >= 2)
evalAdjust += 500;
var mobility = Mobility(8) - Mobility(0);
if (g_toMove == 0) {
// Black
curEval -= mobility;
curEval -= evalAdjust;
}
else {
curEval += mobility;
curEval += evalAdjust;
}
return curEval;
}
function ScoreMove(move){
var moveTo = (move >> 8) & 0xFF;
var captured = g_board[moveTo] & 0x7;
var piece = g_board[move & 0xFF];
var score;
if (captured != 0) {
var pieceType = piece & 0x7;
score = (captured << 5) - pieceType;
} else {
score = historyTable[piece & 0xF][moveTo];
}
return score;
}
function QSearch(alpha, beta, ply) {
g_qNodeCount++;
var realEval = g_inCheck ? (minEval + 1) : Evaluate();
if (realEval >= beta)
return realEval;
if (realEval > alpha)
alpha = realEval;
var moves = new Array();
var moveScores = new Array();
var wasInCheck = g_inCheck;
if (wasInCheck) {
// TODO: Fast check escape generator and fast checking moves generator
GenerateCaptureMoves(moves, null);
GenerateAllMoves(moves);
for (var i = 0; i < moves.length; i++) {
moveScores[i] = ScoreMove(moves[i]);
}
} else {
GenerateCaptureMoves(moves, null);
for (var i = 0; i < moves.length; i++) {
var captured = g_board[(moves[i] >> 8) & 0xFF] & 0x7;
var pieceType = g_board[moves[i] & 0xFF] & 0x7;
moveScores[i] = (captured << 5) - pieceType;
}
}
for (var i = 0; i < moves.length; i++) {
var bestMove = i;
for (var j = moves.length - 1; j > i; j--) {
if (moveScores[j] > moveScores[bestMove]) {
bestMove = j;
}
}
{
var tmpMove = moves[i];
moves[i] = moves[bestMove];
moves[bestMove] = tmpMove;
var tmpScore = moveScores[i];
moveScores[i] = moveScores[bestMove];
moveScores[bestMove] = tmpScore;
}
if (!wasInCheck && !See(moves[i])) {
continue;
}
if (!MakeMove(moves[i])) {
continue;
}
var value = -QSearch(-beta, -alpha, ply - 1);
UnmakeMove(moves[i]);
if (value > realEval) {
if (value >= beta)
return value;
if (value > alpha)
alpha = value;
realEval = value;
}
}
/* Disable checks... Too slow currently
if (ply == 0 && !wasInCheck) {
moves = new Array();
GenerateAllMoves(moves);
for (var i = 0; i < moves.length; i++) {
moveScores[i] = ScoreMove(moves[i]);
}
for (var i = 0; i < moves.length; i++) {
var bestMove = i;
for (var j = moves.length - 1; j > i; j--) {
if (moveScores[j] > moveScores[bestMove]) {
bestMove = j;
}
}
{
var tmpMove = moves[i];
moves[i] = moves[bestMove];
moves[bestMove] = tmpMove;
var tmpScore = moveScores[i];
moveScores[i] = moveScores[bestMove];
moveScores[bestMove] = tmpScore;
}
if (!MakeMove(moves[i])) {
continue;
}
var checking = g_inCheck;
UnmakeMove(moves[i]);
if (!checking) {
continue;
}
if (!See(moves[i])) {
continue;
}
MakeMove(moves[i]);
var value = -QSearch(-beta, -alpha, ply - 1);
UnmakeMove(moves[i]);
if (value > realEval) {
if (value >= beta)
return value;
if (value > alpha)
alpha = value;
realEval = value;
}
}
}
*/
return realEval;
}
function StoreHash(value, flags, ply, move, depth) {
if (value >= maxMateBuffer)
value += depth;
else if (value <= minMateBuffer)
value -= depth;
g_hashTable[g_hashKeyLow & g_hashMask] = new HashEntry(g_hashKeyHigh, value, flags, ply, move);
}
function IsHashMoveValid(hashMove) {
var from = hashMove & 0xFF;
var to = (hashMove >> 8) & 0xFF;
var ourPiece = g_board[from];
var pieceType = ourPiece & 0x7;
if (pieceType < piecePawn || pieceType > pieceKing) return false;
// Can't move a piece we don't control
if (g_toMove != (ourPiece & 0x8))
return false;
// Can't move to a square that has something of the same color
if (g_board[to] != 0 && (g_toMove == (g_board[to] & 0x8)))
return false;
if (pieceType == piecePawn) {
if (hashMove & moveflagEPC) {
return false;
}
// Valid moves are push, capture, double push, promotions
var dir = to - from;
if ((g_toMove == colorWhite) != (dir < 0)) {
// Pawns have to move in the right direction
return false;
}
var row = to & 0xF0;
if (((row == 0x90 && !g_toMove) ||
(row == 0x20 && g_toMove)) != (hashMove & moveflagPromotion)) {
// Handle promotions
return false;
}
if (dir == -16 || dir == 16) {
// White/Black push
return g_board[to] == 0;
} else if (dir == -15 || dir == -17 || dir == 15 || dir == 17) {
// White/Black capture
return g_board[to] != 0;
} else if (dir == -32) {
// Double white push
if (row != 0x60) return false;