forked from Chessforeva/Csharp4chess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OliThink.cs
1725 lines (1558 loc) · 52 KB
/
OliThink.cs
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
/* OliThink5 Java(c) Oliver Brausch 28.Jan.2010, ob112@web.de, http://home.arcor.de/dreamlike */
/* port to C# by Chessforeva.blogspot.com */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;
/*
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.security.AccessControlException;
import java.util.HashMap;
import java.util.Random;
import java.util.StringTokenizer;
*/
namespace OliThink
{
public class OliThink
{
const String VER = "5.3.0 Java";
const int PAWN = 1;
const int KNIGHT = 2;
const int KING = 3;
const int ENP = 4;
const int BISHOP = 5;
const int ROOK = 6;
const int QUEEN = 7;
const int CNODES = 0xFFFF;
const int HEUR = 9900000;
private static int[] pval = { 0, 100, 290, 0, 100, 310, 500, 950 };
private static int[] capval = { 0, HEUR + 1, HEUR + 2, 0, HEUR + 1, HEUR + 2, HEUR + 3, HEUR + 4 };
private static int[] pawnrun = { 0, 0, 1, 8, 16, 32, 64, 128 };
private int FROM(int x) { return ((x) & 63); }
private int TO(int x) { return (((x) >> 6) & 63); }
private int PROM(int x) { return (((x) >> 12) & 7); }
private int PIECE(int x) { return (((x) >> 15) & 7); }
private int ONMV(int x) { return (((x) >> 18) & 1); }
private int CAP(int x) { return (((x) >> 19) & 7); }
private int _TO(int x) { return ((x) << 6); }
private int _PROM(int x) { return ((x) << 12); }
private int _PIECE(int x) { return ((x) << 15); }
private int _ONMV(int x) { return ((x) << 18); }
private int _CAP(int x) { return ((x) << 19); }
private int PREMOVE(int f, int p, int c) { return ((f) | _ONMV(c) | _PIECE(p)); }
private long RATT1(int f) { return rays[((f) << 7) | key000(BOARD(), f)]; }
private long RATT2(int f) { return rays[((f) << 7) | key090(BOARD(), f) | 0x2000]; }
private long BATT3(int f) { return rays[((f) << 7) | key045(BOARD(), f) | 0x4000]; }
private long BATT4(int f) { return rays[((f) << 7) | key135(BOARD(), f) | 0x6000]; }
private long RXRAY1(int f) { return rays[((f) << 7) | key000(BOARD(), f) | 0x8000]; }
private long RXRAY2(int f) { return rays[((f) << 7) | key090(BOARD(), f) | 0xA000]; }
private long BXRAY3(int f) { return rays[((f) << 7) | key045(BOARD(), f) | 0xC000]; }
private long BXRAY4(int f) { return rays[((f) << 7) | key135(BOARD(), f) | 0xE000]; }
private long ROCC1(int f) { return (RATT1(f) & BOARD()); }
private long ROCC2(int f) { return (RATT2(f) & BOARD()); }
private long BOCC3(int f) { return (BATT3(f) & BOARD()); }
private long BOCC4(int f) { return (BATT4(f) & BOARD()); }
private long RMOVE1(int f) { return (RATT1(f) & (~BOARD())); }
private long RMOVE2(int f) { return (RATT2(f) & (~BOARD())); }
private long BMOVE3(int f) { return (BATT3(f) & (~BOARD())); }
private long BMOVE4(int f) { return (BATT4(f) & (~BOARD())); }
private long RCAP1(int f, int c) { return (RATT1(f) & colorb[(c)^1]); }
private long RCAP2(int f, int c) { return (RATT2(f) & colorb[(c)^1]); }
private long BCAP3(int f, int c) { return (BATT3(f) & colorb[(c)^1]); }
private long BCAP4(int f, int c) { return (BATT4(f) & colorb[(c)^1]); }
private long ROCC(int f) { return (ROCC1(f) | ROCC2(f)); }
private long BOCC(int f) { return (BOCC3(f) | BOCC4(f)); }
private long RMOVE(int f) { return (RMOVE1(f) | RMOVE2(f)); }
private long BMOVE(int f) { return (BMOVE3(f) | BMOVE4(f)); }
private long RCAP(int f, int c) { return (ROCC(f) & colorb[(c)^1]); }
private long BCAP(int f, int c) { return (BOCC(f) & colorb[(c)^1]); }
private long SHORTMOVE(long x) { return ((x) & ((x)^BOARD())); }
private long SHORTOCC(long x) { return ((x) & BOARD()); }
private long SHORTCAP(long x, int c) { return ((x) & colorb[(c)^1]); }
private long NMOVE(int x) { return (SHORTMOVE(nmoves[x])); }
private long KMOVE(int x) { return (SHORTMOVE(kmoves[x])); }
private long PMOVE(int x, int c) { return (pmoves[(c)][(x)] & (~BOARD())); }
private long NOCC(int x) { return (SHORTOCC(nmoves[x])); }
private long KOCC(int x) { return (SHORTOCC(kmoves[x])); }
private long POCC(int x, int c) { return (pcaps[(c)][(x)] & BOARD()); }
private long NCAP(int x, int c) { return (SHORTCAP(nmoves[x], (c))); }
private long KCAP(int x, int c) { return (SHORTCAP(kmoves[x], (c))); }
private long PCAP(int x, int c) { return (pcaps[(c)][(x)] & colorb[(c)^1]); }
private long PCA3(int x, int c) { return (pcaps[(c)][(x) | 64] & (colorb[(c)^1] | ((BIT[ENPASS()]) & (c == 1 ? 0xFF0000L : 0xFF0000000000L)))); }
private long PCA4(int x, int c) { return (pcaps[(c)][(x) | 128] & (colorb[(c)^1] | ((BIT[ENPASS()]) & (c == 1? 0xFF0000L : 0xFF0000000000L)))); }
private bool RANK(int x, int y) { return (((x) & 0x38) == (y)); }
private bool TEST(int f, long b) { return (BIT[f] & (b)) != 0; }
private int ENPASS() { return (flags & 63); }
private int CASTLE() { return (flags & 960); }
private int COUNT() { return (count & 0x3FF); }
const long HSIZEB = 0x200000;
const long HMASKB = HSIZEB - 1;
const long HINVB = 0xFFFFFF & (~HMASKB);
const long HSIZEP = 0x400000;
const long HMASKP = HSIZEP - 1;
const long HINVP = 0xFFFFFF & (~HMASKP);
private long[] hashDB = new long[HSIZEB];
private long[] hashDP = new long[HSIZEP];
private long hashb = 0L;
private long[] hstack = new long[0x800];
private long[] mstack = new long[0x800];
private long[] hashxor = new long[4096];
private long[] rays = new long[0x10000];
private long[][] pmoves = { new long[64], new long[64] };
private long[][] pcaps = { new long[192], new long[192] };
private long[] nmoves = new long[64];
private long[] kmoves = new long[64];
private static int[] _knight = { -17, -10, 6, 15, 17, 10, -6, -15 };
private static int[] _king = { -9, -1, 7, 8, 9, 1, -7, -8 };
private long[] BIT = new long[64];
private byte[] LSB = new byte[0x10000];
private byte[] BITC = new byte[0x10000] ;
private int[] crevoke = new int[64];
private int[] nmobil = new int[64];
private int[] kmobil = new int[64];
private int[][] pawnprg = { new int[64], new int[64] };
private long[][] pawnfree = { new long[64], new long[64] };
private long[][] pawnfile = { new long[64], new long[64] };
private long[][] pawnhelp = { new long[64], new long[64] };
private int[][] movelist = new int[64][]; // int[64][256]
private int[] movenum = new int[64];
private int[][] pv = new int[64][]; // int[64][64]
private int[] pvlength = new int[64];
private int[] value = new int[64];
private int iter;
private String pieceChar = "*PNK.BRQ";
private long searchtime, maxtime, starttime;
private bool sabort, noabort;
private int sd = 32;
public String bestmove = "";
private int count, flags, mat, onmove, engine =-1;
private int[] kingpos = new int[2];
private long[] pieceb = new long[8];
private long[] colorb = new long[2];
private String[] irbuf = new String[20];
private long BOARD() { return (colorb[0] | colorb[1]); }
private long RQU() { return (pieceb[QUEEN] | pieceb[ROOK]); }
private long BQU() { return (pieceb[QUEEN] | pieceb[BISHOP]); }
private static int[] nullvar = { 13, 43, 149, 519, 1809, 6311, 22027 };
private long[] bmask45 = new long[64];
private long[] bmask135 = new long[64];
private int[] killer = new int[128];
private int[] history = new int[0x1000];
private int time = 30000;
private int mps = 0;
private int baze = 5;
private int inc = 0;
private bool post = true;
private long getLowestBit(long bb) {
return bb & (-(long)bb);
}
private int _getpiece(char s, int[] c) {
int i;
for (i = 1; i < 8; i++)
if (pieceChar[i] == s) { c[0] = 0; return i; }
else if (pieceChar[i] == s-32) { c[0] = 1; return i; }
return 0;
}
private bool book;
private void _parse_fen(String fen) {
char s, mv = 'w';
String pos = "", cas = "", enps = "";
int c, i, halfm = 0, fullm = 1, col = 0, row = 7;
for (i = 0; i < 8; i++) pieceb[i] = 0L;
colorb[0] = colorb[1] = hashb = 0L;
mat = i = c = 0;
book = false;
String[] st = fen.Split(' ');
if (st.Length>0) pos = st[0];
if (st.Length>1) mv = st[1][0];
if (st.Length>2) cas = st[2];
if (st.Length>3) enps = st[3];
if (st.Length>4) halfm = Convert.ToInt32(st[4]);
if (st.Length>5) fullm = Convert.ToInt32(st[5]);
for (i = 0; i < pos.Length; i++) {
s = pos[i];
if (s == '/') {
row--;
col = 0;
} else if (s >= '1' && s <= '8') {
col += s - '0';
} else {
int[] cp = new int[]{c};
int p = _getpiece(s, cp);
c = cp[0];
if (p == KING) kingpos[c] = row*8 + col;
else mat += c == 1 ? -pval[p] : pval[p];
hashb ^= hashxor[col | row << 3 | i << 6 | (c == 1 ? 512 : 0)];
pieceb[p] |= BIT[row*8 + col];
colorb[c] |= BIT[row*8 + (col++)];
}
}
onmove = mv == 'b' ? 1 : 0;
flags = i = 0;
for (i = 0; i < cas.Length; i++) {
s = cas[i];
if (s == 'K') flags |= (int)BIT[6];
if (s == 'k') flags |= (int)BIT[7];
if (s == 'Q') flags |= (int)BIT[8];
if (s == 'q') flags |= (int)BIT[9];
}
if (enps[0] >= 'a' && enps[0] <= 'h' && enps[1] >= '1' && enps[1] <= '8') flags |= 8*(enps[1] - '1') + enps[0] - 'a';
count = (fullm - 1)*2 + onmove + (halfm << 10);
for (i = 0; i < COUNT(); i++) hstack[i] = 0L;
}
private String sfen = "rnbqkbnr/pppppppp/////PPPPPPPP/RNBQKBNR w KQkq - 0 1";
const int BKSIZE = 1; // We ignore book here porting to C#.
// Not very optimal, may be developed later.
private int[] bkmove = new int[BKSIZE*32];
private int[] bkflag = new int[BKSIZE];
private int[] bkcount = new int[3];
private void _readbook(String bk) {
book = false;
printf("NO BOOK");
_parse_fen(sfen);
engine = 1;
sd = 32;
}
private int LOW16(long x) { return (int)((x) & 0xFFFF); }
private int LOW32(long x) { return (int)((x) & 0xFFFFFFFFL); }
private long L32(long x) { return ((x) & 0xFFFFFFFFL); }
private long r_x = 30903, r_y = 30903, r_z = 30903, r_w = 30903, r_carry = 0;
private long _rand_32() {
r_x = L32(r_x * 69069 + 1);
r_y ^= L32(r_y << 13);
r_y ^= L32(r_y >> 17);
r_y ^= L32(r_y << 5);
r_y = L32(r_y);
long t = L32((r_w << 1)) + r_z + r_carry;
r_carry = (L32(r_z >> 2) + L32(r_w >> 3) + L32(r_carry >> 2)) >> 30;
r_z = r_w;
r_w = L32(t);
return L32(r_x + r_y + r_w);
}
private long _rand_64() { long c = _rand_32(); return _rand_32() | (c << 32); }
private long getTime() {
return DateTime.UtcNow.Millisecond;
}
private byte getLsb(long bm) {
int n = (int) LOW32(bm);
if (n != 0) {
if (LOW16(n) != 0) return LSB[LOW16(n)];
else return (byte)(16 | LSB[LOW16(n >> 16)]);
} else {
n = (int)(bm >> 32);
if (LOW16(n) != 0) return (byte)(32 | LSB[LOW16(n)]);
else return (byte)(48 | LSB[LOW16(n >> 16)]);
}
}
private byte _slow_lsb(long bm) {
int k = -1;
while (bm != 0) { k++; if ((bm & 1) != 0) break; bm >>= 1; }
return (byte)k;
}
private byte _bitcnt(long bit) {
byte c = 0;
while (bit != 0) { bit &= (bit - 1); c++; }
return c;
}
private byte bitcnt (long n) {
byte a1 = (BITC[LOW16(n)]);
long g2 = n >> 16;
byte a2 = (BITC[LOW16(g2)]);
long g3 = n >> 32;
byte a3 = (BITC[LOW16(g3)]);
long g4 = n >> 48;
byte a4 = (BITC[LOW16(g4)]);
return (byte)(BITC[LOW16(n)]
+ BITC[LOW16(n >> 16)]
+ BITC[LOW16(n >> 32)]
+ BITC[LOW16(n >> 48)]);
}
private int identPiece(int f) {
if (TEST(f, pieceb[PAWN])) return PAWN;
if (TEST(f, pieceb[KNIGHT])) return KNIGHT;
if (TEST(f, pieceb[BISHOP])) return BISHOP;
if (TEST(f, pieceb[ROOK])) return ROOK;
if (TEST(f, pieceb[QUEEN])) return QUEEN;
if (TEST(f, pieceb[KING])) return KING;
return ENP;
}
private int key000(long b, int f) {
return (int) ((b >> (f & 56)) & 0x7E);
}
private int key090(long b, int f) {
int h;
b = b >> (f&7);
h = (int)((b & 0x1010101) | ((b >> 31) & 0x2020202));
h = (h & 0x303) | ((h >> 14) & 0xC0C);
return (h & 0xE) | ((h >> 4) & 0x70);
}
private int keyDiag(long _b) {
int h = (int)(_b | _b >> 32);
h |= h >> 16;
h |= h >> 8;
return h & 0x7E;
}
private int key045(long b, int f) {
return keyDiag(b & bmask45[f]);
}
private int key135(long b, int f) {
return keyDiag(b & bmask135[f]);
}
private bool DUALATT(int x, int y, int c) { return (battacked(x, c) || battacked(y, c)); }
private bool battacked(int f, int c) {
if ((PCAP(f, c) & pieceb[PAWN]) != 0) return true;
if ((NCAP(f, c) & pieceb[KNIGHT]) != 0) return true;
if ((KCAP(f, c) & pieceb[KING]) != 0) return true;
if ((RCAP1(f, c) & RQU()) != 0) return true;
if ((RCAP2(f, c) & RQU()) != 0) return true;
if ((BCAP3(f, c) & BQU()) != 0) return true;
if ((BCAP4(f, c) & BQU()) != 0) return true;
return false;
}
private long reach(int f, int c) {
return (NCAP(f, c) & pieceb[KNIGHT])
| (RCAP1(f, c) & RQU())
| (RCAP2(f, c) & RQU())
| (BCAP3(f, c) & BQU())
| (BCAP4(f, c) & BQU());
}
private long attacked(int f, int c) {
return (PCAP(f, c) & pieceb[PAWN]) | reach(f, c);
}
private void _init_pawns(long[] moves, long[] caps, long[] freep, long[] filep, long[] helpp, int c) {
int i, j;
for (i = 0; i < 64; i++) {
int rank = i/8;
int file = i&7;
int m = i + (c == 1 ? -8 : 8);
pawnprg[c][i] = pawnrun[c == 1 ? 7-rank : rank];
for (j = 0; j < 64; j++) {
int jrank = j/8;
int jfile = j&7;
int dfile = (jfile - file)*(jfile - file);
if (dfile > 1) continue;
if ((c == 1 && jrank < rank) || (c == 0 && jrank > rank)) {//The not touched half of the pawn
if (dfile == 0) filep[i] |= BIT[j];
freep[i] |= BIT[j];
} else if (dfile != 0 && (jrank - rank)*(jrank - rank) <= 1) {
helpp[i] |= BIT[j];
}
}
if (m < 0 || m > 63) continue;
moves[i] |= BIT[m];
if (file > 0) {
m = i + (c == 1 ? -9 : 7);
if (m < 0 || m > 63) continue;
caps[i] |= BIT[m];
caps[i + 64*(2 - c)] |= BIT[m];
}
if (file < 7) {
m = i + (c == 1 ? -7 : 9);
if (m < 0 || m > 63) continue;
caps[i] |= BIT[m];
caps[i + 64*(c + 1)] |= BIT[m];
}
}
}
private void _init_shorts(long[] moves, int[] m) {
int i, j, n;
for (i = 0; i < 64; i++) {
for (j = 0; j < 8; j++) {
n = i + m[j];
if (n < 64 && n >= 0 && ((n & 7)-(i & 7))*((n & 7)-(i & 7)) <= 4) {
moves[i] |= BIT[n];
}
}
}
}
private long _occ_free_board(int bc, int del, long free) {
long low, perm = free;
int i;
for (i = 0; i < bc; i++) {
low = getLowestBit(free);
free &= (~low);
if (!TEST(i, del)) perm &= (~low);
}
return perm;
}
private void _init_rays1() {
int i, f, iperm, bc, index;
long board, mmask, occ, move, xray;
for (f = 0; f < 64; f++) {
mmask = _rook0(f, 0L, 0) | BIT[f];
iperm = 1 << (bc = bitcnt(mmask));
for (i = 0; i < iperm; i++) {
board = _occ_free_board(bc, i, mmask);
move = _rook0(f, board, 1);
occ = _rook0(f, board, 2);
xray = _rook0(f, board, 3);
index = key000(board, f);
rays[(f << 7) + index] = occ | move;
rays[(f << 7) + index + 0x8000] = xray;
}
}
}
private void _init_rays2() {
int i, f, iperm, bc, index;
long board, mmask, occ, move, xray;
for (f = 0; f < 64; f++) {
mmask = _rook90(f, 0L, 0) | BIT[f];
iperm = 1 << (bc = bitcnt(mmask));
for (i = 0; i < iperm; i++) {
board = _occ_free_board(bc, i, mmask);
move = _rook90(f, board, 1);
occ = _rook90(f, board, 2);
xray = _rook90(f, board, 3);
index = key090(board, f);
rays[(f << 7) + index + 0x2000] = occ | move;
rays[(f << 7) + index + 0x8000 + 0x2000] = xray;
}
}
}
private void _init_rays3() {
int i, f, iperm, bc, index;
long board, mmask, occ, move, xray;
for (f = 0; f < 64; f++) {
mmask = _bishop45(f, 0L, 0) | BIT[f];
iperm = 1 << (bc = bitcnt(mmask));
for (i = 0; i < iperm; i++) {
board = _occ_free_board(bc, i, mmask);
move = _bishop45(f, board, 1);
occ = _bishop45(f, board, 2);
xray = _bishop45(f, board, 3);
index = key045(board, f);
rays[(f << 7) + index + 0x4000] = occ | move;
rays[(f << 7) + index + 0x8000 + 0x4000] = xray;
}
}
}
private void _init_rays4() {
int i, f, iperm, bc, index;
long board, mmask, occ, move, xray;
for (f = 0; f < 64; f++) {
mmask = _bishop135(f, 0L, 0) | BIT[f];
iperm = 1 << (bc = bitcnt(mmask));
for (i = 0; i < iperm; i++) {
board = _occ_free_board(bc, i, mmask);
move = _bishop135(f, board, 1);
occ = _bishop135(f, board, 2);
xray = _bishop135(f, board, 3);
index = key135(board, f);
rays[(f << 7) + index + 0x6000] = occ | move;
rays[(f << 7) + index + 0x8000 + 0x6000] = xray;
}
}
}
private long _rook0(int f, long board, int t) {
long free = 0L, occ = 0L, xray = 0L;
int i, b;
for (b = 0, i = f+1; i < 64 && i%8 != 0; i++) {
if (TEST(i, board)) {
if (b != 0) { xray |= BIT[i]; break; } else { occ |= BIT[i]; b = 1; }
}
if (b == 0) free |= BIT[i];
}
for (b = 0, i = f-1; i >= 0 && i%8 != 7; i--) {
if (TEST(i, board)) {
if (b != 0) { xray |= BIT[i]; break; } else { occ |= BIT[i]; b = 1; }
}
if (b == 0) free |= BIT[i];
}
return (t < 2) ? free : (t == 2 ? occ : xray);
}
private long _rook90(int f, long board, int t) {
long free = 0L, occ = 0L, xray = 0L;
int i, b;
for (b = 0, i = f-8; i >= 0; i-=8) {
if (TEST(i, board)) {
if (b != 0) { xray |= BIT[i]; break; } else { occ |= BIT[i]; b = 1; }
}
if (b == 0) free |= BIT[i];
}
for (b = 0, i = f+8; i < 64; i+=8) {
if (TEST(i, board)) {
if (b != 0) { xray |= BIT[i]; break; } else { occ |= BIT[i]; b = 1; }
}
if (b == 0) free |= BIT[i];
}
return (t < 2) ? free : (t == 2 ? occ : xray);
}
private long _bishop45(int f, long board, int t) {
long free = 0L, occ = 0L, xray = 0L;
int i, b;
for (b = 0, i = f+9; i < 64 && (i%8 != 0); i+=9) {
if (TEST(i, board)) {
if (b != 0) { xray |= BIT[i]; break; } else { occ |= BIT[i]; b = 1; }
}
if (b == 0) free |= BIT[i];
}
for (b = 0, i = f-9; i >= 0 && (i%8 != 7); i-=9) {
if (TEST(i, board)) {
if (b != 0) { xray |= BIT[i]; break; } else { occ |= BIT[i]; b = 1; }
}
if (b == 0) free |= BIT[i];
}
return (t < 2) ? free : (t == 2 ? occ : xray);
}
private long _bishop135(int f, long board, int t) {
long free = 0L, occ = 0L, xray = 0L;
int i, b;
for (b = 0, i = f-7; i >= 0 && (i%8 != 0); i-=7) {
if (TEST(i, board)) {
if (b != 0) { xray |= BIT[i]; break; } else { occ |= BIT[i]; b = 1; }
}
if (b == 0) free |= BIT[i];
}
for (b = 0, i = f+7; i < 64 && (i%8 != 7); i+=7) {
if (TEST(i, board)) {
if (b != 0) { xray |= BIT[i]; break; } else { occ |= BIT[i]; b = 1; }
}
if (b == 0) free |= BIT[i];
}
return (t < 2) ? free : (t == 2 ? occ : xray);
}
private String displaym(int m) {
String s = "" + ((char)('a' + FROM(m) % 8))
+ ((char)('1' + FROM(m) / 8))
+ ((char)('a' + TO(m) % 8))
+ ((char)('1' + TO(m) / 8));
if (PROM(m) != 0) s+= ("" + (char)(pieceChar[PROM(m)]+32));
return s;
}
private void printf(String s)
{
// do nothing for this silverlight
//System.Diagnostics.Debug.WriteLine(s);
}
private void errprintf(String s) { printf(s); }
private String displaypv() {
int i;
String s = "";
for (i = 0; i < pvlength[0]; i++) {
s+=(displaym(pv[0][i])+" ");
}
return s;
}
private int isDraw(long hp, int nrep) {
if (count > 0xFFF) { //fifty > 3
int i, c = 0, n = COUNT() - (count >> 10);
if (count >= 0x400*100) return 2; //100 plies
for (i = COUNT() - 2; i >= n; i--)
if (hstack[i] == hp && ++c == nrep) return 1;
} else if ((pieceb[PAWN] | RQU()) == 0) { //Check for mating material
if (_bitcnt(colorb[0]) <= 2 && _bitcnt(colorb[1]) <= 2) return 3;
}
return 0;
}
private long pinnedPieces(int f, int oc) {
long pin = 0L;
long b = ((RXRAY1(f) | RXRAY2(f)) & colorb[oc]) & RQU();
while (b != 0) {
int t = getLsb(b);
b ^= BIT[t];
pin |= RCAP(t, oc) & ROCC(f);
}
b = ((BXRAY3(f) | BXRAY4(f)) & colorb[oc]) & BQU();
while (b != 0) {
int t = getLsb(b);
b ^= BIT[t];
pin |= BCAP(t, oc) & BOCC(f);
}
return pin;
}
private byte getDir(int f, int t) {
if (((f ^ t) & 56) == 0) return 8;
if (((f ^ t) & 7) == 0) return 16;
return (byte)(((f - t) % 7) != 0 ? 32 : 64);
}
/* move is both makeMove and unmakeMove, only for unmakeMove the globalflags have to be restored (counter, castle, enpass...) */
private void move(int m, int c) {
int f = FROM(m);
int t = TO(m);
int p = PIECE(m);
int a = CAP(m);
colorb[c] ^= BIT[f];
pieceb[p] ^= BIT[f];
colorb[c] ^= BIT[t];
pieceb[p] ^= BIT[t];
hashb ^= hashxor[(f) | (p) << 6 | (c) << 9];
hashb ^= hashxor[(t) | (p) << 6 | (c) << 9];
flags &= 960;
count += 0x401;
if (a != 0) {
if (a == ENP) { // Enpassant Capture
t = (t&7) | (f&56);
a = PAWN;
} else if (a == ROOK && CASTLE() != 0) { //Revoke castling rights.
flags &= crevoke[t];
}
pieceb[a] ^= BIT[t];
colorb[c^1] ^= BIT[t];
hashb ^= hashxor[(t) | (a) << 6 | (c^1) << 9];
count &= 0x3FF; //Reset Fifty Counter
mat += c == 1 ? -pval[a] : +pval[a];
}
if (p == PAWN) {
if (((f^t)&8) == 0) flags |= f^24; //Enpassant
else if ((t&56) == 0 || (t&56) == 56) {
pieceb[PAWN] ^= BIT[t];
pieceb[PROM(m)] ^= BIT[t];
hashb ^= hashxor[(t) | (PAWN) << 6 | (c) << 9];
hashb ^= hashxor[(t) | (PROM(m)) << 6 | (c) << 9];
mat += c == 1 ? pval[PAWN] - pval[PROM(m)] : -pval[PAWN] + pval[PROM(m)];
}
count &= 0x3FF; //Reset Fifty Counter
} else if (p == KING) {
if (kingpos[c] == f) kingpos[c] = t; else kingpos[c] = f;
flags &= ~(320 << c); // Lose castling rights
if (((f^t)&3) == 2) { // Castle
if (t == 6) { f = 7; t = 5; }
else if (t == 2) { f = 0; t = 3; }
else if (t == 62) { f = 63; t = 61; }
else { f = 56; t = 59; }
colorb[c] ^= BIT[f];
pieceb[ROOK] ^= BIT[f];
colorb[c] ^= BIT[t];
pieceb[ROOK] ^= BIT[t];
hashb ^= hashxor[(f) | (ROOK) << 6 | (c) << 9];
hashb ^= hashxor[(t) | (ROOK) << 6 | (c) << 9];
}
} else if (p == ROOK && CASTLE() != 0) {
flags &= crevoke[f];
}
}
private void doMove(int m, int c) {
mstack[COUNT()] = count | (flags << 17) | (((long)(mat + 0x4000)) << 27) | (((long)m) << 42);
move(m, c);
}
private void undoMove(int m, int c) {
long u = mstack[COUNT() - 1];
move(m, c);
count = (int)(u & 0x1FFFF);
flags = (int)((u >> 17) & 0x3FF);
mat = (int)(((u >> 27) & 0x7FFF) - 0x4000);
}
private void registerCaps(int m, long bc, int[] mlist, int[] mn) {
while (bc != 0) {
int t = getLsb(bc);
bc ^= BIT[t];
mlist[mn[0]++] = m | _TO(t) | _CAP(identPiece(t));
}
}
private void registerMoves(int m, long bc, long bm, int[] mlist, int[] mn) {
while (bc != 0) {
int t = getLsb(bc);
bc ^= BIT[t];
mlist[mn[0]++] = m | _TO(t) | _CAP(identPiece(t));
}
while (bm != 0) {
int t = getLsb(bm);
bm ^= BIT[t];
mlist[mn[0]++] = m | _TO(t);
}
}
private void registerProms(int f, int c, long bc, long bm, int[] mlist, int[] mn) {
while (bc != 0) {
int t = getLsb(bc);
bc ^= BIT[t];
int m = f | _ONMV(c) | _PIECE(PAWN) | _TO(t) | _CAP(identPiece(t));
mlist[mn[0]++] = m | _PROM(QUEEN);
mlist[mn[0]++] = m | _PROM(KNIGHT);
mlist[mn[0]++] = m | _PROM(ROOK);
mlist[mn[0]++] = m | _PROM(BISHOP);
}
while (bm != 0) {
int t = getLsb(bm);
bm ^= BIT[t];
int m = f | _ONMV(c) | _PIECE(PAWN) | _TO(t);
mlist[mn[0]++] = m | _PROM(QUEEN);
mlist[mn[0]++] = m | _PROM(KNIGHT);
mlist[mn[0]++] = m | _PROM(ROOK);
mlist[mn[0]++] = m | _PROM(BISHOP);
}
}
private void registerKing(int m, long bc, long bm, int[] mlist, int[] mn, int c) {
while (bc != 0) {
int t = getLsb(bc);
bc ^= BIT[t];
if (battacked(t, c)) continue;
mlist[mn[0]++] = m | _TO(t) | _CAP(identPiece(t));
}
while (bm != 0) {
int t = getLsb(bm);
bm ^= BIT[t];
if (battacked(t, c)) continue;
mlist[mn[0]++] = m | _TO(t);
}
}
private int generateCheckEsc(long ch, long apin, int c, int k, int[] ml, int[] mn) {
long cc, fl;
int d, bf = _bitcnt(ch);
colorb[c] ^= BIT[k];
registerKing(PREMOVE(k, KING, c), KCAP(k, c), KMOVE(k), ml, mn, c);
colorb[c] ^= BIT[k];
if (bf > 1) return bf; //Doublecheck
bf = getLsb(ch);
cc = attacked(bf, c^1) & apin; //Can we capture the checker?
while (cc != 0) {
int cf = getLsb(cc);
cc ^= BIT[cf];
int p = identPiece(cf);
if (p == PAWN && RANK(cf, c != 0 ? 0x08 : 0x30)) {
registerProms(cf, c, ch, 0L, ml, mn);
} else {
registerMoves(PREMOVE(cf, p, c), ch, 0L, ml, mn);
}
}
if (ENPASS() != 0 && (ch & pieceb[PAWN]) != 0) { //Enpassant capture of attacking Pawn
cc = PCAP(ENPASS(), c^1) & pieceb[PAWN] & apin;
while (cc != 0) {
int cf = getLsb(cc);
cc ^= BIT[cf];
registerMoves(PREMOVE(cf, PAWN, c), BIT[ENPASS()], 0L, ml, mn);
}
}
if ((ch & (nmoves[k] | kmoves[k])) != 0) return 1; //We can't move anything between!
d = getDir(bf, k);
if ((d & 8) != 0) fl = RMOVE1(bf) & RMOVE1(k);
else if ((d & 16) != 0) fl = RMOVE2(bf) & RMOVE2(k);
else if ((d & 32) != 0) fl = BMOVE3(bf) & BMOVE3(k);
else fl = BMOVE4(bf) & BMOVE4(k);
while (fl != 0) {
int f = getLsb(fl);
fl ^= BIT[f];
cc = reach(f, c^1) & apin;
while (cc != 0) {
int cf = getLsb(cc);
cc ^= BIT[cf];
int p = identPiece(cf);
registerMoves(PREMOVE(cf, p, c), 0L, BIT[f], ml, mn);
}
bf = c != 0 ? f+8 : f-8;
if (bf < 0 || bf > 63) continue;
if ((BIT[bf] & pieceb[PAWN] & colorb[c] & apin) != 0) {
if (RANK(bf, c != 0 ? 0x08 : 0x30))
registerProms(bf, c, 0L, BIT[f], ml, mn);
else
registerMoves(PREMOVE(bf, PAWN, c), 0L, BIT[f], ml, mn);
}
if (RANK(f, c != 0 ? 0x20 : 0x18) && (BOARD() & BIT[bf]) == 0 && (BIT[c != 0 ? f+16 : f-16] & pieceb[PAWN] & colorb[c] & apin) != 0)
registerMoves(PREMOVE(c != 0 ? f+16 : f-16, PAWN, c), 0L, BIT[f], ml, mn);
}
return 1;
}
private int generateMoves(long ch, int c, int ply) {
int t, f = kingpos[c];
long m, b, a, cb = colorb[c];
long pin = pinnedPieces(f, c^1);
int[] ml = movelist[ply];
int[] mn = new int[]{0};
if (ch != 0) {
int ret = generateCheckEsc(ch, ~pin, c, f, ml, mn);
movenum[ply] = mn[0];
return ret;
}
registerKing(PREMOVE(f, KING, c), KCAP(f, c), KMOVE(f), ml, mn, c);
cb = colorb[c] & (~pin);
b = pieceb[PAWN] & cb;
while (b != 0) {
f = getLsb(b);
b ^= BIT[f];
m = PMOVE(f, c);
a = PCAP(f, c);
if (m != 0 && RANK(f, c != 0 ? 0x30 : 0x08)) m |= PMOVE(c != 0 ? f-8 : f+8, c);
if (RANK(f, c != 0 ? 0x08 : 0x30)) {
registerProms(f, c, a, m, ml, mn);
} else {
if (ENPASS() != 0 && (BIT[ENPASS()] & pcaps[(c)][(f)]) != 0) {
long hh;
int clbd = ENPASS()^8;
colorb[c] ^= BIT[clbd];
hh = ROCC1(f);
if ((hh & BIT[kingpos[c]]) == 0 || (hh & colorb[c^1] & RQU()) == 0) {
a = a | BIT[ENPASS()];
}
colorb[c] ^= BIT[clbd];
}
registerMoves(PREMOVE(f, PAWN, c), a, m, ml, mn);
}
}
b = pin & pieceb[PAWN];
while (b != 0) {
f = getLsb(b);
b ^= BIT[f];
t = getDir(f, kingpos[c]);
if ((t & 8) != 0) continue;
m = a = 0L;
if ((t & 16) != 0) {
m = PMOVE(f, c);
if (m != 0 && RANK(f, c != 0 ? 0x30 : 0x08)) m |= PMOVE(c != 0 ? f-8 : f+8, c);
} else if ((t & 32) != 0) {
a = PCA3(f, c);
} else {
a = PCA4(f, c);
}
if (RANK(f, c != 0 ? 0x08 : 0x30)) {
registerProms(f, c, a, m, ml, mn);
} else {
registerMoves(PREMOVE(f, PAWN, c), a, m, ml, mn);
}
}
b = pieceb[KNIGHT] & cb;
while (b != 0) {
f = getLsb(b);
b ^= BIT[f];
registerMoves(PREMOVE(f, KNIGHT, c), NCAP(f, c), NMOVE(f), ml, mn);
}
b = pieceb[ROOK] & cb;
while (b != 0) {
f = getLsb(b);
b ^= BIT[f];
registerMoves(PREMOVE(f, ROOK, c), RCAP(f, c), RMOVE(f), ml, mn);
if (CASTLE() != 0 && ch == 0) {
if (c != 0) {
if ((flags & 128) != 0 && (f == 63) && (RMOVE1(63) & BIT[61]) != 0)
if (!DUALATT(61, 62, c)) registerMoves(PREMOVE(60, KING, c), 0L, BIT[62], ml, mn);
if ((flags & 512) != 0 && (f == 56) && (RMOVE1(56) & BIT[59]) != 0)
if (!DUALATT(59, 58, c)) registerMoves(PREMOVE(60, KING, c), 0L, BIT[58], ml, mn);
} else {
if ((flags & 64) != 0 && (f == 7) && (RMOVE1(7) & BIT[5]) != 0)
if (!DUALATT(5, 6, c)) registerMoves(PREMOVE(4, KING, c), 0L, BIT[6], ml, mn);
if ((flags & 256) != 0 && (f == 0) && (RMOVE1(0) & BIT[3]) != 0)
if (!DUALATT(3, 2, c)) registerMoves(PREMOVE(4, KING, c), 0L, BIT[2], ml, mn);
}
}
}
b = pieceb[BISHOP] & cb;
while (b != 0) {
f = getLsb(b);
b ^= BIT[f];
registerMoves(PREMOVE(f, BISHOP, c), BCAP(f, c), BMOVE(f), ml, mn);
}
b = pieceb[QUEEN] & cb;
while (b != 0) {
f = getLsb(b);
b ^= BIT[f];
registerMoves(PREMOVE(f, QUEEN, c), RCAP(f, c) | BCAP(f,c), RMOVE(f) | BMOVE(f), ml, mn);
}
b = pin & (pieceb[ROOK] | pieceb[BISHOP] | pieceb[QUEEN]);
while (b != 0) {
int p;
f = getLsb(b);
b ^= BIT[f];
p = identPiece(f);
t = p | getDir(f, kingpos[c]);
if ((t & 10) == 10) registerMoves(PREMOVE(f, p, c), RCAP1(f, c), RMOVE1(f), ml, mn);
if ((t & 18) == 18) registerMoves(PREMOVE(f, p, c), RCAP2(f, c), RMOVE2(f), ml, mn);
if ((t & 33) == 33) registerMoves(PREMOVE(f, p, c), BCAP3(f, c), BMOVE3(f), ml, mn);
if ((t & 65) == 65) registerMoves(PREMOVE(f, p, c), BCAP4(f, c), BMOVE4(f), ml, mn);
}
movenum[ply] = mn[0];
return 0;
}
private int generateCaps(long ch, int c, int ply) {
int t, f = kingpos[c];
long m, b, a, cb = colorb[c];
long pin = pinnedPieces(f, c^1);
int[] ml = movelist[ply];
int[] mn = new int[]{0};
if (ch != 0) {
int ret = generateCheckEsc(ch, ~pin, c, f, ml, mn);
movenum[ply] = mn[0];
return ret;
}
registerKing(PREMOVE(f, KING, c), KCAP(f, c), 0L, ml, mn, c);
cb = colorb[c] & (~pin);
b = pieceb[PAWN] & cb;
while (b != 0) {
f = getLsb(b);
b ^= BIT[f];
a = PCAP(f, c);
if (RANK(f, c != 0 ? 0x08 : 0x30)) {
registerMoves(PREMOVE(f, PAWN, c) | _PROM(QUEEN), a, PMOVE(f, c), ml, mn);
} else {
if (ENPASS() != 0 && (BIT[ENPASS()] & pcaps[(c)][(f)]) != 0) {
long hh;
int clbd = ENPASS()^8;
colorb[c] ^= BIT[clbd];
hh = ROCC1(f);
if ((hh & BIT[kingpos[c]]) == 0 || (hh & colorb[c^1] & RQU()) == 0) {
a = a | BIT[ENPASS()];
}
colorb[c] ^= BIT[clbd];
}
registerCaps(PREMOVE(f, PAWN, c), a, ml, mn);
}
}
b = pin & pieceb[PAWN];
while (b != 0) {
f = getLsb(b);
b ^= BIT[f];
t = getDir(f, kingpos[c]);
if ((t & 8) != 0) continue;
m = a = 0L;
if ((t & 16) != 0) {
m = PMOVE(f, c);
} else if ((t & 32) != 0) {
a = PCA3(f, c);
} else {
a = PCA4(f, c);
}
if (RANK(f, c != 0 ? 0x08 : 0x30)) {