1 /***********************************************************************/
\r
3 /* A WinBoard engine for large (dropless) Shogi variants by H.G.Muller */
\r
4 /* The engine is based on incremental updating of an attack map and */
\r
5 /* mobility scores, since the effort in this only grows proportional */
\r
6 /* to board edge length, rather than board area. */
\r
7 /***********************************************************************/
\r
10 // in GenCapts we do not generate jumps of more than two squares yet
\r
11 // promotions by pieces with Lion power stepping in & out the zone in same turn
\r
12 // promotion on capture
\r
14 #define VERSION "0.11 kill"
\r
16 #define PATH level==0 || path[0] == 0x848f1 && (level==1 /*|| path[1] == 0x3f081 && (level == 2 || path[2] == 0x6f0ac && (level == 3 || path[3] == 0x3e865 && (level == 4 || path[4] == 0x4b865 && (level == 5))))*/)
\r
32 # include <windows.h>
\r
34 { // checks for waiting input in pipe
\r
35 static int pipe, init;
\r
39 if(!init) inp = GetStdHandle(STD_INPUT_HANDLE);
\r
40 if(!PeekNamedPipe(inp, NULL, 0, NULL, &cnt, NULL)) return 1;
\r
44 # include <sys/time.h>
\r
45 # include <sys/ioctl.h>
\r
49 if(ioctl(0, FIONREAD, &cnt)) return 1;
\r
52 int GetTickCount() // with thanks to Tord
\r
54 gettimeofday(&t, NULL);
\r
55 return t.tv_sec*1000 + t.tv_usec/1000;
\r
62 #define BWMAX (2*BHMAX)
\r
63 #define BSIZE BWMAX*BHMAX
\r
66 #define ONE (currentVariant == V_SHO || currentVariant == V_CHESS || currentVariant == V_SHATRANJ || currentVariant == V_MAKRUK)
\r
71 #define EDGE (1<<11)
\r
72 #define TYPE (WHITE|BLACK|EDGE)
\r
75 #define NPIECES EDGE+1 /* length of piece list */
\r
77 #define SQLEN 11 /* bits in square number */
\r
78 #define SQUARE ((1<<SQLEN) - 1) /* mask for square in move */
\r
79 #define DEFER (1<<2*SQLEN) /* deferral on zone entry */
\r
80 #define PROMOTE (1<<2*SQLEN+1) /* promotion bit in move */
\r
81 #define SPECIAL 1400 /* start of special moves */
\r
82 #define BURN (SPECIAL + 96) /* start of burn encodings */
\r
83 #define STEP(X,Y) (BW*(X)+ (Y))
\r
84 #define SORTKEY(X) 0
\r
86 #define UPDATE_MOBILITY(X,Y)
\r
87 #define ADDMOVE(X,Y)
\r
90 #define CAN_PROMOTE 0x11
\r
91 #define DONT_DEFER 0x22
\r
92 #define CANT_DEFER 0x44
\r
93 #define LAST_RANK 0x88
\r
94 #define P_WHITE 0x0F
\r
95 #define P_BLACK 0xF0
\r
97 typedef unsigned int Move;
\r
99 char *MoveToText(Move move, int m); // from WB driver
\r
100 void pmap(int *m, int col);
\r
101 void pboard(int *b);
\r
102 void pbytes(unsigned char *b);
\r
107 short int score[5];
\r
113 HashBucket *hashTable;
\r
120 char *name, *promoted;
\r
122 signed char range[8];
\r
125 int whiteKey, blackKey;
\r
129 int from, to, piece, victim, new, booty, epSquare, epVictim[8], ep2Square, revMoveCount;
\r
130 int savKeyL, savKeyH, gain, loss, filling, saveDelta;
\r
134 char *array, fenArray[4000], *reason;
\r
135 int bWidth, bHeight, bsize, zone, currentVariant, chuFlag, tenFlag, chessFlag, repDraws, tsume;
\r
136 int stm, xstm, hashKeyH=1, hashKeyL=1, framePtr, msp, nonCapts, rootEval, filling, promoDelta;
\r
137 int retMSP, retFirst, retDep, pvPtr, level, cnt50, mobilityScore;
\r
138 int nodes, startTime, lastRootMove, lastRootIter, tlim1, tlim2, tlim3, repCnt, comp, abortFlag;
\r
140 Move retMove, moveStack[10000], path[100], repStack[300], pv[1000], repeatMove[300], killer[100][2];
\r
142 int maxDepth; // used by search
\r
144 #define X 36 /* slider */
\r
145 #define R 37 /* jump capture */
\r
146 #define N -1 /* Knight */
\r
147 #define J -2 /* jump */
\r
148 #define D -3 /* linear double move */
\r
149 #define T -4 /* linear triple move */
\r
150 #define L -5 /* true Lion move */
\r
151 #define F -6 /* Lion + 3-step */
\r
152 #define S -7 /* Lion + range */
\r
153 #define H -9 /* hook move */
\r
154 #define C -10 /* capture only */
\r
155 #define M -11 /* non-capture only */
\r
157 #define LVAL 100 /* piece value of Lion. Used in chu for recognizing it to implement Lion-trade rules */
\r
158 #define FVAL 500 /* piece value of Fire Demon. Used in code for recognizing moves with it and do burns */
\r
160 PieceDesc chuPieces[] = {
\r
161 {"LN", "", LVAL, { L,L,L,L,L,L,L,L }, 4 }, // lion
\r
162 {"FK", "", 60, { X,X,X,X,X,X,X,X }, 4 }, // free king
\r
163 {"SE", "", 55, { X,D,X,X,X,X,X,D }, 4 }, // soaring eagle
\r
164 {"HF", "", 50, { D,X,X,X,X,X,X,X }, 4 }, // horned falcon
\r
165 {"FO", "", 40, { X,X,0,X,X,X,0,X }, 4 }, // flying ox
\r
166 {"FB", "", 40, { 0,X,X,X,0,X,X,X }, 4 }, // free boar
\r
167 {"DK", "SE", 40, { X,1,X,1,X,1,X,1 }, 4 }, // dragon king
\r
168 {"DH", "HF", 35, { 1,X,1,X,1,X,1,X }, 4 }, // dragon horse
\r
169 {"WH", "", 35, { X,X,0,0,X,0,0,X }, 3 }, // white horse
\r
170 {"R", "DK", 30, { X,0,X,0,X,0,X,0 }, 4 }, // rook
\r
171 {"FS", "", 30, { X,1,1,1,X,1,1,1 }, 3 }, // flying stag
\r
172 {"WL", "", 25, { X,0,0,X,X,X,0,0 }, 4 }, // whale
\r
173 {"K", "", 28, { 1,1,1,1,1,1,1,1 }, 2, 4 }, // king
\r
174 {"CP", "", 27, { 1,1,1,1,1,1,1,1 }, 2, 4 }, // king
\r
175 {"B", "DH", 25, { 0,X,0,X,0,X,0,X }, 2 }, // bishop
\r
176 {"VM", "FO", 20, { X,0,1,0,X,0,1,0 }, 2 }, // vertical mover
\r
177 {"SM", "FB", 20, { 1,0,X,0,1,0,X,0 }, 6 }, // side mover
\r
178 {"DE", "CP", 20, { 1,1,1,1,0,1,1,1 }, 2 }, // drunk elephant
\r
179 {"BT", "FS", 15, { 0,1,1,1,1,1,1,1 }, 2 }, // blind tiger
\r
180 {"G", "R", 15, { 1,1,1,0,1,0,1,1 }, 2 }, // gold
\r
181 {"FL", "B", 15, { 1,1,0,1,1,1,0,1 }, 2 }, // ferocious leopard
\r
182 {"KN", "LN", 15, { J,1,J,1,J,1,J,1 }, 2 }, // kirin
\r
183 {"PH", "FK", 15, { 1,J,1,J,1,J,1,J }, 2 }, // phoenix
\r
184 {"RV", "WL", 15, { X,0,0,0,X,0,0,0 }, 1 }, // reverse chariot
\r
185 {"L", "WH", 15, { X,0,0,0,0,0,0,0 }, 1 }, // lance
\r
186 {"S", "VM", 10, { 1,1,0,1,0,1,0,1 }, 2 }, // silver
\r
187 {"C", "SM", 10, { 1,1,0,0,1,0,0,1 }, 2 }, // copper
\r
188 {"GB", "DE", 5, { 1,0,0,0,1,0,0,0 }, 1 }, // go between
\r
189 {"P", "G", 4, { 1,0,0,0,0,0,0,0 }, 2 }, // pawn
\r
190 { NULL } // sentinel
\r
193 PieceDesc shoPieces[] = {
\r
194 {"DK", "", 70, { X,1,X,1,X,1,X,1 } }, // dragon king
\r
195 {"DH", "", 52, { 1,X,1,X,1,X,1,X } }, // dragon horse
\r
196 {"R", "DK", 50, { X,0,X,0,X,0,X,0 } }, // rook
\r
197 {"B", "DH", 32, { 0,X,0,X,0,X,0,X } }, // bishop
\r
198 {"K", "", 41, { 1,1,1,1,1,1,1,1 } }, // king
\r
199 {"CP", "", 40, { 1,1,1,1,1,1,1,1 } }, // king
\r
200 {"DE", "CP", 25, { 1,1,1,1,0,1,1,1 } }, // silver
\r
201 {"G", "", 22, { 1,1,1,0,1,0,1,1 } }, // gold
\r
202 {"S", "G", 20, { 1,1,0,1,0,1,0,1 } }, // silver
\r
203 {"L", "G", 15, { X,0,0,0,0,0,0,0 } }, // lance
\r
204 {"N", "G", 11, { N,0,0,0,0,0,0,N } }, // Knight
\r
205 {"P", "G", 8, { 1,0,0,0,0,0,0,0 } }, // pawn
\r
206 { NULL } // sentinel
\r
209 PieceDesc daiPieces[] = {
\r
210 {"FD", "G", 15, { 0,2,0,2,0,2,0,2 }, 2 }, // Flying Dragon
\r
211 {"VO", "G", 20, { 2,0,2,0,2,0,2,0 }, 2 }, // Violent Ox
\r
212 {"EW", "G", 8, { 1,1,1,0,0,0,1,1 }, 2 }, // Evil Wolf
\r
213 {"CS", "G", 7, { 0,1,0,1,0,1,0,1 }, 1 }, // Cat Sword
\r
214 {"AB", "G", 6, { 1,0,1,0,1,0,1,0 }, 1 }, // Angry Boar
\r
215 {"I", "G", 8, { 1,1,0,0,0,0,0,1 }, 2 }, // Iron
\r
216 {"N", "G", 6, { N,0,0,0,0,0,0,N }, 0 }, // Knight
\r
217 {"ST", "G", 5, { 0,1,0,0,0,0,0,1 }, 0 }, // Stone
\r
218 { NULL } // sentinel
\r
221 PieceDesc ddPieces[] = {
\r
222 {"LO", "", 1, { 1,H,1,H,1,H,1,H } }, // Long-Nosed Goblin
\r
223 {"OK", "LO", 1, { 2,1,2,0,2,0,2,1 } }, // Old Kite
\r
224 {"PS", "HM", 1, { J,0,1,J,0,J,1,0 } }, // Poisonous Snake
\r
225 {"GE", "", 1, { 3,3,5,5,3,5,5,3 } }, // Great Elephant
\r
226 {"WS", "LD", 1, { 1,1,2,0,1,0,2,1 } }, // Western Barbarian
\r
227 {"EA", "LN", 1, { 2,1,1,0,2,0,1,1 } }, // Eastern Barbarian
\r
228 {"NO", "FE", 1, { 0,2,1,1,0,1,1,2 } }, // Northern Barbarian
\r
229 {"SO", "WE", 1, { 0,1,1,2,0,2,1,1 } }, // Southern Barbarian
\r
230 {"FE", "", 1, { 2,X,2,2,2,2,2,X } }, // Fragrant Elephant
\r
231 {"WE", "", 1, { 2,2,2,X,2,X,2,2 } }, // White Elephant
\r
232 {"FT", "", 1, { X,X,5,0,X,0,5,X } }, // Free Dream-Eater
\r
233 {"FR", "", 1, { 5,X,X,0,5,0,X,X } }, // Free Demon
\r
234 {"WB", "FT", 1, { 2,X,X,X,2,X,X,X } }, // Water Buffalo
\r
235 {"RB", "FR", 1, { X,X,X,X,0,X,X,X } }, // Rushing Bird
\r
236 {"SB", "", 1, { X,X,2,2,2,2,2,X } }, // Standard Bearer
\r
238 {"FH", "FK", 1, { 1,2,1,0,1,0,1,2 } }, // Flying Horse
\r
239 {"NK", "SB", 1, { 1,1,1,1,1,1,1,1 } }, // Neighbor King
\r
240 {"BM", "MW", 1, { 0,1,1,1,0,1,1,1 } }, // Blind Monkey
\r
241 {"DO", "", 1, { 2,X,2,X,2,X,2,X } }, // Dove
\r
242 {"EB", "DO", 1, { 2,0,2,0,0,0,2,0 } }, // Enchanted Badger
\r
243 {"EF", "SD", 1, { 0,2,0,0,2,0,0,2 } }, // Enchanted Fox
\r
244 {"RA", "", 1, { X,0,X,1,X,1,X,0 } }, // Racing Chariot
\r
245 {"SQ", "", 1, { X,1,X,0,X,0,X,1 } }, // Square Mover
\r
246 {"PR", "SQ", 1, { 1,1,2,1,0,1,2,1 } }, // Prancing Stag
\r
247 {"WT", "", 1, { X,1,2,0,X,0,2,X } }, // White Tiger
\r
248 {"BD", "", 1, { 2,X,X,0,2,0,X,1 } }, // Blue Dragon
\r
249 {"HD", "", 1, { X,0,0,0,1,0,0,0 } }, // Howling Dog
\r
250 {"VB", "", 1, { 0,2,1,0,0,0,1,2 } }, // Violent Bear
\r
251 {"SA", "", 1, { 2,1,0,0,2,0,0,1 } }, // Savage Tiger
\r
252 {"W", "", 1, { 0,2,0,0,0,0,0,2 } }, // Wood
\r
253 {"CS", "DH", 7, { 0,1,0,1,0,1,0,1 } }, // cat sword
\r
254 {"FD", "DK", 15, { 0,2,0,2,0,2,0,2 } }, // flying dragon
\r
255 {"KN", "GD", 15, { J,1,J,1,J,1,J,1 } }, // kirin
\r
256 {"PH", "GB", 15, { 1,J,1,J,1,J,1,J } }, // phoenix
\r
257 {"LN", "FF", 100, { L,L,L,L,L,L,L,L } }, // lion
\r
258 {"LD", "GE", 1, { T,T,T,T,T,T,T,T } }, // Lion Dog
\r
259 {"AB", "", 1, { 1,0,1,0,1,0,1,0 } }, // Angry Boar
\r
260 {"B", "", 1, { 0,X,0,X,0,X,0,X } }, // Bishop
\r
261 {"C", "", 1, { 1,1,0,0,1,0,0,1 } }, // Copper
\r
262 {"DH", "", 1, { 1,X,1,X,1,X,1,X } }, // Dragon Horse
\r
263 {"DK", "", 1, { X,1,X,1,X,1,X,1 } }, // Dragon King
\r
264 {"FK", "", 1, { } }, //
\r
265 {"EW", "", 1, { 1,1,1,0,0,0,1,1 } }, // Evil Wolf
\r
266 {"FL", "", 1, { } }, //
\r
267 {"", "", 1, { } }, //
\r
268 {"", "", 1, { } }, //
\r
269 {"", "", 1, { } }, //
\r
270 {"", "", 1, { } }, //
\r
271 { NULL } // sentinel
\r
274 PieceDesc makaPieces[] = {
\r
275 {"DV", "", 1, { 0,1,0,1,0,0,1,1 } }, // Deva
\r
276 {"DS", "", 1, { 0,1,1,0,0,1,0,1 } }, // Dark Spirit
\r
277 {"T", "", 1, { 0,1,0,0,1,0,0,1 } }, // Tile
\r
278 {"CS", "", 1, { 1,0,0,1,1,1,0,0 } }, // Coiled Serpent
\r
279 {"RD", "", 1, { 1,0,1,1,1,1,1,0 } }, // Reclining Dragon
\r
280 {"CC", "", 1, { 0,1,1,0,1,0,1,1 } }, // Chinese Cock
\r
281 {"OM", "", 1, { 0,1,0,1,1,1,0,1 } }, // Old Monkey
\r
282 {"BB", "", 1, { 0,1,0,1,X,1,0,1 } }, // Blind Bear
\r
283 {"OR", "", 1, { 0,2,0,0,2,0,0,2 } }, // Old Rat
\r
284 {"LD", "WS", 1, { T,T,T,T,T,T,T,T } }, // Lion Dog
\r
285 {"WR", "", 1, { 0,3,1,3,0,3,1,3 } }, // Wrestler
\r
286 {"GG", "", 1, { 3,1,3,0,3,0,3,1 } }, // Guardian of the Gods
\r
287 {"BD", "", 1, { 0,3,1,0,1,0,1,3 } }, // Budhist Devil
\r
288 {"SD", "", 1, { 5,2,5,2,5,2,5,2 } }, // She-Devil
\r
289 {"DY", "", 1, { J,0,1,0,J,0,1,0 } }, // Donkey
\r
290 {"CP", "", 1, { 0,H,0,2,0,2,0,H } }, // Capricorn
\r
291 {"HM", "", 1, { H,0,H,0,H,0,H,0 } }, // Hook Mover
\r
292 {"SF", "", 1, { 0,1,X,1,0,1,0,1 } }, // Side Flier
\r
293 {"LC", "", 1, { X,0,0,X,1,0,0,X } }, // Left Chariot
\r
294 {"RC", "", 1, { X,X,0,0,1,X,0,0 } }, // Right Chariot
\r
295 {"FG", "", 1, { X,X,X,0,X,0,X,X } }, // Free Gold
\r
296 {"FS", "", 1, { X,X,0,X,0,X,0,X } }, // Free Silver
\r
297 {"FC", "", 1, { X,X,0,0,X,0,0,X } }, // Free Copper
\r
298 {"FI", "", 1, { X,X,0,0,0,0,0,X } }, // Free Iron
\r
299 {"FT", "", 1, { 0,X,0,0,X,0,0,X } }, // Free Tile
\r
300 {"FN", "", 1, { 0,X,0,0,0,0,0,X } }, // Free Stone
\r
301 {"FTg", "", 1, { 0,X,X,X,X,X,X,X } }, // Free Tiger
\r
302 {"FLp", "", 1, { X,X,0,X,X,X,0,X } }, // Free Leopard (Free Boar?)
\r
303 {"FSp", "", 1, { X,0,0,X,X,X,0,0 } }, // Free Serpent (Whale?)
\r
304 {"FrD", "", 1, { X,0,X,X,X,X,X,0 } }, // Free Dragon
\r
305 {"FC", "", 1, { 0,X,0,X,0,X,0,X } }, // Free Cat (Bishop?)
\r
306 {"EM", "", 1, { } }, // Emperor
\r
307 {"TK", "", 1, { } }, // Teaching King
\r
308 {"BS", "", 1, { } }, // Budhist Spirit
\r
309 {"WS", "", 1, { X,X,0,X,1,X,0,X } }, // Wizard Stork
\r
310 {"MW", "", 1, { 1,X,0,X,X,X,0,X } }, // Mountain Witch
\r
311 {"FF", "", 1, { } }, // Furious Fiend
\r
312 {"GD", "", 1, { 2,3,X,3,2,3,X,3 } }, // Great Dragon
\r
313 {"GB", "", 1, { X,3,2,3,X,3,2,3 } }, // Golden Bird
\r
314 {"FrW", "", 1, { } }, // Free Wolf
\r
315 {"FrB", "", 1, { } }, // Free Bear
\r
316 {"BT", "", 1, { X,0,0,X,0,X,0,0 } }, // Bat
\r
317 {"", "", 1, { } }, //
\r
318 { NULL } // sentinel
\r
321 PieceDesc taiPieces[] = {
\r
322 {"", "", 1, { } }, // Peacock
\r
323 {"", "", 1, { } }, // Vermillion Sparrow
\r
324 {"", "", 1, { } }, // Turtle Snake
\r
325 {"", "", 1, { } }, // Silver Hare
\r
326 {"", "", 1, { } }, // Golden Deer
\r
327 {"", "", 1, { } }, //
\r
328 {"", "", 1, { } }, //
\r
329 {"", "", 1, { } }, //
\r
330 { NULL } // sentinel
\r
333 PieceDesc tenjikuPieces[] = { // only those not in Chu, or different (because of different promotion)
\r
334 {"FI", "", FVAL, { X,X,0,X,X,X,0,X } }, // Fire Demon
\r
335 {"GG", "", 150, { R,R,R,R,R,R,R,R }, 0, 3 }, // Great General
\r
336 {"VG", "", 140, { 0,R,0,R,0,R,0,R }, 0, 2 }, // Vice General
\r
337 {"RG", "GG",120, { R,0,R,0,R,0,R,0 }, 0, 1 }, // Rook General
\r
338 {"BG", "VG",110, { 0,R,0,R,0,R,0,R }, 0, 1 }, // Bishop General
\r
339 {"SE", "RG", 1, { X,D,X,X,X,X,X,D } }, // Soaring Eagle
\r
340 {"HF", "BG", 1, { D,X,X,X,X,X,X,X } }, // Horned Falcon
\r
341 {"LH", "", 1, { L,S,L,S,L,S,L,S } }, // Lion-Hawk
\r
342 {"LN", "LH", LVAL, { L,L,L,L,L,L,L,L } }, // Lion
\r
343 {"FE", "", 1, { X,X,X,X,X,X,X,X } }, // Free Eagle
\r
344 {"FK", "FE", 60, { X,X,X,X,X,X,X,X } }, // Free King
\r
345 {"HT", "", 1, { X,X,2,X,X,X,2,X } }, // Heavenly Tetrarchs
\r
346 {"CS", "HT", 1, { X,X,2,X,X,X,2,X } }, // Chariot Soldier
\r
347 {"WB", "FI", 1, { 2,X,X,X,2,X,X,X } }, // Water Buffalo
\r
348 {"VS", "CS", 1, { X,0,2,0,1,0,2,0 } }, // Vertical Soldier
\r
349 {"SS", "WB", 1, { 2,0,X,0,1,0,X,0 } }, // Side Soldier
\r
350 {"I", "VS", 1, { 1,1,0,0,0,0,0,1 } }, // Iron
\r
351 {"N", "SS", 1, { N,0,0,0,0,0,0,N } }, // Knight
\r
352 {"MG", "", 1, { X,0,0,X,0,X,0,0 } }, // Multi-General
\r
353 {"D", "MG", 1, { 1,0,0,1,0,1,0,0 } }, // Dog
\r
354 { NULL } // sentinel
\r
357 PieceDesc taikyokuPieces[] = {
\r
358 {"", "", 1, { } }, //
\r
359 { NULL } // sentinel
\r
362 PieceDesc chessPieces[] = {
\r
363 {"Q", "", 95, { X,X,X,X,X,X,X,X } },
\r
364 {"R", "", 50, { X,0,X,0,X,0,X,0 } },
\r
365 {"B", "", 32, { 0,X,0,X,0,X,0,X } },
\r
366 {"N", "", 30, { N,N,N,N,N,N,N,N } },
\r
367 {"K", "", 28, { 1,1,1,1,1,1,1,1 } },
\r
368 {"P", "Q", 8, { M,C,0,0,0,0,0,C } },
\r
369 { NULL } // sentinel
\r
372 PieceDesc shatranjPieces[] = {
\r
373 {"FK", "", 15, { 0,1,0,1,0,1,0,1 } },
\r
374 {"R", "", 50, { X,0,X,0,X,0,X,0 } },
\r
375 {"B", "", 9, { 0,J,0,J,0,J,0,J } },
\r
376 {"N", "", 30, { N,N,N,N,N,N,N,N } },
\r
377 {"K", "", 28, { 1,1,1,1,1,1,1,1 } },
\r
378 {"P", "FK", 8, { M,C,0,0,0,0,0,C } },
\r
379 { NULL } // sentinel
\r
382 PieceDesc makrukPieces[] = {
\r
383 {"SM","", 15, { 0,1,0,1,0,1,0,1 } },
\r
384 {"R", "", 50, { X,0,X,0,X,0,X,0 } },
\r
385 {"S", "", 20, { 1,1,0,1,0,1,0,1 } }, // silver
\r
386 {"N", "", 30, { N,N,N,N,N,N,N,N } },
\r
387 {"K", "", 28, { 1,1,1,1,1,1,1,1 } },
\r
388 {"P", "SM", 8, { M,C,0,0,0,0,0,C } },
\r
389 { NULL } // sentinel
\r
392 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
393 "/............/............/"
\r
394 "...: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
395 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
396 "/PPPPPPPPPPPPPPP/....:GB.....:GB..../.............../.............../.............../....:gb.....:gb..../ppppppppppppppp/"
\r
397 "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
398 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
399 ":SM:VMR:HF:SE:BG:RG:GG:VG:RG:BG:SE:HFR:VM:SM/PPPPPPPPPPPPPPPP/....D......D..../"
\r
400 "................/................/................/................/"
\r
401 "....d......d..../pppppppppppppppp/:sm:vmr:hf:se:bg:rg:vg:gg:rg:bg:se:hfr:vm:sm/"
\r
402 ":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
403 char shoArray[] = "LNSGKGSNL/.B..:DE..R./PPPPPPPPP/........./........./........./ppppppppp/.r..:de..b./lnsgkgsnl";
\r
404 char chessArray[] = "RNBQKBNR/PPPPPPPP/......../......../......../......../pppppppp/rnbqkbnr";
\r
405 char shatArray[]= "RNBK:FKBNR/PPPPPPPP/......../......../......../......../pppppppp/rnbk:fkbnr";
\r
406 char thaiArray[]= "RNSK:SMSNR/......../PPPPPPPP/......../......../pppppppp/......../rnsk:smsnr";
\r
409 int boardWidth, boardFiles, boardRanks, zoneDepth, varNr; // board sizes
\r
410 char *name; // WinBoard name
\r
411 char *array; // initial position
\r
414 typedef enum { V_CHESS, V_SHO, V_CHU, V_DAI, V_TENJIKU, V_DADA, V_MAKA, V_TAI, V_KYOKU, V_SHATRANJ, V_MAKRUK } Variant;
\r
416 VariantDesc variants[] = {
\r
417 { 16, 8, 8, 1, V_CHESS, "normal", chessArray }, // FIDE
\r
418 { 18, 9, 9, 3, V_SHO, "9x9+0_shogi", shoArray }, // Sho
\r
419 { 24, 12, 12, 4, V_CHU, "chu", chuArray }, // Chu
\r
420 { 30, 15, 15, 5, V_DAI, "dai", daiArray }, // Dai
\r
421 { 32, 16, 16, 5, V_TENJIKU, "tenjiku", tenArray }, // Tenjiku
\r
422 { 16, 8, 8, 1, V_SHATRANJ,"shatranj",shatArray}, // Shatranj
\r
423 { 16, 8, 8, 3, V_MAKRUK, "makruk", thaiArray}, // Makruk
\r
425 // { 0, 0, 0, 0, 0 }, // sentinel
\r
426 { 34, 17, 17, 0, V_DADA, "dada", chuArray }, // Dai Dai
\r
427 { 38, 19, 19, 0, V_MAKA, "maka", chuArray }, // Maka Dai Dai
\r
428 { 50, 25, 25, 0, V_TAI, "tai", chuArray }, // Tai
\r
429 { 40, 36, 36, 0, V_KYOKU, "kyoku", chuArray } // Taikyoku
\r
436 Vector direction[] = { // clockwise!
\r
446 { 2, 1}, // Knight jumps
\r
456 int epList[96], ep2List[96], toList[96], reverse[96]; // decoding tables for double and triple moves
\r
457 int kingStep[10], knightStep[10]; // raw tables for step vectors (indexed as -1 .. 8)
\r
458 char fireFlags[10]; // flags for Fire-Demon presence (last two are dummies, which stay 0, for compactify)
\r
459 #define kStep (kingStep+1)
\r
460 #define nStep (knightStep+1)
\r
462 int attackMask[8] = { // indicate which bits in attack-map item are used to count attacks from which direction
\r
473 int rayMask[8] = { // indicate which bits in attack-map item are used to count attacks from which direction
\r
484 int one[] = { // 1 in the bit fields for the various directions
\r
493 0100000000 // marks knight jumps
\r
496 // Main Data structures
\r
499 // Interleaved lists for black and white, white taking the odd slots
\r
500 // Pieces in general have two entries: one for the basic, and one for the promoted version
\r
501 // The unused one gets its position set to the invalid square number ABSENT
\r
502 // The list is sorted by piece value, most valuable pieces first
\r
503 // When a piece is captured in the search, both its versions are marked ABSENT
\r
504 // In the root the list is packed to eliminate all captured pieces
\r
505 // The list contains a table for how far the piece moves in each direction
\r
506 // Range encoding: -3 = full Lion, -2 = on-ray Lion, -1 = plain jump, 0 = none, 1 = step, >1 = (limited) range
\r
508 // A table in board format, containing pairs of consecutive integers for each square (indexed as 2*sqr and 2*sqr+1)
\r
509 // The first integer contains info on black attacks to the square, the second similarly for white attacks
\r
510 // Each integer contains eight 3-bit fields, which count the number of attacks on it with moves in a particular direction
\r
511 // (If there are attacks by range-jumpers, the 3-bit count is increased by 2 over the actual value)
\r
513 // The board has twice as many files as actually used, in 0x88 fashion
\r
514 // The used squares hold the piece numbers (for use as index in the piece list)
\r
515 // Unused squares are set to the invalid piece number EDGE
\r
516 // There are also 3 guard ranks of EDGE on each side
\r
518 // Moves are encoded as 11-bit from-square and to-square numbers packed in the low bits of an int
\r
519 // Special moves (like Lion double moves) are encoded by off-board to-squares above a certain value
\r
520 // Promotions are indicated by bit 22
\r
522 // Entries of 16 bytes, holding a 32-bit signature, 16-bit lower- and upper-bound scores,
\r
523 // 8-bit draft of each of those scores, an age counter that stores the search number of the last access.
\r
524 // The hash key is derived as the XOR of the products pieceKey[piece]*squareKey[square].
\r
526 // the promoBoard contains one byte with flags for each square, to indicate for each side whether the square
\r
527 // is in the promotion zone (twice), on the last two ranks, or on the last rank
\r
528 // the promoFlag field in the piece list can select which bits of this are tested, to see if it
\r
529 // (1) can promote (2) can defer when the to-square is on last rank, last two ranks, or anywhere.
\r
530 // Pawns normally can't defer anywhere, but if the user defers with them, their promoFlag is set to promote on last rank only
\r
538 signed char range[8];
\r
543 unsigned char promoGain;
\r
545 } PieceInfo; // piece-list entry
\r
547 int last[2], royal[2];
\r
548 PieceInfo p[NPIECES]; // piece list
\r
559 } HashEntry; // hash-table entry
\r
561 // Some global variables that control your engine's behavior
\r
565 int resign; // engine-defined option
\r
566 int contemptFactor; // likewise
\r
569 int squareKey[BSIZE];
\r
571 int rawBoard[BSIZE + 11*BHMAX + 6];
\r
572 //int attacks[2*BSIZE]; // attack map
\r
573 int attackMaps[200*BSIZE], *attacks = attackMaps;
\r
574 char distance[2*BSIZE]; // distance table
\r
575 char promoBoard[BSIZE]; // flags to indicate promotion zones
\r
576 char rawFire[BSIZE+2*BWMAX]; // flags to indicate squares controlled by Fire Demons
\r
577 signed char PST[3*BSIZE];
\r
579 #define board (rawBoard + 6*BHMAX + 3)
\r
580 #define fireBoard (rawFire + BWMAX + 1)
\r
581 #define dist (distance + BSIZE)
\r
584 ListLookUp (char *name, PieceDesc *list)
\r
585 { // find piece of given name in list of descriptors
\r
586 while(list->name && strcmp(name, list->name)) list++;
\r
587 return (list->name == NULL ? NULL : list);
\r
591 LookUp (char *name, int var)
\r
592 { // search piece of given name in all lists relevant for given variant
\r
595 case V_TENJIKU: // Tenjiku
\r
596 desc = ListLookUp(name, tenjikuPieces);
\r
597 if(desc) return desc;
\r
598 return ListLookUp(name, chuPieces);
\r
600 return ListLookUp(name, shoPieces);
\r
602 desc = ListLookUp(name, daiPieces);
\r
603 if(desc) return desc;
\r
605 return ListLookUp(name, chuPieces);
\r
606 case V_CHESS: // FIDE
\r
607 return ListLookUp(name, chessPieces);
\r
608 case V_SHATRANJ: // Shatranj
\r
609 return ListLookUp(name, shatranjPieces);
\r
610 case V_MAKRUK: // Makruk
\r
611 return ListLookUp(name, makrukPieces);
\r
618 { // remove piece number n from the mentioned side's piece list (and adapt the reference to the displaced pieces!)
\r
620 for(i=stm+2; i<last[stm]; i+=2)
\r
621 if(p[i].promo > n) p[i].promo -= 2;
\r
622 for(i=n; i<last[stm]; i+=2) {
\r
624 if(i+2 == royal[stm]) royal[stm] -= 2; // NB: squeezing out the King moves up Crown Prince to royal[stm]
\r
625 if(i < 10) fireFlags[i-2] = fireFlags[i];
\r
631 Worse (int a, int b)
\r
632 { // determine if range a not upward compatible with b
\r
633 if(a == b) return 0;
\r
634 if(a >= 0 && b >= 0) return a < b;
\r
635 if(a >= 0) return 1; // a (limited) range can never do the same as a special move
\r
637 case J: return b < J; // any special move is better than a plain jump
\r
638 case D: return b > 2 || b < D;
\r
639 case T: return b > 3 || b < T;
\r
640 case L: return b > 2 || b < D;
\r
641 case F: return b > 3 || b < F || b == T;
\r
642 case S: return b == H || b == T;
\r
643 case H: return b < 0;
\r
644 default: return 1; // a >= 0, so b must be < 0 and can always do something a ranging move cannot do
\r
651 Lance (signed char *r)
\r
652 { // File-bound forward slider
\r
654 for(i=1; i<4; i++) if(r[i] || r[i+4]) return 0;
\r
659 EasyProm (signed char *r)
\r
662 return r[0] == X || r[1] == X || r[7] == X;
\r
666 IsUpwardCompatible (signed char *r, signed char *s)
\r
669 for(i=0; i<8; i++) {
\r
670 if(Worse(r[i], s[i])) return 0;
\r
676 Forward (signed char *r)
\r
679 for(i=2; i<7; i++) if(r[i]) return 0;
\r
684 Range (signed char *r)
\r
687 for(i=0; i<8; i++) {
\r
689 if(r[i] < 0) d == r[i] >= L ? 2 : 36;
\r
695 int multis[2], multiMovers[100];
\r
698 StackMultis (int col)
\r
702 for(i=col+2; i<last[col]; i+=2) { // scan piece list for multi-capturers
\r
703 for(j=0; j<8; j++) if(p[i].range[j] < J && p[i].range[j] >= S || p[i].value == 10*FVAL) {
\r
704 multiMovers[multis[col]] = i; // found one: put its piece number in list
\r
712 Compactify (int stm)
\r
713 { // remove pieces that are permanently gone (captured or promoted) from one side's piece list
\r
715 for(i=stm+2; i<last[stm]; i+=2) { // first pass: unpromoted pieces
\r
716 if((k = p[i].promo) >= 0 && p[i].pos == ABSENT) { // unpromoted piece no longer there
\r
717 p[k].promo = -2; // orphan promoted version
\r
721 for(i=stm+2; i<last[stm]; i+=2) { // second pass: promoted pieces
\r
723 if((k = p[i].promo) == -2 && p[i].pos == ABSENT) { // orphaned promoted piece not present
\r
731 AddPiece (int stm, PieceDesc *list)
\r
734 for(i=stm+2; i<=last[stm]; i += 2) {
\r
735 if(p[i].value < 10*list->value || p[i].value == 10*list->value && (p[i].promo < 0)) break;
\r
738 for(j=last[stm]; j>i; j-= 2) p[j] = p[j-2];
\r
739 p[i].value = v = 10*list->value;
\r
740 for(j=0; j<8; j++) p[i].range[j] = list->range[j^4*(WHITE-stm)];
\r
741 switch(Range(p[i].range)) {
\r
742 case 1: p[i].pst = BH; break;
\r
743 case 2: p[i].pst = bsize; break;
\r
744 default: p[i].pst = bsize + BH; break;
\r
746 key = (stm == WHITE ? &list->whiteKey : &list->blackKey);
\r
747 if(!*key) *key = ~(myRandom()*myRandom());
\r
748 p[i].promoGain = EasyProm(list->range); // flag easy promotion based on white view
\r
749 p[i].pieceKey = *key;
\r
750 p[i].promoFlag = 0;
\r
751 p[i].bulk = list->bulk;
\r
752 p[i].mobWeight = v > 600 ? 0 : v >= 400 ? 1 : v >= 300 ? 2 : v > 150 ? 3 : v >= 100 ? 2 : 0;
\r
753 // if(Lance(list->range)) p[i].mobWeight = 5, p[i].pst = 0; // clear path but don't move forward
\r
754 for(j=stm+2; j<= last[stm]; j+=2) {
\r
755 if(p[j].promo >= i) p[j].promo += 2;
\r
757 if(royal[stm] >= i) royal[stm] += 2;
\r
758 if(p[i].value == (currentVariant == V_SHO ? 410 : 280) ) royal[stm] = i, p[i].pst = 0;
\r
759 p[i].qval = (currentVariant == V_TENJIKU ? list->ranking : 0); // jump-capture hierarchy
\r
764 SetUp(char *array, int var)
\r
766 int i, j, n, m, nr, color;
\r
767 char c, *q, name[3], prince = 0;
\r
768 PieceDesc *p1, *p2;
\r
769 last[WHITE] = 1; last[BLACK] = 0;
\r
771 //printf("next rank: %s\n", array);
\r
772 for(j = BW*i; ; j++) {
\r
774 if(*array == '+') pflag++, array++;
\r
775 c = name[0] = *array++;
\r
777 if(c == '.') continue;
\r
778 if(c == '/') break;
\r
779 name[1] = name[2] = 0;
\r
780 if(c == ':') name[0] = *array++, name[1] = *array++;
\r
781 if(name[0] >= 'a') {
\r
783 name[0] += 'A' - 'a';
\r
784 if(name[1]) name[1] += 'A' - 'a';
\r
785 } else color = WHITE;
\r
786 if(!strcmp(name, "CP")) prince |= color+1; // remember if we added Crown Prince
\r
787 p1 = LookUp(name, var);
\r
788 if(!p1) printf("tellusererror Unknown piece '%s' in setup\n", name), exit(-1);
\r
789 if(pflag && p1->promoted) p1 = LookUp(p1->promoted, var); // use promoted piece instead
\r
790 n = AddPiece(color, p1);
\r
792 if(p1->promoted[0] && !pflag) {
\r
793 if(!strcmp(p1->promoted, "CP")) prince |= color+1; // remember if we added Crown Prince
\r
794 p2 = LookUp(p1->promoted, var);
\r
795 m = AddPiece(color, p2);
\r
798 p[n].promoFlag = IsUpwardCompatible(p2->range, p1->range) * DONT_DEFER + CAN_PROMOTE;
\r
799 if(Forward(p1->range)) p[n].promoFlag |= LAST_RANK; // Pieces that only move forward can't defer on last rank
\r
800 if(!strcmp(p1->name, "N")) p[n].promoFlag |= CANT_DEFER; // Knights can't defer on last 2 ranks
\r
801 p[n].promoFlag &= n&1 ? P_WHITE : P_BLACK;
\r
804 } else p[n].promo = -1; // unpromotable piece
\r
805 //printf("piece = %c%-2s %d(%d) %d/%d\n", color ? 'w' : 'b', name, n, m, last[color], last[!color]);
\r
809 // add dummy Crown Princes if not yet added
\r
810 if(!(prince & WHITE+1)) p[AddPiece(WHITE, LookUp("CP", V_CHU))].pos = ABSENT;
\r
811 if(!(prince & BLACK+1)) p[AddPiece(BLACK, LookUp("CP", V_CHU))].pos = ABSENT;
\r
812 for(i=0; i<8; i++) fireFlags[i] = 0;
\r
813 for(i=2, n=1; i<10; i++) if(p[i].value == 10*FVAL) {
\r
814 int x = p[i].pos; // mark all burn zones
\r
815 fireFlags[i-2] = n;
\r
816 if(x != ABSENT) for(j=0; j<8; j++) fireBoard[x+kStep[j]] |= n;
\r
819 for(i=0; i<BH; i++) for(j=0; j<BH; j++) board[BW*i+j] = EMPTY;
\r
820 for(i=WHITE+2; i<=last[WHITE]; i+=2) if(p[i].pos != ABSENT) {
\r
821 if(p[i].promoGain && (j = p[i].promo) > 0)
\r
822 p[i].promoGain = (p[j].value - p[i].value - 30)*1.25, p[i].value = p[j].value - 30;
\r
823 else p[i].promoGain = 0;
\r
824 board[p[i].pos] = i;
\r
825 rootEval += p[i].value + PST[p[i].pst + p[i].pos];
\r
826 promoDelta += p[i].promoGain;
\r
827 filling += p[i].bulk;
\r
828 } else p[i].promoGain = 0;
\r
829 for(i=BLACK+2; i<=last[BLACK]; i+=2) if(p[i].pos != ABSENT) {
\r
830 if(p[i].promoGain && (j = p[i].promo) > 0)
\r
831 p[i].promoGain = (p[j].value - p[i].value - 30)*1.25, p[i].value = p[j].value - 30;
\r
832 else p[i].promoGain = 0;
\r
833 board[p[i].pos] = i;
\r
834 rootEval -= p[i].value + PST[p[i].pst + p[i].pos];
\r
835 promoDelta -= p[i].promoGain;
\r
836 filling += p[i].bulk;
\r
837 } else p[i].promoGain = 0;
\r
838 StackMultis(WHITE);
\r
839 StackMultis(BLACK);
\r
840 stm = WHITE; xstm = BLACK;
\r
845 return rand() ^ rand()>>10 ^ rand() << 10 ^ rand() << 20;
\r
853 currentVariant = variants[var].varNr;
\r
854 bWidth = variants[var].boardWidth;
\r
855 bHeight = variants[var].boardRanks;
\r
856 zone = variants[var].zoneDepth;
\r
857 array = variants[var].array;
\r
858 bsize = bWidth*bHeight;
\r
859 chuFlag = (currentVariant == V_CHU);
\r
860 tenFlag = (currentVariant == V_TENJIKU);
\r
861 chessFlag = (currentVariant == V_CHESS);
\r
862 repDraws = (currentVariant == V_CHESS || currentVariant == V_SHATRANJ || currentVariant == V_MAKRUK);
\r
864 for(i= -1; i<9; i++) { // board steps in linear coordinates
\r
865 kStep[i] = STEP(direction[i&7].x, direction[i&7].y); // King
\r
866 nStep[i] = STEP(direction[(i&7)+8].x, direction[(i&7)+8].y); // Knight
\r
869 for(i=0; i<8; i++) { // Lion double-move decoding tables
\r
870 for(j=0; j<8; j++) {
\r
871 epList[8*i+j] = kStep[i];
\r
872 toList[8*i+j] = kStep[j] + kStep[i];
\r
873 for(k=0; k<8*i+j; k++)
\r
874 if(epList[k] == toList[8*i+j] && toList[k] == epList[8*i+j])
\r
875 reverse[k] = 8*i+j, reverse[8*i+j] = k;
\r
877 // Lion-Dog triple moves
\r
878 toList[64+i] = 3*kStep[i]; epList[64+i] = kStep[i];
\r
879 toList[72+i] = 3*kStep[i]; epList[72+i] = 2*kStep[i];
\r
880 toList[80+i] = 3*kStep[i]; epList[80+i] = kStep[i]; ep2List[80+i] = 2*kStep[i];
\r
881 toList[88+i] = kStep[i]; epList[88+i] = 2*kStep[i];
\r
884 // fill distance table
\r
885 for(i=0; i<2*BSIZE; i++) {
\r
888 // for(i=0; i<8; i++)
\r
889 // for(j=1; j<BH; j++)
\r
890 // dist[j * kStep[i]] = j;
\r
891 // if(currentVariant == V_TENJIKU)
\r
892 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
895 for(i=0; i<bsize; i++) squareKey[i] = ~(myRandom()*myRandom());
\r
898 for(i=0; i<BSIZE + 11*BHMAX + 6; i++) rawBoard[i] = EDGE;
\r
901 for(i=0; i<BH; i++) for(j=0; j<BH; j++) {
\r
903 if(i == 0) v |= LAST_RANK & P_BLACK;
\r
904 if(i < 2) v |= CANT_DEFER & P_BLACK;
\r
905 if(i < ZONE) v |= (CAN_PROMOTE | DONT_DEFER) & P_BLACK; else v &= ~P_BLACK;
\r
906 if(i >= BH-ZONE) v |= (CAN_PROMOTE | DONT_DEFER) & P_WHITE; else v &= ~P_WHITE;
\r
907 if(i >= BH-2) v |= CANT_DEFER & P_WHITE;
\r
908 if(i == BH-1) v |= LAST_RANK & P_WHITE;
\r
909 promoBoard[BW*i + j] = v;
\r
912 // piece-square tables
\r
913 for(i=0; i<BH; i++) for(j=0; j<BH; j++) {
\r
914 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
916 PST[BH+s] = d/4 - (i == 0 || i == BH-1 ? 5 : 0) - (j == 0 || j == BH-1 ? 5 : 0)
\r
917 + 2*(i==zone || i==BH-zone-1);
\r
918 PST[BH*BW+s] = d/6;
\r
919 PST[BH*BW+BH+s] = d/12;
\r
920 PST[2*BH*BW+s] = j < 3 || j > BH-4 ? (i < 3 ? 5 : i == 3 ? 2 : i == 4 ? 1 : 0) : 0;
\r
923 p[EDGE].qval = 5; // tenjiku jump-capturer sentinel
\r
929 int r, f, score, tot=0;
\r
930 for(r=0; r<BH; r++) for(f=0; f<BH; f++) {
\r
932 int piece = board[s];
\r
933 if(!piece) continue;
\r
934 score = p[piece].value + PST[p[piece].pst + s];
\r
935 if(piece & 1) tot += score; else tot -= score;
\r
943 int r, f, score, tot=0;
\r
944 for(r=0; r<BH; r++) for(f=0; f<BH; f++) {
\r
946 int piece = board[s];
\r
947 if(!piece) continue;
\r
948 score = p[piece].promoGain;
\r
949 if(piece & 1) tot += score; else tot -= score;
\r
957 NewNonCapture (int x, int y, int promoFlags)
\r
959 if(board[y] != EMPTY) return 1; // edge, capture or own piece
\r
960 //if(flag) printf("# add %c%d%c%d, pf=%d\n", x%BW+'a',x/BW,y%BW+'a',y/BW, promoFlags);
\r
961 if( (promoBoard[x] | promoBoard[y]) & promoFlags) { // piece can promote with this move
\r
962 moveStack[msp++] = moveStack[nonCapts]; // create space for promotion
\r
963 moveStack[nonCapts++] = x<<SQLEN | y | PROMOTE; // push promotion
\r
964 if((promoFlags & promoBoard[y] & (CANT_DEFER | DONT_DEFER | LAST_RANK)) == 0) { // deferral could be a better alternative
\r
965 moveStack[msp++] = x<<SQLEN | y; // push deferral
\r
966 if( (promoBoard[x] & CAN_PROMOTE) == 0 ) { // enters zone
\r
967 moveStack[msp-1] |= DEFER; // flag that promo-suppression takes place after this move
\r
971 moveStack[msp++] = x<<SQLEN | y; // push normal move
\r
972 //if(flag) printf("msp=%d nc=%d\n", msp, nonCapts);
\r
977 NewCapture (int x, int y, int promoFlags)
\r
979 if( (promoBoard[x] | promoBoard[y]) & promoFlags) { // piece can promote with this move
\r
980 moveStack[msp++] = x<<SQLEN | y | PROMOTE; // push promotion
\r
981 if((promoFlags & promoBoard[y] & (CANT_DEFER | DONT_DEFER | LAST_RANK)) == 0) { // deferral could be a better alternative
\r
982 moveStack[msp++] = x<<SQLEN | y; // push deferral
\r
983 if( (promoBoard[x] & CAN_PROMOTE) == 0 ) { // enters zone
\r
984 moveStack[msp-1] |= DEFER; // flag that promo-suppression takes place after this move
\r
988 moveStack[msp++] = x<<SQLEN | y; // push normal move
\r
992 char map[49]; // 7x7 map for area movers
\r
993 char mapStep[] = { 7, 8, 1, -6, -7, -8, -1, 6 };
\r
994 char rowMask[] = { 0100, 0140, 0160, 070, 034, 016, 07, 03, 01 };
\r
998 AreaStep (int from, int x, int flags, int n, int d)
\r
1001 for(i=0; i<8; i++) {
\r
1002 int to = x + kStep[i], m = n + mapStep[i];
\r
1003 if(board[to] == EDGE) continue; // off board
\r
1004 if(map[m] >= d) continue; // already done
\r
1005 if(!map[m]) moveStack[msp++] = from<<SQLEN | to;
\r
1007 if(d > 1 && board[to] == EMPTY) AreaStep(from, to, flags, m, d-1);
\r
1012 AreaMoves (int from, int piece, int range)
\r
1015 for(i=0; i<49; i++) map[i] = 0;
\r
1016 map[3*7+7] = range;
\r
1017 AreaStep(from, from, p[piece].promoFlag, 3*7+3, range);
\r
1022 { // make bitmap of squares in FI (7x7) neighborhood where opponents can be captured or burned
\r
1023 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
1024 if(r < 4) bottom = 4 - r, rows[b=bottom-1] = 0; else
\r
1025 if(r > 11) top = 19 - r, rows[t=top+1] = 0; // adjust area to board edges
\r
1026 if(f < 4) left = 4 - f; else if(f > 11) right = 19 - f;
\r
1027 for(r=bottom; r<=top; r++) {
\r
1028 int mask = 0, y = x + 16*r;
\r
1029 for(f=left; f <= right; f++) {
\r
1030 if(board[y + f - (4*16+4)] != EMPTY && (board[y + f - (4*16+4)] & TYPE) == xstm)
\r
1031 mask |= rowMask[f]; // on-rank attacks
\r
1035 for(r=b; r<=t-2; r++) rows[r] |= rows[r+1] | rows[r+2]; // smear vertically
\r
1039 GenNonCapts (int promoSuppress)
\r
1041 int i, j, nullMove = ABSENT;
\r
1042 for(i=stm+2; i<=last[stm]; i+=2) {
\r
1043 int x = p[i].pos, pFlag = p[i].promoFlag;
\r
1044 if(x == ABSENT) continue;
\r
1045 if(x == promoSuppress && chuFlag) pFlag = 0;
\r
1046 for(j=0; j<8; j++) {
\r
1047 int y, v = kStep[j], r = p[i].range[j];
\r
1048 if(r < 0) { // jumping piece, special treatment
\r
1049 if(r == N) { // pure Knightm do off-ray jump
\r
1050 NewNonCapture(x, x + nStep[j], pFlag);
\r
1052 if(r >= S) { // in any case, do a jump of 2
\r
1053 int occup = NewNonCapture(x, x + 2*v, pFlag);
\r
1054 if(r < J) { // Lion power, also single step
\r
1055 if(!NewNonCapture(x, x + v, pFlag)) nullMove = x; else occup = 1;
\r
1056 if(r <= L) { // true Lion, also Knight jump
\r
1057 if(!occup & r < L) for(y=x+2*v; !NewNonCapture(x, y+=v, pFlag) && r == S; ); // BS and FF moves
\r
1059 NewNonCapture(x, x + v, pFlag);
\r
1063 if(r == M) { // FIDE Pawn; check double-move
\r
1064 if(!NewNonCapture(x, x+v, pFlag) && chessFlag && promoBoard[x-v])
\r
1065 NewNonCapture(x, x+2*v, pFlag), moveStack[msp-1] |= DEFER; // use promoSuppress flag as e.p. flag
\r
1071 if(NewNonCapture(x, y+=v, pFlag)) break;
\r
1078 report (int x, int y, int i)
\r
1083 MapOneColor (int start, int last, int *map)
\r
1085 int i, j, k, totMob = 0;
\r
1086 for(i=start+2; i<=last; i+=2) {
\r
1088 if(p[i].pos == ABSENT) continue;
\r
1089 for(j=0; j<8; j++) {
\r
1090 int x = p[i].pos, v = kStep[j], r = p[i].range[j];
\r
1091 if(r < 0) { // jumping piece, special treatment
\r
1094 if(board[x] != EMPTY && board[x] != EDGE)
\r
1095 map[2*x + start] += one[8];
\r
1097 if(r >= S) { // in any case, do a jump of 2
\r
1098 if(board[x + 2*v] != EMPTY && board[x + 2*v] != EDGE)
\r
1099 map[2*(x + 2*v) + start] += one[j], mob += (board[x + 2*v] ^ start) & 1;
\r
1100 if(r < J) { // Lion power, also single step
\r
1101 if(board[x + v] != EMPTY && board[x + v] != EDGE)
\r
1102 map[2*(x + v) + start] += one[j];
\r
1103 if(r == T) { // Lion Dog, also jump of 3
\r
1104 if(board[x + 3*v] != EMPTY && board[x + 3*v] != EDGE)
\r
1105 map[2*(x + 3*v) + start] += one[j];
\r
1107 if(r <= L) { // true Lion, also Knight jump
\r
1108 if(r < L) { // Lion plus (limited) range
\r
1110 r = (r == S ? 36 : 3);
\r
1112 if(board[y+=v] == EDGE) break;
\r
1113 if(board[y] != EMPTY) {
\r
1114 if(n > 2) map[2*y + start] += one[j]; // outside Lion range
\r
1120 if(board[x + v] != EMPTY && board[x + v] != EDGE)
\r
1121 map[2*(x + v) + start] += one[8];
\r
1125 if(r == C) { // FIDE Pawn diagonal
\r
1126 if(board[x + v] != EMPTY && board[x + v] != EDGE)
\r
1127 map[2*(x + v) + start] += one[j];
\r
1132 if(board[x+=v] != EMPTY) {
\r
1133 mob += dist[x-v-p[i].pos];
\r
1134 if(board[x] != EDGE) map[2*x + start] += one[j], mob += (board[x] ^ start) & 1;
\r
1136 if(p[i].range[j] > X) { // jump capturer
\r
1137 int c = p[i].qval;
\r
1138 if(p[board[x]].qval < c) {
\r
1139 x += v; // go behind directly captured piece, if jumpable
\r
1140 while(p[board[x]].qval < c) { // kludge alert: EDGE has qval = 5, blocking everything
\r
1141 if(board[x] != EMPTY) {
\r
1142 // int n = map[2*x + start] & attackMask[j];
\r
1143 // map[2*x + start] += (n < 3*one[j] ? 3*one[j] : one[j]); // first jumper gets 2 extra (to ease incremental update)
\r
1144 map[2*x + start] += one[j]; // for now use true count
\r
1155 totMob += mob * p[i].mobWeight;
\r
1157 if(!level) printf("# mobility %d = %d\n", start, totMob);
\r
1162 MapFromScratch (int *map)
\r
1165 for(i=0; i<2*bsize; i++) map[i] = 0;
\r
1166 mobilityScore = MapOneColor(1, last[WHITE], map);
\r
1167 mobilityScore -= MapOneColor(0, last[BLACK], map);
\r
1171 Connect (int sqr, int piece, int dir)
\r
1172 { // scan to both sides along ray to elongate attacks from there, and remove our own attacks on there, if needed
\r
1173 int x, step = kStep[dir], r1 = p[piece].range[dir], r2 = p[piece].range[dir+4], r3, r4, piece1, piece2;
\r
1174 int d1, d2, r, y, c;
\r
1176 if((attacks[2*sqr] + attacks[2*sqr+1]) & attackMask[dir]) { // there are incoming attack(s) from 'behind'
\r
1178 while(board[x-=step] == EMPTY); // in any case, scan to attacker, to see where / what it is
\r
1179 d1 = dist[x-sqr]; piece1 = board[x];
\r
1180 attacks[2*x + stm] -= -(d1 <= r2) & one[dir+4]; // remove our attack on it if in-range
\r
1181 if((attacks[2*sqr] + attacks[2*sqr+1]) & attackMask[dir+4]) { // there are also incoming attack(s) from 'ahead'
\r
1184 while(board[y+=step] == EMPTY); // also always scan to that one to see what it is
\r
1185 d2 = dist[y-sqr]; piece2 = board[y];
\r
1186 attacks[2*y+stm] -= -(d2 <= r1) & one[dir]; // remove our attack on it if in-range
\r
1187 // we have two pieces now shooting at each other. See how far they get.
\r
1188 if(d1 + d2 <= (r3 = p[piece1].range[dir])) { // 1 hits 2
\r
1189 attacks[2*y + (piece1 & WHITE)] += one[dir]; // count attack
\r
1190 UPDATE_MOBILITY(piece1, d2);
\r
1191 } else UPDATE_MOBILITY(piece1, r3 - d1); // does not connect, but could still gain mobility
\r
1192 if(d1 + d2 <= (r4 = p[piece2].range[dir+4])) { // 2 hits 1
\r
1193 attacks[2*x + (piece2 & WHITE)] += one[dir+4]; // count attack
\r
1194 UPDATE_MOBILITY(piece2, d1);
\r
1195 } else UPDATE_MOBILITY(piece2, r4 - d2); // does not connect, but could still gain mobility
\r
1196 // if r1 or r2<0, moves typically jump, and thus cannot be unblocked. Exceptions are FF and BS distant moves.
\r
1197 // test for d1+d2 > 2 && rN == F && d== 3 or rN == S
\r
1198 if(d1 <= 2) { // could be jump interactions
\r
1200 if(r2 <= J) attacks[2*x + stm] -= one[dir+4];
\r
1201 if(r1 <= J) attacks[2*y + stm] -= one[dir];
\r
1202 } else { // d1 == 1
\r
1203 if(r2 < J) attacks[2*x + stm] -= one[dir+4];
\r
1204 if(r1 < J) attacks[2*y + stm] -= one[dir];
\r
1205 if(board[x-step] != EMPTY && board[x-step] != EDGE)
\r
1206 attacks[2*(x-step) + stm] -= one[dir+4];
\r
1210 } else { // we were only attacked from behind
\r
1212 r = (r2 = p[piece1].range[dir]) - d1;
\r
1213 if(r < 0 || c > one[dir+4]) { // Oops! This was not our attacker, or not the only one. There must be a jump attack from even further behind!
\r
1214 // for now, forget jumpers
\r
1218 if(board[y+=step] != EMPTY) {
\r
1219 d2 = dist[y-sqr]; piece2 = board[y];
\r
1220 if(piece2 != EDGE) { // extended move hits a piece
\r
1221 attacks[2*y + (piece1 & WHITE)] += one[dir]; // count attack
\r
1222 attacks[2*y + stm] -= -(d2 <= r1) & one[dir]; // remove our own attack on it, if in-range
\r
1224 UPDATE_MOBILITY(piece1, d2); // count extra mobility even if we hit edge
\r
1227 // we hit nothing with the extended move of the attacker behind us.
\r
1228 UPDATE_MOBILITY(piece1, r2 - d1);
\r
1229 r = r1 - r2 + d1; // extra squares covered by mover
\r
1231 if(board[y+=step] != EMPTY) {
\r
1232 d2 = dist[y-sqr]; piece2 = board[y];
\r
1233 if(piece2 != EDGE) { // extended move hits a piece
\r
1234 attacks[2*y + stm] -= one[dir]; // count attack
\r
1239 // if r2<0 we should again test for F and S moves
\r
1241 } else // no incoming attack from behind
\r
1242 if(c = (attacks[2*sqr] + attacks[2*sqr+1]) & attackMask[dir+4]) { // but incoming attack(s) from 'ahead'
\r
1244 y = sqr; while(board[y+=step]); // locate attacker
\r
1245 d2 = dist[y-sqr]; piece2 = board[y];
\r
1246 attacks[2*y + stm] -= -(d2 <= r1) & one[dir]; // remove our attack on it if in-range
\r
1247 r = (r1 = p[piece1].range[dir]) - d2;
\r
1248 if(r < 0 || c > one[dir]) { // Oops! This was not our attacker, or not the only one. There must be a jump attack from even further behind!
\r
1249 // for now, forget jumpers
\r
1253 if(board[x-=step] != EMPTY) {
\r
1254 d1 = dist[x-sqr]; piece1 = board[x];
\r
1255 if(piece1 != EDGE) { // extended move hits a piece
\r
1256 attacks[2*x + (piece2 & WHITE)] += one[dir+4]; // count attack
\r
1257 attacks[2*x + stm] -= -(d1 <= r2) & one[dir+4]; // remove our own attack on it, if in-range
\r
1259 UPDATE_MOBILITY(piece2, d1); // count extra mobility even if we hit edge
\r
1262 // we hit nothing with the extended move of the attacker behind us.
\r
1263 UPDATE_MOBILITY(piece2, r2 - d1);
\r
1264 r = r2 - r1 + d2; // extra squares covered by mover
\r
1266 if(board[x-=step] != EMPTY) {
\r
1267 d1 = dist[x-sqr]; piece1 = board[x];
\r
1268 if(piece1 != EDGE) { // extended move hits a piece
\r
1269 attacks[2*x + stm] -= one[dir+4]; // count attack
\r
1274 } else { // no incoming attacks from either side. Only delete attacks of mover on others
\r
1278 if(board[x+=step] != EMPTY) { // piece found that we attacked
\r
1279 attacks[2*x + stm] -= one[dir]; // decrement attacks along that direction
\r
1285 if(board[x-=step] != EMPTY) { // piece found that we attacked
\r
1286 attacks[2*x + stm] -= one[dir+4]; // decrement attacks along opposite direction
\r
1294 Hit (int r, int d)
\r
1295 { // test if move with range r reaches over (un-obstructed) distance d
\r
1296 if(r < 0) switch(r) {
\r
1297 case J: return (d == 2);
\r
1299 case L: return (d <= 2);
\r
1301 case F: return (d <= 3);
\r
1303 default: return 0;
\r
1304 } else return (d <= r);
\r
1305 return 0; // not reached
\r
1309 Disconnect (int sqr, int piece, int dir)
\r
1311 int x = sqr, step = kStep[dir], piece1, piece2, d1, d2, r1, r2, y;
\r
1312 while( board[x+=step] == EMPTY );
\r
1313 piece1 = board[x];
\r
1314 if(piece1 != EDGE) { // x has hit a piece
\r
1316 r1 = p[piece1].range[dir+4];
\r
1317 y = sqr; while( board[y-=step] == EMPTY );
\r
1318 piece2 = board[y];
\r
1319 if(piece2 != EDGE) { // both ends of the ray hit a piece
\r
1321 r2 = p[piece2].range[dir];
\r
1322 if(r1 >= d1) { // piece1 hits us
\r
1323 attacks[2*sqr + (piece1 & WHITE)] += one[dir+4];
\r
1324 if(r1 >= d1 + d2) // was hitting piece2 before, now blocked
\r
1325 attacks[2*y + (piece1 & WHITE)] -= one[dir+4];
\r
1327 if(r2 >= d2) { // piece2 hits us
\r
1328 attacks[2*sqr + (piece2 & WHITE)] += one[dir];
\r
1329 if(r2 >= d1 + d2) // was hitting piece1 before, now blocked
\r
1330 attacks[2*x + (piece2 & WHITE)] -= one[dir];
\r
1332 if( Hit(p[piece].range[dir], d1) )
\r
1333 attacks[2*sqr + stm] += one[dir];
\r
1334 if( Hit(p[piece].range[dir+4], d2) )
\r
1335 attacks[2*sqr + stm] += one[dir+4];
\r
1339 x = sqr; while( board[x-=step] == EMPTY );
\r
1340 piece1 = board[x];
\r
1341 if(piece1 == EDGE) return; // ray empty on both sides
\r
1343 r1 = p[piece1].range[dir];
\r
1346 // we only get here if one side looks to the board edge
\r
1347 if(r1 >= d1) // piece1 hits us
\r
1348 attacks[2*sqr + (piece1 & WHITE)] += one[dir^4];
\r
1349 if( Hit(p[piece].range[dir], d1) )
\r
1350 attacks[2*sqr + stm] += one[dir];
\r
1355 { // determines attacks on square and blocking when a piece lands on an empty square
\r
1357 for(i=0; i<4; i++) {
\r
1358 Disconnect(sqr, board[sqr], i);
\r
1363 Evacuate (int sqr, int piece)
\r
1364 { // determines change in attacks on neighbors due to unblocking and mover when the mentioned piece vacates the given square
\r
1366 for(i=0; i<4; i++) Connect(sqr, piece, i);
\r
1370 MakeMove(Move m, UndoInfo *u)
\r
1372 int deferred = ABSENT;
\r
1373 // first execute move on board
\r
1374 u->from = m>>SQLEN & SQUARE;
\r
1375 u->to = m & SQUARE;
\r
1376 u->piece = board[u->from];
\r
1377 board[u->from] = EMPTY;
\r
1381 u->revMoveCount = cnt50++;
\r
1382 u->savKeyL = hashKeyL;
\r
1383 u->savKeyH = hashKeyH;
\r
1384 u->epVictim[0] = EMPTY;
\r
1385 u->saveDelta = promoDelta;
\r
1386 u->filling = filling;
\r
1388 if(p[u->piece].promoFlag & LAST_RANK) cnt50 = 0; // forward piece: move is irreversible
\r
1389 // TODO: put in some test for forward moves of non-backward pieces?
\r
1391 if(p[u->piece].value == 10*FVAL) { // move with Fire Demon
\r
1392 int i, f=~fireFlags[u->piece-2];
\r
1393 for(i=0; i<8; i++) fireBoard[u->from + kStep[i]] &= f; // clear old burn zone
\r
1396 if(m & (PROMOTE | DEFER)) {
\r
1397 if(m & DEFER) { // essential deferral: inform caller, but nothing special
\r
1399 u->new = u->piece;
\r
1401 p[u->piece].pos = ABSENT;
\r
1402 u->new = p[u->piece].promo;
\r
1403 u->booty = p[u->new].value - p[u->piece].value;
\r
1404 cnt50 = 0; // promotion irreversible
\r
1406 } else u->new = u->piece;
\r
1408 if(u->to >= SPECIAL) { // two-step Lion move
\r
1409 // take care of first e.p. victim
\r
1410 u->epSquare = u->from + epList[u->to - SPECIAL]; // decode
\r
1411 u->epVictim[0] = board[u->epSquare]; // remember for takeback
\r
1412 board[u->epSquare] = EMPTY; // and remove
\r
1413 p[u->epVictim[0]].pos = ABSENT;
\r
1414 // now take care of (optional) second e.p. victim
\r
1415 u->ep2Square = u->from + ep2List[u->to - SPECIAL]; // This is the (already evacuated) from-square when there is none!
\r
1416 u->epVictim[1] = board[u->ep2Square]; // remember
\r
1417 board[u->ep2Square] = EMPTY; // and remove
\r
1418 p[u->epVictim[1]].pos = ABSENT;
\r
1419 // decode the true to-square, and correct difEval and hash key for the e.p. captures
\r
1420 u->to = u->from + toList[u->to - SPECIAL];
\r
1421 u->booty += p[u->epVictim[1]].value + PST[p[u->epVictim[1]].pst + u->ep2Square];
\r
1422 u->booty += p[u->epVictim[0]].value + PST[p[u->epVictim[0]].pst + u->epSquare];
\r
1423 u->gain += p[u->epVictim[1]].value;
\r
1424 u->gain += p[u->epVictim[0]].value;
\r
1425 promoDelta += p[u->epVictim[0]].promoGain;
\r
1426 promoDelta += p[u->epVictim[1]].promoGain;
\r
1427 filling -= p[u->epVictim[0]].bulk;
\r
1428 filling -= p[u->epVictim[1]].bulk;
\r
1429 hashKeyL ^= p[u->epVictim[0]].pieceKey * squareKey[u->epSquare];
\r
1430 hashKeyH ^= p[u->epVictim[0]].pieceKey * squareKey[u->epSquare+BH];
\r
1431 hashKeyL ^= p[u->epVictim[1]].pieceKey * squareKey[u->ep2Square];
\r
1432 hashKeyH ^= p[u->epVictim[1]].pieceKey * squareKey[u->ep2Square+BH];
\r
1433 if(p[u->piece].value != 10*LVAL && p[u->epVictim[0]].value == 10*LVAL) deferred |= PROMOTE; // flag non-Lion x Lion
\r
1434 cnt50 = 0; // double capture irreversible
\r
1437 if(u->fireMask & fireBoard[u->to]) { // we moved next to enemy Fire Demon (must be done after SPECIAL, to decode to-sqr)
\r
1438 p[u->piece].pos = ABSENT; // this is suicide: implement as promotion to EMPTY
\r
1440 u->booty -= p[u->piece].value;
\r
1443 if(p[u->piece].value == 10*FVAL) { // move with Fire Demon that survives: burn
\r
1444 int i, f=fireFlags[u->piece-2];
\r
1445 for(i=0; i<8; i++) {
\r
1446 int x = u->to + kStep[i], burnVictim = board[x];
\r
1447 fireBoard[x] |= f; // mark new burn zone
\r
1448 u->epVictim[i+1] = burnVictim; // remember all neighbors, just in case
\r
1449 if(burnVictim != EMPTY && (burnVictim & TYPE) == xstm) { // opponent => actual burn
\r
1450 board[x] = EMPTY; // remove it
\r
1451 p[burnVictim].pos = ABSENT;
\r
1452 u->booty += p[burnVictim].value + PST[p[burnVictim].pst + x];
\r
1453 u->gain += p[burnVictim].value;
\r
1454 promoDelta += p[burnVictim].promoGain;
\r
1455 filling -= p[burnVictim].bulk;
\r
1456 hashKeyL ^= p[burnVictim].pieceKey * squareKey[x];
\r
1457 hashKeyH ^= p[burnVictim].pieceKey * squareKey[x + BH];
\r
1458 cnt50 = 0; // actually burning something makes the move irreversible
\r
1461 u->epVictim[0] = EDGE; // kludge to flag to UnMake this is special move
\r
1464 u->booty += PST[p[u->new].pst + u->to] - PST[p[u->piece].pst + u->from];
\r
1466 u->victim = board[u->to];
\r
1467 p[u->victim].pos = ABSENT;
\r
1468 filling += p[u->new].bulk - p[u->piece].bulk - p[u->victim].bulk;
\r
1469 promoDelta += p[u->new].promoGain - p[u->piece].promoGain + p[u->victim].promoGain;
\r
1470 u->booty += p[u->victim].value + PST[p[u->victim].pst + u->to];
\r
1471 u->gain += p[u->victim].value;
\r
1472 if(u->victim != EMPTY) {
\r
1473 cnt50 = 0; // capture irreversible
\r
1474 if(attacks[2*u->to + xstm]) u->loss = p[u->piece].value; // protected
\r
1477 p[u->new].pos = u->to;
\r
1478 board[u->to] = u->new;
\r
1479 promoDelta = -promoDelta;
\r
1481 hashKeyL ^= p[u->new].pieceKey * squareKey[u->to]
\r
1482 ^ p[u->piece].pieceKey * squareKey[u->from]
\r
1483 ^ p[u->victim].pieceKey * squareKey[u->to];
\r
1484 hashKeyH ^= p[u->new].pieceKey * squareKey[u->to+BH]
\r
1485 ^ p[u->piece].pieceKey * squareKey[u->from+BH]
\r
1486 ^ p[u->victim].pieceKey * squareKey[u->to+BH];
\r
1492 UnMake(UndoInfo *u)
\r
1494 if(u->epVictim[0]) { // move with side effects
\r
1495 if(u->epVictim[0] == EDGE) { // fire-demon burn
\r
1496 int i, f=~fireFlags[u->piece-2];
\r
1497 for(i=0; i<8; i++) {
\r
1498 int x = u->to + kStep[i];
\r
1499 fireBoard[x] &= f;
\r
1500 board[x] = u->epVictim[i+1];
\r
1501 p[board[x]].pos = x; // even EDGE should have dummy entry in piece list
\r
1503 } else { // put Lion victim of first leg back
\r
1504 p[u->epVictim[1]].pos = u->ep2Square;
\r
1505 board[u->ep2Square] = u->epVictim[1];
\r
1506 p[u->epVictim[0]].pos = u->epSquare;
\r
1507 board[u->epSquare] = u->epVictim[0];
\r
1511 if(p[u->piece].value == 10*FVAL) {
\r
1512 int i, f=fireFlags[u->piece-2];
\r
1513 for(i=0; i<8; i++) fireBoard[u->from + kStep[i]] |= f; // restore old burn zone
\r
1516 p[u->victim].pos = u->to;
\r
1517 board[u->to] = u->victim; // can be EMPTY
\r
1519 p[u->new].pos = ABSENT;
\r
1520 p[u->piece].pos = u->from; // this can be the same as above
\r
1521 board[u->from] = u->piece;
\r
1523 cnt50 = u->revMoveCount;
\r
1524 hashKeyL = u->savKeyL;
\r
1525 hashKeyH = u->savKeyH;
\r
1526 filling = u->filling;
\r
1527 promoDelta = u->saveDelta;
\r
1531 GenCapts(int sqr, int victimValue)
\r
1532 { // generate all moves that capture the piece on the given square
\r
1533 int i, range, att = attacks[2*sqr + stm];
\r
1534 //printf("GenCapts(%c%d,%d) %08x\n",sqr%BW+'a',sqr/BW,victimValue,att);
\r
1535 if(!att) return; // no attackers at all!
\r
1536 for(i=0; i<8; i++) { // try all rays
\r
1537 int x, v, jumper, jcapt=0;
\r
1538 if(att & attackMask[i]) { // attacked by move in this direction
\r
1539 v = -kStep[i]; x = sqr;
\r
1540 while( board[x+=v] == EMPTY ); // scan towards source until we encounter a 'stop'
\r
1541 //printf("stop @ %c%d (dir %d)\n",x%BW+'a',x/BW,i);
\r
1542 if((board[x] & TYPE) == stm) { // stop is ours
\r
1543 int attacker = board[x], d = dist[x-sqr], r = p[attacker].range[i];
\r
1544 //printf("attacker %d, range %d, dist %d\n", attacker, r, d);
\r
1545 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
1546 NewCapture(x, sqr + victimValue - SORTKEY(attacker), p[attacker].promoFlag);
\r
1548 if(!(att & attackMask[i])) continue; // no more; next direction
\r
1549 jcapt = p[board[x]].qval; // jump-capturer hierarchy
\r
1550 while(board[x+=v] == EMPTY);// one attack accounted for, but more to come, so skip to next stop
\r
1553 // we get here when we are on a piece that dous not attack us through a (limited) ranging move,
\r
1554 // it can be our own or an enemy with not (enough) range, or which is blocked
\r
1556 //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
1557 if((board[x] & TYPE) == stm) { // stop is ours
\r
1558 int attacker = board[x], d = dist[x-sqr], r = p[attacker].range[i];
\r
1559 if(jcapt < p[attacker].qval) { // it is a range jumper that jumps over the barrier
\r
1560 if(p[attacker].range[i] > 1) { // assumes all jump-captures are infinite range
\r
1561 NewCapture(x, sqr, p[attacker].promoFlag);
\r
1564 //if(board[x] == EDGE) { printf("edge hit %x-%x dir=%d att=%o\n", sqr, x, i, att); continue; }
\r
1566 if(r < 0) { // stop has non-standard moves
\r
1567 switch(p[attacker].range[i]) { // figure out what he can do (including multi-captures, which causes the complexity)
\r
1568 case F: // Lion power + 3-step (as in FF)
\r
1569 case S: // Lion power + ranging (as in BS)
\r
1572 NewCapture(x, sqr + victimValue - SORTKEY(attacker), p[attacker].promoFlag);
\r
1574 // now the multi-captures of designated victim together with lower-valued piece
\r
1575 if(d == 2) { // primary victim on second ring; look for victims to take in passing
\r
1576 if((board[sqr+v] & TYPE) == xstm && board[sqr+v] > board[sqr])
\r
1577 NewCapture(x, SPECIAL + 9*i + victimValue - SORTKEY(attacker), p[attacker].promoFlag);
\r
1578 if((i&1) == 0) { // orthogonal: two extra bent paths
\r
1580 if((board[x+v] & TYPE) == xstm && board[x+v] > board[sqr])
\r
1581 NewCapture(x, SPECIAL + 8*(i-1&7) + (i+1&7) + victimValue - SORTKEY(attacker), p[attacker].promoFlag);
\r
1583 if((board[x+v] & TYPE) == xstm && board[x+v] > board[sqr])
\r
1584 NewCapture(x, SPECIAL + 8*(i+1&7) + (i-1&7) + victimValue - SORTKEY(attacker), p[attacker].promoFlag);
\r
1586 } else { // primary victim on first ring
\r
1588 for(j=0; j<8; j++) { // we can go on in 8 directions after we captured it in passing
\r
1590 if(sqr + v == x || board[sqr+v] == EMPTY) // hit & run; make sure we include igui (attacker is still at x!)
\r
1591 NewCapture(x, SPECIAL + 8*i + j + victimValue, p[attacker].promoFlag);
\r
1592 else if((board[sqr+v] & TYPE) == xstm && board[sqr+v] > board[sqr]) { // double capture
\r
1593 NewCapture(x, SPECIAL + 8*i + j + victimValue, p[attacker].promoFlag); // other victim after primary
\r
1594 if(dist[sqr+v-x] == 1) // other victim also on first ring; reverse order is possible
\r
1595 NewCapture(x, SPECIAL + reverse[8*i + j] + victimValue, p[attacker].promoFlag);
\r
1600 case D: // linear Lion move (as in HF, SE)
\r
1602 NewCapture(x, sqr + victimValue - SORTKEY(attacker), p[attacker].promoFlag);
\r
1604 if(d == 2) { // check if we can take intermediate with it
\r
1605 if((board[x-v] & TYPE) == xstm && board[x-v] > board[sqr])
\r
1606 NewCapture(x, SPECIAL + 9*i + victimValue - SORTKEY(attacker), p[attacker].promoFlag); // e.p.
\r
1607 } else { // d=1; can move on to second, or move back for igui
\r
1608 NewCapture(x, SPECIAL + 8*i + (i^4) + victimValue, p[attacker].promoFlag); // igui
\r
1609 if(board[sqr-v] == EMPTY || (board[sqr-v] & TYPE) == xstm && board[sqr-v] > board[sqr])
\r
1610 NewCapture(x, SPECIAL + 9*i + victimValue - SORTKEY(attacker), p[attacker].promoFlag); // hit and run
\r
1613 case J: // plain jump (as in KY, PH)
\r
1615 NewCapture(x, sqr + victimValue - SORTKEY(attacker), p[attacker].promoFlag);
\r
1618 case C: // FIDE Pawn
\r
1620 NewCapture(x, sqr + victimValue - SORTKEY(attacker), p[attacker].promoFlag);
\r
1624 //printf("mask[%d] = %o\n", i, att);
\r
1625 if((att & attackMask[i]) == 0) break;
\r
1627 // more attacks to come; san for next stop
\r
1628 if(jcapt < p[board[x]].qval) jcapt = p[board[x]].qval; // raise barrier for range jumpers further upstream
\r
1629 while(board[x+=v] == EMPTY); // this should never run off-board, if the attack map is not corrupted
\r
1633 // off-ray attacks
\r
1634 if(att & 0700000000) { // Knight attack
\r
1635 for(i=0; i<8; i++) { // scan all knight jumps to locate source
\r
1636 int x = sqr - nStep[i], attacker = board[x];
\r
1637 if(attacker == EMPTY || (attacker & TYPE) != stm) continue;
\r
1638 if(p[attacker].range[i] <= L && p[attacker].range[i] >= S || p[attacker].range[i] == N) { // has Knight jump in our direction
\r
1639 NewCapture(x, sqr + victimValue, p[attacker].promoFlag); // plain jump (as in N)
\r
1640 if(p[attacker].range[i] < N) { // Lion power; generate double captures over two possible intermediates
\r
1641 int v = kStep[i]; // leftish path
\r
1642 if((board[x+v] & TYPE) == xstm && board[x+v] > board[sqr])
\r
1643 NewCapture(x, SPECIAL + 8*i + (i+1&7) + victimValue, p[attacker].promoFlag);
\r
1644 v = kStep[i+1]; // rightish path
\r
1645 if((board[x+v] & TYPE) == xstm && board[x+v] > board[sqr])
\r
1646 NewCapture(x, SPECIAL + 8*(i+1&7) + i + victimValue, p[attacker].promoFlag);
\r
1654 Evaluate (int difEval)
\r
1656 int wLion, bLion, score=mobilityScore;
\r
1659 #define lionTrap (PST + 2*BH*BW)
\r
1660 // penalty for Lion in enemy corner, when enemy Lion is nearby
\r
1661 if(p[WHITE+2].value == 10*LVAL && (wLion = p[WHITE+2].pos) != ABSENT)
\r
1662 if(p[BLACK+2].value == 10*LVAL && (bLion = p[BLACK+2].pos) != ABSENT) { // both have a Lion
\r
1663 static int distFac[36] = { 0, 0, 10, 9, 8, 7, 5, 3, 1 };
\r
1664 score -= ( (1+9*!attacks[2*wLion+WHITE]) * lionTrap[BW*(BH-1)+BH-1-wLion]
\r
1665 - (1+9*!attacks[2*bLion+BLACK]) * lionTrap[bLion] ) * distFac[dist[wLion - bLion]];
\r
1669 return difEval - (filling*filling*promoDelta >> 16) + (stm ? score : -score);
\r
1673 FireSet (UndoInfo *tb)
\r
1674 { // set fireFlags acording to remaining presene of Fire Demons
\r
1676 for(i=stm+2; p[i].value == 10*FVAL; i++) // Fire Demons are always leading pieces in list
\r
1677 if(p[i].pos != ABSENT) tb->fireMask |= fireFlags[i-2];
\r
1680 void TerminationCheck();
\r
1685 Search (int alpha, int beta, int difEval, int depth, int oldPromo, int promoSuppress, int threshold)
\r
1687 int i, j, k, phase, king, nextVictim, to, defer, autoFail=0;
\r
1688 int firstMove, oldMSP = msp, curMove, sorted, bad, dubious, bestMoveNr;
\r
1689 int resDep, iterDep, ext;
\r
1691 int score, bestScore, oldBest, curEval, iterAlpha;
\r
1692 Move move, nullMove;
\r
1695 Move hashMove; int index, nr, hit;
\r
1697 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
1698 xstm = stm ^ WHITE;
\r
1699 //printf("map made\n");fflush(stdout);
\r
1702 if(tsume && tsume & stm+1) {
\r
1703 k = p[king=royal[stm]].pos;
\r
1704 // if( k == ABSENT) k = p[king + 2].pos;
\r
1705 if( k != ABSENT && !attacks[2*k + xstm]) {
\r
1706 retDep = 60; return INF; // we win when not in check
\r
1711 k = p[king=royal[xstm]].pos;
\r
1712 if( k != ABSENT) {
\r
1713 if(attacks[2*k + stm]) {
\r
1714 if( p[king + 2].pos == ABSENT ) return INF; // we have an attack on his only King
\r
1716 } else { // he has no king! Test for attacks on Crown Prince
\r
1717 k = p[king + 2].pos;
\r
1718 if(attacks[2*k + stm]) return INF; // we have attack on Crown Prince
\r
1720 //printf("King safe\n");fflush(stdout);
\r
1721 // EVALUATION & WINDOW SHIFT
\r
1722 curEval = Evaluate(difEval);
\r
1723 alpha -= (alpha < curEval);
\r
1724 beta -= (beta <= curEval);
\r
1726 if(!(nodes++ & 4095)) TerminationCheck();
\r
1727 pv[pvPtr++] = 0; // start empty PV, directly behind PV of parent
\r
1730 firstMove = curMove = sorted = msp += 50; // leave 50 empty slots in front of move list
\r
1731 iterDep = -(depth == 0); tb.fireMask = phase = 0;
\r
1734 index = (hashKeyL ^ 327*stm ^ oldPromo*(63121 + promoSuppress)) & hashMask;
\r
1735 nr = (hashKeyL >> 30) & 3; hit = -1;
\r
1736 if(hashTable[index].lock[nr] == hashKeyH) hit = nr; else
\r
1737 if(hashTable[index].lock[4] == hashKeyH) hit = 4;
\r
1738 if(PATH) printf("# probe hash index=%x hit=%d\n", index, hit),fflush(stdout);
\r
1740 bestScore = hashTable[index].score[hit];
\r
1741 hashMove = hashTable[index].move[hit];
\r
1742 if((bestScore <= alpha || hashTable[index].flag[hit] & H_LOWER) &&
\r
1743 (bestScore >= beta || hashTable[index].flag[hit] & H_UPPER) ) {
\r
1744 iterDep = resDep = hashTable[index].depth[hit]; bestMoveNr = 0;
\r
1745 if(!level) iterDep = 0; // no hash cutoff in root
\r
1747 } else { // decide on replacement
\r
1748 if(depth >= hashTable[index].depth[nr]) hit = nr; else hit = 4;
\r
1751 if(PATH) printf("# iterDep = %d score = %d move = %s\n",iterDep,bestScore,MoveToText(hashMove,0)),fflush(stdout);
\r
1754 if(depth > QSdepth && iterDep < QSdepth) iterDep = QSdepth; // full-width: start at least from 1-ply
\r
1755 if(PATH)printf("iterDep=%d\n", iterDep);
\r
1756 while(++iterDep <= depth) {
\r
1757 if(flag && depth>= 0) printf("iter %d:%d\n", depth,iterDep),fflush(stdout);
\r
1758 oldBest = bestScore;
\r
1759 iterAlpha = alpha; bestScore = -INF; bestMoveNr = 0; resDep = 60;
\r
1760 if(PATH)printf("new iter %d\n", iterDep);
\r
1761 if(depth <= QSdepth) {
\r
1762 bestScore = curEval; resDep = QSdepth;
\r
1763 if(bestScore > alpha) {
\r
1764 alpha = bestScore;
\r
1765 if(PATH)printf("stand pat %d\n", bestScore);
\r
1766 if(bestScore >= beta) goto cutoff;
\r
1769 for(curMove = firstMove; ; curMove++) { // loop over moves
\r
1770 if(flag && depth>= 0) printf("phase=%d: first/curr/last = %d / %d / %d\n", phase, firstMove, curMove, msp);fflush(stdout);
\r
1772 if(curMove >= msp) { // we ran out of moves; generate some new
\r
1773 if(PATH)printf("new moves, phase=%d\n", phase);
\r
1775 case 0: // null move
\r
1777 if(depth > QSdepth && curEval >= beta) {
\r
1778 int nullDep = depth - 3;
\r
1780 score = -Search(-beta, 1-beta, -difEval, nullDep<QSdepth ? QSdepth : nullDep, promoSuppress & SQUARE, ABSENT, INF);
\r
1782 if(score >= beta) { msp = oldMSP; retDep += 3; return score + (score < curEval); }
\r
1785 if(tenFlag) FireSet(&tb); // in tenjiku we must identify opposing Fire Demons to perform any moves
\r
1786 //if(PATH) printf("mask=%x\n",tb.fireMask),pbytes(fireBoard);
\r
1788 case 1: // hash move
\r
1791 if(hashMove && (depth > QSdepth || // must be capture in QS
\r
1792 (hashMove & SQUARE) >= SPECIAL || board[hashMove & SQUARE] != EMPTY)) {
\r
1793 moveStack[sorted = msp++] = hashMove;
\r
1797 case 2: // capture-gen init
\r
1798 nextVictim = xstm; autoFail = (depth == 0);
\r
1800 case 3: // generate captures
\r
1801 if(PATH) printf("%d:%2d:%2d next victim %d/%d\n",level,depth,iterDep,curMove,msp);
\r
1802 while(nextVictim < last[xstm]) { // more victims exist
\r
1803 int group, to = p[nextVictim += 2].pos; // take next
\r
1804 if(to == ABSENT) continue; // ignore if absent
\r
1805 if(!attacks[2*to + stm]) continue; // skip if not attacked
\r
1806 group = p[nextVictim].value; // remember value of this found victim
\r
1807 if(iterDep <= QSdepth + 1 && 2*group + curEval + 30 < alpha) { resDep = QSdepth + 1; goto cutoff; }
\r
1808 if(PATH) printf("%d:%2d:%2d group=%d, to=%c%d\n",level,depth,iterDep,group,to%BW+'a',to/BW+ONE);
\r
1810 if(PATH) printf("%d:%2d:%2d first=%d msp=%d\n",level,depth,iterDep,firstMove,msp);
\r
1811 while(nextVictim < last[xstm] && p[nextVictim+2].value == group) { // more victims of same value exist
\r
1812 to = p[nextVictim += 2].pos; // take next
\r
1813 if(to == ABSENT) continue; // ignore if absent
\r
1814 if(!attacks[2*to + stm]) continue; // skip if not attacked
\r
1815 if(PATH) printf("%d:%2d:%2d p=%d, to=%c%d\n",level,depth,iterDep,nextVictim,to%BW+'a',to/BW+ONE);
\r
1817 if(PATH) printf("%d:%2d:%2d msp=%d\n",level,depth,iterDep,msp);
\r
1819 if(PATH) printf("captures on %d generated, msp=%d, group=%d, threshold=%d\n", nextVictim, msp, group, threshold);
\r
1820 goto extractMove; // in auto-fail phase, only search if they might auto-fail-hi
\r
1822 if(PATH) printf("# autofail=%d\n", autoFail);
\r
1823 if(autoFail) { // non-captures cannot auto-fail; flush queued captures first
\r
1824 if(PATH) printf("# autofail end (%d-%d)\n", firstMove, msp);
\r
1825 autoFail = 0; curMove = firstMove - 1; continue; // release stashed moves for search
\r
1827 // if(currentVariant == V_CHESS && promoSuppress != ABSENT) { // e.p.
\r
1828 // int n = board[promoSuppress-1];
\r
1829 // if( n != EMPTY && (n&TYPE) == xstm && p[n].value == 8 )
\r
1831 phase = 4; // out of victims: all captures generated
\r
1832 case 4: // dubious captures
\r
1834 while( dubious < framePtr + 250 ) // add dubious captures back to move stack
\r
1835 moveStack[msp++] = moveStack[dubious++];
\r
1836 if(curMove != msp) break;
\r
1839 case 5: // killers
\r
1840 if(depth <= QSdepth) { if(resDep > QSdepth) resDep = QSdepth; goto cutoff; }
\r
1842 case 6: // non-captures
\r
1844 nullMove = GenNonCapts(oldPromo);
\r
1846 { // swap killers to front
\r
1847 Move h = killer[level][0]; int j = curMove;
\r
1848 for(i=curMove; i<msp; i++) if(moveStack[i] == h) { moveStack[i] = moveStack[j]; moveStack[j++] = h; break; }
\r
1849 h = killer[level][1];
\r
1850 for(i=curMove; i<msp; i++) if(moveStack[i] == h) { moveStack[i] = moveStack[j]; moveStack[j++] = h; break; }
\r
1854 sorted = msp; // do not sort noncapts
\r
1856 case 7: // bad captures
\r
1857 case 8: // PV null move
\r
1859 if(nullMove != ABSENT) {
\r
1860 moveStack[msp++] = nullMove + (nullMove << SQLEN) | DEFER; // kludge: setting DEFER guarantees != 0, and has no effect
\r
1862 //printf("# %d. sqr = %08x null = %08x\n", msp, nullMove, moveStack[msp-1]);
\r
1868 // MOVE EXTRACTION
\r
1870 //if(flag & depth >= 0 || (PATH)) printf("%2d:%d extract %d/%d\n", depth, iterDep, curMove, msp);
\r
1871 if(curMove > sorted) {
\r
1872 move = moveStack[sorted=j=curMove];
\r
1873 for(i=curMove+1; i<msp; i++)
\r
1874 if(moveStack[i] > move) move = moveStack[j=i]; // search move with highest priority
\r
1875 moveStack[j] = moveStack[curMove]; moveStack[curMove] = move; // swap highest-priority move to front of remaining
\r
1876 if(move == 0) { msp = curMove--; continue; } // all remaining moves must be 0; clip off move list
\r
1878 move = moveStack[curMove];
\r
1879 if(move == 0) continue; // skip invalidated move
\r
1881 if(flag & depth >= 0) printf("%2d:%d found %d/%d %08x %s\n", depth, iterDep, curMove, msp, moveStack[curMove], MoveToText(moveStack[curMove], 0));
\r
1885 defer = MakeMove(move, &tb);
\r
1886 ext = (depth == 0); // when out of depth we extend captures if there was no auto-fail-hi
\r
1888 // if(level == 1 && randomize) tb.booty += (hashKeyH * seed >> 24 & 31) - 20;
\r
1891 UnMake(&tb); // never search moves during auto-fail phase
\r
1892 xstm = stm; stm ^= WHITE;
\r
1893 if(tb.gain <= threshold) { // we got to moves that cannot possibly auto-fail
\r
1894 autoFail = 0; curMove = firstMove-1; continue; // release all for search
\r
1896 if(tb.gain - tb.loss > threshold) {
\r
1897 bestScore = INF+1; resDep = 0; goto leave; // auto-fail-hi
\r
1898 } else continue; // ignore for now if not obvious refutation
\r
1901 if(flag & depth >= 0) printf("%2d:%d made %d/%d %s\n", depth, iterDep, curMove, msp, MoveToText(moveStack[curMove], 0));
\r
1902 for(i=2; i<=cnt50; i+=2) if(repStack[level-i+200] == hashKeyH) {
\r
1903 if(repDraws) { score = 0; goto repetition; }
\r
1904 moveStack[curMove] = 0; // erase forbidden move
\r
1905 if(!level) repeatMove[repCnt++] = move & 0xFFFFFF; // remember outlawed move
\r
1906 score = -INF; moveStack[curMove] = 0; goto repetition;
\r
1908 repStack[level+200] = hashKeyH;
\r
1910 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
1911 path[level++] = move;
\r
1912 attacks += 2*bsize;
\r
1913 MapFromScratch(attacks); // for as long as incremental update does not work.
\r
1914 //if(flag & depth >= 0) printf("%2d:%d mapped %d/%d %s\n", depth, iterDep, curMove, msp, MoveToText(moveStack[curMove], 0));
\r
1915 //if(PATH) pmap(attacks, stm);
\r
1916 if(chuFlag && p[tb.victim].value == 10*LVAL) {// verify legality of Lion capture in Chu Shogi
\r
1918 if(p[tb.piece].value == 10*LVAL) { // Ln x Ln: can make Ln 'vulnerable' (if distant and not through intemediate > GB)
\r
1919 if(dist[tb.from-tb.to] != 1 && attacks[2*tb.to + stm] && p[tb.epVictim[0]].value <= 50)
\r
1920 score = -INF; // our Lion is indeed made vulnerable and can be recaptured
\r
1921 } else { // other x Ln
\r
1922 if(promoSuppress & PROMOTE) score = -INF; // non-Lion captures Lion after opponent did same
\r
1923 defer |= PROMOTE; // if we started, flag he cannot do it in reply
\r
1925 if(score == -INF) {
\r
1926 if(level == 1) repeatMove[repCnt++] = move & 0xFFFFFF | (p[tb.piece].value == 10*LVAL ? 3<<24 : 1 << 24);
\r
1927 moveStack[curMove] = 0; // zap illegal moves
\r
1932 score = -Search(-beta, -iterAlpha, -difEval - tb.booty, iterDep-1+ext, promoSuppress & ~PROMOTE, defer, depth ? INF : tb.gain);
\r
1937 attacks -= 2*bsize;
\r
1941 xstm = stm; stm ^= WHITE;
\r
1942 if(abortFlag > 0) { // unwind search
\r
1943 printf("# abort (%d) @ %d\n", abortFlag, level);
\r
1944 if(curMove == firstMove) bestScore = oldBest, bestMoveNr = firstMove; // none searched yet
\r
1948 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
1950 // ALPHA-BETA STUFF
\r
1951 if(score > bestScore) {
\r
1952 bestScore = score; bestMoveNr = curMove;
\r
1953 if(score > iterAlpha) {
\r
1954 iterAlpha = score;
\r
1955 if(curMove < firstMove + 5) { // if not too much work, sort move to front
\r
1957 for(i=curMove; i>firstMove; i--) {
\r
1958 moveStack[i] = moveStack[i-1];
\r
1960 moveStack[firstMove] = move;
\r
1961 } else { // late move appended in front of list, and leaves hole in list
\r
1962 moveStack[--firstMove] = move;
\r
1963 moveStack[curMove] = 0;
\r
1965 bestMoveNr = firstMove;
\r
1966 if(score >= beta) { // beta cutoff
\r
1968 if(iterDep == depth && move != killer[level][0]
\r
1969 && (tb.victim == EMPTY && (move & SQUARE) < SPECIAL)) {
\r
1971 killer[level][1] = killer[level][0]; killer[level][0] = move;
\r
1974 resDep = retDep+1-ext;
\r
1978 for(pvPtr = myPV+1; pv[pvPtr++] = pv[i++]; ); // copy daughter PV
\r
1979 pv[myPV] = move; // behind our move (pvPtr left at end of copy)
\r
1984 if(retDep+1-ext < resDep) resDep = retDep+1-ext;
\r
1988 if(!level) { // root node
\r
1989 lastRootIter = GetTickCount() - startTime;
\r
1990 if(postThinking > 0) {
\r
1991 int i; // WB thinking output
\r
1992 printf("%d %d %d %d", iterDep-QSdepth, bestScore, lastRootIter/10, nodes);
\r
1993 if(ponderMove) printf(" (%s)", MoveToText(ponderMove, 0));
\r
1994 for(i=0; pv[i]; i++) printf(" %s", MoveToText(pv[i], 0));
\r
1995 if(iterDep == QSdepth+1) printf(" { root eval = %4.2f dif = %4.2f; abs = %4.2f f=%d D=%4.2f/%4.2f}", curEval/100., difEval/100., PSTest()/100., filling, promoDelta/100., Dtest()/100.);
\r
1999 if(!(abortFlag & 1) && GetTickCount() - startTime > tlim1) break; // do not start iteration we can (most likely) not finish
\r
2001 if(resDep > iterDep) iterDep = resDep; // skip iterations if we got them for free
\r
2004 hashTable[index].lock[hit] = hashKeyH;
\r
2005 hashTable[index].depth[hit] = iterDep;
\r
2006 hashTable[index].score[hit] = bestScore;
\r
2007 hashTable[index].flag[hit] = (bestScore < beta) * H_UPPER;
\r
2008 if(bestScore > alpha) {
\r
2009 hashTable[index].flag[hit] |= H_LOWER;
\r
2010 hashTable[index].move[hit] = bestMoveNr ? moveStack[bestMoveNr] : 0;
\r
2011 } else hashTable[index].move[hit] = 0;
\r
2016 retFirst = firstMove;
\r
2017 msp = oldMSP; // pop move list
\r
2018 pvPtr = myPV; // pop PV
\r
2019 retMove = bestMoveNr ? moveStack[bestMoveNr] : 0;
\r
2021 if(PATH) printf("return %d: %d %d (t=%d s=%d lim=%d)\n", depth, bestScore, curEval, GetTickCount(), startTime, tlim1),fflush(stdout);
\r
2022 return bestScore + (bestScore < curEval);
\r
2029 for(i=0; i<182; i++) {
\r
2030 printf("%3d. %3d %3d %4d %02x %d %x %3d ", i, p[i].value, p[i].promo, p[i].pos, p[i].promoFlag&255, p[i].qval, p[i].bulk, p[i].promoGain);
\r
2031 for(j=0; j<8; j++) printf(" %2d", p[i].range[j]);
\r
2032 if(i<2 || i>11) printf("\n"); else printf(" %02x\n", fireFlags[i-2]&255);
\r
2034 printf("last: %d / %d\nroyal %d / %d\n", last[WHITE], last[BLACK], royal[WHITE], royal[BLACK]);
\r
2041 for(i=BH+2; i>-4; i--) {
\r
2043 for(j=-3; j<BH+3; j++) printf("%4d", b[BW*i+j]);
\r
2049 pbytes (unsigned char *b)
\r
2052 for(i=BH-1; i>=0; i--) {
\r
2053 for(j=0; j<BH; j++) printf("%3x", b[BW*i+j]);
\r
2059 pmap (int *m, int col)
\r
2062 for(i=BH-1; i>=0; i--) {
\r
2064 for(j=0; j<BH; j++) printf("%10o", m[2*(BW*i+j)+col]);
\r
2070 pmoves(int start, int end)
\r
2073 printf("# move stack from %d to %d\n", start, end);
\r
2074 for(i=start; i<end; i++) {
\r
2076 f = m>>SQLEN & SQUARE;
\r
2078 printf("# %3d. %08x %3d-%3d %s\n", i, m, f, t, MoveToText(m, 0));
\r
2082 /********************************************************/
\r
2083 /* Example of a WinBoard-protocol driver, by H.G.Muller */
\r
2084 /********************************************************/
\r
2086 #include <stdio.h>
\r
2088 // four different constants, with values for WHITE and BLACK that suit your engine
\r
2092 // some value that cannot occur as a valid move
\r
2095 // some parameter of your engine
\r
2096 #define MAXMOVES 2000 /* maximum game length */
\r
2097 #define MAXPLY 30 /* maximum search depth */
\r
2102 typedef Move MOVE;
\r
2104 int moveNr; // part of game state; incremented by MakeMove
\r
2105 MOVE gameMove[MAXMOVES]; // holds the game history
\r
2107 // Some routines your engine should have to do the various essential things
\r
2108 int MakeMove2(int stm, MOVE move); // performs move, and returns new side to move
\r
2109 void UnMake2(MOVE move); // unmakes the move;
\r
2110 int Setup2(char *fen); // sets up the position from the given FEN, and returns the new side to move
\r
2111 void SetMemorySize(int n); // if n is different from last time, resize all tables to make memory usage below n MB
\r
2112 char *MoveToText(MOVE move, int m); // converts the move from your internal format to text like e2e2, e1g1, a7a8q.
\r
2113 MOVE ParseMove(char *moveText); // converts a long-algebraic text move to your internal move format
\r
2114 int SearchBestMove(MOVE *move, MOVE *ponderMove);
\r
2115 void PonderUntilInput(int stm); // Search current position for stm, deepening forever until there is input.
\r
2117 UndoInfo undoInfo;
\r
2118 int sup0, sup1, sup2; // promo suppression squares
\r
2119 int lastLift, lastPut;
\r
2122 MakeMove2 (int stm, MOVE move)
\r
2125 FireSet(&undoInfo);
\r
2126 sup0 = sup1; sup1 = sup2;
\r
2127 sup2 = MakeMove(move, &undoInfo);
\r
2128 if(chuFlag && p[undoInfo.victim].value == 10*LVAL && p[undoInfo.piece].value != 10*LVAL) sup2 |= PROMOTE;
\r
2129 rootEval = -rootEval - undoInfo.booty;
\r
2130 for(i=0; i<200; i++) repStack[i] = repStack[i+1];
\r
2131 repStack[199] = hashKeyH;
\r
2132 printf("# makemove %08x %c%d %c%d\n", move, sup1%BW+'a', sup1/BW, sup2%BW+'a', sup2/BW);
\r
2133 return stm ^ WHITE;
\r
2137 UnMake2 (MOVE move)
\r
2140 rootEval = -rootEval - undoInfo.booty;
\r
2141 UnMake(&undoInfo);
\r
2142 for(i=200; i>0; i--) repStack[i] = repStack[i-1];
\r
2143 sup2 = sup1; sup1 = sup0;
\r
2146 char fenNames[] = "RV....DKDEFL..DHGB......SMLNKN..FK....BT..VM..PH...."; // pairs of char
\r
2147 char fenPromo[] = "WLDHSMSECPB R HFDE....WHFB..LNG ..DKVMFS..FO..FK...."; // pairs of char
\r
2150 Convert (char *fen)
\r
2152 char *p = fenArray, *q, *rows[36], tmp[4000];
\r
2154 printf("# convert FEN '%s'\n", fen);
\r
2155 q = strchr(fen, ' '); if(q) *q = 0; q = fen;
\r
2156 do { rows[n++] = q; q = strchr(q, '/'); if(!q) break; *q++ = 0; } while(1);
\r
2158 while(--n >= 0) { strcat(tmp, rows[n]); if(n) strcat(tmp, "/"); }
\r
2160 printf("# flipped FEN '%s'\n", fen);
\r
2162 if(*fen == ' ') { *p = 0; break; }
\r
2163 if(n=atoi(fen)) fen++; // digits read
\r
2164 if(n > 9) fen++; // double digit
\r
2165 while(n-- > 0) *p++ = '.'; // expand to empty squares
\r
2166 if(isalpha(*fen)) {
\r
2167 char *table = fenNames;
\r
2168 n = *fen > 'Z' ? 'a' - 'A' : 0;
\r
2169 if((currentVariant == V_CHESS || currentVariant == V_SHATRANJ || currentVariant == V_MAKRUK) && *fen - n == 'N' // In Chess N is Knight, not Lion
\r
2170 || table[2* (*fen - 'A' - n)] == '.') *p++ = *fen; else {
\r
2172 *p++ = table[2* (*fen - 'A' - n)] + n;
\r
2173 *p++ = table[2* (*fen - 'A' - n)+1] + n;
\r
2175 } else *p++ = *fen;
\r
2178 printf("# converted FEN '%s'\n", fenArray);
\r
2183 Setup2 (char *fen)
\r
2187 char *q = strchr(fen, '\n');
\r
2189 if(q = strchr(fen, ' ')) stm = (q[1] == 'b' ? BLACK : WHITE); // fen contains color field
\r
2190 if(strchr(fen, '.') || strchr(fen, ':')) array = fen; else array = Convert(fen);
\r
2192 rootEval = promoDelta = filling = cnt50 = moveNr = 0;
\r
2193 SetUp(array, currentVariant);
\r
2194 sup0 = sup1 = sup2 = ABSENT;
\r
2195 hashKeyH = hashKeyL = 87620895*currentVariant;
\r
2200 SetMemorySize (int n)
\r
2203 static HashBucket *realHash;
\r
2204 static intptr_t oldSize;
\r
2205 intptr_t l, m = 1;
\r
2206 while(m*sizeof(HashBucket) <= n*512UL) m <<= 1; // take largest power-of-2 that fits
\r
2207 if(m != oldSize) {
\r
2208 if(oldSize) free(realHash);
\r
2209 hashMask = m*1024 - 1; oldSize = m;
\r
2210 realHash = malloc(m*1024*sizeof(HashBucket) + 64);
\r
2211 l = (intptr_t) realHash; hashTable = (HashBucket*) (l & ~63UL); // align with cache line
\r
2217 MoveToText (MOVE move, int multiLine)
\r
2219 static char buf[50];
\r
2220 int f = move>>SQLEN & SQUARE, g = f, t = move & SQUARE;
\r
2221 char *promoChar = "";
\r
2222 if(f == t) { sprintf(buf, "@@@@"); return buf; } // null-move notation in WB protocol
\r
2224 if(t >= SPECIAL) { // kludgy! Print as side effect non-standard WB command to remove victims from double-capture (breaks hint command!)
\r
2225 int e = f + epList[t - SPECIAL];
\r
2226 // printf("take %c%d\n", e%BW+'a', e/BW+ONE);
\r
2227 sprintf(buf, "%c%d%c%d,", f%BW+'a', f/BW+ONE, e%BW+'a', e/BW+ONE); f = e;
\r
2228 if(multiLine) printf("move %s\n", buf), buf[0] = '\0';
\r
2229 if(ep2List[t - SPECIAL]) {
\r
2230 e = g + ep2List[t - SPECIAL];
\r
2231 // printf("take %c%d\n", e%BW+'a', e/BW+ONE);
\r
2232 sprintf(buf+strlen(buf), "%c%d%c%d,", f%BW+'a', f/BW+ONE, e%BW+'a', e/BW+ONE); f = e;
\r
2233 if(multiLine) printf("move %s\n", buf), buf[0] = '\0';
\r
2235 t = g + toList[t - SPECIAL];
\r
2237 if(move & PROMOTE) promoChar = currentVariant == V_MAKRUK ? "m" : repDraws ? "q" : "+";
\r
2238 sprintf(buf+strlen(buf), "%c%d%c%d%s", f%BW+'a', f/BW+ONE, t%BW+'a', t/BW+ONE, promoChar);
\r
2243 ReadSquare (char *p, int *sqr)
\r
2247 r = atoi(p + 1) - ONE;
\r
2249 return 2 + (r + ONE > 9);
\r
2252 int listStart, listEnd;
\r
2253 char boardCopy[BSIZE];
\r
2257 { // create move list on move stack
\r
2259 for(i=0; i< BSIZE; i++) boardCopy[i] = !!board[i];
\r
2260 MapFromScratch(attacks);
\r
2261 postThinking--; repCnt = 0; tlim1 = tlim2 = tlim3 = 1e8; abortFlag = msp = 0;
\r
2262 Search(-INF-1, INF+1, 0, QSdepth+1, sup1 & ~PROMOTE, sup2, INF);
\r
2264 listStart = retFirst; listEnd = msp = retMSP;
\r
2268 ParseMove (char *moveText)
\r
2270 int i, j, f, t, t2, e, ret, deferred=0;
\r
2271 char c = moveText[0];
\r
2272 moveText += ReadSquare(moveText, &f);
\r
2273 moveText += ReadSquare(moveText, &t); t2 = t;
\r
2274 if(*moveText == ',') {
\r
2276 moveText += ReadSquare(moveText, &e);
\r
2277 if(e != t) return INVALID; // must continue with same piece
\r
2279 moveText += ReadSquare(moveText, &t);
\r
2280 for(i=0; i<8; i++) if(f + kStep[i] == e) break;
\r
2281 if(i >= 8) return INVALID; // this rejects Lion Dog 2+1 and 2-1 moves!
\r
2282 for(j=0; j<8; j++) if(e + kStep[j] == t) break;
\r
2283 if(j >= 8) return INVALID; // this rejects Lion Dog 1+2 moves!
\r
2284 t2 = SPECIAL + 8*i + j;
\r
2286 ret = f<<SQLEN | t2;
\r
2287 if(*moveText != '\n' && *moveText != '=') ret |= PROMOTE;
\r
2288 printf("# suppress = %c%d\n", sup1%BW+'a', sup1/BW);
\r
2290 for(i=listStart; i<listEnd; i++) {
\r
2291 if(moveStack[i] == INVALID) continue;
\r
2292 if(c == '@' && (moveStack[i] & SQUARE) == (moveStack[i] >> SQLEN & SQUARE)) break; // any null move matches @@@@
\r
2293 if((moveStack[i] & (PROMOTE | DEFER-1)) == ret) break;
\r
2294 if((moveStack[i] & DEFER-1) == ret) deferred = i; // promoted version of entered non-promotion is legal
\r
2296 printf("# moveNr = %d in {%d,%d}\n", i, listStart, listEnd);
\r
2297 if(i>=listEnd) { // no exact match
\r
2298 if(deferred) { // but maybe non-sensical deferral
\r
2299 int flags = p[board[f]].promoFlag;
\r
2300 printf("# deferral of %d\n", deferred);
\r
2301 i = deferred; // in any case we take that move
\r
2302 if(!(flags & promoBoard[t] & (CANT_DEFER | LAST_RANK))) { // but change it into a deferral if that is allowed
\r
2303 moveStack[i] &= ~PROMOTE;
\r
2304 if(p[board[f]].value == 40) p[board[f]].promoFlag &= LAST_RANK; else
\r
2305 if(!(flags & promoBoard[f])) moveStack[i] |= DEFER; // came from outside zone, so essential deferral
\r
2308 if(i >= listEnd) {
\r
2309 for(i=listStart; i<listEnd; i++) printf("# %d. %08x %08x %s\n", i-50, moveStack[i], ret, MoveToText(moveStack[i], 0));
\r
2311 for(i=0; i<repCnt; i++) {if((repeatMove[i] & 0xFFFFFF) == ret) {
\r
2312 if(repeatMove[i] & 1<<24) reason = (repeatMove[i] & 1<<25 ? "Distant capture of protected Lion" : "Counterstrike against Lion");
\r
2313 else reason = "Repeats earlier position";
\r
2316 printf("# %d. %08x %08x %s\n", i, repeatMove[i], ret, MoveToText(repeatMove[i], 0));
\r
2320 return (i >= listEnd ? INVALID : moveStack[i]);
\r
2324 Highlight(char *coords)
\r
2326 int i, j, n, sqr, cnt=0;
\r
2327 char b[BSIZE], buf[2000], *q;
\r
2328 for(i=0; i<bsize; i++) b[i] = 0;
\r
2329 ReadSquare(coords, &sqr);
\r
2331 for(i=listStart; i<listEnd; i++) {
\r
2332 if(sqr == (moveStack[i]>>SQLEN & SQUARE)) {
\r
2333 int t = moveStack[i] & SQUARE;
\r
2334 if(t >= SPECIAL) continue;
\r
2335 b[t] = (!boardCopy[t] ? 'Y' : 'R'); cnt++;
\r
2338 if(!cnt) { // no moves from given square
\r
2339 if(sqr != lastPut) return; // refrain from sending empty FEN
\r
2340 // we lifted a piece for second leg of move
\r
2341 for(i=listStart; i<listEnd; i++) {
\r
2342 if(lastLift == (moveStack[i]>>SQLEN & SQUARE)) {
\r
2343 int e, t = moveStack[i] & SQUARE;
\r
2344 if(t < SPECIAL) continue; // only special moves
\r
2345 e = lastLift + epList[t - SPECIAL]; // decode
\r
2346 t = lastLift + toList[t - SPECIAL];
\r
2347 if(e != sqr) continue;
\r
2348 b[t] = (!boardCopy[t] ? 'Y' : 'R'); cnt++;
\r
2352 } else lastLift = sqr; // remember
\r
2355 for(i=BH-1; i>=0; i--) {
\r
2356 for(j=0; j<BH; j++) {
\r
2358 if(b[n]) *q++ = b[n]; else {
\r
2359 if(q > buf && q[-1] <= '9' && q[-1] >= '0') {
\r
2362 if(q > buf+1 && q[-2] <= '9' && q[-2] >= '0') q[-2]++; else q[-1] = '1', *q++ = '0';
\r
2364 } else *q++ = '1';
\r
2370 printf("highlight %s\n", buf);
\r
2373 int timeLeft; // timeleft on engine's clock
\r
2374 int mps, timeControl, inc, timePerMove; // time-control parameters, to be used by Search
\r
2375 char inBuf[8000], command[80], ponderMoveText[20];
\r
2378 SetSearchTimes (int timeLeft)
\r
2380 int targetTime, movesLeft = BW*BH/4 + 20;
\r
2381 if(mps) movesLeft = mps - (moveNr>>1)%mps;
\r
2382 targetTime = (timeLeft - 1000*inc) / (movesLeft + 2) + 1000 * inc;
\r
2383 if(moveNr < 30) targetTime *= 0.5 + moveNr/60.; // speedup in opening
\r
2384 if(timePerMove > 0) targetTime = 0.5*timeLeft, movesLeft = 1;
\r
2385 tlim1 = 0.2*targetTime;
\r
2386 tlim2 = 1.9*targetTime;
\r
2387 tlim3 = 5*timeLeft / (movesLeft + 4.1);
\r
2388 printf("# limits %d, %d, %d mode = %d\n", tlim1, tlim2, tlim3, abortFlag);
\r
2392 SearchBestMove (MOVE *move, MOVE *ponderMove)
\r
2395 printf("# SearchBestMove\n");
\r
2396 startTime = GetTickCount();
\r
2398 printf("# s=%d\n", startTime);fflush(stdout);
\r
2399 MapFromScratch(attacks);
\r
2400 retMove = INVALID; repCnt = 0;
\r
2401 score = Search(-INF-1, INF+1, rootEval, maxDepth, sup1, sup2, INF);
\r
2403 *ponderMove = pv[1];
\r
2404 printf("# best=%s\n", MoveToText(pv[0],0));
\r
2405 printf("# ponder=%s\n", MoveToText(pv[1],0));
\r
2410 int TakeBack(int n)
\r
2411 { // reset the game and then replay it to the desired point
\r
2413 Init(currentVariant); stm = Setup2(NULL);
\r
2414 printf("# setup done");fflush(stdout);
\r
2415 last = moveNr - n; if(last < 0) last = 0;
\r
2416 for(moveNr=0; moveNr<last; moveNr++) stm = MakeMove2(stm, gameMove[moveNr]),printf("make %2d: %x\n", moveNr, gameMove[moveNr]);
\r
2420 void PrintResult(int stm, int score)
\r
2423 if(reason) sprintf(tail, " {%s}", reason); else *tail = 0;
\r
2424 if(score == 0) printf("1/2-1/2%s\n", tail);
\r
2425 if(score > 0 && stm == WHITE || score < 0 && stm == BLACK) printf("1-0%s\n", tail);
\r
2426 else printf("0-1%s\n", tail);
\r
2429 void GetLine(int root)
\r
2433 // wait for input, and read it until we have collected a complete line
\r
2434 for(i = 0; (inBuf[i] = c = getchar()) != '\n'; i++) if(c == EOF || i>7997) exit(0);
\r
2437 // extract the first word
\r
2438 sscanf(inBuf, "%s", command);
\r
2439 printf("# in (mode = %d,%d): %s\n", root, abortFlag, command);
\r
2440 if(!strcmp(command, "otim")) { continue; } // do not start pondering after receiving time commands, as move will follow immediately
\r
2441 if(!strcmp(command, "time")) { sscanf(inBuf, "time %d", &timeLeft); continue; }
\r
2442 if(!strcmp(command, "put")) { ReadSquare(inBuf+4, &lastPut); continue; } // ditto
\r
2443 if(!strcmp(command, ".")) { inBuf[0] = 0; return; } // ignore for now
\r
2444 if(!strcmp(command, "lift")) { inBuf[0] = 0; Highlight(inBuf+5); return; } // treat here
\r
2445 if(!root && !strcmp(command, "usermove")) {
\r
2446 printf("# move = %s#ponder = %s", inBuf+9, ponderMoveText);
\r
2447 abortFlag = !!strcmp(inBuf+9, ponderMoveText);
\r
2448 if(!abortFlag) { // ponder hit, continue as time-based search
\r
2449 printf("# ponder hit\n");
\r
2450 SetSearchTimes(10*timeLeft + GetTickCount() - startTime); // add time we already have been pondering to total
\r
2451 if(lastRootIter > tlim1) abortFlag = 2; // abort instantly if we are in iteration we should not have started
\r
2452 inBuf[0] = 0; ponderMove = INVALID;
\r
2462 TerminationCheck()
\r
2464 if(abortFlag < 0) { // check for input
\r
2465 if(InputWaiting()) GetLine(0); // read & examine input command
\r
2466 } else { // check for time
\r
2467 if(GetTickCount() - startTime > tlim3) abortFlag = 2;
\r
2473 int engineSide=NONE; // side played by engine
\r
2475 int i, score, curVarNr;
\r
2477 Init(V_CHU); // Chu
\r
2478 seed = GetTickCount(); moveNr = 0; // initialize random
\r
2480 while(1) { // infinite loop
\r
2482 fflush(stdout); // make sure everything is printed before we do something that might take time
\r
2483 *inBuf = 0; if(moveNr >= 20) randomize = OFF;
\r
2484 //if(moveNr >20) printf("resign\n");
\r
2489 if(listEnd == 0) ListMoves(); // always maintain a list of legal moves in root position
\r
2490 abortFlag = -(ponder && WHITE+BLACK-stm == engineSide && moveNr); // pondering and opponent on move
\r
2491 if(stm == engineSide || abortFlag && ponderMove) { // if it is the engine's turn to move, set it thinking, and let it move
\r
2492 printf("# start search: stm=%d engine=%d (flag=%d)\n", stm, engineSide, abortFlag);
\r
2494 stm = MakeMove2(stm, ponderMove); // for pondering, play speculative move
\r
2495 gameMove[moveNr++] = ponderMove; // remember in game
\r
2496 sprintf(ponderMoveText, "%s\n", MoveToText(ponderMove, 0)); // for detecting ponder hits
\r
2497 printf("# ponder move = %s", ponderMoveText);
\r
2498 } else SetSearchTimes(10*timeLeft); // for thinking, schedule end time
\r
2500 score = SearchBestMove(&move, &ponderMove);
\r
2501 if(abortFlag == 1) { // ponder search was interrupted (and no hit)
\r
2502 UnMake2(INVALID); moveNr--; stm ^= WHITE; // take ponder move back if we made one
\r
2505 if(move == INVALID) { // game apparently ended
\r
2506 int kcapt = 0, xstm = stm ^ WHITE, king, k = p[king=royal[xstm]].pos;
\r
2507 if( k != ABSENT) { // test if King capture possible
\r
2508 if(attacks[2*k + stm]) {
\r
2509 if( p[king + 2].pos == ABSENT ) kcapt = 1; // we have an attack on his only King
\r
2511 } else { // he has no king! Test for attacks on Crown Prince
\r
2512 k = p[king + 2].pos;
\r
2513 if(attacks[2*k + stm]) kcapt = 1; // we have attack on Crown Prince
\r
2515 if(kcapt) { // print King capture before claiming
\r
2517 printf("move %s\n", MoveToText(moveStack[msp-1], 1));
\r
2518 reason = "king capture";
\r
2519 } else reason = "resign";
\r
2520 engineSide = NONE; // so stop playing
\r
2521 PrintResult(stm, score);
\r
2523 stm = MakeMove2(stm, move); // assumes MakeMove returns new side to move
\r
2524 gameMove[moveNr++] = move; // remember game
\r
2525 printf("move %s\n", MoveToText(move, 1));
\r
2527 continue; // go check if we should ponder
\r
2530 if(engineSide == ANALYZE || abortFlag) { // in analysis, we always ponder the position
\r
2532 *ponderMoveText = 0; // forces miss on any move
\r
2533 abortFlag = -1; // set pondering
\r
2534 SearchBestMove(&dummy, &dummy);
\r
2538 fflush(stdout); // make sure everything is printed before we do something that might take time
\r
2539 if(!*inBuf) GetLine(1); // takes care of time and otim commands
\r
2541 // recognize the command,and execute it
\r
2542 if(!strcmp(command, "quit")) { break; } // breaks out of infinite loop
\r
2543 if(!strcmp(command, "force")) { engineSide = NONE; continue; }
\r
2544 if(!strcmp(command, "analyze")) { engineSide = ANALYZE; continue; }
\r
2545 if(!strcmp(command, "exit")) { engineSide = NONE; continue; }
\r
2546 if(!strcmp(command, "level")) {
\r
2548 sscanf(inBuf, "level %d %d %d", &mps, &min, &inc) == 3 || // if this does not work, it must be min:sec format
\r
2549 sscanf(inBuf, "level %d %d:%d %d", &mps, &min, &sec, &inc);
\r
2550 timeControl = 60*min + sec; timePerMove = -1;
\r
2553 if(!strcmp(command, "protover")){
\r
2554 printf("feature ping=1 setboard=1 colors=0 usermove=1 memory=1 debug=1 sigint=0 sigterm=0\n");
\r
2555 printf("feature variants=\"normal,shatranj,makruk,chu,dai,tenjiku,12x12+0_fairy,9x9+0_shogi\"\n");
\r
2556 printf("feature myname=\"HaChu " VERSION "\" highlight=1\n");
\r
2557 printf("feature option=\"Resign -check 0\"\n"); // example of an engine-defined option
\r
2558 printf("feature option=\"Contempt -spin 0 -200 200\"\n"); // and another one
\r
2559 printf("feature option=\"Tsume -combo no /// White Mates /// Black mates\"\n");
\r
2560 printf("feature done=1\n");
\r
2563 if(!strcmp(command, "option")) { // setting of engine-define option; find out which
\r
2564 if(sscanf(inBuf+7, "Resign=%d", &resign) == 1) continue;
\r
2565 if(sscanf(inBuf+7, "Contempt=%d", &contemptFactor) == 1) continue;
\r
2566 if(sscanf(inBuf+7, "Tsume=%s", command) == 1) {
\r
2567 if(!strcmp(command, "no")) tsume = 0; else
\r
2568 if(!strcmp(command, "White")) tsume = 1; else
\r
2569 if(!strcmp(command, "Black")) tsume = 2;
\r
2574 if(!strcmp(command, "sd")) { sscanf(inBuf, "sd %d", &maxDepth); continue; }
\r
2575 if(!strcmp(command, "st")) { sscanf(inBuf, "st %d", &timePerMove); continue; }
\r
2576 if(!strcmp(command, "memory")) { SetMemorySize(atoi(inBuf+7)); continue; }
\r
2577 if(!strcmp(command, "ping")) { printf("pong%s", inBuf+4); continue; }
\r
2578 // if(!strcmp(command, "")) { sscanf(inBuf, " %d", &); continue; }
\r
2579 if(!strcmp(command, "easy")) { ponder = OFF; continue; }
\r
2580 if(!strcmp(command, "hard")) { ponder = ON; continue; }
\r
2581 if(!strcmp(command, "go")) { engineSide = stm; continue; }
\r
2582 if(!strcmp(command, "post")) { postThinking = ON; continue; }
\r
2583 if(!strcmp(command, "nopost")) { postThinking = OFF;continue; }
\r
2584 if(!strcmp(command, "random")) { randomize = ON; continue; }
\r
2585 if(!strcmp(command, "hint")) { if(ponderMove != INVALID) printf("Hint: %s\n", MoveToText(ponderMove, 0)); continue; }
\r
2586 if(!strcmp(command, "book")) { continue; }
\r
2587 // non-standard commands
\r
2588 if(!strcmp(command, "p")) { pboard(board); continue; }
\r
2589 if(!strcmp(command, "f")) { pbytes(fireBoard); continue; }
\r
2590 if(!strcmp(command, "w")) { MapOneColor(WHITE, last[WHITE], attacks); pmap(attacks, WHITE); continue; }
\r
2591 if(!strcmp(command, "b")) { MapOneColor(BLACK, last[BLACK], attacks); pmap(attacks, BLACK); continue; }
\r
2592 if(!strcmp(command, "l")) { pplist(); continue; }
\r
2593 // ignored commands:
\r
2594 if(!strcmp(command, "xboard")) { continue; }
\r
2595 if(!strcmp(command, "computer")){ comp = 1; continue; }
\r
2596 if(!strcmp(command, "name")) { continue; }
\r
2597 if(!strcmp(command, "ics")) { continue; }
\r
2598 if(!strcmp(command, "accepted")){ continue; }
\r
2599 if(!strcmp(command, "rejected")){ continue; }
\r
2600 if(!strcmp(command, "result")) { continue; }
\r
2601 if(!strcmp(command, "hover")) { continue; }
\r
2602 if(!strcmp(command, "")) { continue; }
\r
2603 if(!strcmp(command, "usermove")){
\r
2604 int move = ParseMove(inBuf+9);
\r
2606 if(move == INVALID) {
\r
2607 if(reason) printf("Illegal move {%s}\n", reason); else printf("%s\n", reason="Illegal move");
\r
2608 if(comp) PrintResult(stm, -INF); // against computer: claim
\r
2610 stm = MakeMove2(stm, move);
\r
2611 ponderMove = INVALID; listEnd = 0;
\r
2612 gameMove[moveNr++] = move; // remember game
\r
2616 ponderMove = INVALID; // the following commands change the position, invalidating ponder move
\r
2618 if(!strcmp(command, "new")) {
\r
2619 engineSide = BLACK; Init(V_CHESS); stm = Setup2(NULL); maxDepth = MAXPLY; randomize = OFF; curVarNr = comp = 0;
\r
2622 if(!strcmp(command, "variant")) {
\r
2623 for(i=0; i<7; i++) {
\r
2624 sscanf(inBuf+8, "%s", command);
\r
2625 if(!strcmp(variants[i].name, command)) {
\r
2626 Init(curVarNr = i); stm = Setup2(NULL); break;
\r
2631 if(!strcmp(command, "setboard")){ engineSide = NONE; Init(curVarNr); stm = Setup2(inBuf+9); continue; }
\r
2632 if(!strcmp(command, "undo")) { stm = TakeBack(1); continue; }
\r
2633 if(!strcmp(command, "remove")) { stm = TakeBack(2); continue; }
\r
2634 printf("Error: unknown command\n");
\r