-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTools.cs
355 lines (329 loc) · 15.4 KB
/
Tools.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
using System;
namespace ChessBitboard{
public class Tools{
public static string move2Algebra(string move){ // convert coordinates to algebraic notation
string moveString="";
if(move[3] == 'P'){
moveString = moveString +""+ (char)(move[0]+49);
moveString = moveString +""+ (char)(move[1]+49);
moveString = moveString + ""+move[2];
moveString = moveString + ""+move[3];
return moveString;
}
moveString = moveString + ""+(char)(move[1]+49);
moveString = moveString + ""+('8'- (move[0]));
moveString = moveString + ""+(char)(move[3]+49);
moveString = moveString + ""+('8' - (move[2]));
return moveString;
}
public static string algebra2Move(string move){
string moveString="";
if((move.Length > 4 && move[4] == 'e') && (IsBetweenAandH(move[0]) && IsBetweenAandH(move[2]))){
moveString = "";
moveString = moveString + ""+(char)(move[0]-49);
moveString = moveString + ""+(char)(move[2]-49);
moveString = moveString + "W";
moveString = moveString + ""+char.ToUpper(move[4]);
return moveString;
}
if((move.Length > 5 && move[4] == 'p') && (IsBetweenAandH(move[0]) && IsBetweenAandH(move[2]))){
moveString = "";
moveString = moveString + ""+(char)(move[0]-49);
moveString = moveString + ""+(char)(move[2]-49);
moveString = moveString + ""+char.ToUpper(move[5]);
moveString = moveString + ""+char.ToUpper(move[4]);
return moveString;
}
if(IsBetweenAandH(move[0]) && IsBetweenAandH(move[2])){
moveString = moveString + ""+('8' - (move[1]));
moveString = moveString + ""+(char)(move[0]-49);
moveString = moveString + ""+('8' - (move[3]));
moveString = moveString + ""+(char)(move[2]-49);
return moveString;
}
return "7777";
}
public static bool IsBetweenAandH(char c)
{
if(c >= 'a' && c <= 'h'){
return true;
}else{
return false;
}
}
public static int trailingZerosRight(UInt64 move){
if (((move)&1)==1) return 0 ;
if (((move >>1 )&1)==1) return 1 ;
if (((move >>2 )&1)==1) return 2 ;
if (((move >>3 )&1)==1) return 3 ;
if (((move >>4 )&1)==1) return 4 ;
if (((move >>5 )&1)==1) return 5 ;
if (((move >>6 )&1)==1) return 6 ;
if (((move >>7 )&1)==1) return 7 ;
if (((move >>8 )&1)==1) return 8 ;
if (((move >>9 )&1)==1) return 9 ;
if (((move >>10)&1)==1) return 10;
if (((move >>11)&1)==1) return 11;
if (((move >>12)&1)==1) return 12;
if (((move >>13)&1)==1) return 13;
if (((move >>14)&1)==1) return 14;
if (((move >>15)&1)==1) return 15;
if (((move >>16)&1)==1) return 16;
if (((move >>17)&1)==1) return 17;
if (((move >>18)&1)==1) return 18;
if (((move >>19)&1)==1) return 19;
if (((move >>20)&1)==1) return 20;
if (((move >>21)&1)==1) return 21;
if (((move >>22)&1)==1) return 22;
if (((move >>23)&1)==1) return 23;
if (((move >>24)&1)==1) return 24;
if (((move >>25)&1)==1) return 25;
if (((move >>26)&1)==1) return 26;
if (((move >>27)&1)==1) return 27;
if (((move >>28)&1)==1) return 28;
if (((move >>29)&1)==1) return 29;
if (((move >>30)&1)==1) return 30;
if (((move >>31)&1)==1) return 31;
if (((move >>32)&1)==1) return 32;
if (((move >>33)&1)==1) return 33;
if (((move >>34)&1)==1) return 34;
if (((move >>35)&1)==1) return 35;
if (((move >>36)&1)==1) return 36;
if (((move >>37)&1)==1) return 37;
if (((move >>38)&1)==1) return 38;
if (((move >>39)&1)==1) return 39;
if (((move >>40)&1)==1) return 40;
if (((move >>41)&1)==1) return 41;
if (((move >>42)&1)==1) return 42;
if (((move >>43)&1)==1) return 43;
if (((move >>44)&1)==1) return 44;
if (((move >>45)&1)==1) return 45;
if (((move >>46)&1)==1) return 46;
if (((move >>47)&1)==1) return 47;
if (((move >>48)&1)==1) return 48;
if (((move >>49)&1)==1) return 49;
if (((move >>50)&1)==1) return 50;
if (((move >>51)&1)==1) return 51;
if (((move >>52)&1)==1) return 52;
if (((move >>53)&1)==1) return 53;
if (((move >>54)&1)==1) return 54;
if (((move >>55)&1)==1) return 55;
if (((move >>56)&1)==1) return 56;
if (((move >>57)&1)==1) return 57;
if (((move >>58)&1)==1) return 58;
if (((move >>59)&1)==1) return 59;
if (((move >>60)&1)==1) return 60;
if (((move >>61)&1)==1) return 61;
if (((move >>62)&1)==1) return 62;
if (((move >>63)&1)==1) return 63;
return 64;
}
// 6.3 seconds faster on 5 ply deep search than doing with for loop
public static UInt64 reverseBit(UInt64 bitboard){
UInt64 reverse = 0;
if (((bitboard)&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>1 )&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>2 )&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>3 )&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>4 )&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>5 )&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>6 )&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>7 )&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>8 )&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>9 )&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>10)&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>11)&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>12)&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>13)&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>14)&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>15)&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>16)&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>17)&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>18)&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>19)&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>20)&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>21)&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>22)&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>23)&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>24)&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>25)&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>26)&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>27)&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>28)&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>29)&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>30)&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>31)&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>32)&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>33)&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>34)&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>35)&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>36)&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>37)&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>38)&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>39)&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>40)&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>41)&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>42)&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>43)&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>44)&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>45)&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>46)&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>47)&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>48)&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>49)&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>50)&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>51)&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>52)&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>53)&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>54)&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>55)&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>56)&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>57)&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>58)&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>59)&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>60)&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>61)&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>62)&1)==1) { reverse = reverse ^ 1; }
reverse = reverse << 1;
if (((bitboard >>63)&1)==1) { reverse = reverse ^ 1; }
return reverse;
}
public static char unicode2ShortHand(char c){
switch(c){
case BoardGeneration.bQC:
return 'q';
case BoardGeneration.wQC:
return 'Q';
case BoardGeneration.bBC:
return 'b';
case BoardGeneration.bRC:
return 'r';
case BoardGeneration.bNC:
return 'n';
case BoardGeneration.wRC:
return 'R';
case BoardGeneration.wBC:
return 'B';
case BoardGeneration.wNC:
return 'N';
default:
return '0';
}
}
public static bool ArrC(char[] arr1, char[] arr2){
if( arr1[0] != arr2[0] ) { return false;}
if( arr1[1] != arr2[1] ) { return false;}
if( arr1[2] != arr2[2] ) { return false;}
if( arr1[3] != arr2[3] ) { return false;}
return true;
}
public static bool IsCharDigit(char c){
return ((c >= '0') && (c <= '9'));
}
public static int Abs(int i){
return ((i + (i >> 31)) ^ (i >> 31));
}
public static char[] CharCombine(char c0, char c1, char c2, char c3){
char[] c = new char[4];
c[0] = c0;
c[1] = c1;
c[2] = c2;
c[3] = c3;
return c;
}
public static UInt64 reverseBitSingle(UInt64 bitboard){
// almost twice as fast as other method but only for single bit
// almost 3 seconds faster at perft search depth of 5 almost 10% faster
UInt64 reverse = 1;
int shift = Tools.trailingZerosRight(bitboard);
reverse = reverse << (63-shift);
return reverse;
}
public static UInt64 reverseBitxx(UInt64 bitboard){
// is doing XOR on every last 0 faster checking a flag and skip the XOR and just shift the rest?
UInt64 reverse = 0;
for(int i = 0; i < 64; i++){
reverse = reverse << 1;
if(((bitboard & 1) == 1)){
reverse = reverse ^ 1;
}
bitboard = bitboard >> 1;
}
return reverse;
}
public static int trailingZerosLeft(UInt64 move){
for(int i = 0; i<64; i++){
if (((move <<i)&0x8000000000000000)==0x8000000000000000) return i;
}
return 0;
}
public static int trailingZerosRightLoop(UInt64 move){
for(int i = 0; i<64; i++){
if (((move >>i)&1)==1) return i;
}
return 64;
}
}
}