/
p4wn-engine.js
1589 lines (1489 loc) · 49.8 KB
/
p4wn-engine.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
/* p4wn, AKA 5k chess - by Douglas Bagnall <douglas@paradise.net.nz>
*
* This code is in the public domain, or as close to it as various
* laws allow. No warranty; no restrictions.
*
* lives at http://p4wn.sf.net/
*/
/*Compatibility tricks:
* backwards for old MSIEs (to 5.5)
* sideways for seed command-line javascript.*/
var p4_log = function () {};
if (debug) {
if (this.imports !== undefined &&
this.printerr !== undefined){//seed or gjs
p4_log = function(){
var args = Array.prototype.slice.call(arguments);
printerr(args.join(', '));
};
}
else if (this.console === undefined){//MSIE
p4_log = function(){};
}
else {
p4_log = function(){console.log.apply(console, arguments);};
}
}
/*MSIE Date.now backport */
if (Date.now === undefined)
Date.now = function(){return (new Date).getTime();};
/* The pieces are stored as numbers between 2 and 13, inclusive.
* Empty squares are stored as 0, and off-board squares as 16.
* There is some bitwise logic to it:
* piece & 1 -> colour (white: 0, black: 1)
* piece & 2 -> single move piece (including pawn)
* if (piece & 2) == 0:
* piece & 4 -> row and column moves
* piece & 8 -> diagonal moves
*/
var P4_PAWN = 2, P4_ROOK = 4, P4_KNIGHT = 6, P4_BISHOP = 8, P4_QUEEN = 12, P4_KING = 10;
var P4_EDGE = 16;
/* in order, even indices: <nothing>, pawn, rook, knight, bishop, king, queen. Only the
* even indices are used.*/
var P4_MOVES = [[], [],
[], [],
[1,10,-1,-10], [],
[21,19,12,8,-21,-19,-12,-8], [],
[11,9,-11,-9], [],
[1,10,11,9,-1,-10,-11,-9], [],
[1,10,11,9,-1,-10,-11,-9], []
];
/*P4_VALUES defines the relative value of various pieces.
*
* It follows the 1,3,3,5,9 pattern you learn as a kid, multiplied by
* 20 to give sub-pawn resolution to other factors, with bishops given
* a wee boost over knights.
*/
var P4_VALUES=[0, 0, //Piece values
20, 20, //pawns
100, 100, //rooks
60, 60, //knights
61, 61, //bishops
8000, 8000,//kings
180, 180, //queens
0];
/* A score greater than P4_WIN indicates a king has been taken. It is
* less than the value of a king, in case someone finds a way to, say,
* sacrifice two queens in order to checkmate.
*/
var P4_KING_VALUE = P4_VALUES[10];
var P4_WIN = P4_KING_VALUE >> 1;
/* every move, a winning score decreases by this much */
var P4_WIN_DECAY = 300;
var P4_WIN_NOW = P4_KING_VALUE - 250;
/* P4_{MAX,MIN}_SCORE should be beyond any possible evaluated score */
var P4_MAX_SCORE = 9999; // extremes of evaluation range
var P4_MIN_SCORE = -P4_MAX_SCORE;
/*initialised in p4_initialise_state */
var P4_CENTRALISING_WEIGHTS;
var P4_BASE_PAWN_WEIGHTS;
var P4_KNIGHT_WEIGHTS;
/*P4_DEBUG turns on debugging features */
var P4_DEBUG = 0;
var P4_INITIAL_BOARD = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 1 1";
/*use javascript typed arrays rather than plain arrays
* (faster in some browsers, unsupported in others, possibly slower elsewhere) */
var P4_USE_TYPED_ARRAYS = this.Int32Array !== undefined;
var P4_PIECE_LUT = { /*for FEN, PGN interpretation */
P: 2,
p: 3,
R: 4,
r: 5,
N: 6,
n: 7,
B: 8,
b: 9,
K: 10,
k: 11,
Q: 12,
q: 13
};
var P4_ENCODE_LUT = ' PPRRNNBBKKQQ';
function p4_alphabeta_treeclimber(state, count, colour, score, s, e, alpha, beta){
var move = p4_make_move(state, s, e, P4_QUEEN);
var i;
var ncolour = 1 - colour;
var movelist = p4_parse(state, colour, move.ep, -score);
var movecount = movelist.length;
if(count){
//branch nodes
var t;
for(i = 0; i < movecount; i++){
var mv = movelist[i];
var mscore = mv[0];
var ms = mv[1];
var me = mv[2];
if (mscore > P4_WIN){ //we won! Don't look further.
alpha = P4_KING_VALUE;
break;
}
t = -p4_alphabeta_treeclimber(state, count - 1, ncolour, mscore, ms, me,
-beta, -alpha);
if (t > alpha){
alpha = t;
}
if (alpha >= beta){
break;
}
}
if (alpha < -P4_WIN_NOW && ! p4_check_check(state, colour)){
/* Whatever we do, we lose the king.
* But if it is not check then this is stalemate, and the
* score doesn't apply.
*/
alpha = state.stalemate_scores[colour];
}
if (alpha < -P4_WIN){
/*make distant checkmate seem less bad */
alpha += P4_WIN_DECAY;
}
}
else{
//leaf nodes
while(beta > alpha && --movecount != -1){
if(movelist[movecount][0] > alpha){
alpha = movelist[movecount][0];
}
}
}
p4_unmake_move(state, move);
return alpha;
}
/* p4_prepare() works out weightings for assessing various moves,
* favouring centralising moves early, for example.
*
* It is called before each tree search, not for each parse(), so it
* is OK for it to be a little bit slow. But that also means it drifts
* out of sync with the real board state, especially on deep searches.
*/
function p4_prepare(state){
var i, j, x, y, a;
var pieces = state.pieces = [[], []];
/*convert state.moveno half move count to move cycle count */
var moveno = state.moveno >> 1;
var board = state.board;
/* high earliness_weight indicates a low move number. The formula
* should work above moveno == 50, but this is javascript.
*/
var earliness_weight = (moveno > 50) ? 0 : parseInt(6 * Math.exp(moveno * -0.07));
var king_should_hide = moveno < 12;
var early = moveno < 5;
/* find the pieces, kings, and weigh material*/
var kings = [0, 0];
var material = [0, 0];
var best_pieces = [0, 0];
for(i = 20; i < 100; i++){
a = board[i];
var piece = a & 14;
var colour = a & 1;
if(piece){
pieces[colour].push([a, i]);
if (piece == P4_KING){
kings[colour] = i;
}
else{
material[colour] += P4_VALUES[piece];
best_pieces[colour] = Math.max(best_pieces[colour], P4_VALUES[piece]);
}
}
}
/*does a draw seem likely soon?*/
var draw_likely = (state.draw_timeout > 90 || state.current_repetitions >= 2);
if (draw_likely)
p4_log("draw likely", state.current_repetitions, state.draw_timeout);
state.values = [[], []];
var qvalue = P4_VALUES[P4_QUEEN]; /*used as ballast in various ratios*/
var material_sum = material[0] + material[1] + 2 * qvalue;
var wmul = 2 * (material[1] + qvalue) / material_sum;
var bmul = 2 * (material[0] + qvalue) / material_sum;
var multipliers = [wmul, bmul];
var emptiness = 4 * P4_QUEEN / material_sum;
state.stalemate_scores = [parseInt(0.5 + (wmul - 1) * 2 * qvalue),
parseInt(0.5 + (bmul - 1) * 2 * qvalue)];
//p4_log("value multipliers (W, B):", wmul, bmul,
// "stalemate scores", state.stalemate_scores);
for (i = 0; i < P4_VALUES.length; i++){
var v = P4_VALUES[i];
if (v < P4_WIN){//i.e., not king
state.values[0][i] = parseInt(v * wmul + 0.5);
state.values[1][i] = parseInt(v * bmul + 0.5);
}
else {
state.values[0][i] = v;
state.values[1][i] = v;
}
}
/*used for pruning quiescence search */
state.best_pieces = [parseInt(best_pieces[0] * wmul + 0.5),
parseInt(best_pieces[1] * bmul + 0.5)];
var kx = [kings[0] % 10, kings[1] % 10];
var ky = [parseInt(kings[0] / 10), parseInt(kings[1] / 10)];
/* find the frontmost pawns in each file */
var pawn_cols = [[], []];
for (y = 3; y < 9; y++){
for (x = 1; x < 9; x++){
i = y * 10 + x;
a = board[i];
if ((a & 14) != P4_PAWN)
continue;
if ((a & 1) == 0){
pawn_cols[0][x] = y;
}
else if (pawn_cols[1][x] === undefined){
pawn_cols[1][x] = y;
}
}
}
var target_king = (moveno >= 20 || material_sum < 5 * qvalue);
var weights = state.weights;
for (y = 2; y < 10; y++){
for (x = 1; x < 9; x++){
i = y * 10 + x;
var early_centre = P4_CENTRALISING_WEIGHTS[i] * earliness_weight;
var plateau = P4_KNIGHT_WEIGHTS[i];
for (var c = 0; c < 2; c++){
var dx = Math.abs(kx[1 - c] - x);
var dy = Math.abs(ky[1 - c] - y);
var our_dx = Math.abs(kx[c] - x);
var our_dy = Math.abs(ky[c] - y);
var d = Math.max(Math.sqrt(dx * dx + dy * dy), 1) + 1;
var mul = multipliers[c]; /*(mul < 1) <==> we're winning*/
var mul3 = mul * mul * mul;
var at_home = y == 2 + c * 7;
var pawn_home = y == 3 + c * 5;
var row4 = y == 5 + c;
var promotion_row = y == 9 - c * 7;
var get_out = (early && at_home) * -5;
var knight = parseInt(early_centre * 0.3) + 2 * plateau + get_out;
var rook = parseInt(early_centre * 0.3);
var bishop = parseInt(early_centre * 0.6) + plateau + get_out;
if (at_home){
rook += (x == 4 || x == 5) * (earliness_weight + ! target_king);
rook += (x == 1 || x == 8) * (moveno > 10 && moveno < 20) * -3;
rook += (x == 2 || x == 7) * (moveno > 10 && moveno < 20) * -1;
}
/*Queen wants to stay home early, then jump right in*/
/*keep kings back on home row for a while*/
var queen = parseInt(plateau * 0.5 + early_centre * (0.5 - early));
var king = (king_should_hide && at_home) * 2 * earliness_weight;
/*empty board means pawn advancement is more urgent*/
var get_on_with_it = Math.max(emptiness * 2, 1);
var pawn = get_on_with_it * P4_BASE_PAWN_WEIGHTS[c ? 119 - i : i];
if (early){
/* Early pawn weights are slightly randomised, so each game is different.
*/
if (y >= 4 && y <= 7){
var boost = 1 + 3 * (y == 5 || y == 6);
pawn += parseInt((boost + p4_random_int(state, 4)) * 0.1 *
early_centre);
}
if (x == 4 || x == 5){
//discourage middle pawns from waiting at home
pawn -= 3 * pawn_home;
pawn += 3 * row4;
}
}
/*pawn promotion row is weighted as a queen minus a pawn.*/
if (promotion_row)
pawn += state.values[c][P4_QUEEN] - state.values[c][P4_PAWN];
/*pawns in front of a castled king should stay there*/
pawn += 4 * (y == 3 && ky[c] == 2 && Math.abs(our_dx) < 2 &&
kx[c] != 5 && x != 4 && x != 5);
/*passed pawns (having no opposing pawn in front) are encouraged. */
var cols = pawn_cols[1 - c];
if (cols[x] == undefined ||
(c == 0 && cols[x] < y) ||
(c == 1 && cols[x] > y))
pawn += 2;
/* After a while, start going for opposite king. Just
* attract pieces into the area so they can mill about in
* the area, waiting for an opportunity.
*
* As prepare is only called at the beginning of each tree
* search, the king could wander out of the targetted area
* in deep searches. But that's OK. Heuristics are
* heuristics.
*/
if (target_king){
knight += 2 * parseInt(8 * mul / d);
rook += 2 * ((dx < 2) + (dy < 2));
bishop += 3 * (Math.abs((dx - dy)) < 2);
queen += 2 * parseInt(8 / d) + (dx * dy == 0) + (dx - dy == 0);
/* The losing king wants to stay in the middle, while
the winning king goes in for the kill.*/
var king_centre_wt = 8 * emptiness * P4_CENTRALISING_WEIGHTS[i];
king += parseInt(150 * emptiness / (mul3 * d) + king_centre_wt * mul3);
}
weights[P4_PAWN + c][i] = pawn;
weights[P4_KNIGHT + c][i] = knight;
weights[P4_ROOK + c][i] = rook;
weights[P4_BISHOP + c][i] = bishop;
weights[P4_QUEEN + c][i] = queen;
weights[P4_KING + c][i] = king;
if (draw_likely && mul < 1){
/*The winning side wants to avoid draw, so adds jitter to its weights.*/
var range = 3 / mul3;
for (j = 2 + c; j < 14; j += 2){
weights[j][i] += p4_random_int(state, range);
}
}
}
}
}
state.prepared = true;
}
function p4_maybe_prepare(state){
if (! state.prepared)
p4_prepare(state);
}
function p4_parse(state, colour, ep, score) {
var board = state.board;
var s, e; //start and end position
var E, a; //E=piece at end place, a= piece moving
var i, j;
var other_colour = 1 - colour;
var dir = (10 - 20 * colour); //dir= 10 for white, -10 for black
var movelist = [];
var captures = [];
var weight;
var pieces = state.pieces[colour];
var castle_flags = (state.castles >> (colour * 2)) & 3;
var values = state.values[other_colour];
var all_weights = state.weights;
for (j = pieces.length - 1; j >= 0; j--){
s = pieces[j][1]; // board position
a = board[s]; //piece number
var weight_lut = all_weights[a];
weight = score - weight_lut[s];
a &= 14;
if(a > 2){ //non-pawns
var moves = P4_MOVES[a];
if(a & 2){
for(i = 0; i < 8; i++){
e = s + moves[i];
E = board[e];
if(!E){
movelist.push([weight + values[E] + weight_lut[e], s, e]);
}
else if((E&17)==other_colour){
captures.push([weight + values[E] + weight_lut[e] + all_weights[E][e], s, e]);
}
}
if(a == P4_KING && castle_flags){
if((castle_flags & 1) &&
(board[s - 1] + board[s - 2] + board[s - 3] == 0) &&
p4_check_castling(board, s - 2, other_colour, dir, -1)){//Q side
movelist.push([weight + 12, s, s - 2]); //no analysis, just encouragement
}
if((castle_flags & 2) && (board[s + 1] + board[s + 2] == 0) &&
p4_check_castling(board, s, other_colour, dir, 1)){//K side
movelist.push([weight + 13, s, s + 2]);
}
}
}
else{//rook, bishop, queen
var mlen = moves.length;
for(i=0;i<mlen;){ //goeth thru list of moves
var m = moves[i++];
e=s;
do {
e+=m;
E=board[e];
if(!E){
movelist.push([weight + values[E] + weight_lut[e], s, e]);
}
else if((E&17)==other_colour){
captures.push([weight + values[E] + weight_lut[e] + all_weights[E][e], s, e]);
}
}while(!E);
}
}
}
else{ //pawns
e=s+dir;
if(!board[e]){
movelist.push([weight + weight_lut[e], s, e]);
/* s * (120 - s) < 3200 true for outer two rows on either side.*/
var e2 = e + dir;
if(s * (120 - s) < 3200 && (!board[e2])){
movelist.push([weight + weight_lut[e2], s, e2]);
}
}
/* +/-1 for pawn capturing */
E = board[--e];
if(E && (E & 17) == other_colour){
captures.push([weight + values[E] + weight_lut[e] + all_weights[E][e], s, e]);
}
e += 2;
E = board[e];
if(E && (E & 17) == other_colour){
captures.push([weight + values[E] + weight_lut[e] + all_weights[E][e], s, e]);
}
}
}
if(ep){
var pawn = P4_PAWN | colour;
var taken;
/* Some repetitive calculation here could be hoisted out, but that would
probably slow things: the common case is no pawns waiting to capture
enpassant, not 2.
*/
s = ep - dir - 1;
if (board[s] == pawn){
taken = values[P4_PAWN] + all_weights[P4_PAWN | other_colour][ep - dir];
captures.push([score - weight_lut[s] + weight_lut[ep] + taken, s, ep]);
}
s += 2;
if (board[s] == pawn){
taken = values[P4_PAWN] + all_weights[P4_PAWN | other_colour][ep - dir];
captures.push([score - weight_lut[s] + weight_lut[ep] + taken, s, ep]);
}
}
return captures.concat(movelist);
}
/*Explaining the bit tricks used in check_castling and check_check:
*
* in binary: 16 8 4 2 1
* empty
* pawn 1 c
* rook 1 c
* knight 1 1 c
* bishop 1 c
* king 1 1 c
* queen 1 1 c
* wall 1
*
* so:
*
* piece & (16 | 4 | 2 | 1) is:
* 2 + c for kings and pawns
* 4 + c for rooks and queens
* 6 + c for knights
* 0 + c for bishops
* 16 for walls
*
* thus:
* ((piece & 23) == 4 | colour) separates the rooks and queens out
* from the rest.
* ((piece & 27) == 8 | colour) does the same for queens and bishops.
*/
/* check_castling
*
* s - "start" location (either king home square, or king destination)
* the checks are done left to right.
* * dir - direction of travel (White: 10, Black: -10)
* side: -1 means Q side; 1, K side
*/
function p4_check_castling(board, s, colour, dir, side){
var e;
var E;
var m, p;
var knight = colour + P4_KNIGHT;
var diag_slider = P4_BISHOP | colour;
var diag_mask = 27;
var grid_slider = P4_ROOK | colour;
var king_pawn = 2 | colour;
var grid_mask = 23;
/* go through 3 positions, checking for check in each
*/
for(p = s; p < s + 3; p++){
//bishops, rooks, queens
e = p;
do{
e += dir;
E=board[e];
} while (! E);
if((E & grid_mask) == grid_slider)
return 0;
e = p;
var delta = dir - 1;
do{
e += delta;
E=board[e];
} while (! E);
if((E & diag_mask) == diag_slider)
return 0;
e = p;
delta += 2;
do{
e += delta;
E=board[e];
} while (! E);
if((E & diag_mask) == diag_slider)
return 0;
/*knights on row 7. (row 6 is handled below)*/
if (board[p + dir - 2] == knight ||
board[p + dir + 2] == knight)
return 0;
}
/* a pawn or king in any of 5 positions on row 7.
* or a knight on row 6. */
for(p = s + dir - 1; p < s + dir + 4; p++){
E = board[p] & grid_mask;
if(E == king_pawn || board[p + dir] == knight)
return 0;
}
/* scan back row for rooks, queens on the other side.
* Same side check is impossible, because the castling rook is there
*/
e = (side < 0) ? s + 2 : s;
do {
e -= side;
E=board[e];
} while (! E);
if((E & grid_mask) == grid_slider)
return 0;
return 1;
}
function p4_check_check(state, colour){
var board = state.board;
/*find the king. The pieces list updates from the end,
* so the last-most king is correctly placed.*/
var pieces = state.pieces[colour];
var p;
var i = pieces.length;
do {
p = pieces[--i];
} while (p[0] != (P4_KING | colour));
var s = p[1];
var other_colour = 1 - colour;
var dir = 10 - 20 * colour;
if (board[s + dir - 1] == (P4_PAWN | other_colour) ||
board[s + dir + 1] == (P4_PAWN | other_colour))
return true;
var knight_moves = P4_MOVES[P4_KNIGHT];
var king_moves = P4_MOVES[P4_KING];
var knight = P4_KNIGHT | other_colour;
var king = P4_KING | other_colour;
for (i = 0; i < 8; i++){
if (board[s + knight_moves[i]] == knight ||
board[s + king_moves[i]] == king)
return true;
}
var diagonal_moves = P4_MOVES[P4_BISHOP];
var grid_moves = P4_MOVES[P4_ROOK];
/* diag_mask ignores rook moves of queens,
* grid_mask ignores the bishop moves*/
var diag_slider = P4_BISHOP | other_colour;
var diag_mask = 27;
var grid_slider = P4_ROOK | other_colour;
var grid_mask = 23;
for (i = 0; i < 4; i++){
var m = diagonal_moves[i];
var e = s;
var E;
do {
e += m;
E = board[e];
} while (!E);
if((E & diag_mask) == diag_slider)
return true;
m = grid_moves[i];
e = s;
do {
e += m;
E = board[e];
} while (!E);
if((E & grid_mask) == grid_slider)
return true;
}
return false;
}
function p4_optimise_piece_list(state){
var i, p, s, e;
var movelists = [
p4_parse(state, 0, 0, 0),
p4_parse(state, 1, 0, 0)
];
var weights = state.weights;
var board = state.board;
for (var colour = 0; colour < 2; colour++){
var our_values = state.values[colour];
var pieces = state.pieces[colour];
var movelist = movelists[colour];
var threats = movelists[1 - colour];
/* sparse array to index by score. */
var scores = [];
for (i = 0; i < pieces.length; i++){
p = pieces[i];
scores[p[1]] = {
score: 0,
piece: p[0],
pos: p[1],
threatened: 0
};
}
/* Find the best score for each piece by pure static weights,
* ignoring captures, which have their own path to the top. */
for(i = movelist.length - 1; i >= 0; i--){
var mv = movelist[i];
var score = mv[0];
s = mv[1];
e = mv[2];
if(! board[e]){
var x = scores[s];
x.score = Math.max(x.score, score);
}
}
/* moving out of a threat is worth considering, especially
* if it is a pawn and you are not.*/
for(i = threats.length - 1; i >= 0; i--){
var mv = threats[i];
var x = scores[mv[2]];
if (x !== undefined){
var S = board[mv[1]];
var r = (1 + x.piece > 3 + S < 4) * 0.01;
if (x.threatened < r)
x.threatened = r;
}
}
var pieces2 = [];
for (i = 20; i < 100; i++){
p = scores[i];
if (p !== undefined){
p.score += p.threatened * our_values[p.piece];
pieces2.push(p);
}
}
pieces2.sort(function(a, b){return a.score - b.score;});
for (i = 0; i < pieces2.length; i++){
p = pieces2[i];
pieces[i] = [p.piece, p.pos];
}
}
}
function p4_findmove(state, level, colour, ep){
p4_prepare(state);
p4_optimise_piece_list(state);
var board = state.board;
if (arguments.length == 2){
colour = state.to_play;
ep = state.enpassant;
}
var movelist = p4_parse(state, colour, ep, 0);
var alpha = P4_MIN_SCORE;
var mv, t, i;
var bs = 0;
var be = 0;
if (level <= 0){
for (i = 0; i < movelist.length; i++){
mv = movelist[i];
if(movelist[i][0] > alpha){
alpha = mv[0];
bs = mv[1];
be = mv[2];
}
}
return [bs, be, alpha];
}
for(i = 0; i < movelist.length; i++){
mv = movelist[i];
var mscore = mv[0];
var ms = mv[1];
var me = mv[2];
if (mscore > P4_WIN){
p4_log("XXX taking king! it should never come to this");
alpha = P4_KING_VALUE;
bs = ms;
be = me;
break;
}
t = -state.treeclimber(state, level - 1, 1 - colour, mscore, ms, me,
P4_MIN_SCORE, -alpha);
if (t > alpha){
alpha = t;
bs = ms;
be = me;
}
}
if (alpha < -P4_WIN_NOW && ! p4_check_check(state, colour)){
alpha = state.stalemate_scores[colour];
}
return [bs, be, alpha];
}
/*p4_make_move changes the state and returns an object containing
* everything necesary to undo the change.
*
* p4_unmake_move uses the p4_make_move return value to restore the
* previous state.
*/
function p4_make_move(state, s, e, promotion){
var board = state.board;
var S = board[s];
var E = board[e];
board[e] = S;
board[s] = 0;
var piece = S & 14;
var moved_colour = S & 1;
var end_piece = S; /* can differ from S in queening*/
//now some stuff to handle queening, castling
var rs = 0, re, rook;
var ep_taken = 0, ep_position;
var ep = 0;
if(piece == P4_PAWN){
if((60 - e) * (60 - e) > 900){
/*got to end; replace the pawn on board and in pieces cache.*/
promotion |= moved_colour;
board[e] = promotion;
end_piece = promotion;
}
else if (((s ^ e) & 1) && E == 0){
/*this is a diagonal move, but the end spot is empty, so we surmise enpassant */
ep_position = e - 10 + 20 * moved_colour;
ep_taken = board[ep_position];
board[ep_position] = 0;
}
else if ((s - e) * (s - e) == 400){
/*delta is 20 --> two row jump at start*/
ep = (s + e) >> 1;
}
}
else if (piece == P4_KING && ((s - e) * (s - e) == 4)){ //castling - move rook too
rs = s - 4 + (s < e) * 7;
re = (s + e) >> 1; //avg of s,e=rook's spot
rook = moved_colour + P4_ROOK;
board[rs] = 0;
board[re] = rook;
//piece_locations.push([rook, re]);
}
var old_castle_state = state.castles;
if (old_castle_state){
var mask = 0;
var shift = moved_colour * 2;
var side = moved_colour * 70;
var s2 = s - side;
var e2 = e + side;
//wipe both our sides if king moves
if (s2 == 25)
mask |= 3 << shift;
//wipe one side on any move from rook points
else if (s2 == 21)
mask |= 1 << shift;
else if (s2 == 28)
mask |= 2 << shift;
//or on any move *to* opposition corners
if (e2 == 91)
mask |= 4 >> shift;
else if (e2 == 98)
mask |= 8 >> shift;
state.castles &= ~mask;
}
var old_pieces = state.pieces.concat();
var our_pieces = old_pieces[moved_colour];
var dest = state.pieces[moved_colour] = [];
for (var i = 0; i < our_pieces.length; i++){
var x = our_pieces[i];
var pp = x[0];
var ps = x[1];
if (ps != s && ps != rs){
dest.push(x);
}
}
dest.push([end_piece, e]);
if (rook)
dest.push([rook, re]);
if (E || ep_taken){
var their_pieces = old_pieces[1 - moved_colour];
dest = state.pieces[1 - moved_colour] = [];
var gone = ep_taken ? ep_position : e;
for (i = 0; i < their_pieces.length; i++){
var x = their_pieces[i];
if (x[1] != gone){
dest.push(x);
}
}
}
return {
/*some of these (e.g. rook) could be recalculated during
* unmake, possibly more cheaply. */
s: s,
e: e,
S: S,
E: E,
ep: ep,
castles: old_castle_state,
rs: rs,
re: re,
rook: rook,
ep_position: ep_position,
ep_taken: ep_taken,
pieces: old_pieces
};
}
function p4_unmake_move(state, move){
var board = state.board;
if (move.ep_position){
board[move.ep_position] = move.ep_taken;
}
board[move.s] = move.S;
board[move.e] = move.E;
//move.piece_locations.length--;
if(move.rs){
board[move.rs] = move.rook;
board[move.re] = 0;
//move.piece_locations.length--;
}
state.pieces = move.pieces;
state.castles = move.castles;
}
function p4_insufficient_material(state){
var knights = false;
var bishops = undefined;
var i;
var board = state.board;
for(i = 20; i < 100; i++){
var piece = board[i] & 14;
if(piece == 0 || piece == P4_KING){
continue;
}
if (piece == P4_KNIGHT){
/* only allow one knight of either colour, never with a bishop */
if (knights || bishops !== undefined){
return false;
}
knights = true;
}
else if (piece == P4_BISHOP){
/*any number of bishops, but on only one colour square */
var x = i & 1;
var y = parseInt(i / 10) & 1;
var parity = x ^ y;
if (knights){
return false;
}
else if (bishops === undefined){
bishops = parity;
}
else if (bishops != parity){
return false;
}
}
else {
return false;
}
}
return true;
}
/* p4_move(state, s, e, promotion)
* s, e are start and end positions
*
* promotion is the desired pawn promotion if the move gets a pawn to the other
* end.
*
* return value contains bitwise flags
*/
var P4_MOVE_FLAG_OK = 1;
var P4_MOVE_FLAG_CHECK = 2;
var P4_MOVE_FLAG_MATE = 4;
var P4_MOVE_FLAG_CAPTURE = 8;
var P4_MOVE_FLAG_CASTLE_KING = 16;
var P4_MOVE_FLAG_CASTLE_QUEEN = 32;
var P4_MOVE_FLAG_DRAW = 64;
var P4_MOVE_ILLEGAL = 0;
var P4_MOVE_MISSED_MATE = P4_MOVE_FLAG_CHECK | P4_MOVE_FLAG_MATE;
var P4_MOVE_CHECKMATE = P4_MOVE_FLAG_OK | P4_MOVE_FLAG_CHECK | P4_MOVE_FLAG_MATE;
var P4_MOVE_STALEMATE = P4_MOVE_FLAG_OK | P4_MOVE_FLAG_MATE;
function p4_move(state, s, e, promotion){
var board = state.board;
var colour = state.to_play;
var other_colour = 1 - colour;
if (s != parseInt(s)){
if (e === undefined){
var mv = p4_interpret_movestring(state, s);
s = mv[0];
e = mv[1];
if (s == 0)
return {flags: P4_MOVE_ILLEGAL, ok: false};
promotion = mv[2];
}
else {/*assume two point strings: 'e2', 'e4'*/
s = p4_destringify_point(s);
e = p4_destringify_point(e);
}
}
if (promotion === undefined)
promotion = P4_QUEEN;
var E=board[e];
var S=board[s];
/*See if this move is even slightly legal, disregarding check.
*/
var i;
var legal = false;
p4_maybe_prepare(state);
var moves = p4_parse(state, colour, state.enpassant, 0);
for (i = 0; i < moves.length; i++){
if (e == moves[i][2] && s == moves[i][1]){
legal = true;
break;
}
}
if (! legal) {
return {flags: P4_MOVE_ILLEGAL, ok: false};
}
/*Try the move, and see what the response is.*/
var changes = p4_make_move(state, s, e, promotion);
/*is it check? */
if (p4_check_check(state, colour)){
p4_unmake_move(state, changes);
p4_log('in check', changes);
return {flags: P4_MOVE_ILLEGAL, ok: false, string: "in check!"};
}
/*The move is known to be legal. We won't be undoing it.*/
var flags = P4_MOVE_FLAG_OK;
state.enpassant = changes.ep;
state.history.push([s, e, promotion]);