1 /**************************************************************************/
\r
3 /* A WinBoard engine for Chu Shogi (and some related games) by H.G.Muller */
\r
4 /**************************************************************************/
\r
5 /* This source code is released in the public domain */
\r
6 /**************************************************************************/
\r
9 // in GenCapts we do not generate jumps of more than two squares yet
\r
10 // promotions by pieces with Lion power stepping in & out the zone in same turn
\r
11 // promotion on capture
\r
13 #define VERSION "0.21"
\r
15 //define PATH level==0 || path[0] == 0x590cb && (level==1 || path[1] == 0x4c0c9 && (level == 2 || path[2] == 0x8598ca && (level == 3 /*|| path[3] == 0x3e865 && (level == 4 || path[4] == 0x4b865 && (level == 5))*/)))
\r
29 #define TANDEM 100 /* bonus for pairs of attacking light steppers */
\r
30 #define KYLIN 100 /* extra end-game value of Kylin for promotability */
\r
31 #define PROMO 0 /* extra bonus for 'vertical' piece when it actually promotes (diagonal pieces get half) */
\r
41 # include <windows.h>
\r
43 { // checks for waiting input in pipe
\r
44 static int pipe, init;
\r
48 if(!init) inp = GetStdHandle(STD_INPUT_HANDLE);
\r
49 if(!PeekNamedPipe(inp, NULL, 0, NULL, &cnt, NULL)) return 1;
\r
53 # include <sys/time.h>
\r
54 # include <sys/ioctl.h>
\r
58 if(ioctl(0, FIONREAD, &cnt)) return 1;
\r
61 int GetTickCount() // with thanks to Tord
\r
63 gettimeofday(&t, NULL);
\r
64 return t.tv_sec*1000 + t.tv_usec/1000;
\r
71 #define BWMAX (2*BHMAX)
\r
72 #define BSIZE BWMAX*BHMAX
\r
75 #define ONE 1 /* currently no variants with 10-deep board */
\r
80 #define EDGE (1<<11)
\r
81 #define TYPE (WHITE|BLACK|EDGE)
\r
84 #define NPIECES EDGE+1 /* length of piece list */
\r
86 #define SQLEN 11 /* bits in square number */
\r
87 #define SQUARE ((1<<SQLEN) - 1) /* mask for square in move */
\r
88 #define DEFER (1<<2*SQLEN) /* deferral on zone entry */
\r
89 #define PROMOTE (1<<2*SQLEN+1) /* promotion bit in move */
\r
90 #define SPECIAL 1400 /* start of special moves */
\r
91 #define BURN (SPECIAL + 96) /* start of burn encodings */
\r
92 #define CASTLE (SPECIAL + 100) /* castling encodings */
\r
93 #define STEP(X,Y) (BW*(X)+ (Y))
\r
94 #define SORTKEY(X) 0
\r
96 #define UPDATE_MOBILITY(X,Y)
\r
97 #define ADDMOVE(X,Y)
\r
100 #define CAN_PROMOTE 0x11
\r
101 #define DONT_DEFER 0x22
\r
102 #define CANT_DEFER 0x44
\r
103 #define LAST_RANK 0x88
\r
104 #define P_WHITE 0x0F
\r
105 #define P_BLACK 0xF0
\r
107 // Piece-Square Tables
\r
108 #define PST_NEUTRAL 0
\r
109 #define PST_STEPPER BH
\r
110 #define PST_WJUMPER (BW*BH)
\r
111 #define PST_SLIDER (BW*BH+BH)
\r
112 #define PST_TRAP (2*BW*BH)
\r
113 #define PST_CENTER (2*BW*BH+BH)
\r
114 #define PST_WPPROM (3*BW*BH)
\r
115 #define PST_BPPROM (3*BW*BH+BH)
\r
116 #define PST_BJUMPER (4*BW*BH)
\r
117 #define PST_ZONDIST (4*BW*BH+BH)
\r
118 #define PST_ADVANCE (5*BW*BH)
\r
119 #define PST_RETRACT (5*BW*BH+BH)
\r
120 #define PST_WFLYER (6*BW*BH)
\r
121 #define PST_BFLYER (6*BW*BH+BH)
\r
122 #define PST_LANCE (7*BW*BH)
\r
123 #define PST_END (8*BW*BH)
\r
125 typedef unsigned int Move;
\r
127 char *MoveToText(Move move, int m); // from WB driver
\r
128 void pmap(int *m, int col);
\r
129 void pboard(int *b);
\r
130 void pbytes(unsigned char *b);
\r
136 short int score[5];
\r
142 HashBucket *hashTable;
\r
149 char *name, *promoted;
\r
151 signed char range[8];
\r
154 int whiteKey, blackKey;
\r
158 int from, to, piece, victim, new, booty, epSquare, epVictim[9], ep2Square, revMoveCount;
\r
159 int savKeyL, savKeyH, gain, loss, filling, saveDelta;
\r
163 char *array, fenArray[4000], startPos[4000], *reason, checkStack[300];
\r
164 int bWidth, bHeight, bsize, zone, currentVariant, chuFlag, tenFlag, chessFlag, repDraws, stalemate;
\r
165 int tsume, pvCuts, allowRep, entryProm, okazaki, pVal;
\r
166 int stm, xstm, hashKeyH=1, hashKeyL=1, framePtr, msp, nonCapts, rootEval, filling, promoDelta;
\r
167 int retMSP, retFirst, retDep, pvPtr, level, cnt50, mobilityScore;
\r
168 int ll, lr, ul, ur; // corner squares
\r
169 int nodes, startTime, lastRootMove, lastRootIter, tlim1, tlim2, tlim3, repCnt, comp, abortFlag;
\r
171 Move retMove, moveStack[20000], path[100], repStack[300], pv[1000], repeatMove[300], killer[100][2];
\r
173 int maxDepth; // used by search
\r
175 #define X 36 /* slider */
\r
176 #define R 37 /* jump capture */
\r
177 #define N -1 /* Knight */
\r
178 #define J -2 /* jump */
\r
179 #define I -3 /* jump + step */
\r
180 #define D -4 /* linear double move */
\r
181 #define T -5 /* linear triple move */
\r
182 #define L -6 /* true Lion move */
\r
183 #define F -7 /* Lion + 3-step */
\r
184 #define S -8 /* Lion + range */
\r
185 #define H -9 /* hook move */
\r
186 #define C -10 /* capture only */
\r
187 #define M -11 /* non-capture only */
\r
189 #define LVAL 1000 /* piece value of Lion. Used in chu for recognizing it to implement Lion-trade rules */
\r
190 #define FVAL 5000 /* piece value of Fire Demon. Used in code for recognizing moves with it and do burns */
\r
192 PieceDesc chuPieces[] = {
\r
193 {"LN", "", LVAL, { L,L,L,L,L,L,L,L }, 4 }, // lion
\r
194 {"FK", "", 600, { X,X,X,X,X,X,X,X }, 4 }, // free king
\r
195 {"SE", "", 550, { X,D,X,X,X,X,X,D }, 4 }, // soaring eagle
\r
196 {"HF", "", 500, { D,X,X,X,X,X,X,X }, 4 }, // horned falcon
\r
197 {"FO", "", 400, { X,X,0,X,X,X,0,X }, 4 }, // flying ox
\r
198 {"FB", "", 400, { 0,X,X,X,0,X,X,X }, 4 }, // free boar
\r
199 {"DK", "SE", 400, { X,1,X,1,X,1,X,1 }, 4 }, // dragon king
\r
200 {"DH", "HF", 350, { 1,X,1,X,1,X,1,X }, 4 }, // dragon horse
\r
201 {"WH", "", 350, { X,X,0,0,X,0,0,X }, 3 }, // white horse
\r
202 {"R", "DK", 300, { X,0,X,0,X,0,X,0 }, 4 }, // rook
\r
203 {"FS", "", 300, { X,1,1,1,X,1,1,1 }, 3 }, // flying stag
\r
204 {"WL", "", 250, { X,0,0,X,X,X,0,0 }, 4 }, // whale
\r
205 {"K", "", 280, { 1,1,1,1,1,1,1,1 }, 2, 4 }, // king
\r
206 {"CP", "", 270, { 1,1,1,1,1,1,1,1 }, 2, 4 }, // king
\r
207 {"B", "DH", 250, { 0,X,0,X,0,X,0,X }, 2 }, // bishop
\r
208 {"VM", "FO", 200, { X,0,1,0,X,0,1,0 }, 2 }, // vertical mover
\r
209 {"SM", "FB", 200, { 1,0,X,0,1,0,X,0 }, 6 }, // side mover
\r
210 {"DE", "CP", 201, { 1,1,1,1,0,1,1,1 }, 2 }, // drunk elephant
\r
211 {"BT", "FS", 152, { 0,1,1,1,1,1,1,1 }, 2 }, // blind tiger
\r
212 {"G", "R", 151, { 1,1,1,0,1,0,1,1 }, 2 }, // gold
\r
213 {"FL", "B", 150, { 1,1,0,1,1,1,0,1 }, 2 }, // ferocious leopard
\r
214 {"KN", "LN", 154, { J,1,J,1,J,1,J,1 }, 2 }, // kirin
\r
215 {"PH", "FK", 153, { 1,J,1,J,1,J,1,J }, 2 }, // phoenix
\r
216 {"RV", "WL", 150, { X,0,0,0,X,0,0,0 }, 1 }, // reverse chariot
\r
217 {"L", "WH", 150, { X,0,0,0,0,0,0,0 }, 1 }, // lance
\r
218 {"S", "VM", 100, { 1,1,0,1,0,1,0,1 }, 2 }, // silver
\r
219 {"C", "SM", 100, { 1,1,0,0,1,0,0,1 }, 2 }, // copper
\r
220 {"GB", "DE", 50, { 1,0,0,0,1,0,0,0 }, 1 }, // go between
\r
221 {"P", "G", 40, { 1,0,0,0,0,0,0,0 }, 2 }, // pawn
\r
222 { NULL } // sentinel
\r
225 PieceDesc shoPieces[] = {
\r
226 {"DK", "", 700, { X,1,X,1,X,1,X,1 } }, // dragon king
\r
227 {"DH", "", 520, { 1,X,1,X,1,X,1,X } }, // dragon horse
\r
228 {"R", "DK", 500, { X,0,X,0,X,0,X,0 } }, // rook
\r
229 {"B", "DH", 320, { 0,X,0,X,0,X,0,X } }, // bishop
\r
230 {"K", "", 410, { 1,1,1,1,1,1,1,1 } }, // king
\r
231 {"CP", "", 400, { 1,1,1,1,1,1,1,1 } }, // king
\r
232 {"DE", "CP", 250, { 1,1,1,1,0,1,1,1 } }, // silver
\r
233 {"G", "", 220, { 1,1,1,0,1,0,1,1 } }, // gold
\r
234 {"S", "G", 200, { 1,1,0,1,0,1,0,1 } }, // silver
\r
235 {"L", "G", 150, { X,0,0,0,0,0,0,0 } }, // lance
\r
236 {"N", "G", 110, { N,0,0,0,0,0,0,N } }, // Knight
\r
237 {"P", "G", 80, { 1,0,0,0,0,0,0,0 } }, // pawn
\r
238 { NULL } // sentinel
\r
241 PieceDesc daiPieces[] = {
\r
242 {"FD", "G", 150, { 0,2,0,2,0,2,0,2 }, 2 }, // Flying Dragon
\r
243 {"VO", "G", 200, { 2,0,2,0,2,0,2,0 }, 2 }, // Violent Ox
\r
244 {"EW", "G", 80, { 1,1,1,0,0,0,1,1 }, 2 }, // Evil Wolf
\r
245 {"CS", "G", 70, { 0,1,0,1,0,1,0,1 }, 1 }, // Cat Sword
\r
246 {"AB", "G", 60, { 1,0,1,0,1,0,1,0 }, 1 }, // Angry Boar
\r
247 {"I", "G", 80, { 1,1,0,0,0,0,0,1 }, 2 }, // Iron
\r
248 {"N", "G", 60, { N,0,0,0,0,0,0,N }, 0 }, // Knight
\r
249 {"ST", "G", 50, { 0,1,0,0,0,0,0,1 }, 0 }, // Stone
\r
250 { NULL } // sentinel
\r
253 PieceDesc ddPieces[] = {
\r
254 {"LO", "", 10, { 1,H,1,H,1,H,1,H } }, // Long-Nosed Goblin
\r
255 {"OK", "LO", 10, { 2,1,2,0,2,0,2,1 } }, // Old Kite
\r
256 {"PS", "HM", 10, { J,0,1,J,0,J,1,0 } }, // Poisonous Snake
\r
257 {"GE", "", 10, { 3,3,5,5,3,5,5,3 } }, // Great Elephant
\r
258 {"WS", "LD", 10, { 1,1,2,0,1,0,2,1 } }, // Western Barbarian
\r
259 {"EA", "LN", 10, { 2,1,1,0,2,0,1,1 } }, // Eastern Barbarian
\r
260 {"NO", "FE", 10, { 0,2,1,1,0,1,1,2 } }, // Northern Barbarian
\r
261 {"SO", "WE", 10, { 0,1,1,2,0,2,1,1 } }, // Southern Barbarian
\r
262 {"FE", "", 10, { 2,X,2,2,2,2,2,X } }, // Fragrant Elephant
\r
263 {"WE", "", 10, { 2,2,2,X,2,X,2,2 } }, // White Elephant
\r
264 {"FT", "", 10, { X,X,5,0,X,0,5,X } }, // Free Dream-Eater
\r
265 {"FR", "", 10, { 5,X,X,0,5,0,X,X } }, // Free Demon
\r
266 {"WB", "FT", 10, { 2,X,X,X,2,X,X,X } }, // Water Buffalo
\r
267 {"RB", "FR", 10, { X,X,X,X,0,X,X,X } }, // Rushing Bird
\r
268 {"SB", "", 10, { X,X,2,2,2,2,2,X } }, // Standard Bearer
\r
270 {"FH", "FK", 10, { 1,2,1,0,1,0,1,2 } }, // Flying Horse
\r
271 {"NK", "SB", 10, { 1,1,1,1,1,1,1,1 } }, // Neighbor King
\r
272 {"BM", "MW", 10, { 0,1,1,1,0,1,1,1 } }, // Blind Monkey
\r
273 {"DO", "", 10, { 2,X,2,X,2,X,2,X } }, // Dove
\r
274 {"EB", "DO", 10, { 2,0,2,0,0,0,2,0 } }, // Enchanted Badger
\r
275 {"EF", "SD", 10, { 0,2,0,0,2,0,0,2 } }, // Enchanted Fox
\r
276 {"RA", "", 10, { X,0,X,1,X,1,X,0 } }, // Racing Chariot
\r
277 {"SQ", "", 10, { X,1,X,0,X,0,X,1 } }, // Square Mover
\r
278 {"PR", "SQ", 10, { 1,1,2,1,0,1,2,1 } }, // Prancing Stag
\r
279 {"WT", "", 10, { X,1,2,0,X,0,2,X } }, // White Tiger
\r
280 {"BD", "", 10, { 2,X,X,0,2,0,X,1 } }, // Blue Dragon
\r
281 {"HD", "", 10, { X,0,0,0,1,0,0,0 } }, // Howling Dog
\r
282 {"VB", "", 10, { 0,2,1,0,0,0,1,2 } }, // Violent Bear
\r
283 {"SA", "", 10, { 2,1,0,0,2,0,0,1 } }, // Savage Tiger
\r
284 {"W", "", 10, { 0,2,0,0,0,0,0,2 } }, // Wood
\r
285 {"CS", "DH", 70, { 0,1,0,1,0,1,0,1 } }, // cat sword
\r
286 {"FD", "DK", 150, { 0,2,0,2,0,2,0,2 } }, // flying dragon
\r
287 {"KN", "GD", 150, { J,1,J,1,J,1,J,1 } }, // kirin
\r
288 {"PH", "GB", 150, { 1,J,1,J,1,J,1,J } }, // phoenix
\r
289 {"LN", "FF", 1000, { L,L,L,L,L,L,L,L } }, // lion
\r
290 {"LD", "GE", 10, { T,T,T,T,T,T,T,T } }, // Lion Dog
\r
291 {"AB", "", 10, { 1,0,1,0,1,0,1,0 } }, // Angry Boar
\r
292 {"B", "", 10, { 0,X,0,X,0,X,0,X } }, // Bishop
\r
293 {"C", "", 10, { 1,1,0,0,1,0,0,1 } }, // Copper
\r
294 {"DH", "", 10, { 1,X,1,X,1,X,1,X } }, // Dragon Horse
\r
295 {"DK", "", 10, { X,1,X,1,X,1,X,1 } }, // Dragon King
\r
296 {"FK", "", 10, { } }, //
\r
297 {"EW", "", 10, { 1,1,1,0,0,0,1,1 } }, // Evil Wolf
\r
298 {"FL", "", 10, { } }, //
\r
299 {"", "", 10, { } }, //
\r
300 {"", "", 10, { } }, //
\r
301 {"", "", 10, { } }, //
\r
302 {"", "", 10, { } }, //
\r
303 { NULL } // sentinel
\r
306 PieceDesc makaPieces[] = {
\r
307 {"DV", "", 10, { 0,1,0,1,0,0,1,1 } }, // Deva
\r
308 {"DS", "", 10, { 0,1,1,0,0,1,0,1 } }, // Dark Spirit
\r
309 {"T", "", 10, { 0,1,0,0,1,0,0,1 } }, // Tile
\r
310 {"CS", "", 10, { 1,0,0,1,1,1,0,0 } }, // Coiled Serpent
\r
311 {"RD", "", 10, { 1,0,1,1,1,1,1,0 } }, // Reclining Dragon
\r
312 {"CC", "", 10, { 0,1,1,0,1,0,1,1 } }, // Chinese Cock
\r
313 {"OM", "", 10, { 0,1,0,1,1,1,0,1 } }, // Old Monkey
\r
314 {"BB", "", 10, { 0,1,0,1,X,1,0,1 } }, // Blind Bear
\r
315 {"OR", "", 10, { 0,2,0,0,2,0,0,2 } }, // Old Rat
\r
316 {"LD", "WS", 10, { T,T,T,T,T,T,T,T } }, // Lion Dog
\r
317 {"WR", "", 10, { 0,3,1,3,0,3,1,3 } }, // Wrestler
\r
318 {"GG", "", 10, { 3,1,3,0,3,0,3,1 } }, // Guardian of the Gods
\r
319 {"BD", "", 10, { 0,3,1,0,1,0,1,3 } }, // Budhist Devil
\r
320 {"SD", "", 10, { 5,2,5,2,5,2,5,2 } }, // She-Devil
\r
321 {"DY", "", 10, { J,0,1,0,J,0,1,0 } }, // Donkey
\r
322 {"CP", "", 10, { 0,H,0,2,0,2,0,H } }, // Capricorn
\r
323 {"HM", "", 10, { H,0,H,0,H,0,H,0 } }, // Hook Mover
\r
324 {"SF", "", 10, { 0,1,X,1,0,1,0,1 } }, // Side Flier
\r
325 {"LC", "", 10, { X,0,0,X,1,0,0,X } }, // Left Chariot
\r
326 {"RC", "", 10, { X,X,0,0,1,X,0,0 } }, // Right Chariot
\r
327 {"FG", "", 10, { X,X,X,0,X,0,X,X } }, // Free Gold
\r
328 {"FS", "", 10, { X,X,0,X,0,X,0,X } }, // Free Silver
\r
329 {"FC", "", 10, { X,X,0,0,X,0,0,X } }, // Free Copper
\r
330 {"FI", "", 10, { X,X,0,0,0,0,0,X } }, // Free Iron
\r
331 {"FT", "", 10, { 0,X,0,0,X,0,0,X } }, // Free Tile
\r
332 {"FN", "", 10, { 0,X,0,0,0,0,0,X } }, // Free Stone
\r
333 {"FTg", "", 10, { 0,X,X,X,X,X,X,X } }, // Free Tiger
\r
334 {"FLp", "", 10, { X,X,0,X,X,X,0,X } }, // Free Leopard (Free Boar?)
\r
335 {"FSp", "", 10, { X,0,0,X,X,X,0,0 } }, // Free Serpent (Whale?)
\r
336 {"FrD", "", 10, { X,0,X,X,X,X,X,0 } }, // Free Dragon
\r
337 {"FC", "", 10, { 0,X,0,X,0,X,0,X } }, // Free Cat (Bishop?)
\r
338 {"EM", "", 10, { } }, // Emperor
\r
339 {"TK", "", 10, { } }, // Teaching King
\r
340 {"BS", "", 10, { } }, // Budhist Spirit
\r
341 {"WS", "", 10, { X,X,0,X,1,X,0,X } }, // Wizard Stork
\r
342 {"MW", "", 10, { 1,X,0,X,X,X,0,X } }, // Mountain Witch
\r
343 {"FF", "", 10, { } }, // Furious Fiend
\r
344 {"GD", "", 10, { 2,3,X,3,2,3,X,3 } }, // Great Dragon
\r
345 {"GB", "", 10, { X,3,2,3,X,3,2,3 } }, // Golden Bird
\r
346 {"FrW", "", 10, { } }, // Free Wolf
\r
347 {"FrB", "", 10, { } }, // Free Bear
\r
348 {"BT", "", 10, { X,0,0,X,0,X,0,0 } }, // Bat
\r
349 {"", "", 10, { } }, //
\r
350 { NULL } // sentinel
\r
353 PieceDesc taiPieces[] = {
\r
354 {"", "", 10, { } }, // Peacock
\r
355 {"", "", 10, { } }, // Vermillion Sparrow
\r
356 {"", "", 10, { } }, // Turtle Snake
\r
357 {"", "", 10, { } }, // Silver Hare
\r
358 {"", "", 10, { } }, // Golden Deer
\r
359 {"", "", 10, { } }, //
\r
360 {"", "", 10, { } }, //
\r
361 {"", "", 10, { } }, //
\r
362 { NULL } // sentinel
\r
365 PieceDesc tenjikuPieces[] = { // only those not in Chu, or different (because of different promotion)
\r
366 {"FI", "", FVAL, { X,X,0,X,X,X,0,X } }, // Fire Demon
\r
367 {"GG", "", 1500, { R,R,R,R,R,R,R,R }, 0, 3 }, // Great General
\r
368 {"VG", "", 1400, { 0,R,0,R,0,R,0,R }, 0, 2 }, // Vice General
\r
369 {"RG", "GG",1200, { R,0,R,0,R,0,R,0 }, 0, 1 }, // Rook General
\r
370 {"BG", "VG",1100, { 0,R,0,R,0,R,0,R }, 0, 1 }, // Bishop General
\r
371 {"SE", "RG", 10, { X,D,X,X,X,X,X,D } }, // Soaring Eagle
\r
372 {"HF", "BG", 10, { D,X,X,X,X,X,X,X } }, // Horned Falcon
\r
373 {"LH", "", 10, { L,S,L,S,L,S,L,S } }, // Lion-Hawk
\r
374 {"LN", "LH",LVAL, { L,L,L,L,L,L,L,L } }, // Lion
\r
375 {"FE", "", 1, { X,X,X,X,X,X,X,X } }, // Free Eagle
\r
376 {"FK", "FE", 600, { X,X,X,X,X,X,X,X } }, // Free King
\r
377 {"HT", "", 10, { X,X,2,X,X,X,2,X } }, // Heavenly Tetrarchs
\r
378 {"CS", "HT", 10, { X,X,2,X,X,X,2,X } }, // Chariot Soldier
\r
379 {"WB", "FI", 10, { 2,X,X,X,2,X,X,X } }, // Water Buffalo
\r
380 {"VS", "CS", 10, { X,0,2,0,1,0,2,0 } }, // Vertical Soldier
\r
381 {"SS", "WB", 10, { 2,0,X,0,1,0,X,0 } }, // Side Soldier
\r
382 {"I", "VS", 10, { 1,1,0,0,0,0,0,1 } }, // Iron
\r
383 {"N", "SS", 10, { N,0,0,0,0,0,0,N } }, // Knight
\r
384 {"MG", "", 10, { X,0,0,X,0,X,0,0 } }, // Multi-General
\r
385 {"D", "MG", 10, { 1,0,0,1,0,1,0,0 } }, // Dog
\r
386 { NULL } // sentinel
\r
389 PieceDesc taikyokuPieces[] = {
\r
390 {"", "", 10, { } }, //
\r
391 { NULL } // sentinel
\r
394 PieceDesc chessPieces[] = {
\r
395 {"FK", "", 950, { X,X,X,X,X,X,X,X } },
\r
396 {"R", "", 500, { X,0,X,0,X,0,X,0 } },
\r
397 {"B", "", 320, { 0,X,0,X,0,X,0,X } },
\r
398 {"N", "", 300, { N,N,N,N,N,N,N,N } },
\r
399 {"K", "", 280, { 1,1,1,1,1,1,1,1 } },
\r
400 {"P", "FK", 80, { M,C,0,0,0,0,0,C } },
\r
401 { NULL } // sentinel
\r
404 PieceDesc lionPieces[] = {
\r
405 {"LN","", LVAL, { L,L,L,L,L,L,L,L } },
\r
406 {"FK", "", 600, { X,X,X,X,X,X,X,X } },
\r
407 {"R", "", 300, { X,0,X,0,X,0,X,0 } },
\r
408 {"K", "", 280, { 1,1,1,1,1,1,1,1 } },
\r
409 {"B", "", 190, { 0,X,0,X,0,X,0,X } },
\r
410 {"N", "", 180, { N,N,N,N,N,N,N,N } },
\r
411 {"P", "FK", 50, { M,C,0,0,0,0,0,C } },
\r
412 { NULL } // sentinel
\r
415 PieceDesc shatranjPieces[] = {
\r
416 {"FK", "", 150, { 0,1,0,1,0,1,0,1 } },
\r
417 {"R", "", 500, { X,0,X,0,X,0,X,0 } },
\r
418 {"B", "", 90, { 0,J,0,J,0,J,0,J } },
\r
419 {"N", "", 300, { N,N,N,N,N,N,N,N } },
\r
420 {"K", "", 280, { 1,1,1,1,1,1,1,1 } },
\r
421 {"P", "FK", 80, { M,C,0,0,0,0,0,C } },
\r
422 { NULL } // sentinel
\r
425 PieceDesc makrukPieces[] = {
\r
426 {"SM","", 150, { 0,1,0,1,0,1,0,1 } },
\r
427 {"R", "", 500, { X,0,X,0,X,0,X,0 } },
\r
428 {"S", "", 200, { 1,1,0,1,0,1,0,1 } }, // silver
\r
429 {"N", "", 300, { N,N,N,N,N,N,N,N } },
\r
430 {"K", "", 280, { 1,1,1,1,1,1,1,1 } },
\r
431 {"P", "SM", 80, { M,C,0,0,0,0,0,C } },
\r
432 { NULL } // sentinel
\r
435 char chuArray[] = "L:FLCSGK:DEGSC:FLL/:RV.B.:BT:KN:PH:BT.B.:RV/:SM:VMR:DH:DK:LN:FK:DK:DHR:VM:SM/PPPPPPPPPPPP/...:GB....:GB..."
\r
436 "/............/............/"
\r
437 "...:gb....:gb.../pppppppppppp/:sm:vmr:dh:dk:fk:ln:dk:dhr:vm:sm/:rv.b.:bt:ph:kn:bt.b.:rv/l:flcsg:dekgsc:fll";
\r
438 char daiArray[] = "LN:STICSGKGSCI:STNL/:RV.:CS.:FL.:BT:DE:BT.:FL.:CS.:RV/.:VO.:AB.:EW:KN:LN:PH:EW.:AB.:VO./R:FD:SM:VMB:DH:DK:FK:DK:DHB:VM:SM:FDR"
\r
439 "/PPPPPPPPPPPPPPP/....:GB.....:GB..../.............../.............../.............../....:gb.....:gb..../ppppppppppppppp/"
\r
440 "r:fd:sm:vmb:dh:dk:fk:dk:dhb:vm:sm:fdr/.:vo.:ab.:ew:ph:ln:kn:ew.:ab.:vo./:rv.:cs.:fl.:bt:de:bt.:fl.:cs.:rv/ln:sticsgkgsci:stnl";
\r
441 char tenArray[] = "LN:FLICSGK:DEGSCI:FLNL/:RV.:CS:CS.:BT:KN:LN:FK:PH:BT.:CS:CS.:RV/:SS:VSB:DH:DK:WB:FI:LH:FE:FI:WB:DK:DHB:VS:SS/"
\r
442 ":SM:VMR:HF:SE:BG:RG:GG:VG:RG:BG:SE:HFR:VM:SM/PPPPPPPPPPPPPPPP/....D......D..../"
\r
443 "................/................/................/................/"
\r
444 "....d......d..../pppppppppppppppp/:sm:vmr:hf:se:bg:rg:vg:gg:rg:bg:se:hfr:vm:sm/"
\r
445 ":ss:vsb:dh:dk:wb:fi:fe:lh:fi:wb:dk:dhb:vs:ss/:rv.:cs:cs.:bt:ph:fk:ln:kn:bt.:cs:cs.:rv/ln:flicsg:dekgsci:flnl";
\r
446 char shoArray[] = "LNSGKGSNL/.B..:DE..R./PPPPPPPPP/........./........./........./ppppppppp/.r..:de..b./lnsgkgsnl";
\r
447 char chessArray[] = "RNB:FKKBNR/PPPPPPPP/......../......../......../......../pppppppp/rnb:fkkbnr";
\r
448 char lionArray[] = "R:LNB:FKKBNR/PPPPPPPP/......../......../......../......../pppppppp/r:lnb:fkkbnr";
\r
449 char shatArray[]= "RNBK:FKBNR/PPPPPPPP/......../......../......../......../pppppppp/rnbk:fkbnr";
\r
450 char thaiArray[]= "RNSK:SMSNR/......../PPPPPPPP/......../......../pppppppp/......../rns:smksnr";
\r
453 int boardWidth, boardFiles, boardRanks, zoneDepth, varNr; // board sizes
\r
454 char *name; // WinBoard name
\r
455 char *array; // initial position
\r
458 typedef enum { V_CHESS, V_SHO, V_CHU, V_DAI, V_DADA, V_MAKA, V_TAI, V_KYOKU, V_TENJIKU, V_SHATRANJ, V_MAKRUK, V_LION } Variant;
\r
462 VariantDesc variants[] = {
\r
463 { 24, 12, 12, 4, V_CHU, "chu", chuArray }, // Chu
\r
464 { 16, 8, 8, 1, V_CHESS, "nocastle", chessArray }, // FIDE
\r
465 { 18, 9, 9, 3, V_SHO, "9x9+0_shogi", shoArray }, // Sho
\r
466 { 18, 9, 9, 3, V_SHO, "sho", shoArray }, // Sho duplicat
\r
467 { 30, 15, 15, 5, V_DAI, "dai", daiArray }, // Dai
\r
468 { 32, 16, 16, 5, V_TENJIKU, "tenjiku", tenArray }, // Tenjiku
\r
469 { 16, 8, 8, 1, V_SHATRANJ,"shatranj",shatArray}, // Shatranj
\r
470 { 16, 8, 8, 3, V_MAKRUK, "makruk", thaiArray}, // Makruk
\r
471 { 16, 8, 8, 1, V_LION, "lion", lionArray}, // Mighty Lion
\r
473 { 0, 0, 0, 0, 0 }, // sentinel
\r
474 { 34, 17, 17, 0, V_DADA, "dada", chuArray }, // Dai Dai
\r
475 { 38, 19, 19, 0, V_MAKA, "maka", chuArray }, // Maka Dai Dai
\r
476 { 50, 25, 25, 0, V_TAI, "tai", chuArray }, // Tai
\r
477 { 40, 36, 36, 0, V_KYOKU, "kyoku", chuArray } // Taikyoku
\r
484 Vector direction[] = { // clockwise!
\r
494 { 2, 1}, // Knight jumps
\r
504 int epList[104], ep2List[104], toList[104], reverse[104]; // decoding tables for double and triple moves
\r
505 int kingStep[10], knightStep[10]; // raw tables for step vectors (indexed as -1 .. 8)
\r
506 int neighbors[9]; // similar to kingStep, but starts with null-step
\r
507 char fireFlags[10]; // flags for Fire-Demon presence (last two are dummies, which stay 0, for compactify)
\r
508 #define kStep (kingStep+1)
\r
509 #define nStep (knightStep+1)
\r
511 int attackMask[8] = { // indicate which bits in attack-map item are used to count attacks from which direction
\r
522 int rayMask[8] = { // indicate which bits in attack-map item are used to count attacks from which direction
\r
533 int one[] = { // 1 in the bit fields for the various directions
\r
542 0100000000 // marks knight jumps
\r
545 // Main Data structures
\r
548 // Interleaved lists for black and white, white taking the odd slots
\r
549 // Pieces in general have two entries: one for the basic, and one for the promoted version
\r
550 // The unused one gets its position set to the invalid square number ABSENT
\r
551 // The list is sorted by piece value, most valuable pieces first
\r
552 // When a piece is captured in the search, both its versions are marked ABSENT
\r
553 // In the root the list is packed to eliminate all captured pieces
\r
554 // The list contains a table for how far the piece moves in each direction
\r
555 // Range encoding: -3 = full Lion, -2 = on-ray Lion, -1 = plain jump, 0 = none, 1 = step, >1 = (limited) range
\r
557 // A table in board format, containing pairs of consecutive integers for each square (indexed as 2*sqr and 2*sqr+1)
\r
558 // The first integer contains info on black attacks to the square, the second similarly for white attacks
\r
559 // Each integer contains eight 3-bit fields, which count the number of attacks on it with moves in a particular direction
\r
560 // (If there are attacks by range-jumpers, the 3-bit count is increased by 2 over the actual value)
\r
562 // The board has twice as many files as actually used, in 0x88 fashion
\r
563 // The used squares hold the piece numbers (for use as index in the piece list)
\r
564 // Unused squares are set to the invalid piece number EDGE
\r
565 // There are also 3 guard ranks of EDGE on each side
\r
567 // Moves are encoded as 11-bit from-square and to-square numbers packed in the low bits of an int
\r
568 // Special moves (like Lion double moves) are encoded by off-board to-squares above a certain value
\r
569 // Promotions are indicated by bit 22
\r
571 // Entries of 16 bytes, holding a 32-bit signature, 16-bit lower- and upper-bound scores,
\r
572 // 8-bit draft of each of those scores, an age counter that stores the search number of the last access.
\r
573 // The hash key is derived as the XOR of the products pieceKey[piece]*squareKey[square].
\r
575 // the promoBoard contains one byte with flags for each square, to indicate for each side whether the square
\r
576 // is in the promotion zone (twice), on the last two ranks, or on the last rank
\r
577 // the promoFlag field in the piece list can select which bits of this are tested, to see if it
\r
578 // (1) can promote (2) can defer when the to-square is on last rank, last two ranks, or anywhere.
\r
579 // Pawns normally can't defer anywhere, but if the user defers with them, their promoFlag is set to promote on last rank only
\r
587 signed char range[8];
\r
592 unsigned char promoGain;
\r
594 } PieceInfo; // piece-list entry
\r
596 int last[2], royal[2], kylin[2];
\r
597 PieceInfo p[NPIECES]; // piece list
\r
608 } HashEntry; // hash-table entry
\r
610 // Some global variables that control your engine's behavior
\r
614 int noCut=1; // engine-defined option
\r
615 int resign; // engine-defined option
\r
616 int contemptFactor; // likewise
\r
619 int squareKey[BSIZE];
\r
621 int rawBoard[BSIZE + 11*BHMAX + 6];
\r
622 //int attacks[2*BSIZE]; // attack map
\r
623 int attackMaps[200*BSIZE], *attacks = attackMaps;
\r
624 char distance[2*BSIZE]; // distance table
\r
625 char promoBoard[BSIZE]; // flags to indicate promotion zones
\r
626 unsigned char rawFire[BSIZE+2*BWMAX]; // flags to indicate squares controlled by Fire Demons
\r
627 signed char PST[7*BSIZE];
\r
629 #define board (rawBoard + 6*BHMAX + 3)
\r
630 #define fireBoard (rawFire + BWMAX + 1)
\r
631 #define dist (distance + BSIZE)
\r
634 ListLookUp (char *name, PieceDesc *list)
\r
635 { // find piece of given name in list of descriptors
\r
636 while(list->name && strcmp(name, list->name)) list++;
\r
637 return (list->name == NULL ? NULL : list);
\r
641 LookUp (char *name, int var)
\r
642 { // search piece of given name in all lists relevant for given variant
\r
645 case V_TENJIKU: // Tenjiku
\r
646 desc = ListLookUp(name, tenjikuPieces);
\r
647 if(desc) return desc;
\r
648 return ListLookUp(name, chuPieces);
\r
650 return ListLookUp(name, shoPieces);
\r
652 desc = ListLookUp(name, daiPieces);
\r
653 if(desc) return desc;
\r
655 return ListLookUp(name, chuPieces);
\r
656 case V_CHESS: // FIDE
\r
657 return ListLookUp(name, chessPieces);
\r
658 case V_SHATRANJ: // Shatranj
\r
659 return ListLookUp(name, shatranjPieces);
\r
660 case V_MAKRUK: // Makruk
\r
661 return ListLookUp(name, makrukPieces);
\r
662 case V_LION: // Mighty Lion
\r
663 return ListLookUp(name, lionPieces);
\r
670 { // remove piece number n from the mentioned side's piece list (and adapt the reference to the displaced pieces!)
\r
672 for(i=stm+2; i<=last[stm]; i+=2)
\r
673 if(p[i].promo > n) p[i].promo -= 2;
\r
674 for(i=n; i<last[stm]; i+=2) {
\r
676 if(i+2 == royal[stm]) royal[stm] -= 2; // NB: squeezing out the King moves up Crown Prince to royal[stm]
\r
677 if(i+2 == kylin[stm]) kylin[stm] -= 2;
\r
678 if(i < 10) fireFlags[i-2] = fireFlags[i];
\r
684 Worse (int a, int b)
\r
685 { // determine if range a not upward compatible with b
\r
686 if(a == b) return 0;
\r
687 if(a >= 0 && b >= 0) return a < b;
\r
688 if(a >= 0) return 1; // a (limited) range can never do the same as a special move
\r
690 case J: return b < J; // any special move is better than a plain jump
\r
691 case D: return b > 2 || b < D;
\r
692 case T: return b > 3 || b < T;
\r
693 case L: return b > 2 || b < D;
\r
694 case F: return b > 3 || b < F || b == T;
\r
695 case S: return b == H || b == T;
\r
696 case H: return b < 0;
\r
697 default: return 1; // a >= 0, so b must be < 0 and can always do something a ranging move cannot do
\r
703 Lance (signed char *r)
\r
704 { // File-bound forward slider
\r
706 for(i=1; i<4; i++) if(r[i] || r[i+4]) return 0;
\r
711 EasyProm (signed char *r)
\r
713 if(r[0] == X) return 30 + PROMO*((unsigned int)(r[1] | r[2] | r[3] | r[5] | r[6] | r[7]) <= 1);
\r
714 if(r[1] == X || r[7] == X) return 30 + PROMO/2;
\r
719 IsUpwardCompatible (signed char *r, signed char *s)
\r
722 for(i=0; i<8; i++) {
\r
723 if(Worse(r[i], s[i])) return 0;
\r
729 Forward (signed char *r)
\r
732 for(i=2; i<7; i++) if(r[i]) return 0;
\r
737 Range (signed char *r)
\r
740 for(i=0; i<8; i++) {
\r
742 if(r[i] < 0) d = r[i] >= L ? 2 : 36;
\r
748 int multis[2], multiMovers[100];
\r
751 StackMultis (int col)
\r
755 for(i=col+2; i<=last[col]; i+=2) { // scan piece list for multi-capturers
\r
756 for(j=0; j<8; j++) if(p[i].range[j] < J && p[i].range[j] >= S || p[i].value == FVAL) {
\r
757 multiMovers[multis[col]] = i; // found one: put its piece number in list
\r
765 Compactify (int stm)
\r
766 { // remove pieces that are permanently gone (captured or promoted) from one side's piece list
\r
768 for(i=stm+2; i<=last[stm]; i+=2) { // first pass: unpromoted pieces
\r
769 if((k = p[i].promo) >= 0 && p[i].pos == ABSENT) { // unpromoted piece no longer there
\r
770 p[k].promo = -2; // orphan promoted version
\r
774 for(i=stm+2; i<=last[stm]; i+=2) { // second pass: promoted pieces
\r
776 if((k = p[i].promo) == -2 && p[i].pos == ABSENT) { // orphaned promoted piece not present
\r
784 AddPiece (int stm, PieceDesc *list)
\r
787 for(i=stm+2; i<=last[stm]; i += 2) {
\r
788 if(p[i].value < list->value || p[i].value == list->value && (p[i].promo < 0)) break;
\r
791 for(j=last[stm]; j>i; j-= 2) p[j] = p[j-2];
\r
792 p[i].value = v = list->value;
\r
793 for(j=0; j<8; j++) p[i].range[j] = list->range[j^4*(WHITE-stm)];
\r
794 switch(Range(p[i].range)) {
\r
795 case 1: p[i].pst = PST_STEPPER; break;
\r
796 case 2: p[i].pst = PST_WJUMPER; break;
\r
797 default: p[i].pst = PST_SLIDER; break;
\r
799 key = (stm == WHITE ? &list->whiteKey : &list->blackKey);
\r
800 if(!*key) *key = ~(myRandom()*myRandom());
\r
801 p[i].promoGain = EasyProm(list->range); // flag easy promotion based on white view
\r
802 p[i].pieceKey = *key;
\r
803 p[i].promoFlag = 0;
\r
804 p[i].bulk = list->bulk;
\r
805 p[i].mobWeight = v > 600 ? 0 : v >= 400 ? 1 : v >= 300 ? 2 : v > 150 ? 3 : v >= 100 ? 2 : 0;
\r
806 if(Lance(list->range))
\r
807 p[i].mobWeight = 0, p[i].pst = list->range[4] ? PST_NEUTRAL : PST_LANCE; // keep back
\r
808 for(j=stm+2; j<= last[stm]; j+=2) {
\r
809 if(p[j].promo >= i) p[j].promo += 2;
\r
811 if(royal[stm] >= i) royal[stm] += 2;
\r
812 if(kylin[stm] >= i) kylin[stm] += 2;
\r
813 if(p[i].value == (currentVariant == V_SHO ? 410 : 280) ) royal[stm] = i, p[i].pst = 0;
\r
814 p[i].qval = (currentVariant == V_TENJIKU ? list->ranking : 0); // jump-capture hierarchy
\r
819 SetUp (char *array, int var)
\r
821 int i, j, n, m, color;
\r
822 char c, name[3], prince = 0;
\r
823 PieceDesc *p1, *p2;
\r
824 last[WHITE] = 1; last[BLACK] = 0;
\r
825 royal[WHITE] = royal[BLACK] = 0;
\r
827 //printf("next rank: %s\n", array);
\r
828 for(j = BW*i; ; j++) {
\r
830 if(*array == '+') pflag++, array++;
\r
831 c = name[0] = *array++;
\r
833 if(c == '.') continue;
\r
834 if(c == '/') break;
\r
835 name[1] = name[2] = 0;
\r
836 if(c == ':') name[0] = *array++, name[1] = *array++;
\r
837 if(name[0] >= 'a') {
\r
839 name[0] += 'A' - 'a';
\r
840 if(name[1]) name[1] += 'A' - 'a';
\r
841 } else color = WHITE;
\r
842 if(!strcmp(name, "CP") || pflag && !strcmp(name, "DE")) prince |= color+1; // remember if we added Crown Prince
\r
843 p1 = LookUp(name, var);
\r
844 if(!p1) printf("tellusererror Unknown piece '%s' in setup\n", name), exit(-1);
\r
845 if(pflag && p1->promoted) p1 = LookUp(p1->promoted, var); // use promoted piece instead
\r
846 n = AddPiece(color, p1);
\r
848 if(p1->promoted[0] && !pflag) {
\r
849 if(!strcmp(p1->promoted, "CP")) prince |= color+1; // remember if we added Crown Prince
\r
850 p2 = LookUp(p1->promoted, var);
\r
851 m = AddPiece(color, p2);
\r
854 p[n].promoFlag = IsUpwardCompatible(p2->range, p1->range) * DONT_DEFER + CAN_PROMOTE;
\r
855 if(Forward(p1->range)) p[n].promoFlag |= LAST_RANK; // Pieces that only move forward can't defer on last rank
\r
856 if(!strcmp(p1->name, "N")) p[n].promoFlag |= CANT_DEFER; // Knights can't defer on last 2 ranks
\r
857 p[n].promoFlag &= n&1 ? P_WHITE : P_BLACK;
\r
860 if(p[m].value == LVAL) kylin[color] = n; // remember piece that promotes to Lion
\r
861 } else p[n].promo = -1; // unpromotable piece
\r
862 //printf("piece = %c%-2s %d(%d) %d/%d\n", color ? 'w' : 'b', name, n, m, last[color], last[!color]);
\r
866 // add dummy Kings if not yet added (needed to set royal[] to valid value!)
\r
867 if(!royal[WHITE]) p[AddPiece(WHITE, LookUp("K", V_CHU))].pos = ABSENT;
\r
868 if(!royal[BLACK]) p[AddPiece(BLACK, LookUp("K", V_CHU))].pos = ABSENT;
\r
869 // add dummy Crown Princes if not yet added
\r
870 if(!(prince & WHITE+1)) p[AddPiece(WHITE, LookUp("CP", V_CHU))].pos = ABSENT;
\r
871 if(!(prince & BLACK+1)) p[AddPiece(BLACK, LookUp("CP", V_CHU))].pos = ABSENT;
\r
872 for(i=0; i<8; i++) fireFlags[i] = 0;
\r
873 for(i=2, n=1; i<10; i++) if(p[i].value == FVAL) {
\r
874 int x = p[i].pos; // mark all burn zones
\r
875 fireFlags[i-2] = n;
\r
876 if(x != ABSENT) for(j=0; j<8; j++) fireBoard[x+kStep[j]] |= n;
\r
879 for(i=0; i<BH; i++) for(j=0; j<BH; j++) board[BW*i+j] = EMPTY;
\r
880 for(i=WHITE+2; i<=last[WHITE]; i+=2) if(p[i].pos != ABSENT) {
\r
881 int g = p[i].promoGain;
\r
882 if(i == kylin[WHITE]) p[i].promoGain = 1.25*KYLIN, p[i].value += KYLIN;
\r
883 // if(j > 0 && p[i].pst == PST_STEPPER) p[i].pst = PST_WPPROM; // use white pre-prom bonus
\r
884 if(j > 0 && p[i].pst == PST_STEPPER && p[i].value >= 100)
\r
885 p[i].pst = p[i].value <= 150 ? PST_ADVANCE : PST_WPPROM; // light steppers advance
\r
886 if(j > 0 && p[i].bulk == 6) p[i].pst = PST_WFLYER, p[i].mobWeight = 4; // SM defends zone
\r
887 if((j = p[i].promo) > 0 && g)
\r
888 p[i].promoGain = (p[j].value - p[i].value - g)*0.9, p[i].value = p[j].value - g;
\r
889 else p[i].promoGain = 0;
\r
890 board[p[i].pos] = i;
\r
891 rootEval += p[i].value + PST[p[i].pst + p[i].pos];
\r
892 promoDelta += p[i].promoGain;
\r
893 filling += p[i].bulk;
\r
894 } else p[i].promoGain = 0;
\r
895 for(i=BLACK+2; i<=last[BLACK]; i+=2) if(p[i].pos != ABSENT) {
\r
896 int g = p[i].promoGain;
\r
897 // if(j > 0 && p[i].pst == PST_STEPPER) p[i].pst = PST_BPPROM; // use black pre-prom bonus
\r
898 if(j > 0 && p[i].pst == PST_STEPPER && p[i].value >= 100)
\r
899 p[i].pst = p[i].value <= 150 ? PST_RETRACT : PST_BPPROM; // light steppers advance
\r
900 if(j > 0 && p[i].pst == PST_WJUMPER) p[i].pst = PST_BJUMPER; // use black pre-prom bonus
\r
901 if(j > 0 && p[i].bulk == 6) p[i].pst = PST_BFLYER, p[i].mobWeight = 4; // SM defends zone
\r
902 if((j = p[i].promo) > 0 && g)
\r
903 p[i].promoGain = (p[j].value - p[i].value - g)*0.9, p[i].value = p[j].value - g;
\r
904 else p[i].promoGain = 0;
\r
905 if(i == kylin[BLACK]) p[i].promoGain = 1.25*KYLIN, p[i].value += KYLIN;
\r
906 board[p[i].pos] = i;
\r
907 rootEval -= p[i].value + PST[p[i].pst + p[i].pos];
\r
908 promoDelta -= p[i].promoGain;
\r
909 filling += p[i].bulk;
\r
910 } else p[i].promoGain = 0;
\r
911 StackMultis(WHITE);
\r
912 StackMultis(BLACK);
\r
913 stm = WHITE; xstm = BLACK;
\r
918 return rand() ^ rand()>>10 ^ rand() << 10 ^ rand() << 20;
\r
927 if(var != SAME) { // the following should be already set if we stay in same variant (for TakeBack)
\r
928 currentVariant = variants[var].varNr;
\r
929 bWidth = variants[var].boardWidth;
\r
930 bHeight = variants[var].boardRanks;
\r
931 zone = variants[var].zoneDepth;
\r
932 array = variants[var].array;
\r
934 bsize = bWidth*bHeight;
\r
935 chuFlag = (currentVariant == V_CHU || currentVariant == V_LION);
\r
936 tenFlag = (currentVariant == V_TENJIKU);
\r
937 chessFlag = (currentVariant == V_CHESS || currentVariant == V_LION);
\r
938 stalemate = (currentVariant == V_CHESS || currentVariant == V_MAKRUK || currentVariant == V_LION);
\r
939 repDraws = (stalemate || currentVariant == V_SHATRANJ);
\r
940 ll = 0; lr = bHeight - 1; ul = (bHeight - 1)*bWidth; ur = ul + bHeight - 1;
\r
941 pawn = LookUp("P", currentVariant); pVal = pawn ? pawn->value : 0; // get Pawn value
\r
943 for(i= -1; i<9; i++) { // board steps in linear coordinates
\r
944 kStep[i] = STEP(direction[i&7].x, direction[i&7].y); // King
\r
945 nStep[i] = STEP(direction[(i&7)+8].x, direction[(i&7)+8].y); // Knight
\r
947 for(i=0; i<8; i++) neighbors[i+1] = kStep[i];
\r
949 for(i=0; i<8; i++) { // Lion double-move decoding tables
\r
950 for(j=0; j<8; j++) {
\r
951 epList[8*i+j] = kStep[i];
\r
952 toList[8*i+j] = kStep[j] + kStep[i];
\r
953 for(k=0; k<8*i+j; k++)
\r
954 if(epList[k] == toList[8*i+j] && toList[k] == epList[8*i+j])
\r
955 reverse[k] = 8*i+j, reverse[8*i+j] = k;
\r
957 // Lion-Dog triple moves
\r
958 toList[64+i] = 3*kStep[i]; epList[64+i] = kStep[i];
\r
959 toList[72+i] = 3*kStep[i]; epList[72+i] = 2*kStep[i];
\r
960 toList[80+i] = 3*kStep[i]; epList[80+i] = kStep[i]; ep2List[80+i] = 2*kStep[i];
\r
961 toList[88+i] = kStep[i]; epList[88+i] = 2*kStep[i];
\r
964 toList[100] = BH - 2; epList[100] = BH - 1; ep2List[100] = BH - 3;
\r
965 toList[100+1] = 2; epList[100+1] = 0; ep2List[100+1] = 3;
\r
966 toList[100+2] = bsize - BH - 2; epList[100+2] = bsize - BH - 1; ep2List[100+2] = bsize - BH - 3;
\r
967 toList[100+3] = bsize - BW + 2; epList[100+3] = bsize - BW; ep2List[100+3] = bsize - BW + 3;
\r
969 // fill distance table
\r
970 for(i=0; i<2*BSIZE; i++) {
\r
973 // for(i=0; i<8; i++)
\r
974 // for(j=1; j<BH; j++)
\r
975 // dist[j * kStep[i]] = j;
\r
976 // if(currentVariant == V_TENJIKU)
\r
977 for(i=1-BH; i<BH; i++) for(j=1-BH; j<BH; j++) dist[BW*i+j] = abs(i) > abs(j) ? abs(i) : abs(j);
\r
980 for(i=0; i<bsize; i++) squareKey[i] = ~(myRandom()*myRandom());
\r
983 for(i=0; i<BSIZE + 11*BHMAX + 6; i++) rawBoard[i] = EDGE;
\r
986 for(i=0; i<BH; i++) for(j=0; j<BH; j++) {
\r
988 if(i == 0) v |= LAST_RANK & P_BLACK;
\r
989 if(i < 2) v |= CANT_DEFER & P_BLACK;
\r
990 if(i < ZONE) v |= (CAN_PROMOTE | DONT_DEFER) & P_BLACK; else v &= ~P_BLACK;
\r
991 if(i >= BH-ZONE) v |= (CAN_PROMOTE | DONT_DEFER) & P_WHITE; else v &= ~P_WHITE;
\r
992 if(i >= BH-2) v |= CANT_DEFER & P_WHITE;
\r
993 if(i == BH-1) v |= LAST_RANK & P_WHITE;
\r
994 promoBoard[BW*i + j] = v;
\r
997 // piece-square tables
\r
998 for(j=0; j<BH; j++) {
\r
999 for(i=0; i<BH; i++) {
\r
1000 int s = BW*i + j, d = BH*(BH-2) - abs(2*i - BH + 1)*(BH-1) - (2*j - BH + 1)*(2*j - BH + 1);
\r
1001 PST[s] = 2*(i==0 | i==BH-1) + (i==1 | i==BH-2); // last-rank markers in null table
\r
1002 PST[PST_STEPPER+s] = d/4 - (i < 2 || i > BH-3 ? 3 : 0) - (j == 0 || j == BH-1 ? 5 : 0)
\r
1003 + 3*(i==zone || i==BH-zone-1); // stepper centralization
\r
1004 PST[PST_WJUMPER+s] = d/6; // double-stepper centralization
\r
1005 PST[PST_SLIDER +s] = d/12 - 15*(i==BH/2 || i==(BH-1)/2);// slider centralization
\r
1006 PST[PST_TRAP +s] = j < 3 || j > BH-4 ? (i < 3 ? 7 : i == 3 ? 4 : i == 4 ? 2 : 0) : 0;
\r
1007 PST[PST_CENTER+s] = ((BH-1)*(BH-1) - (2*i - BH + 1)*(2*i - BH + 1) - (2*j - BH + 1)*(2*j - BH + 1))/6;
\r
1008 PST[PST_WPPROM+s] = PST[PST_BPPROM+s] = PST[PST_STEPPER+s]; // as stepper, but with pre-promotion bonus W/B
\r
1009 PST[PST_BJUMPER+s] = PST[PST_WJUMPER+s]; // as jumper, but with pre-promotion bonus B
\r
1010 PST[PST_ZONDIST+s] = BW*(zone - 1 - i); // board step to enter promo zone black
\r
1011 PST[PST_ADVANCE+s] = PST[PST_WFLYER-s-1] = 2*(5*i+i*i) - (i >= zone)*6*(i-zone+1)*(i-zone+1)
\r
1012 - (2*j - BH + 1)*(2*j - BH + 1)/BH + BH/2
\r
1013 - 50 - 35*(j==0 || j == BH-1) - 15*(j == 1 || BH-2); // advance-encouraging table
\r
1014 PST[PST_WFLYER +s] = PST[PST_LANCE-s-1] = (i == zone-1)*40 + (i == zone-2)*20 - 20;
\r
1015 PST[PST_LANCE +s] = (PST[PST_STEPPER+j] - PST[PST_STEPPER+s])/2;
\r
1017 if(zone > 0) PST[PST_WPPROM+BW*(BH-1-zone) + j] += 10, PST[PST_BPPROM + BW*zone + j] += 10;
\r
1018 if(j > (BH-1)/2 - 3 && j < BH/2 + 3)
\r
1019 PST[PST_WPPROM + j] += 4, PST[PST_BPPROM + BW*(BH-1) + j] += 4; // fortress
\r
1020 if(j > (BH-1)/2 - 2 && j < BH/2 + 2)
\r
1021 PST[PST_WPPROM + BW + j] += 2, PST[PST_BPPROM + BW*(BH-2) + j] += 2; // fortress
\r
1023 // pre-promotion bonuses for jumpers
\r
1024 if(zone > 0) PST[PST_WJUMPER + BW*(BH-2-zone) + j] = PST[PST_BJUMPER + BW*(zone+1) + j] = 100,
\r
1025 PST[PST_WJUMPER + BW*(BH-1-zone) + j] = PST[PST_BJUMPER + BW*zone + j] = 200;
\r
1029 p[EDGE].qval = 5; // tenjiku jump-capturer sentinel
\r
1035 int r, f, score, tot=0;
\r
1036 for(r=0; r<BH; r++) for(f=0; f<BH; f++) {
\r
1038 int piece = board[s];
\r
1039 if(!piece) continue;
\r
1040 score = p[piece].value + PST[p[piece].pst + s];
\r
1041 if(piece & 1) tot += score; else tot -= score;
\r
1049 int r, f, score, tot=0;
\r
1050 for(r=0; r<BH; r++) for(f=0; f<BH; f++) {
\r
1052 int piece = board[s];
\r
1053 if(!piece) continue;
\r
1054 score = p[piece].promoGain;
\r
1055 if(piece & 1) tot += score; else tot -= score;
\r
1063 NewNonCapture (int x, int y, int promoFlags)
\r
1065 if(board[y] != EMPTY) return 1; // edge, capture or own piece
\r
1066 //if(flag) printf("# add %c%d%c%d, pf=%d\n", x%BW+'a',x/BW,y%BW+'a',y/BW, promoFlags);
\r
1067 if( (entryProm ? promoBoard[y] & ~promoBoard[x] & CAN_PROMOTE
\r
1068 : promoBoard[y] | promoBoard[x] ) & promoFlags ){ // piece can promote with this move
\r
1069 moveStack[msp++] = moveStack[nonCapts]; // create space for promotion
\r
1070 moveStack[nonCapts++] = x<<SQLEN | y | PROMOTE; // push promotion
\r
1071 if((promoFlags & promoBoard[y] & (CANT_DEFER | DONT_DEFER | LAST_RANK)) == 0) { // deferral could be a better alternative
\r
1072 moveStack[msp++] = x<<SQLEN | y; // push deferral
\r
1073 if( (promoBoard[x] & CAN_PROMOTE) == 0 ) { // enters zone
\r
1074 moveStack[msp-1] |= DEFER; // flag that promo-suppression takes place after this move
\r
1078 moveStack[msp++] = x<<SQLEN | y; // push normal move
\r
1079 //if(flag) printf("msp=%d nc=%d\n", msp, nonCapts);
\r
1084 NewCapture (int x, int y, int promoFlags)
\r
1086 if( (promoBoard[x] | promoBoard[y]) & promoFlags) { // piece can promote with this move
\r
1087 moveStack[msp++] = x<<SQLEN | y | PROMOTE; // push promotion
\r
1088 if((promoFlags & promoBoard[y] & (CANT_DEFER | DONT_DEFER | LAST_RANK)) == 0) { // deferral could be a better alternative
\r
1089 moveStack[msp++] = x<<SQLEN | y; // push deferral
\r
1090 if( (promoBoard[x] & CAN_PROMOTE) == 0 ) { // enters zone
\r
1091 moveStack[msp-1] |= DEFER; // flag that promo-suppression takes place after this move
\r
1095 moveStack[msp++] = x<<SQLEN | y; // push normal move
\r
1099 char map[49]; // 7x7 map for area movers
\r
1100 char mapStep[] = { 7, 8, 1, -6, -7, -8, -1, 6 };
\r
1101 char rowMask[] = { 0100, 0140, 0160, 070, 034, 016, 07, 03, 01 };
\r
1105 AreaStep (int from, int x, int flags, int n, int d)
\r
1108 for(i=0; i<8; i++) {
\r
1109 int to = x + kStep[i], m = n + mapStep[i];
\r
1110 if(board[to] == EDGE) continue; // off board
\r
1111 if(map[m] >= d) continue; // already done
\r
1112 if(!map[m]) moveStack[msp++] = from<<SQLEN | to;
\r
1114 if(d > 1 && board[to] == EMPTY) AreaStep(from, to, flags, m, d-1);
\r
1119 AreaMoves (int from, int piece, int range)
\r
1122 for(i=0; i<49; i++) map[i] = 0;
\r
1123 map[3*7+7] = range;
\r
1124 AreaStep(from, from, p[piece].promoFlag, 3*7+3, range);
\r
1129 { // make bitmap of squares in FI (7x7) neighborhood where opponents can be captured or burned
\r
1130 int r=x>>5, f=x&15, top=8, bottom=0, b=0, t=8, left=0, right=8; // 9x9 area; assumes 32x16 board
\r
1131 if(r < 4) bottom = 4 - r, rows[b=bottom-1] = 0; else
\r
1132 if(r > 11) top = 19 - r, rows[t=top+1] = 0; // adjust area to board edges
\r
1133 if(f < 4) left = 4 - f; else if(f > 11) right = 19 - f;
\r
1134 for(r=bottom; r<=top; r++) {
\r
1135 int mask = 0, y = x + 16*r;
\r
1136 for(f=left; f <= right; f++) {
\r
1137 if(board[y + f - (4*16+4)] != EMPTY && (board[y + f - (4*16+4)] & TYPE) == xstm)
\r
1138 mask |= rowMask[f]; // on-rank attacks
\r
1142 for(r=b; r<=t-2; r++) rows[r] |= rows[r+1] | rows[r+2]; // smear vertically
\r
1147 { // castings for Lion Chess. Assumes board width = 8 and Kings on e-file, and K/R value = 280/300!
\r
1148 int f = BH>>1, t = CASTLE;
\r
1149 if(stm != WHITE) f += bsize - BW, t += 2;
\r
1150 if(p[board[f]].value = 280) {
\r
1151 if(p[board[f-4]].value == 300 && board[f-3] == EMPTY && board[f-2] == EMPTY && board[f-1] == EMPTY) moveStack[msp++] = f<<SQLEN | t+1;
\r
1152 if(p[board[f+3]].value == 300 && board[f+1] == EMPTY && board[f+2] == EMPTY) moveStack[msp++] = f<<SQLEN | t;
\r
1157 GenNonCapts (int promoSuppress)
\r
1159 int i, j, nullMove = ABSENT;
\r
1160 for(i=stm+2; i<=last[stm]; i+=2) {
\r
1161 int x = p[i].pos, pFlag = p[i].promoFlag;
\r
1162 if(x == ABSENT) continue;
\r
1163 if(x == promoSuppress && chuFlag) pFlag = 0;
\r
1164 for(j=0; j<8; j++) {
\r
1165 int y, v = kStep[j], r = p[i].range[j];
\r
1166 if(r < 0) { // jumping piece, special treatment
\r
1167 if(r == N) { // pure Knightm do off-ray jump
\r
1168 NewNonCapture(x, x + nStep[j], pFlag);
\r
1170 if(r >= S) { // in any case, do a jump of 2
\r
1171 int occup = NewNonCapture(x, x + 2*v, pFlag);
\r
1172 if(r < I) { // Lion power, also single step
\r
1173 if(!NewNonCapture(x, x + v, pFlag)) nullMove = x; else occup = 1;
\r
1174 if(r <= L) { // true Lion, also Knight jump
\r
1175 if(!occup & r < L) for(y=x+2*v; !NewNonCapture(x, y+=v, pFlag) && r == S; ); // BS and FF moves
\r
1177 NewNonCapture(x, x + v, pFlag);
\r
1179 } else if(r == I) NewNonCapture(x, x + v, pFlag); // also do step
\r
1181 if(r == M) { // FIDE Pawn; check double-move
\r
1182 if(!NewNonCapture(x, x+v, pFlag) && chessFlag && promoBoard[x-v] & LAST_RANK)
\r
1183 NewNonCapture(x, x+2*v, pFlag), moveStack[msp-1] |= DEFER; // use promoSuppress flag as e.p. flag
\r
1189 if(NewNonCapture(x, y+=v, pFlag)) break;
\r
1196 report (int x, int y, int i)
\r
1201 MapOneColor (int start, int last, int *map)
\r
1203 int i, j, totMob = 0;
\r
1204 for(i=start+2; i<=last; i+=2) {
\r
1206 if(p[i].pos == ABSENT) continue;
\r
1207 for(j=0; j<8; j++) {
\r
1208 int x = p[i].pos, v = kStep[j], r = p[i].range[j];
\r
1209 if(r < 0) { // jumping piece, special treatment
\r
1212 if(board[x] != EMPTY && board[x] != EDGE)
\r
1213 map[2*x + start] += one[8];
\r
1215 if(r >= S) { // in any case, do a jump of 2
\r
1216 if(board[x + 2*v] != EMPTY && board[x + 2*v] != EDGE)
\r
1217 map[2*(x + 2*v) + start] += one[j], mob += (board[x + 2*v] ^ start) & 1;
\r
1218 if(r < J) { // Lion power, also single step
\r
1219 if(board[x + v] != EMPTY && board[x + v] != EDGE)
\r
1220 map[2*(x + v) + start] += one[j];
\r
1222 if(r == T) { // Lion Dog, also jump of 3
\r
1223 if(board[x + 3*v] != EMPTY && board[x + 3*v] != EDGE)
\r
1224 map[2*(x + 3*v) + start] += one[j];
\r
1226 if(r <= L) { // true Lion, also Knight jump
\r
1227 if(r < L) { // Lion plus (limited) range
\r
1229 r = (r == S ? 36 : 3);
\r
1231 if(board[y+=v] == EDGE) break;
\r
1232 if(board[y] != EMPTY) {
\r
1233 if(n > 2) map[2*y + start] += one[j]; // outside Lion range
\r
1239 if(board[x + v] != EMPTY && board[x + v] != EDGE)
\r
1240 map[2*(x + v) + start] += one[8];
\r
1245 if(r == C) { // FIDE Pawn diagonal
\r
1246 if(board[x + v] != EMPTY && board[x + v] != EDGE)
\r
1247 map[2*(x + v) + start] += one[j];
\r
1252 if(board[x+=v] != EMPTY) {
\r
1253 mob += dist[x-v-p[i].pos];
\r
1254 if(board[x] != EDGE) map[2*x + start] += one[j], mob += (board[x] ^ start) & 1;
\r
1256 if(p[i].range[j] > X) { // jump capturer
\r
1257 int c = p[i].qval;
\r
1258 if(p[board[x]].qval < c) {
\r
1259 x += v; // go behind directly captured piece, if jumpable
\r
1260 while(p[board[x]].qval < c) { // kludge alert: EDGE has qval = 5, blocking everything
\r
1261 if(board[x] != EMPTY) {
\r
1262 // int n = map[2*x + start] & attackMask[j];
\r
1263 // map[2*x + start] += (n < 3*one[j] ? 3*one[j] : one[j]); // first jumper gets 2 extra (to ease incremental update)
\r
1264 map[2*x + start] += one[j]; // for now use true count
\r
1275 totMob += mob * p[i].mobWeight;
\r
1277 if(!level) printf("# mobility %d = %d\n", start, totMob);
\r
1282 MapFromScratch (int *map)
\r
1285 for(i=0; i<2*bsize; i++) map[i] = 0;
\r
1286 mobilityScore = MapOneColor(1, last[WHITE], map);
\r
1288 mobilityScore -= MapOneColor(0, last[BLACK], map);
\r
1292 MakeMove(Move m, UndoInfo *u)
\r
1294 int deferred = ABSENT;
\r
1295 // first execute move on board
\r
1296 u->from = m>>SQLEN & SQUARE;
\r
1297 u->to = m & SQUARE;
\r
1298 u->piece = board[u->from];
\r
1299 board[u->from] = EMPTY;
\r
1303 u->revMoveCount = cnt50++;
\r
1304 u->savKeyL = hashKeyL;
\r
1305 u->savKeyH = hashKeyH;
\r
1306 u->epVictim[0] = EMPTY;
\r
1307 u->saveDelta = promoDelta;
\r
1308 u->filling = filling;
\r
1310 if(p[u->piece].promoFlag & LAST_RANK) cnt50 = 0; // forward piece: move is irreversible
\r
1311 // TODO: put in some test for forward moves of non-backward pieces?
\r
1312 // int n = board[promoSuppress-1];
\r
1313 // if( n != EMPTY && (n&TYPE) == xstm && p[n].value == 8 ) NewNonCapt(promoSuppress-1, 16, 0);
\r
1315 if(p[u->piece].value == FVAL) { // move with Fire Demon
\r
1316 int i, f=~fireFlags[u->piece-2];
\r
1317 for(i=0; i<8; i++) fireBoard[u->from + kStep[i]] &= f; // clear old burn zone
\r
1320 if(m & (PROMOTE | DEFER)) {
\r
1321 if(m & DEFER) { // essential deferral: inform caller, but nothing special
\r
1323 u->new = u->piece;
\r
1325 p[u->piece].pos = ABSENT;
\r
1326 u->new = p[u->piece].promo;
\r
1327 u->booty = p[u->new].value - p[u->piece].value;
\r
1328 cnt50 = 0; // promotion irreversible
\r
1330 } else u->new = u->piece;
\r
1332 if(u->to >= SPECIAL) { // two-step Lion move
\r
1333 if(u->to >= CASTLE) { // move Rook, faking it was an e.p. victim so that UnMake works automatically
\r
1334 u->epSquare = epList[u->to - SPECIAL];
\r
1335 u->ep2Square = ep2List[u->to - SPECIAL];
\r
1336 u->epVictim[0] = board[u->epSquare]; // kludgy: fake that King e.p. captured the Rook!
\r
1337 u->epVictim[1] = board[u->ep2Square]; // should be EMPTY (but you never know, so save as well).
\r
1338 board[u->ep2Square] = u->epVictim[0]; // but put Rook back
\r
1339 board[u->epSquare] = EMPTY;
\r
1340 p[u->epVictim[0]].pos = u->ep2Square;
\r
1341 p[u->epVictim[1]].pos = ABSENT;
\r
1342 u->to = toList[u->to - SPECIAL];
\r
1343 hashKeyL ^= p[u->epVictim[0]].pieceKey * squareKey[u->epSquare];
\r
1344 hashKeyH ^= p[u->epVictim[0]].pieceKey * squareKey[u->epSquare+BH];
\r
1345 hashKeyL ^= p[u->epVictim[0]].pieceKey * squareKey[u->ep2Square];
\r
1346 hashKeyH ^= p[u->epVictim[0]].pieceKey * squareKey[u->ep2Square+BH];
\r
1348 // take care of first e.p. victim
\r
1349 u->epSquare = u->from + epList[u->to - SPECIAL]; // decode
\r
1350 u->epVictim[0] = board[u->epSquare]; // remember for takeback
\r
1351 board[u->epSquare] = EMPTY; // and remove
\r
1352 p[u->epVictim[0]].pos = ABSENT;
\r
1353 // now take care of (optional) second e.p. victim
\r
1354 u->ep2Square = u->from + ep2List[u->to - SPECIAL]; // This is the (already evacuated) from-square when there is none!
\r
1355 u->epVictim[1] = board[u->ep2Square]; // remember
\r
1356 board[u->ep2Square] = EMPTY; // and remove
\r
1357 p[u->epVictim[1]].pos = ABSENT;
\r
1358 // decode the true to-square, and correct difEval and hash key for the e.p. captures
\r
1359 u->to = u->from + toList[u->to - SPECIAL];
\r
1360 u->booty += p[u->epVictim[1]].value + PST[p[u->epVictim[1]].pst + u->ep2Square];
\r
1361 u->booty += p[u->epVictim[0]].value + PST[p[u->epVictim[0]].pst + u->epSquare];
\r
1362 u->gain += p[u->epVictim[1]].value;
\r
1363 u->gain += p[u->epVictim[0]].value;
\r
1364 promoDelta += p[u->epVictim[0]].promoGain;
\r
1365 promoDelta += p[u->epVictim[1]].promoGain;
\r
1366 filling -= p[u->epVictim[0]].bulk;
\r
1367 filling -= p[u->epVictim[1]].bulk;
\r
1368 hashKeyL ^= p[u->epVictim[0]].pieceKey * squareKey[u->epSquare];
\r
1369 hashKeyH ^= p[u->epVictim[0]].pieceKey * squareKey[u->epSquare+BH];
\r
1370 hashKeyL ^= p[u->epVictim[1]].pieceKey * squareKey[u->ep2Square];
\r
1371 hashKeyH ^= p[u->epVictim[1]].pieceKey * squareKey[u->ep2Square+BH];
\r
1372 if(p[u->piece].value != LVAL && p[u->epVictim[0]].value == LVAL) deferred |= PROMOTE; // flag non-Lion x Lion
\r
1373 cnt50 = 0; // double capture irreversible
\r
1377 if(u->fireMask & fireBoard[u->to]) { // we moved next to enemy Fire Demon (must be done after SPECIAL, to decode to-sqr)
\r
1378 p[u->piece].pos = ABSENT; // this is suicide: implement as promotion to EMPTY
\r
1380 u->booty -= p[u->piece].value;
\r
1383 if(p[u->piece].value == FVAL) { // move with Fire Demon that survives: burn
\r
1384 int i, f=fireFlags[u->piece-2];
\r
1385 for(i=0; i<8; i++) {
\r
1386 int x = u->to + kStep[i], burnVictim = board[x];
\r
1387 fireBoard[x] |= f; // mark new burn zone
\r
1388 u->epVictim[i+1] = burnVictim; // remember all neighbors, just in case
\r
1389 if(burnVictim != EMPTY && (burnVictim & TYPE) == xstm) { // opponent => actual burn
\r
1390 board[x] = EMPTY; // remove it
\r
1391 p[burnVictim].pos = ABSENT;
\r
1392 u->booty += p[burnVictim].value + PST[p[burnVictim].pst + x];
\r
1393 u->gain += p[burnVictim].value;
\r
1394 promoDelta += p[burnVictim].promoGain;
\r
1395 filling -= p[burnVictim].bulk;
\r
1396 hashKeyL ^= p[burnVictim].pieceKey * squareKey[x];
\r
1397 hashKeyH ^= p[burnVictim].pieceKey * squareKey[x + BH];
\r
1398 cnt50 = 0; // actually burning something makes the move irreversible
\r
1401 u->epVictim[0] = EDGE; // kludge to flag to UnMake this is special move
\r
1404 u->booty += PST[p[u->new].pst + u->to] - PST[p[u->piece].pst + u->from];
\r
1406 u->victim = board[u->to];
\r
1407 p[u->victim].pos = ABSENT;
\r
1408 filling += p[u->new].bulk - p[u->piece].bulk - p[u->victim].bulk;
\r
1409 promoDelta += p[u->new].promoGain - p[u->piece].promoGain + p[u->victim].promoGain;
\r
1410 u->booty += p[u->victim].value + PST[p[u->victim].pst + u->to];
\r
1411 u->gain += p[u->victim].value;
\r
1412 if(u->victim != EMPTY) {
\r
1413 cnt50 = 0; // capture irreversible
\r
1414 if(attacks[2*u->to + xstm]) u->loss = p[u->piece].value; // protected
\r
1417 p[u->new].pos = u->to;
\r
1418 board[u->to] = u->new;
\r
1419 promoDelta = -promoDelta;
\r
1421 hashKeyL ^= p[u->new].pieceKey * squareKey[u->to]
\r
1422 ^ p[u->piece].pieceKey * squareKey[u->from]
\r
1423 ^ p[u->victim].pieceKey * squareKey[u->to];
\r
1424 hashKeyH ^= p[u->new].pieceKey * squareKey[u->to+BH]
\r
1425 ^ p[u->piece].pieceKey * squareKey[u->from+BH]
\r
1426 ^ p[u->victim].pieceKey * squareKey[u->to+BH];
\r
1432 UnMake(UndoInfo *u)
\r
1434 if(u->epVictim[0]) { // move with side effects
\r
1435 if(u->epVictim[0] == EDGE) { // fire-demon burn
\r
1436 int i, f=~fireFlags[u->piece-2];
\r
1437 for(i=0; i<8; i++) {
\r
1438 int x = u->to + kStep[i];
\r
1439 fireBoard[x] &= f;
\r
1440 board[x] = u->epVictim[i+1];
\r
1441 p[board[x]].pos = x; // even EDGE should have dummy entry in piece list
\r
1443 } else { // put Lion victim of first leg back
\r
1444 p[u->epVictim[1]].pos = u->ep2Square;
\r
1445 board[u->ep2Square] = u->epVictim[1];
\r
1446 p[u->epVictim[0]].pos = u->epSquare;
\r
1447 board[u->epSquare] = u->epVictim[0];
\r
1451 if(p[u->piece].value == FVAL) {
\r
1452 int i, f=fireFlags[u->piece-2];
\r
1453 for(i=0; i<8; i++) fireBoard[u->from + kStep[i]] |= f; // restore old burn zone
\r
1456 p[u->victim].pos = u->to;
\r
1457 board[u->to] = u->victim; // can be EMPTY
\r
1459 p[u->new].pos = ABSENT;
\r
1460 p[u->piece].pos = u->from; // this can be the same as above
\r
1461 board[u->from] = u->piece;
\r
1463 cnt50 = u->revMoveCount;
\r
1464 hashKeyL = u->savKeyL;
\r
1465 hashKeyH = u->savKeyH;
\r
1466 filling = u->filling;
\r
1467 promoDelta = u->saveDelta;
\r
1471 GenCapts (int sqr, int victimValue)
\r
1472 { // generate all moves that capture the piece on the given square
\r
1473 int i, att = attacks[2*sqr + stm];
\r
1474 //printf("GenCapts(%c%d,%d) %08x\n",sqr%BW+'a',sqr/BW,victimValue,att);
\r
1475 if(!att) return; // no attackers at all!
\r
1476 for(i=0; i<8; i++) { // try all rays
\r
1477 int x, v, jcapt=0;
\r
1478 if(att & attackMask[i]) { // attacked by move in this direction
\r
1479 v = -kStep[i]; x = sqr;
\r
1480 while( board[x+=v] == EMPTY ); // scan towards source until we encounter a 'stop'
\r
1481 //printf("stop @ %c%d (dir %d)\n",x%BW+'a',x/BW,i);
\r
1482 if((board[x] & TYPE) == stm) { // stop is ours
\r
1483 int attacker = board[x], d = dist[x-sqr], r = p[attacker].range[i];
\r
1484 //printf("attacker %d, range %d, dist %d\n", attacker, r, d);
\r
1485 if(r >= d || r < L && (d > 3 && r == S || d == 3 && r >= S)) { // it has a plain move in our direction that hits us
\r
1486 NewCapture(x, sqr + victimValue - SORTKEY(attacker), p[attacker].promoFlag);
\r
1488 if(!(att & attackMask[i])) continue; // no more; next direction
\r
1489 jcapt = p[board[x]].qval; // jump-capturer hierarchy
\r
1490 while(board[x+=v] == EMPTY);// one attack accounted for, but more to come, so skip to next stop
\r
1493 // we get here when we are on a piece that dous not attack us through a (limited) ranging move,
\r
1494 // it can be our own or an enemy with not (enough) range, or which is blocked
\r
1496 //printf("scan %x-%x (%d) dir=%d d=%d r=%d att=%o jcapt=%d qval=%d\n", sqr, x, board[x], i, dist[x-sqr], p[board[x]].range[i], att, jcapt, p[board[x]].qval);
\r
1497 if((board[x] & TYPE) == stm) { // stop is ours
\r
1498 int attacker = board[x], d = dist[x-sqr], r = p[attacker].range[i];
\r
1499 if(jcapt < p[attacker].qval) { // it is a range jumper that jumps over the barrier
\r
1500 if(p[attacker].range[i] > 1) { // assumes all jump-captures are infinite range
\r
1501 NewCapture(x, sqr, p[attacker].promoFlag);
\r
1504 //if(board[x] == EDGE) { printf("edge hit %x-%x dir=%d att=%o\n", sqr, x, i, att); continue; }
\r
1506 if(r < 0) { // stop has non-standard moves
\r
1507 switch(p[attacker].range[i]) { // figure out what he can do (including multi-captures, which causes the complexity)
\r
1508 case F: // Lion power + 3-step (as in FF)
\r
1509 case S: // Lion power + ranging (as in BS)
\r
1512 NewCapture(x, sqr + victimValue - SORTKEY(attacker), p[attacker].promoFlag);
\r
1514 // now the multi-captures of designated victim together with lower-valued piece
\r
1515 if(d == 2) { // primary victim on second ring; look for victims to take in passing
\r
1516 if((board[sqr+v] & TYPE) == xstm && board[sqr+v] > board[sqr])
\r
1517 NewCapture(x, SPECIAL + 9*i + victimValue - SORTKEY(attacker), p[attacker].promoFlag);
\r
1518 if((i&1) == 0) { // orthogonal: two extra bent paths
\r
1520 if((board[x+v] & TYPE) == xstm && board[x+v] > board[sqr])
\r
1521 NewCapture(x, SPECIAL + 8*(i-1&7) + (i+1&7) + victimValue - SORTKEY(attacker), p[attacker].promoFlag);
\r
1523 if((board[x+v] & TYPE) == xstm && board[x+v] > board[sqr])
\r
1524 NewCapture(x, SPECIAL + 8*(i+1&7) + (i-1&7) + victimValue - SORTKEY(attacker), p[attacker].promoFlag);
\r
1526 } else { // primary victim on first ring
\r
1528 for(j=0; j<8; j++) { // we can go on in 8 directions after we captured it in passing
\r
1530 if(sqr + v == x || board[sqr+v] == EMPTY) // hit & run; make sure we include igui (attacker is still at x!)
\r
1531 NewCapture(x, SPECIAL + 8*i + j + victimValue, p[attacker].promoFlag);
\r
1532 else if((board[sqr+v] & TYPE) == xstm && board[sqr+v] > board[sqr]) { // double capture
\r
1533 NewCapture(x, SPECIAL + 8*i + j + victimValue, p[attacker].promoFlag); // other victim after primary
\r
1534 if(dist[sqr+v-x] == 1) // other victim also on first ring; reverse order is possible
\r
1535 NewCapture(x, SPECIAL + reverse[8*i + j] + victimValue, p[attacker].promoFlag);
\r
1540 case D: // linear Lion move (as in HF, SE)
\r
1542 NewCapture(x, sqr + victimValue - SORTKEY(attacker), p[attacker].promoFlag);
\r
1544 if(d == 2) { // check if we can take intermediate with it
\r
1545 if((board[x-v] & TYPE) == xstm && board[x-v] > board[sqr])
\r
1546 NewCapture(x, SPECIAL + 9*i + victimValue - SORTKEY(attacker), p[attacker].promoFlag); // e.p.
\r
1547 } else { // d=1; can move on to second, or move back for igui
\r
1548 NewCapture(x, SPECIAL + 8*i + (i^4) + victimValue, p[attacker].promoFlag); // igui
\r
1549 if(board[sqr-v] == EMPTY || (board[sqr-v] & TYPE) == xstm && board[sqr-v] > board[sqr])
\r
1550 NewCapture(x, SPECIAL + 9*i + victimValue - SORTKEY(attacker), p[attacker].promoFlag); // hit and run
\r
1553 case J: // plain jump (as in KY, PH)
\r
1555 case I: // jump + step (as in Wa TF)
\r
1557 NewCapture(x, sqr + victimValue - SORTKEY(attacker), p[attacker].promoFlag);
\r
1560 case C: // FIDE Pawn
\r
1562 NewCapture(x, sqr + victimValue - SORTKEY(attacker), p[attacker].promoFlag);
\r
1566 //printf("mask[%d] = %o\n", i, att);
\r
1567 if((att & attackMask[i]) == 0) break;
\r
1569 // more attacks to come; san for next stop
\r
1570 if(jcapt < p[board[x]].qval) jcapt = p[board[x]].qval; // raise barrier for range jumpers further upstream
\r
1571 while(board[x+=v] == EMPTY); // this should never run off-board, if the attack map is not corrupted
\r
1575 // off-ray attacks
\r
1576 if(att & 0700000000) { // Knight attack
\r
1577 for(i=0; i<8; i++) { // scan all knight jumps to locate source
\r
1578 int x = sqr - nStep[i], attacker = board[x];
\r
1579 if(attacker == EMPTY || (attacker & TYPE) != stm) continue;
\r
1580 if(p[attacker].range[i] <= L && p[attacker].range[i] >= S || p[attacker].range[i] == N) { // has Knight jump in our direction
\r
1581 NewCapture(x, sqr + victimValue, p[attacker].promoFlag); // plain jump (as in N)
\r
1582 if(p[attacker].range[i] < N) { // Lion power; generate double captures over two possible intermediates
\r
1583 int v = kStep[i]; // leftish path
\r
1584 if((board[x+v] & TYPE) == xstm && board[x+v] > board[sqr])
\r
1585 NewCapture(x, SPECIAL + 8*i + (i+1&7) + victimValue, p[attacker].promoFlag);
\r
1586 v = kStep[i+1]; // rightish path
\r
1587 if((board[x+v] & TYPE) == xstm && board[x+v] > board[sqr])
\r
1588 NewCapture(x, SPECIAL + 8*(i+1&7) + i + victimValue, p[attacker].promoFlag);
\r
1598 int piece = board[sqr], val;
\r
1599 if(piece == EDGE) return 0;
\r
1600 val = p[piece].value;
\r
1601 if(val == 201) return 3; // Elephant
\r
1602 if(val == 152) return 2; // Tiger
\r
1603 if(val == 151) return 1; // Gold
\r
1608 Fortress (int forward, int king, int lion)
\r
1609 { // penalty for lack of Lion-proof fortress
\r
1610 int rank = PST[king], anchor, r, l, q, res = 0;
\r
1611 if(rank != 2) return 25*(rank-2);
\r
1612 anchor = king + forward*(rank-1);
\r
1614 q = Guard(anchor); l = Guard(anchor-1); r = Guard(anchor+1);
\r
1615 if(!q) return l > 1 || r > 1 ? 0 : -25;
\r
1616 if(q == 1) res = 40*(l > 1 && r > 1); // TGT, EGT or TGE get 40
\r
1617 else { // T or E in front of King
\r
1618 if(l > 1) res = 30 + (r > 1)*(20 + 5*(q==2)); // TT., ET. or TE. (30), TET (50), TTE (55)
\r
1619 else if(r > 1) res = 30; // .TT, .ET or .TE (30)
\r
1622 if(filling > 32) {
\r
1623 if(r > 1 && Guard(king+2) == 1) q += 10;
\r
1624 if(l > 1 && Guard(king-2) == 1) q += 10;
\r
1625 q += 5*(Guard(king+1) == 1);
\r
1626 q += 5*(Guard(king-1) == 1);
\r
1627 if(filling < 96) q = q*(filling - 32)>>6;
\r
1631 if(Guard(anchor) == 3 || Guard(anchor+1) == 3 || Guard(anchor-1) == 3) return 0;
\r
1632 if(rank == 2 && Guard(king+1) == 3 || Guard(king-1) == 3) return -50;
\r
1633 if(Guard(r=anchor) == 2 || Guard(r=anchor+1) == 2 || Guard(r=anchor-1) == 2)
\r
1634 return -100 + 50*(Guard(r + forward + 1) == 3 || Guard(r + forward - 1) == 3);
\r
1637 for(r=anchor+1; Guard(r) > 1; r++);
\r
1638 for(l=anchor-1; Guard(l) > 1; l--);
\r
1639 //if(PATH) printf("# l=%d r=%d\n", l, r);
\r
1640 if(Guard(anchor) < 2) {
\r
1641 if(r - anchor > anchor - l || // largest group, or if equal, group that contains elephant
\r
1642 r - anchor == anchor - l && Guard(r-1) == 3) l = anchor; else r = anchor;
\r
1645 case 1: q = 15; break; // no shelter at all, maximum penalty
\r
1646 case 2: if(Guard(l+1) == 3) q = 10; // single Elephant offers some shelter
\r
1647 else if(Guard(l+forward) == 3 || Guard(l+forward+2) == 3) q = 8; // better if Tiger diagonally in front of it
\r
1648 else q = 14; // singe tiger almost no help;
\r
1650 case 3: q = 5 - (Guard(l+1) == 3 || Guard(l+3) == 3); break; // pair is better if it contains Elephant
\r
1651 case 4: q = (Guard(l+2) != 3); // 3 wide: perfect, or nearly so if Elephant not in middle
\r
1654 //if(PATH) printf("# fortress %d: q=%d l=%d r=%d\n", anchor, q, l, r);
\r
1655 return (dist[lion - king] - 23)*q; // reduce by ~half if Lion very far away
\r
1659 Surround (int stm, int king, int start, int max)
\r
1662 for(i=start; i<9; i++) {
\r
1663 int v, piece, sq = king + neighbors[i];
\r
1664 if((piece = board[sq]) == EDGE || !piece || piece&1^stm) continue;
\r
1665 if(p[piece].promoGain) continue;
\r
1666 v = p[piece].value;
\r
1667 s += -(v > 70) & v;
\r
1669 return (s > max ? max : s);
\r
1675 int lion = ABSENT, king;
\r
1676 if(p[side+2].value == LVAL) lion = p[side+2].pos;
\r
1677 if(lion == ABSENT && p[side+4].value == LVAL) lion = p[side+4].pos;
\r
1678 king = p[royal[1-side]].pos; if(king == ABSENT) king = p[royal[1-side]+1].pos;
\r
1679 return lion == ABSENT ? 0 : Fortress(side ? -BW : BW, king, lion);
\r
1683 Evaluate (int difEval)
\r
1685 int wLion = ABSENT, bLion = ABSENT, wKing, bKing, score=mobilityScore, f, i, j, max=512;
\r
1687 if(tsume) return difEval;
\r
1689 if(p[WHITE+2].value == LVAL) wLion = p[WHITE+2].pos;
\r
1690 if(p[BLACK+2].value == LVAL) bLion = p[BLACK+2].pos;
\r
1691 if(wLion == ABSENT && p[WHITE+4].value == LVAL) wLion = p[WHITE+4].pos;
\r
1692 if(bLion == ABSENT && p[BLACK+4].value == LVAL) bLion = p[BLACK+4].pos;
\r
1695 # define lionTrap (PST + PST_TRAP)
\r
1696 // penalty for Lion in enemy corner, when enemy Lion is nearby
\r
1697 if(wLion != ABSENT && bLion != ABSENT) { // both have a Lion
\r
1698 static int distFac[36] = { 0, 0, 10, 9, 8, 7, 5, 3, 1 };
\r
1699 score -= ( (1+9*!attacks[2*wLion+WHITE]) * lionTrap[BW*(BH-1)+BH-1-wLion]
\r
1700 - (1+9*!attacks[2*bLion+BLACK]) * lionTrap[bLion] ) * distFac[dist[wLion - bLion]];
\r
1704 // bonus if corner lances are protected by Lion-proof setup (FL + C/S)
\r
1705 if(bLion != ABSENT) {
\r
1706 if((p[board[BW+lr]].value == 320 || p[board[BW+lr]].value == 220) &&
\r
1707 p[board[ll+1]].value == 150 && p[board[ll+BW+2]].value == 100) score += 20 + 20*!p[board[ll]].range[2];
\r
1708 if((p[board[BW+lr]].value == 320 || p[board[BW+lr]].value == 220) &&
\r
1709 p[board[lr-1]].value == 150 && p[board[lr+BW-2]].value == 100) score += 20 + 20*!p[board[lr]].range[2];
\r
1711 if(wLion != ABSENT) {
\r
1712 if((p[board[ul-BW]].value == 320 || p[board[ul-BW]].value == 220) &&
\r
1713 p[board[ul+1]].value == 150 && p[board[ul-BW+2]].value == 100) score -= 20 + 20*!p[board[ul]].range[2];
\r
1714 if((p[board[ur-BW]].value == 320 || p[board[ur-BW]].value == 220) &&
\r
1715 p[board[ur-1]].value == 150 && p[board[ur-BW-2]].value == 100) score -= 20 + 20*!p[board[ur]].range[2];
\r
1721 // basic centralization in end-game (also facilitates bare-King mating)
\r
1722 wKing = p[royal[WHITE]].pos; if(wKing == ABSENT) wKing = p[royal[WHITE]+2].pos;
\r
1723 bKing = p[royal[BLACK]].pos; if(bKing == ABSENT) bKing = p[royal[BLACK]+2].pos;
\r
1724 if(filling < 32) {
\r
1725 int lead = (stm == WHITE ? difEval : -difEval);
\r
1726 score += (PST[PST_CENTER+wKing] - PST[PST_CENTER+bKing])*(32 - filling) >> 7;
\r
1727 if(lead > 100) score -= PST[PST_CENTER+bKing]*(32 - filling) >> 3; // white leads, drive black K to corner
\r
1728 if(lead < -100) score += PST[PST_CENTER+wKing]*(32 - filling) >> 3; // black leads, drive white K to corner
\r
1734 if(bLion != ABSENT) f += Fortress( BW, wKing, bLion);
\r
1735 if(wLion != ABSENT) f -= Fortress(-BW, bKing, wLion);
\r
1736 score += (filling < 192 ? f : f*(224 - filling) >> 5); // build up slowly
\r
1740 score += Surround(WHITE, wKing, 1, max) - Surround(BLACK, bKing, 1, max) >> 3;
\r
1746 // bonus for having Kylin in end-game, where it could promote to Lion
\r
1747 // depends on board population, defenders around zone entry and proximity to zone
\r
1748 if(filling < 128) {
\r
1750 if((wLion = kylin[WHITE]) && (sq = p[wLion].pos) != ABSENT) {
\r
1751 int anchor = sq - PST[5*BW*BH - 1 - sq]; // FIXME: PST_ZONDIST indexed backwards
\r
1752 score += (512 - Surround(BLACK, anchor, 0, 512))*(128 - filling)*PST[p[wLion].pst + sq] >> 15;
\r
1754 if((bLion = kylin[BLACK]) && (sq = p[bLion].pos) != ABSENT) {
\r
1755 int anchor = sq + PST[PST_ZONDIST + sq];
\r
1756 score -= (512 - Surround(WHITE, anchor, 0, 512))*(128 - filling)*PST[p[bLion].pst + sq] >> 15;
\r
1762 // penalty for blocking own P or GB: 20 by slider, 10 by other, but 50 if only RETRACT mode is straight back
\r
1763 for(i=last[WHITE]; i > 1 && p[i].value<=50; i-=2) {
\r
1764 if((f = p[i].pos) != ABSENT) { // P present,
\r
1765 if((j = board[f + BW])&1) // square before it white (odd) piece
\r
1766 score -= 10 + 10*(p[j].promoGain > 0) + 30*!(p[j].range[3] || p[j].range[5] || p[j].value==50);
\r
1767 if((j = board[f - BW])&1) // square behind it white (odd) piece
\r
1768 score += 7*(p[j].promoGain == 0 & p[j].value<=151);
\r
1771 for(i=last[BLACK]; i > 1 && p[i].value<=50; i-=2) {
\r
1772 if((f = p[i].pos) != ABSENT) { // P present,
\r
1773 if((j = board[f - BW]) && !(j&1)) // square before non-empty and even (black)
\r
1774 score += 10 + 10*(p[j].promoGain > 0) + 30*!(p[j].range[1] || p[j].range[7] || p[j].value==50);
\r
1775 if((j = board[f + BW]) && !(j&1)) // square behind non-empty and even (black)
\r
1776 score -= 7*(p[j].promoGain == 0 & p[j].value<=151);
\r
1783 int rw = BW*(BH-1-zone), rb = BW*zone, h=0;
\r
1784 for(f=0; f<BH; f++) {
\r
1785 if(p[board[rw+f]].pst == PST_ADVANCE) {
\r
1786 h += (p[board[rw+f-BW]].pst == PST_ADVANCE);
\r
1787 if(f > 0) h += (p[board[rw+f-BW-1]].pst == PST_ADVANCE);
\r
1788 if(f+1 < BH) h += (p[board[rw+f-BW+1]].pst == PST_ADVANCE);
\r
1790 if(p[board[rb+f]].pst == PST_ADVANCE) {
\r
1791 h -= (p[board[rb+f+BW]].pst == PST_RETRACT);
\r
1792 if(f > 0) h -= (p[board[rb+f+BW-1]].pst == PST_RETRACT);
\r
1793 if(f+1 < BH) h -= (p[board[rb+f+BW+1]].pst == PST_RETRACT);
\r
1796 score += h*TANDEM;
\r
1800 return difEval - (filling*promoDelta >> 8) + (stm ? score : -score);
\r
1804 FireSet (UndoInfo *tb)
\r
1805 { // set fireFlags acording to remaining presene of Fire Demons
\r
1807 for(i=stm+2; p[i].value == FVAL; i++) // Fire Demons are always leading pieces in list
\r
1808 if(p[i].pos != ABSENT) tb->fireMask |= fireFlags[i-2];
\r
1811 void TerminationCheck();
\r
1816 Search (int alpha, int beta, int difEval, int depth, int lmr, int oldPromo, int promoSuppress, int threshold)
\r
1818 int i, j, k, phase, king, nextVictim, to, defer, autoFail=0, inCheck = 0, late=100000, ep;
\r
1819 int firstMove, oldMSP = msp, curMove, sorted, bad, dubious, bestMoveNr;
\r
1820 int resDep, iterDep, ext;
\r
1822 int score, bestScore, oldBest, curEval, iterAlpha;
\r
1823 Move move, nullMove;
\r
1826 Move hashMove; int index, nr, hit;
\r
1828 if(PATH) /*pboard(board),pmap(attacks, BLACK),*/printf("search(%d) {%d,%d} eval=%d, stm=%d (flag=%d)\n",depth,alpha,beta,difEval,stm,abortFlag),fflush(stdout);
\r
1829 xstm = stm ^ WHITE;
\r
1830 //printf("map made\n");fflush(stdout);
\r
1832 // in-check test and TSUME filter
\r
1834 k = p[king=royal[stm]].pos;
\r
1835 if( k == ABSENT) {
\r
1836 if((k = p[king + 2].pos) == ABSENT && (!tsume || tsume & stm+1))
\r
1837 return -INF; // lose when no King (in tsume only for side to be mated)
\r
1838 } else if(p[king + 2].pos != ABSENT) {
\r
1839 if(tsume && tsume & stm+1) {
\r
1840 retDep = 60; return INF; // we win when not in check
\r
1842 k = ABSENT; // two kings is no king...
\r
1844 if( k != ABSENT) { // check is possible
\r
1845 if(!attacks[2*k + xstm]) {
\r
1846 if(tsume && tsume & stm+1) {
\r
1847 retDep = 60; return INF; // we win when not in check
\r
1851 else { inCheck = 1; if(depth >= QSdepth) depth++; }
\r
1856 if(!level) {for(i=0; i<5; i++)printf("# %d %08x, %d\n", i, repStack[200-i], checkStack[200-i]);}
\r
1858 k = p[king=royal[xstm]].pos;
\r
1859 if( k != ABSENT) {
\r
1860 if(attacks[2*k + stm]) {
\r
1861 if( p[king + 2].pos == ABSENT ) return INF; // we have an attack on his only King
\r
1863 } else { // he has no king! Test for attacks on Crown Prince
\r
1864 k = p[king + 2].pos;
\r
1865 if(k == ABSENT ? !tsume : attacks[2*k + stm]) return INF; // we have attack on Crown Prince
\r
1867 //printf("King safe\n");fflush(stdout);
\r
1868 // EVALUATION & WINDOW SHIFT
\r
1869 curEval = Evaluate(difEval) -20*inCheck;
\r
1870 alpha -= (alpha < curEval);
\r
1871 beta -= (beta <= curEval);
\r
1873 if(!(nodes++ & 4095)) TerminationCheck();
\r
1874 pv[pvPtr++] = 0; // start empty PV, directly behind PV of parent
\r
1875 if(inCheck) lmr = 0; else depth -= lmr; // no LMR of checking moves
\r
1877 firstMove = curMove = sorted = msp += 50; // leave 50 empty slots in front of move list
\r
1878 iterDep = -(depth == 0); tb.fireMask = phase = 0;
\r
1881 index = hashKeyL ^ 327*stm ^ (oldPromo + 987981)*(63121 + promoSuppress);
\r
1882 index = index + (index >> 16) & hashMask;
\r
1883 nr = (hashKeyL >> 30) & 3; hit = -1;
\r
1884 if(hashTable[index].lock[nr] == hashKeyH) hit = nr; else
\r
1885 if(hashTable[index].lock[4] == hashKeyH) hit = 4;
\r
1886 if(PATH) printf("# probe hash index=%x hit=%d\n", index, hit),fflush(stdout);
\r
1888 bestScore = hashTable[index].score[hit];
\r
1889 hashMove = hashTable[index].move[hit];
\r
1890 if((bestScore <= alpha || hashTable[index].flag[hit] & H_LOWER) &&
\r
1891 (bestScore >= beta || hashTable[index].flag[hit] & H_UPPER) ) {
\r
1892 iterDep = resDep = hashTable[index].depth[hit]; bestMoveNr = 0;
\r
1893 if(!level) iterDep = 0; // no hash cutoff in root
\r
1894 if(lmr && bestScore <= alpha && iterDep == depth) depth ++, lmr--; // self-deepening LMR
\r
1895 if(pvCuts && iterDep >= depth && hashMove && bestScore < beta && bestScore > alpha)
\r
1896 iterDep = depth - 1; // prevent hash cut in PV node
\r
1898 } else { // decide on replacement
\r
1899 if(depth >= hashTable[index].depth[nr] ||
\r
1900 depth+1 == hashTable[index].depth[nr] && !(nodes&3)) hit = nr; else hit = 4;
\r
1903 if(PATH) printf("# iterDep = %d score = %d move = %s\n",iterDep,bestScore,MoveToText(hashMove,0)),fflush(stdout);
\r
1906 if(depth > QSdepth && iterDep < QSdepth) iterDep = QSdepth; // full-width: start at least from 1-ply
\r
1907 if(PATH)printf("iterDep=%d\n", iterDep);
\r
1908 while(++iterDep <= depth) {
\r
1909 if(flag && depth>= 0) printf("iter %d:%d\n", depth,iterDep),fflush(stdout);
\r
1910 oldBest = bestScore;
\r
1911 iterAlpha = alpha; bestScore = -INF; bestMoveNr = 0; resDep = 60;
\r
1912 if(PATH)printf("new iter %d\n", iterDep);
\r
1913 if(depth <= QSdepth) {
\r
1914 bestScore = curEval; resDep = QSdepth;
\r
1915 if(bestScore > alpha) {
\r
1916 alpha = bestScore;
\r
1917 if(PATH)printf("stand pat %d\n", bestScore);
\r
1918 if(bestScore >= beta) goto cutoff;
\r
1921 for(curMove = firstMove; ; curMove++) { // loop over moves
\r
1922 if(flag && depth>= 0) printf("phase=%d: first/curr/last = %d / %d / %d\n", phase, firstMove, curMove, msp);fflush(stdout);
\r
1924 if(curMove >= msp) { // we ran out of moves; generate some new
\r
1925 if(PATH)printf("new moves, phase=%d\n", phase);
\r
1927 case 0: // null move
\r
1929 if(depth > QSdepth && curEval >= beta && !inCheck && filling > 10) {
\r
1930 int nullDep = depth - 3;
\r
1932 score = -Search(-beta, 1-beta, -difEval, nullDep<QSdepth ? QSdepth : nullDep, 0, promoSuppress & SQUARE, ABSENT, INF);
\r
1933 xstm = stm; stm ^= WHITE;
\r
1934 if(score >= beta) { msp = oldMSP; retDep += 3; pvPtr = myPV; return score + (score < curEval); }
\r
1935 // else depth += lmr, lmr = 0;
\r
1938 if(tenFlag) FireSet(&tb); // in tenjiku we must identify opposing Fire Demons to perform any moves
\r
1939 //if(PATH) printf("mask=%x\n",tb.fireMask),pbytes(fireBoard);
\r
1941 case 1: // hash move
\r
1944 if(hashMove && (depth > QSdepth || // must be capture in QS
\r
1945 (hashMove & SQUARE) >= SPECIAL || board[hashMove & SQUARE] != EMPTY)) {
\r
1946 moveStack[sorted = msp++] = hashMove;
\r
1950 case 2: // capture-gen init
\r
1951 nextVictim = xstm; autoFail = (depth == 0);
\r
1953 case 3: // generate captures
\r
1954 if(PATH) printf("%d:%2d:%2d next victim %d/%d\n",level,depth,iterDep,curMove,msp);
\r
1955 while(nextVictim < last[xstm]) { // more victims exist
\r
1956 int group, to = p[nextVictim += 2].pos; // take next
\r
1957 if(to == ABSENT) continue; // ignore if absent
\r
1958 if(!attacks[2*to + stm]) continue; // skip if not attacked
\r
1959 group = p[nextVictim].value; // remember value of this found victim
\r
1960 if(iterDep <= QSdepth + 1 && 2*group + curEval + 30 < alpha) {
\r
1961 resDep = QSdepth + 1; nextVictim -= 2;
\r
1962 if(bestScore < 2*group + curEval + 30) bestScore = 2*group + curEval + 30;
\r
1965 if(PATH) printf("%d:%2d:%2d group=%d, to=%c%d\n",level,depth,iterDep,group,to%BW+'a',to/BW+ONE);
\r
1967 if(PATH) printf("%d:%2d:%2d first=%d msp=%d\n",level,depth,iterDep,firstMove,msp);
\r
1968 while(nextVictim < last[xstm] && p[nextVictim+2].value == group) { // more victims of same value exist
\r
1969 to = p[nextVictim += 2].pos; // take next
\r
1970 if(to == ABSENT) continue; // ignore if absent
\r
1971 if(!attacks[2*to + stm]) continue; // skip if not attacked
\r
1972 if(PATH) printf("%d:%2d:%2d p=%d, to=%c%d\n",level,depth,iterDep,nextVictim,to%BW+'a',to/BW+ONE);
\r
1974 if(PATH) printf("%d:%2d:%2d msp=%d\n",level,depth,iterDep,msp);
\r
1976 if(PATH) printf("captures on %d generated, msp=%d, group=%d, threshold=%d\n", nextVictim, msp, group, threshold);
\r
1977 goto extractMove; // in auto-fail phase, only search if they might auto-fail-hi
\r
1979 if(PATH) printf("# autofail=%d\n", autoFail);
\r
1980 if(autoFail) { // non-captures cannot auto-fail; flush queued captures first
\r
1981 if(PATH) printf("# autofail end (%d-%d)\n", firstMove, msp);
\r
1982 autoFail = 0; curMove = firstMove - 1; continue; // release stashed moves for search
\r
1984 phase = 4; // out of victims: all captures generated
\r
1985 if(chessFlag && (ep = promoSuppress & SQUARE) != ABSENT) { // e.p. rights. Create e.p. captures as Lion moves
\r
1986 int n = board[ep-1], old = msp; // a-side neighbor of pushed pawn
\r
1987 if( n != EMPTY && (n&TYPE) == stm && p[n].value == pVal ) NewCapture(ep-1, SPECIAL + 20 - 4*stm, 0);
\r
1988 n = board[ep+1]; // h-side neighbor of pushed pawn
\r
1989 if( n != EMPTY && (n&TYPE) == stm && p[n].value == pVal ) NewCapture(ep+1, SPECIAL + 52 - 4*stm, 0);
\r
1990 if(msp != old) goto extractMove; // one or more e.p. capture were generated
\r
1992 case 4: // dubious captures
\r
1994 while( dubious < framePtr + 250 ) // add dubious captures back to move stack
\r
1995 moveStack[msp++] = moveStack[dubious++];
\r
1996 if(curMove != msp) break;
\r
1999 case 5: // killers
\r
2000 if(depth <= QSdepth) { if(resDep > QSdepth) resDep = QSdepth; goto cutoff; }
\r
2002 case 6: // non-captures
\r
2004 nullMove = GenNonCapts(oldPromo);
\r
2005 if(msp == nonCapts) goto cutoff;
\r
2007 { // swap killers to front
\r
2008 Move h = killer[level][0]; int j = curMove;
\r
2009 for(i=curMove; i<msp; i++) if(moveStack[i] == h) { moveStack[i] = moveStack[j]; moveStack[j++] = h; break; }
\r
2010 h = killer[level][1];
\r
2011 for(i=curMove; i<msp; i++) if(moveStack[i] == h) { moveStack[i] = moveStack[j]; moveStack[j++] = h; break; }
\r
2018 sorted = msp; // do not sort noncapts
\r
2020 case 7: // bad captures
\r
2021 case 8: // PV null move
\r
2023 if(PATH) printf("# null = %0x\n", nullMove);
\r
2024 if(nullMove != ABSENT) {
\r
2025 moveStack[msp++] = nullMove + (nullMove << SQLEN) | DEFER; // kludge: setting DEFER guarantees != 0, and has no effect
\r
2028 //printf("# %d. sqr = %08x null = %08x\n", msp, nullMove, moveStack[msp-1]);
\r
2034 // MOVE EXTRACTION
\r
2036 //if(flag & depth >= 0 || (PATH)) printf("%2d:%d extract %d/%d\n", depth, iterDep, curMove, msp);
\r
2037 if(curMove > sorted) {
\r
2038 move = moveStack[sorted=j=curMove];
\r
2039 for(i=curMove+1; i<msp; i++)
\r
2040 if(moveStack[i] > move) move = moveStack[j=i]; // search move with highest priority
\r
2041 moveStack[j] = moveStack[curMove]; moveStack[curMove] = move; // swap highest-priority move to front of remaining
\r
2042 if(move == 0) { msp = curMove--; continue; } // all remaining moves must be 0; clip off move list
\r
2044 move = moveStack[curMove];
\r
2045 if(move == 0) continue; // skip invalidated move
\r
2047 if(flag & depth >= 0) printf("%2d:%d found %d/%d %08x %s\n", depth, iterDep, curMove, msp, moveStack[curMove], MoveToText(moveStack[curMove], 0));
\r
2051 defer = MakeMove(move, &tb);
\r
2052 ext = (depth == 0); // when out of depth we extend captures if there was no auto-fail-hi
\r
2054 // if(level == 1 && randomize) tb.booty += (hashKeyH * seed >> 24 & 31) - 20;
\r
2057 UnMake(&tb); // never search moves during auto-fail phase
\r
2058 xstm = stm; stm ^= WHITE;
\r
2059 if(tb.gain <= threshold) { // we got to moves that cannot possibly auto-fail
\r
2060 autoFail = 0; curMove = firstMove-1; continue; // release all for search
\r
2062 if(tb.gain - tb.loss > threshold) {
\r
2063 bestScore = INF+1; resDep = 0; goto leave; // auto-fail-hi
\r
2064 } else continue; // ignore for now if not obvious refutation
\r
2067 if(flag & depth >= 0) printf("%2d:%d made %d/%d %s\n", depth, iterDep, curMove, msp, MoveToText(moveStack[curMove], 0));
\r
2068 for(i=2; i<=cnt50; i+=2) if(repStack[level-i+200] == hashKeyH) {
\r
2070 if(repDraws) { score = 0; goto repetition; }
\r
2072 moveStack[curMove] = 0; // erase forbidden move
\r
2073 if(!level) repeatMove[repCnt++] = move & 0xFFFFFF; // remember outlawed move
\r
2074 } else { // check for perpetuals
\r
2075 // int repKey = 1;
\r
2076 // for(i-=level; i>1; i-=2) {repKey &= checkStack[200-i]; if(!level)printf("# repkey[%d] = %d\n", 200-i, repKey);}
\r
2077 if(inCheck) { score = INF-20; goto repetition; } // we might be subject to perpetual check: score as win
\r
2078 if(i == 2 && repStack[level+199] == hashKeyH) { score = INF-20; goto repetition; } // consecutive passing
\r
2080 score = -INF + 8*allowRep; goto repetition;
\r
2082 repStack[level+200] = hashKeyH;
\r
2084 if(PATH) printf("%d:%2d:%d %3d %6x %-10s %6d %6d\n", level, depth, iterDep, curMove, moveStack[curMove], MoveToText(moveStack[curMove], 0), score, bestScore),fflush(stdout);
\r
2085 path[level++] = move;
\r
2086 attacks += 2*bsize;
\r
2087 MapFromScratch(attacks); // for as long as incremental update does not work.
\r
2088 //if(flag & depth >= 0) printf("%2d:%d mapped %d/%d %s\n", depth, iterDep, curMove, msp, MoveToText(moveStack[curMove], 0));
\r
2089 //if(PATH) pmap(attacks, stm);
\r
2090 if(chuFlag && p[tb.victim].value == LVAL) {// verify legality of Lion capture in Chu Shogi
\r
2092 if(p[tb.piece].value == LVAL) { // Ln x Ln: can make Ln 'vulnerable' (if distant and not through intemediate > GB)
\r
2093 if(dist[tb.from-tb.to] != 1 && attacks[2*tb.to + stm] && p[tb.epVictim[0]].value <= 50)
\r
2094 score = -INF; // our Lion is indeed made vulnerable and can be recaptured
\r
2095 } else { // other x Ln
\r
2096 if(promoSuppress & PROMOTE) { // non-Lion captures Lion after opponent did same
\r
2097 if(!okazaki || attacks[2*tb.to + stm]) score = -INF;
\r
2099 defer |= PROMOTE; // if we started, flag he cannot do it in reply
\r
2101 if(score == -INF) {
\r
2102 if(level == 1) repeatMove[repCnt++] = move & 0xFFFFFF | (p[tb.piece].value == LVAL ? 3<<24 : 1 << 24);
\r
2103 moveStack[curMove] = 0; // zap illegal moves
\r
2108 score = -Search(-beta, -iterAlpha, -difEval - tb.booty, iterDep-1+ext,
\r
2109 curMove >= late && iterDep > QSdepth + LMR,
\r
2110 promoSuppress & ~PROMOTE, defer, depth ? INF : tb.gain);
\r
2116 attacks -= 2*bsize;
\r
2120 xstm = stm; stm ^= WHITE;
\r
2121 if(abortFlag > 0) { // unwind search
\r
2122 printf("# abort (%d) @ %d\n", abortFlag, level);
\r
2123 if(curMove == firstMove) bestScore = oldBest, bestMoveNr = firstMove; // none searched yet
\r
2127 if(PATH) printf("%d:%2d:%d %3d %6x %-10s %6d %6d (%d)\n", level, depth, iterDep, curMove, moveStack[curMove], MoveToText(moveStack[curMove], 0), score, bestScore, GetTickCount()),fflush(stdout);
\r
2129 // ALPHA-BETA STUFF
\r
2130 if(score > bestScore) {
\r
2131 bestScore = score; bestMoveNr = curMove;
\r
2132 if(score > iterAlpha) {
\r
2133 iterAlpha = score;
\r
2134 if(curMove < firstMove + 5) { // if not too much work, sort move to front
\r
2136 for(i=curMove; i>firstMove; i--) {
\r
2137 moveStack[i] = moveStack[i-1];
\r
2139 moveStack[firstMove] = move;
\r
2140 } else { // late move appended in front of list, and leaves hole in list
\r
2141 moveStack[--firstMove] = move;
\r
2142 moveStack[curMove] = 0;
\r
2144 bestMoveNr = firstMove;
\r
2145 if(score >= beta) { // beta cutoff
\r
2147 if(iterDep == depth && move != killer[level][0]
\r
2148 && (tb.victim == EMPTY && (move & SQUARE) < SPECIAL)) {
\r
2150 killer[level][1] = killer[level][0]; killer[level][0] = move;
\r
2153 resDep = retDep+1-ext;
\r
2157 for(pvPtr = myPV+1; pv[pvPtr++] = pv[i++]; ); // copy daughter PV
\r
2158 pv[myPV] = move; // behind our move (pvPtr left at end of copy)
\r
2163 if(retDep+1-ext < resDep) resDep = retDep+1-ext;
\r
2167 if(!level) { // root node
\r
2168 lastRootIter = GetTickCount() - startTime;
\r
2169 if(postThinking > 0) {
\r
2170 int i; // WB thinking output
\r
2171 printf("%d %d %d %d", iterDep-QSdepth, bestScore, lastRootIter/10, nodes);
\r
2172 if(ponderMove) printf(" (%s)", MoveToText(ponderMove, 0));
\r
2173 for(i=0; pv[i]; i++) printf(" %s", MoveToText(pv[i], 0));
\r
2174 if(iterDep == QSdepth+1) printf(" { root eval = %4.2f dif = %4.2f; abs = %4.2f f=%d D=%4.2f %d/%d}", curEval/100., difEval/100., PSTest()/100., filling, promoDelta/100., Ftest(0), Ftest(1));
\r
2178 if(!(abortFlag & 1) && GetTickCount() - startTime > tlim1) break; // do not start iteration we can (most likely) not finish
\r
2180 if(resDep > iterDep) iterDep = resDep; // skip iterations if we got them for free
\r
2182 if(lmr && bestScore <= alpha && iterDep == depth)
\r
2183 depth++, lmr--; // self-deepen on fail-low reply to late move by lowering reduction
\r
2185 if(stalemate && bestScore == -INF && !inCheck) bestScore = 0; // stalemate
\r
2188 hashTable[index].lock[hit] = hashKeyH;
\r
2189 hashTable[index].depth[hit] = resDep;
\r
2190 hashTable[index].score[hit] = bestScore;
\r
2191 hashTable[index].flag[hit] = (bestScore < beta) * H_UPPER;
\r
2192 if(bestScore > alpha) {
\r
2193 hashTable[index].flag[hit] |= H_LOWER;
\r
2194 hashTable[index].move[hit] = bestMoveNr ? moveStack[bestMoveNr] : 0;
\r
2195 } else hashTable[index].move[hit] = 0;
\r
2200 retFirst = firstMove;
\r
2201 msp = oldMSP; // pop move list
\r
2202 pvPtr = myPV; // pop PV
\r
2203 retMove = bestMoveNr ? moveStack[bestMoveNr] : 0;
\r
2204 retDep = resDep - (inCheck & depth >= QSdepth) + lmr;
\r
2205 if(PATH) printf("return %d: %d %d (t=%d s=%d lim=%d)\n", depth, bestScore, curEval, GetTickCount(), startTime, tlim1),fflush(stdout);
\r
2206 return bestScore + (bestScore < curEval);
\r
2213 for(i=0; i<182; i++) {
\r
2214 printf("%3d. %3d %3d %4d %02x %d %d %x %3d %4d ", i, p[i].value, p[i].promo, p[i].pos, p[i].promoFlag&255, p[i].mobWeight, p[i].qval, p[i].bulk, p[i].promoGain, p[i].pst);
\r
2215 for(j=0; j<8; j++) printf(" %2d", p[i].range[j]);
\r
2216 if(i<2 || i>11) printf("\n"); else printf(" %02x\n", fireFlags[i-2]&255);
\r
2218 printf("last: %d / %d\nroyal %d / %d\n", last[WHITE], last[BLACK], royal[WHITE], royal[BLACK]);
\r
2225 for(i=BH+2; i>-4; i--) {
\r
2227 for(j=-3; j<BH+3; j++) printf("%4d", b[BW*i+j]);
\r
2233 pbytes (unsigned char *b)
\r
2236 for(i=BH-1; i>=0; i--) {
\r
2237 for(j=0; j<BH; j++) printf("%3x", b[BW*i+j]);
\r
2244 pmap (int *m, int col)
\r
2247 for(i=BH-1; i>=0; i--) {
\r
2249 for(j=0; j<BH; j++) printf("%10o", m[2*(BW*i+j)+col]);
\r
2255 pmoves(int start, int end)
\r
2258 printf("# move stack from %d to %d\n", start, end);
\r
2259 for(i=start; i<end; i++) {
\r
2261 f = m>>SQLEN & SQUARE;
\r
2263 printf("# %3d. %08x %3d-%3d %s\n", i, m, f, t, MoveToText(m, 0));
\r
2267 /********************************************************/
\r
2268 /* Example of a WinBoard-protocol driver, by H.G.Muller */
\r
2269 /********************************************************/
\r
2271 #include <stdio.h>
\r
2273 // four different constants, with values for WHITE and BLACK that suit your engine
\r
2277 // some value that cannot occur as a valid move
\r
2280 // some parameter of your engine
\r
2281 #define MAXMOVES 2000 /* maximum game length */
\r
2282 #define MAXPLY 60 /* maximum search depth */
\r
2287 typedef Move MOVE;
\r
2289 int moveNr; // part of game state; incremented by MakeMove
\r
2290 MOVE gameMove[MAXMOVES]; // holds the game history
\r
2292 // Some routines your engine should have to do the various essential things
\r
2293 int MakeMove2(int stm, MOVE move); // performs move, and returns new side to move
\r
2294 void UnMake2(MOVE move); // unmakes the move;
\r
2295 int Setup2(char *fen); // sets up the position from the given FEN, and returns the new side to move
\r
2296 void SetMemorySize(int n); // if n is different from last time, resize all tables to make memory usage below n MB
\r
2297 char *MoveToText(MOVE move, int m); // converts the move from your internal format to text like e2e2, e1g1, a7a8q.
\r
2298 MOVE ParseMove(char *moveText); // converts a long-algebraic text move to your internal move format
\r
2299 int SearchBestMove(MOVE *move, MOVE *ponderMove);
\r
2300 void PonderUntilInput(int stm); // Search current position for stm, deepening forever until there is input.
\r
2302 UndoInfo undoInfo;
\r
2303 int sup0, sup1, sup2; // promo suppression squares
\r
2304 int lastLift, lastPut;
\r
2309 int k = p[royal[stm]].pos;
\r
2310 if( k == ABSENT) k = p[royal[stm] + 2].pos;
\r
2311 else if(p[royal[stm] + 2].pos != ABSENT) k = ABSENT; // two kings is no king...
\r
2312 if( k != ABSENT) {
\r
2313 MapFromScratch(attacks);
\r
2314 if(attacks[2*k + 1 - stm]) return 1;
\r
2320 MakeMove2 (int stm, MOVE move)
\r
2322 int i, inCheck = InCheck();
\r
2323 FireSet(&undoInfo);
\r
2324 sup0 = sup1; sup1 = sup2;
\r
2325 sup2 = MakeMove(move, &undoInfo);
\r
2326 if(chuFlag && p[undoInfo.victim].value == LVAL && p[undoInfo.piece].value != LVAL) sup2 |= PROMOTE;
\r
2327 rootEval = -rootEval - undoInfo.booty;
\r
2328 for(i=0; i<200; i++) repStack[i] = repStack[i+1], checkStack[i] = checkStack[i+1];
\r
2329 repStack[199] = hashKeyH, checkStack[199] = inCheck;
\r
2330 printf("# makemove %08x %c%d %c%d\n", move, sup1%BW+'a', sup1/BW, sup2%BW+'a', sup2/BW);
\r
2331 return stm ^ WHITE;
\r
2335 UnMake2 (MOVE move)
\r
2338 rootEval = -rootEval - undoInfo.booty;
\r
2339 UnMake(&undoInfo);
\r
2340 for(i=200; i>0; i--) repStack[i] = repStack[i-1], checkStack[i] = checkStack[i-1];
\r
2341 sup2 = sup1; sup1 = sup0;
\r
2344 char fenNames[] = "RV....DKDEFL..DHGB......SMLNKN..FK....BT..VM..PH..LN"; // pairs of char
\r
2345 char fenPromo[] = "WLDHSMSECPB R HFDE....WHFB..LNG ..DKVMFS..FO..FK...."; // pairs of char
\r
2348 Convert (char *fen)
\r
2350 char *p = fenArray, *q, *rows[36], tmp[4000];
\r
2352 printf("# convert FEN '%s'\n", fen);
\r
2353 q = strchr(fen, ' '); if(q) *q = 0; q = fen;
\r
2354 do { rows[n++] = q; q = strchr(q, '/'); if(!q) break; *q++ = 0; } while(1);
\r
2356 while(--n >= 0) { strcat(tmp, rows[n]); if(n) strcat(tmp, "/"); }
\r
2358 printf("# flipped FEN '%s'\n", fen);
\r
2360 if(*fen == ' ') { *p = 0; break; }
\r
2361 if(n=atoi(fen)) fen++; // digits read
\r
2362 if(n > 9) fen++; // double digit
\r
2363 while(n-- > 0) *p++ = '.'; // expand to empty squares
\r
2364 if(currentVariant == V_LION && (*fen == 'L' || *fen == 'l')) *fen += 'Z' - 'L'; // L in Mighty-Lion Chess changed in Z for Lion
\r
2365 if(isalpha(*fen)) {
\r
2366 char *table = fenNames;
\r
2367 n = *fen > 'Z' ? 'a' - 'A' : 0;
\r
2368 if((currentVariant == V_CHESS || currentVariant == V_SHATRANJ || currentVariant == V_LION ||
\r
2369 currentVariant == V_MAKRUK || currentVariant == V_SHO) && *fen - n == 'N' // In Chess N is Knight, not Lion
\r
2370 || table[2* (*fen - 'A' - n)] == '.') *p++ = *fen; else {
\r
2372 *p++ = table[2* (*fen - 'A' - n)] + n;
\r
2373 *p++ = table[2* (*fen - 'A' - n)+1] + n;
\r
2375 } else *p++ = *fen;
\r
2380 printf("# converted FEN '%s'\n", fenArray);
\r
2385 Setup2 (char *fen)
\r
2389 char *q = strchr(fen, '\n');
\r
2391 if(q = strchr(fen, ' ')) stm = (q[1] == 'b' ? BLACK : WHITE); // fen contains color field
\r
2392 if(strchr(fen, '.') || strchr(fen, ':')) array = fen; else array = Convert(fen);
\r
2394 rootEval = promoDelta = filling = cnt50 = moveNr = 0;
\r
2395 SetUp(array, currentVariant);
\r
2396 strcpy(startPos, array);
\r
2397 sup0 = sup1 = sup2 = ABSENT;
\r
2398 hashKeyH = hashKeyL = 87620895*currentVariant + !!fen;
\r
2403 SetMemorySize (int n)
\r
2406 static HashBucket *realHash;
\r
2407 static intptr_t oldSize;
\r
2408 intptr_t l, m = 1;
\r
2409 while(m*sizeof(HashBucket) <= n*512UL) m <<= 1; // take largest power-of-2 that fits
\r
2410 if(m != oldSize) {
\r
2411 if(oldSize) free(realHash);
\r
2412 hashMask = m*1024 - 1; oldSize = m;
\r
2413 realHash = malloc(m*1024*sizeof(HashBucket) + 64);
\r
2414 l = (intptr_t) realHash; hashTable = (HashBucket*) (l & ~63UL); // align with cache line
\r
2420 MoveToText (MOVE move, int multiLine)
\r
2422 static char buf[50];
\r
2423 int f = move>>SQLEN & SQUARE, g = f, t = move & SQUARE;
\r
2424 char *promoChar = "";
\r
2425 if(f == t) { sprintf(buf, "@@@@"); return buf; } // null-move notation in WB protocol
\r
2427 if(t >= SPECIAL) {
\r
2428 if(t < CASTLE) { // castling is printed as a single move, implying its side effects
\r
2429 int e = f + epList[t - SPECIAL];
\r
2430 // printf("take %c%d\n", e%BW+'a', e/BW+ONE);
\r
2431 sprintf(buf, "%c%d%c%d,", f%BW+'a', f/BW+ONE, e%BW+'a', e/BW+ONE); f = e;
\r
2432 if(multiLine) printf("move %s\n", buf), buf[0] = '\0';
\r
2433 if(ep2List[t - SPECIAL]) {
\r
2434 e = g + ep2List[t - SPECIAL];
\r
2435 // printf("take %c%d\n", e%BW+'a', e/BW+ONE);
\r
2436 sprintf(buf+strlen(buf), "%c%d%c%d,", f%BW+'a', f/BW+ONE, e%BW+'a', e/BW+ONE); f = e;
\r
2437 if(multiLine) printf("move %s\n", buf), buf[0] = '\0';
\r
2440 t = g + toList[t - SPECIAL];
\r
2442 if(move & PROMOTE) promoChar = currentVariant == V_MAKRUK ? "m" : repDraws ? "q" : "+";
\r
2443 sprintf(buf+strlen(buf), "%c%d%c%d%s", f%BW+'a', f/BW+ONE, t%BW+'a', t/BW+ONE, promoChar);
\r
2448 ReadSquare (char *p, int *sqr)
\r
2452 r = atoi(p + 1) - ONE;
\r
2454 return 2 + (r + ONE > 9);
\r
2457 int listStart, listEnd;
\r
2458 char boardCopy[BSIZE];
\r
2462 { // create move list on move stack
\r
2464 for(i=0; i< BSIZE; i++) boardCopy[i] = !!board[i];
\r
2465 MapFromScratch(attacks);
\r
2466 postThinking--; repCnt = 0; tlim1 = tlim2 = tlim3 = 1e8; abortFlag = msp = 0;
\r
2467 Search(-INF-1, INF+1, 0, QSdepth+1, 0, sup1 & ~PROMOTE, sup2, INF);
\r
2470 listStart = retFirst; msp = retMSP;
\r
2471 if(currentVariant == V_LION) GenCastlings(); listEnd = msp;
\r
2475 ParseMove (char *moveText)
\r
2477 int i, j, f, t, t2, e, ret, deferred=0;
\r
2478 char c = moveText[0];
\r
2479 moveText += ReadSquare(moveText, &f);
\r
2480 moveText += ReadSquare(moveText, &t); t2 = t;
\r
2481 if(*moveText == ',') {
\r
2483 moveText += ReadSquare(moveText, &e);
\r
2484 if(e != t) return INVALID; // must continue with same piece
\r
2486 moveText += ReadSquare(moveText, &t);
\r
2487 for(i=0; i<8; i++) if(f + kStep[i] == e) break;
\r
2488 if(i >= 8) return INVALID; // this rejects Lion Dog 2+1 and 2-1 moves!
\r
2489 for(j=0; j<8; j++) if(e + kStep[j] == t) break;
\r
2490 if(j >= 8) return INVALID; // this rejects Lion Dog 1+2 moves!
\r
2491 t2 = SPECIAL + 8*i + j;
\r
2492 } else if(chessFlag && board[f] != EMPTY && p[board[f]].value == pVal && board[t] == EMPTY) { // Pawn to empty, could be e.p.
\r
2493 if(t == f + BW + 1) t2 = SPECIAL + 16; else
\r
2494 if(t == f + BW - 1) t2 = SPECIAL + 48; else
\r
2495 if(t == f - BW + 1) t2 = SPECIAL + 20; else
\r
2496 if(t == f - BW - 1) t2 = SPECIAL + 52; // fake double-move
\r
2497 } else if(currentVariant == V_LION && board[f] != EMPTY && p[board[f]].value == 280 && (t-f == 2 || f-t == 2)) { // castling
\r
2498 if(t == f+2 && f < BW) t2 = CASTLE; else
\r
2499 if(t == f-2 && f < BW) t2 = CASTLE + 1; else
\r
2500 if(t == f+2 && f > BW) t2 = CASTLE + 2; else
\r
2501 if(t == f-2 && f > BW) t2 = CASTLE + 3;
\r
2503 ret = f<<SQLEN | t2;
\r
2504 if(*moveText != '\n' && *moveText != '=') ret |= PROMOTE;
\r
2505 printf("# suppress = %c%d\n", sup1%BW+'a', sup1/BW);
\r
2507 for(i=listStart; i<listEnd; i++) {
\r
2508 if(moveStack[i] == INVALID) continue;
\r
2509 if(c == '@' && (moveStack[i] & SQUARE) == (moveStack[i] >> SQLEN & SQUARE)) break; // any null move matches @@@@
\r
2510 if((moveStack[i] & (PROMOTE | DEFER-1)) == ret) break;
\r
2511 if((moveStack[i] & DEFER-1) == ret) deferred = i; // promoted version of entered non-promotion is legal
\r
2513 printf("# moveNr = %d in {%d,%d}\n", i, listStart, listEnd);
\r
2514 if(i>=listEnd) { // no exact match
\r
2515 if(deferred) { // but maybe non-sensical deferral
\r
2516 int flags = p[board[f]].promoFlag;
\r
2517 printf("# deferral of %d\n", deferred);
\r
2518 i = deferred; // in any case we take that move
\r
2519 if(!(flags & promoBoard[t] & (CANT_DEFER | LAST_RANK))) { // but change it into a deferral if that is allowed
\r
2520 moveStack[i] &= ~PROMOTE;
\r
2521 if(p[board[f]].value == 40) p[board[f]].promoFlag &= LAST_RANK; else
\r
2522 if(!(flags & promoBoard[f])) moveStack[i] |= DEFER; // came from outside zone, so essential deferral
\r
2525 if(i >= listEnd) {
\r
2526 for(i=listStart; i<listEnd; i++) printf("# %d. %08x %08x %s\n", i-50, moveStack[i], ret, MoveToText(moveStack[i], 0));
\r
2528 for(i=0; i<repCnt; i++) {if((repeatMove[i] & 0xFFFFFF) == ret) {
\r
2529 if(repeatMove[i] & 1<<24) reason = (repeatMove[i] & 1<<25 ? "Distant capture of protected Lion" : "Counterstrike against Lion");
\r
2530 else reason = "Repeats earlier position";
\r
2533 printf("# %d. %08x %08x %s\n", i, repeatMove[i], ret, MoveToText(repeatMove[i], 0));
\r
2537 return (i >= listEnd ? INVALID : moveStack[i]);
\r
2541 Highlight(char *coords)
\r
2543 int i, j, n, sqr, cnt=0;
\r
2544 char b[BSIZE], buf[2000], *q;
\r
2545 for(i=0; i<bsize; i++) b[i] = 0;
\r
2546 ReadSquare(coords, &sqr);
\r
2548 for(i=listStart; i<listEnd; i++) {
\r
2549 if(sqr == (moveStack[i]>>SQLEN & SQUARE)) {
\r
2550 int t = moveStack[i] & SQUARE;
\r
2551 if(t >= CASTLE) t = toList[t - SPECIAL]; else // decode castling
\r
2552 if(t >= SPECIAL) {
\r
2553 int e = sqr + epList[t - SPECIAL]; // decode
\r
2557 if(!b[t]) b[t] = (!boardCopy[t] ? 'Y' : 'R'); cnt++;
\r
2558 if(moveStack[i] & PROMOTE) b[t] = 'M';
\r
2561 if(!cnt) { // no moves from given square
\r
2562 if(sqr != lastPut) return; // refrain from sending empty FEN
\r
2563 // we lifted a piece for second leg of move
\r
2564 for(i=listStart; i<listEnd; i++) {
\r
2565 if(lastLift == (moveStack[i]>>SQLEN & SQUARE)) {
\r
2566 int e, t = moveStack[i] & SQUARE;
\r
2567 if(t < SPECIAL) continue; // only special moves
\r
2568 e = lastLift + epList[t - SPECIAL]; // decode
\r
2569 t = lastLift + toList[t - SPECIAL];
\r
2570 if(e != sqr) continue;
\r
2571 b[t] = (!boardCopy[t] ? 'Y' : 'R'); cnt++;
\r
2575 } else lastLift = sqr; // remember
\r
2578 for(i=BH-1; i>=0; i--) {
\r
2579 for(j=0; j<BH; j++) {
\r
2581 if(b[n]) *q++ = b[n]; else {
\r
2582 if(q > buf && q[-1] <= '9' && q[-1] >= '0') {
\r
2585 if(q > buf+1 && q[-2] <= '9' && q[-2] >= '0') q[-2]++; else q[-1] = '1', *q++ = '0';
\r
2587 } else *q++ = '1';
\r
2593 printf("highlight %s\n", buf);
\r
2596 int timeLeft; // timeleft on engine's clock
\r
2597 int mps, timeControl, inc, timePerMove; // time-control parameters, to be used by Search
\r
2598 char inBuf[8000], command[80], ponderMoveText[20];
\r
2601 SetSearchTimes (int timeLeft)
\r
2603 int targetTime, movesLeft = BW*BH/4 + 20;
\r
2604 if(mps) movesLeft = mps - (moveNr>>1)%mps;
\r
2605 targetTime = (timeLeft - 1000*inc) / (movesLeft + 2) + 1000 * inc;
\r
2606 if(moveNr < 30) targetTime *= 0.5 + moveNr/60.; // speedup in opening
\r
2607 if(timePerMove > 0) targetTime = 0.4*timeLeft, movesLeft = 1;
\r
2608 tlim1 = 0.4*targetTime;
\r
2609 tlim2 = 2.4*targetTime;
\r
2610 tlim3 = 5*timeLeft / (movesLeft + 4.1);
\r
2611 printf("# limits %d, %d, %d mode = %d\n", tlim1, tlim2, tlim3, abortFlag);
\r
2615 SearchBestMove (MOVE *move, MOVE *ponderMove)
\r
2618 printf("# SearchBestMove\n");
\r
2619 startTime = GetTickCount();
\r
2621 printf("# s=%d\n", startTime);fflush(stdout);
\r
2622 MapFromScratch(attacks);
\r
2623 retMove = INVALID; repCnt = 0;
\r
2624 score = Search(-INF-1, INF+1, rootEval, maxDepth + QSdepth, 0, sup1, sup2, INF);
\r
2626 *ponderMove = pv[1];
\r
2627 printf("# best=%s\n", MoveToText(pv[0],0));
\r
2628 printf("# ponder=%s\n", MoveToText(pv[1],0));
\r
2633 int TakeBack(int n)
\r
2634 { // reset the game and then replay it to the desired point
\r
2636 last = moveNr - n; if(last < 0) last = 0;
\r
2637 Init(SAME); stm = Setup2(startPos);
\r
2638 printf("# setup done");fflush(stdout);
\r
2639 for(moveNr=0; moveNr<last; moveNr++) stm = MakeMove2(stm, gameMove[moveNr]),printf("make %2d: %x\n", moveNr, gameMove[moveNr]);
\r
2643 void PrintResult(int stm, int score)
\r
2646 if(reason) sprintf(tail, " {%s}", reason); else *tail = 0;
\r
2647 if(score == 0) printf("1/2-1/2%s\n", tail);
\r
2648 if(score > 0 && stm == WHITE || score < 0 && stm == BLACK) printf("1-0%s\n", tail);
\r
2649 else printf("0-1%s\n", tail);
\r
2652 void GetLine(int root)
\r
2657 // wait for input, and read it until we have collected a complete line
\r
2658 for(i = 0; (inBuf[i] = c = getchar()) != '\n'; i++) if(c == EOF || i>7997) exit(0);
\r
2661 // extract the first word
\r
2662 sscanf(inBuf, "%s", command);
\r
2663 printf("# in (mode = %d,%d): %s\n", root, abortFlag, command);
\r
2664 if(!strcmp(command, "otim")) { continue; } // do not start pondering after receiving time commands, as move will follow immediately
\r
2665 if(!strcmp(command, "time")) { sscanf(inBuf, "time %d", &timeLeft); continue; }
\r
2666 if(!strcmp(command, "put")) { ReadSquare(inBuf+4, &lastPut); continue; } // ditto
\r
2667 if(!strcmp(command, ".")) { inBuf[0] = 0; return; } // ignore for now
\r
2668 if(!strcmp(command, "hover")) { inBuf[0] = 0; return; } // ignore for now
\r
2669 if(!strcmp(command, "lift")) { inBuf[0] = 0; Highlight(inBuf+5); return; } // treat here
\r
2670 if(!root && !strcmp(command, "usermove")) {
\r
2671 printf("# move = %s#ponder = %s", inBuf+9, ponderMoveText);
\r
2672 abortFlag = !!strcmp(inBuf+9, ponderMoveText);
\r
2673 if(!abortFlag) { // ponder hit, continue as time-based search
\r
2674 printf("# ponder hit\n");
\r
2675 SetSearchTimes(10*timeLeft + GetTickCount() - startTime); // add time we already have been pondering to total
\r
2676 if(lastRootIter > tlim1) abortFlag = 2; // abort instantly if we are in iteration we should not have started
\r
2677 inBuf[0] = 0; ponderMove = INVALID;
\r
2687 TerminationCheck()
\r
2689 if(abortFlag < 0) { // check for input
\r
2690 if(InputWaiting()) GetLine(0); // read & examine input command
\r
2691 } else { // check for time
\r
2692 if(GetTickCount() - startTime > tlim3) abortFlag = 2;
\r
2699 int engineSide=NONE; // side played by engine
\r
2701 int i, score, curVarNr;
\r
2703 setvbuf(stdin, NULL, _IOLBF, 1024); // buffering more than one line flaws test for pending input!
\r
2705 Init(V_CHU); // Chu
\r
2706 seed = startTime = GetTickCount(); moveNr = 0; // initialize random
\r
2708 while(1) { // infinite loop
\r
2710 fflush(stdout); // make sure everything is printed before we do something that might take time
\r
2711 *inBuf = 0; if(moveNr >= 20) randomize = OFF;
\r
2712 //if(moveNr >20) printf("resign\n");
\r
2717 if(listEnd == 0) ListMoves(); // always maintain a list of legal moves in root position
\r
2718 abortFlag = -(ponder && WHITE+BLACK-stm == engineSide && moveNr); // pondering and opponent on move
\r
2719 if(stm == engineSide || abortFlag && ponderMove) { // if it is the engine's turn to move, set it thinking, and let it move
\r
2720 printf("# start search: stm=%d engine=%d (flag=%d)\n", stm, engineSide, abortFlag);
\r
2722 stm = MakeMove2(stm, ponderMove); // for pondering, play speculative move
\r
2723 gameMove[moveNr++] = ponderMove; // remember in game
\r
2724 sprintf(ponderMoveText, "%s\n", MoveToText(ponderMove, 0)); // for detecting ponder hits
\r
2725 printf("# ponder move = %s", ponderMoveText);
\r
2726 } else SetSearchTimes(10*timeLeft); // for thinking, schedule end time
\r
2728 score = SearchBestMove(&move, &ponderMove);
\r
2729 if(abortFlag == 1) { // ponder search was interrupted (and no hit)
\r
2730 UnMake2(INVALID); moveNr--; stm ^= WHITE; // take ponder move back if we made one
\r
2733 if(move == INVALID) { // game apparently ended
\r
2734 int kcapt = 0, xstm = stm ^ WHITE, king, k = p[king=royal[xstm]].pos;
\r
2735 if( k != ABSENT) { // test if King capture possible
\r
2736 if(attacks[2*k + stm]) {
\r
2737 if( p[king + 2].pos == ABSENT ) kcapt = 1; // we have an attack on his only King
\r
2739 } else { // he has no king! Test for attacks on Crown Prince
\r
2740 k = p[king + 2].pos;
\r
2741 if(attacks[2*k + stm]) kcapt = 1; // we have attack on Crown Prince
\r
2743 if(kcapt) { // print King capture before claiming
\r
2745 printf("move %s\n", MoveToText(moveStack[msp-1], 1));
\r
2746 reason = "king capture";
\r
2747 } else reason = "resign";
\r
2748 engineSide = NONE; // so stop playing
\r
2749 PrintResult(stm, score);
\r
2751 MOVE f, pMove = move;
\r
2752 if((move & SQUARE) >= SPECIAL && p[board[f = move>>SQLEN & SQUARE]].value == pVal) { // e.p. capture
\r
2753 pMove = move & ~SQUARE | f + toList[(move & SQUARE) - SPECIAL]; // print as a single move
\r
2755 stm = MakeMove2(stm, move); // assumes MakeMove returns new side to move
\r
2756 gameMove[moveNr++] = move; // remember game
\r
2757 printf("move %s\n", MoveToText(pMove, 1));
\r
2759 continue; // go check if we should ponder
\r
2762 if(engineSide == ANALYZE || abortFlag) { // in analysis, we always ponder the position
\r
2764 *ponderMoveText = 0; // forces miss on any move
\r
2765 abortFlag = -1; // set pondering
\r
2767 SearchBestMove(&dummy, &dummy);
\r
2768 abortFlag = pvCuts = 0;
\r
2771 fflush(stdout); // make sure everything is printed before we do something that might take time
\r
2772 if(!*inBuf) GetLine(1); // takes care of time and otim commands
\r
2774 // recognize the command,and execute it
\r
2775 if(!strcmp(command, "quit")) { break; } // breaks out of infinite loop
\r
2776 if(!strcmp(command, "force")) { engineSide = NONE; continue; }
\r
2777 if(!strcmp(command, "analyze")) { engineSide = ANALYZE; continue; }
\r
2778 if(!strcmp(command, "exit")) { engineSide = NONE; continue; }
\r
2779 if(!strcmp(command, "level")) {
\r
2781 sscanf(inBuf, "level %d %d %d", &mps, &min, &inc) == 3 || // if this does not work, it must be min:sec format
\r
2782 sscanf(inBuf, "level %d %d:%d %d", &mps, &min, &sec, &inc);
\r
2783 timeControl = 60*min + sec; timePerMove = -1;
\r
2786 if(!strcmp(command, "protover")){
\r
2787 for(i=0; variants[i].boardWidth; i++)
\r
2788 printf("%s%s", (i ? "," : "feature variants=\""), variants[i].name); printf("\"\n");
\r
2789 printf("feature ping=1 setboard=1 colors=0 usermove=1 memory=1 debug=1 sigint=0 sigterm=0\n");
\r
2790 printf("feature myname=\"HaChu " VERSION "\" highlight=1\n");
\r
2791 printf("feature option=\"Full analysis PV -check 1\"\n"); // example of an engine-defined option
\r
2792 printf("feature option=\"Allow repeats -check 0\"\n");
\r
2793 printf("feature option=\"Promote on entry -check 0\"\n");
\r
2794 printf("feature option=\"Okazaki rule -check 0\"\n");
\r
2795 printf("feature option=\"Resign -check 0\"\n"); //
\r
2796 printf("feature option=\"Contempt -spin 0 -200 200\"\n"); // and another one
\r
2797 printf("feature option=\"Tsume -combo no /// Sente mates /// Gote mates\"\n");
\r
2798 printf("feature done=1\n");
\r
2801 if(!strcmp(command, "option")) { // setting of engine-define option; find out which
\r
2802 if(sscanf(inBuf+7, "Full analysis PV=%d", &noCut) == 1) continue;
\r
2803 if(sscanf(inBuf+7, "Allow repeats=%d", &allowRep) == 1) continue;
\r
2804 if(sscanf(inBuf+7, "Resign=%d", &resign) == 1) continue;
\r
2805 if(sscanf(inBuf+7, "Contempt=%d", &contemptFactor) == 1) continue;
\r
2806 if(sscanf(inBuf+7, "Okazaki rule=%d", &okazaki) == 1) continue;
\r
2807 if(sscanf(inBuf+7, "Promote on entry=%d", &entryProm) == 1) continue;
\r
2808 if(sscanf(inBuf+7, "Tsume=%s", command) == 1) {
\r
2809 if(!strcmp(command, "no")) tsume = 0; else
\r
2810 if(!strcmp(command, "Sente")) tsume = 1; else
\r
2811 if(!strcmp(command, "Gote")) tsume = 2;
\r
2816 if(!strcmp(command, "sd")) { sscanf(inBuf, "sd %d", &maxDepth); continue; }
\r
2817 if(!strcmp(command, "st")) { sscanf(inBuf, "st %d", &timePerMove); continue; }
\r
2819 if(!strcmp(command, "memory")) { SetMemorySize(atoi(inBuf+7)); continue; }
\r
2820 if(!strcmp(command, "ping")) { printf("pong%s", inBuf+4); continue; }
\r
2821 // if(!strcmp(command, "")) { sscanf(inBuf, " %d", &); continue; }
\r
2822 if(!strcmp(command, "easy")) { ponder = OFF; continue; }
\r
2823 if(!strcmp(command, "hard")) { ponder = ON; continue; }
\r
2824 if(!strcmp(command, "go")) { engineSide = stm; continue; }
\r
2825 if(!strcmp(command, "post")) { postThinking = ON; continue; }
\r
2826 if(!strcmp(command, "nopost")) { postThinking = OFF;continue; }
\r
2827 if(!strcmp(command, "random")) { randomize = ON; continue; }
\r
2828 if(!strcmp(command, "hint")) { if(ponderMove != INVALID) printf("Hint: %s\n", MoveToText(ponderMove, 0)); continue; }
\r
2829 if(!strcmp(command, "book")) { continue; }
\r
2830 // non-standard commands
\r
2831 if(!strcmp(command, "p")) { pboard(board); continue; }
\r
2832 if(!strcmp(command, "f")) { pbytes(fireBoard); continue; }
\r
2833 if(!strcmp(command, "w")) { MapOneColor(WHITE, last[WHITE], attacks); pmap(attacks, WHITE); continue; }
\r
2834 if(!strcmp(command, "b")) { MapOneColor(BLACK, last[BLACK], attacks); pmap(attacks, BLACK); continue; }
\r
2835 if(!strcmp(command, "l")) { pplist(); continue; }
\r
2836 // ignored commands:
\r
2837 if(!strcmp(command, "xboard")) { continue; }
\r
2838 if(!strcmp(command, "computer")){ comp = 1; continue; }
\r
2839 if(!strcmp(command, "name")) { continue; }
\r
2840 if(!strcmp(command, "ics")) { continue; }
\r
2841 if(!strcmp(command, "accepted")){ continue; }
\r
2842 if(!strcmp(command, "rejected")){ continue; }
\r
2843 if(!strcmp(command, "result")) { engineSide = NONE; continue; }
\r
2844 if(!strcmp(command, "hover")) { continue; }
\r
2845 if(!strcmp(command, "")) { continue; }
\r
2846 if(!strcmp(command, "usermove")){
\r
2847 int move = ParseMove(inBuf+9);
\r
2849 if(move == INVALID) {
\r
2850 if(reason) printf("Illegal move {%s}\n", reason); else printf("%s\n", reason="Illegal move");
\r
2851 if(comp) PrintResult(stm, -INF); // against computer: claim
\r
2853 stm = MakeMove2(stm, move);
\r
2854 ponderMove = INVALID; listEnd = 0;
\r
2855 gameMove[moveNr++] = move; // remember game
\r
2859 ponderMove = INVALID; // the following commands change the position, invalidating ponder move
\r
2861 if(!strcmp(command, "new")) {
\r
2862 engineSide = BLACK; Init(V_CHESS); stm = Setup2(NULL); maxDepth = MAXPLY; randomize = OFF; curVarNr = comp = 0;
\r
2865 if(!strcmp(command, "variant")) {
\r
2866 for(i=0; variants[i].boardWidth; i++) {
\r
2867 sscanf(inBuf+8, "%s", command);
\r
2868 if(!strcmp(variants[i].name, command)) {
\r
2869 Init(curVarNr = i); stm = Setup2(NULL); break;
\r
2872 if(currentVariant == V_SHO)
\r
2873 printf("setup (PNBRLSE..G.+++++++Kpnbrlse..g.+++++++k) 9x9+0_shogi lnsgkgsnl/1r2e2b1/ppppppppp/9/9/9/PPPPPPPPP/1B2E2R1/LNSGKGSNL w 0 1\n");
\r
2874 repStack[199] = hashKeyH, checkStack[199] = 0;
\r
2877 if(!strcmp(command, "setboard")){ engineSide = NONE; Init(curVarNr); stm = Setup2(inBuf+9); continue; }
\r
2878 if(!strcmp(command, "undo")) { stm = TakeBack(1); continue; }
\r
2879 if(!strcmp(command, "remove")) { stm = TakeBack(2); continue; }
\r
2880 printf("Error: unknown command\n");
\r