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.1beta"
\r
16 //define PATH level==0 || level==1 && path[0] == 0x55893
\r
26 # include <windows.h>
\r
28 # include <sys/time.h>
\r
29 int GetTickCount() // with thanks to Tord
\r
31 gettimeofday(&t, NULL);
\r
32 return t.tv_sec*1000 + t.tv_usec/1000;
\r
39 #define BWMAX (2*BHMAX)
\r
40 #define BSIZE BWMAX*BHMAX
\r
48 #define EDGE (1<<11)
\r
49 #define TYPE (WHITE|BLACK|EDGE)
\r
52 #define NPIECES 2000 /* length of piece list */
\r
54 #define SQLEN 11 /* bits in square number */
\r
55 #define SQUARE ((1<<SQLEN) - 1) /* mask for square in move */
\r
56 #define DEFER (1<<2*SQLEN) /* deferral on zone entry */
\r
57 #define PROMOTE (1<<2*SQLEN+1) /* promotion bit in move */
\r
58 #define SPECIAL 1500 /* start of special moves */
\r
59 #define STEP(X,Y) (BW*(X)+ (Y))
\r
60 #define SORTKEY(X) 0
\r
62 #define UPDATE_MOBILITY(X,Y)
\r
63 #define ADDMOVE(X,Y)
\r
66 #define CAN_PROMOTE 0x11
\r
67 #define DONT_DEFER 0x22
\r
68 #define CANT_DEFER 0x44
\r
69 #define LAST_RANK 0x88
\r
70 #define P_WHITE 0x0F
\r
71 #define P_BLACK 0xF0
\r
73 typedef unsigned int Move;
\r
75 char *MoveToText(Move move, int m); // from WB driver
\r
76 void pmap (int *m, int col);
\r
79 char *name, *promoted;
\r
81 signed char range[8];
\r
82 int whiteKey, blackKey;
\r
86 int from, to, piece, victim, new, booty, epSquare, epVictim, ep2Square, ep2Victim, revMoveCount, savKeyL, savKeyH;
\r
89 int bWidth, bHeight, bsize, zone, currentVariant;
\r
90 int stm, xstm, hashKeyH, hashKeyL, framePtr, msp, nonCapts, rootEval, retMSP, retFirst, pvPtr, level, cnt50, chuFlag=1, mobilityScore;
\r
91 int nodes, startTime, tlim1, tlim2;
\r
92 Move retMove, moveStack[10000], path[100], repStack[300], pv[1000];
\r
94 #define X 36 /* slider */
\r
95 #define N -1 /* Knight */
\r
96 #define J -2 /* jump */
\r
97 #define D -3 /* linear double move */
\r
98 #define T -4 /* linear triple move */
\r
99 #define L -5 /* true Lion move */
\r
100 #define F -6 /* Lion + 3-step */
\r
101 #define S -7 /* Lion + range */
\r
102 #define H -9 /* hook move */
\r
104 PieceDesc chuPieces[] = {
\r
105 {"LN", "", 100, { L,L,L,L,L,L,L,L } }, // lion
\r
106 {"FK", "", 60, { X,X,X,X,X,X,X,X } }, // free king
\r
107 {"SE", "", 55, { X,D,X,X,X,X,X,D } }, // soaring eagle
\r
108 {"HF", "", 50, { D,X,X,X,X,X,X,X } }, // horned falcon
\r
109 {"FO", "", 40, { X,X,0,X,X,X,0,X } }, // flying ox
\r
110 {"FB", "", 40, { 0,X,X,X,0,X,X,X } }, // free boar
\r
111 {"DK", "SE", 40, { X,1,X,1,X,1,X,1 } }, // dragon king
\r
112 {"DH", "HF", 35, { 1,X,1,X,1,X,1,X } }, // dragon horse
\r
113 {"WH", "", 35, { X,X,0,0,X,0,0,X } }, // white horse
\r
114 {"R", "DK", 30, { X,0,X,0,X,0,X,0 } }, // rook
\r
115 {"FS", "", 30, { X,1,1,1,X,1,1,1 } }, // flying stag
\r
116 {"WL", "", 25, { X,0,0,X,X,X,0,0 } }, // whale
\r
117 {"K", "", 28, { 1,1,1,1,1,1,1,1 } }, // king
\r
118 {"CP", "", 27, { 1,1,1,1,1,1,1,1 } }, // king
\r
119 {"B", "DH", 25, { 0,X,0,X,0,X,0,X } }, // bishop
\r
120 {"VM", "FO", 20, { X,0,1,0,X,0,1,0 } }, // vertical mover
\r
121 {"SM", "FB", 20, { 1,0,X,0,1,0,X,0 } }, // side mover
\r
122 {"DE", "CP", 20, { 1,1,1,1,0,1,1,1 } }, // drunk elephant
\r
123 {"BT", "FS", 15, { 0,1,1,1,1,1,1,1 } }, // blind tiger
\r
124 {"G", "R", 15, { 1,1,1,0,1,0,1,1 } }, // gold
\r
125 {"FL", "B", 15, { 1,1,0,1,1,1,0,1 } }, // ferocious leopard
\r
126 {"KN", "LN", 15, { J,1,J,1,J,1,J,1 } }, // kirin
\r
127 {"PH", "FK", 15, { 1,J,1,J,1,J,1,J } }, // phoenix
\r
128 {"RV", "WL", 15, { X,0,0,0,X,0,0,0 } }, // reverse chariot
\r
129 {"L", "WH", 15, { X,0,0,0,0,0,0,0 } }, // lance
\r
130 {"S", "VM", 10, { 1,1,0,1,0,1,0,1 } }, // silver
\r
131 {"C", "SM", 10, { 1,1,0,0,1,0,0,1 } }, // copper
\r
132 {"GB", "DE", 5, { 1,0,0,0,1,0,0,0 } }, // go between
\r
133 {"P", "G", 4, { 1,0,0,0,0,0,0,0 } }, // pawn
\r
134 { NULL } // sentinel
\r
137 PieceDesc daiPieces[] = {
\r
138 {"FD", "G", 15, { 0,2,0,2,0,2,0,2 } }, // Flying Dragon
\r
139 {"VO", "G", 20, { 2,0,2,0,2,0,2,0 } }, // Violent Ox
\r
140 {"EW", "G", 8, { 1,1,1,0,0,0,1,1 } }, // Evil Wolf
\r
141 {"CS", "G", 7, { 0,1,0,1,0,1,0,1 } }, // Cat Sword
\r
142 {"AB", "G", 6, { 1,0,1,0,1,0,1,0 } }, // Angry Boar
\r
143 {"I", "G", 8, { 1,1,0,0,0,0,0,1 } }, // Iron
\r
144 {"N", "G", 6, { N,0,0,0,0,0,0,N } }, // Knight
\r
145 {"ST", "G", 5, { 0,1,0,0,0,0,0,1 } }, // Stone
\r
146 { NULL } // sentinel
\r
149 PieceDesc ddPieces[] = {
\r
150 {"LO", "", 1, { 1,H,1,H,1,H,1,H } }, // Long-Nosed Goblin
\r
151 {"OK", "LO", 1, { 2,1,2,0,2,0,2,1 } }, // Old Kite
\r
152 {"PS", "HM", 1, { J,0,1,J,0,J,1,0 } }, // Poisonous Snake
\r
153 {"GE", "", 1, { 3,3,5,5,3,5,5,3 } }, // Great Elephant
\r
154 {"WB", "LD", 1, { 1,1,2,0,1,0,2,1 } }, // Western Barbarian
\r
155 {"EA", "LN", 1, { 2,1,1,0,2,0,1,1 } }, // Eastern Barbarian
\r
156 {"NO", "FE", 1, { 0,2,1,1,0,1,1,2 } }, // Northern Barbarian
\r
157 {"S)", "WE", 1, { 0,1,1,2,0,2,1,1 } }, // Southern Barbarian
\r
158 {"FE", "", 1, { 2,X,2,2,2,2,2,X } }, // Fragrant Elephant
\r
159 {"WE", "", 1, { 2,2,2,X,2,X,2,2 } }, // White Elephant
\r
160 {"FT", "", 1, { X,X,5,0,X,0,5,X } }, // Free Dream-Eater
\r
161 {"FR", "", 1, { 5,X,X,0,5,0,X,X } }, // Free Demon
\r
162 {"WB", "FT", 1, { 2,X,X,X,2,X,X,X } }, // Water Buffalo
\r
163 {"RB", "FR", 1, { X,X,X,X,0,X,X,X } }, // Rushing Bird
\r
164 {"SB", "", 1, { X,X,2,2,2,2,2,X } }, // Standard Bearer
\r
165 {"FH", "FK", 1, { 1,2,1,0,1,0,1,2 } }, // Flying Horse
\r
166 {"NK", "SB", 1, { 1,1,1,1,1,1,1,1 } }, // Neighbor King
\r
167 {"BM", "MW", 1, { 0,1,1,1,0,1,1,1 } }, // Blind Monkey
\r
168 {"DO", "", 1, { 2,X,2,X,2,X,2,X } }, // Dove
\r
169 {"EB", "DO", 1, { 2,0,2,0,0,0,2,0 } }, // Enchanted Badger
\r
170 {"EF", "SD", 1, { 0,2,0,0,2,0,0,2 } }, // Enchanted Fox
\r
171 {"RA", "", 1, { X,0,X,1,X,1,X,0 } }, // Racing Chariot
\r
172 {"SQ", "", 1, { X,1,X,0,X,0,X,1 } }, // Square Mover
\r
173 {"PR", "SQ", 1, { 1,1,2,1,0,1,2,1 } }, // Prancing Stag
\r
174 {"WT", "", 1, { X,1,2,0,X,0,2,X } }, // White Tiger
\r
175 {"BD", "", 1, { 2,X,X,0,2,0,X,1 } }, // Blue Dragon
\r
176 {"HD", "", 1, { X,0,0,0,1,0,0,0 } }, // Howling Dog
\r
177 {"VB", "", 1, { 0,2,1,0,0,0,1,2 } }, // Violent Bear
\r
178 {"SA", "", 1, { 2,1,0,0,2,0,0,1 } }, // Savage Tiger
\r
179 {"W", "", 1, { 0,2,0,0,0,0,0,2 } }, // Wood
\r
180 {"CS", "DH", 7, { 0,1,0,1,0,1,0,1 } }, // cat sword
\r
181 {"FD", "DK", 15, { 0,2,0,2,0,2,0,2 } }, // flying dragon
\r
182 {"KN", "GD", 15, { J,1,J,1,J,1,J,1 } }, // kirin
\r
183 {"PH", "GB", 15, { 1,J,1,J,1,J,1,J } }, // phoenix
\r
184 {"LN", "FF", 100, { L,L,L,L,L,L,L,L } }, // lion
\r
185 {"LD", "GE", 1, { T,T,T,T,T,T,T,T } }, // Lion Dog
\r
186 {"AB", "", 1, { 1,0,1,0,1,0,1,0 } }, // Angry Boar
\r
187 {"B", "", 1, { 0,X,0,X,0,X,0,X } }, // Bishop
\r
188 {"C", "", 1, { 1,1,0,0,1,0,0,1 } }, // Copper
\r
189 {"DH", "", 1, { 1,X,1,X,1,X,1,X } }, // Dragon Horse
\r
190 {"DK", "", 1, { X,1,X,1,X,1,X,1 } }, // Dragon King
\r
191 {"FK", "", 1, { } }, //
\r
192 {"EW", "", 1, { 1,1,1,0,0,0,1,1 } }, // Evil Wolf
\r
193 {"FL", "", 1, { } }, //
\r
194 {"", "", 1, { } }, //
\r
195 {"", "", 1, { } }, //
\r
196 {"", "", 1, { } }, //
\r
197 {"", "", 1, { } }, //
\r
198 { NULL } // sentinel
\r
201 PieceDesc makaPieces[] = {
\r
202 {"DV", "", 1, { 0,1,0,1,0,0,1,1 } }, // Deva
\r
203 {"DS", "", 1, { 0,1,1,0,0,1,0,1 } }, // Dark Spirit
\r
204 {"T", "", 1, { 0,1,0,0,1,0,0,1 } }, // Tile
\r
205 {"CS", "", 1, { 1,0,0,1,1,1,0,0 } }, // Coiled Serpent
\r
206 {"RD", "", 1, { 1,0,1,1,1,1,1,0 } }, // Reclining Dragon
\r
207 {"CC", "", 1, { 0,1,1,0,1,0,1,1 } }, // Chinese Cock
\r
208 {"OM", "", 1, { 0,1,0,1,1,1,0,1 } }, // Old Monkey
\r
209 {"BB", "", 1, { 0,1,0,1,X,1,0,1 } }, // Blind Bear
\r
210 {"OR", "", 1, { 0,2,0,0,2,0,0,2 } }, // Old Rat
\r
211 {"LD", "WS", 1, { T,T,T,T,T,T,T,T } }, // Lion Dog
\r
212 {"WR", "", 1, { 0,3,1,3,0,3,1,3 } }, // Wrestler
\r
213 {"GG", "", 1, { 3,1,3,0,3,0,3,1 } }, // Guardian of the Gods
\r
214 {"BD", "", 1, { 0,3,1,0,1,0,1,3 } }, // Budhist Devil
\r
215 {"SD", "", 1, { 5,2,5,2,5,2,5,2 } }, // She-Devil
\r
216 {"DY", "", 1, { J,0,1,0,J,0,1,0 } }, // Donkey
\r
217 {"CP", "", 1, { 0,H,0,2,0,2,0,H } }, // Capricorn
\r
218 {"HM", "", 1, { H,0,H,0,H,0,H,0 } }, // Hook Mover
\r
219 {"SF", "", 1, { 0,1,X,1,0,1,0,1 } }, // Side Flier
\r
220 {"LC", "", 1, { X,0,0,X,1,0,0,X } }, // Left Chariot
\r
221 {"RC", "", 1, { X,X,0,0,1,X,0,0 } }, // Right Chariot
\r
222 {"FG", "", 1, { X,X,X,0,X,0,X,X } }, // Free Gold
\r
223 {"FS", "", 1, { X,X,0,X,0,X,0,X } }, // Free Silver
\r
224 {"FC", "", 1, { X,X,0,0,X,0,0,X } }, // Free Copper
\r
225 {"FI", "", 1, { X,X,0,0,0,0,0,X } }, // Free Iron
\r
226 {"FT", "", 1, { 0,X,0,0,X,0,0,X } }, // Free Tile
\r
227 {"FN", "", 1, { 0,X,0,0,0,0,0,X } }, // Free Stone
\r
228 {"FTg", "", 1, { 0,X,X,X,X,X,X,X } }, // Free Tiger
\r
229 {"FLp", "", 1, { X,X,0,X,X,X,0,X } }, // Free Leopard (Free Boar?)
\r
230 {"FSp", "", 1, { X,0,0,X,X,X,0,0 } }, // Free Serpent (Whale?)
\r
231 {"FrD", "", 1, { X,0,X,X,X,X,X,0 } }, // Free Dragon
\r
232 {"FC", "", 1, { 0,X,0,X,0,X,0,X } }, // Free Cat (Bishop?)
\r
233 {"EM", "", 1, { } }, // Emperor
\r
234 {"TK", "", 1, { } }, // Teaching King
\r
235 {"BS", "", 1, { } }, // Budhist Spirit
\r
236 {"WS", "", 1, { X,X,0,X,1,X,0,X } }, // Wizard Stork
\r
237 {"MW", "", 1, { 1,X,0,X,X,X,0,X } }, // Mountain Witch
\r
238 {"FF", "", 1, { } }, // Furious Fiend
\r
239 {"GD", "", 1, { 2,3,X,3,2,3,X,3 } }, // Great Dragon
\r
240 {"GB", "", 1, { X,3,2,3,X,3,2,3 } }, // Golden Bird
\r
241 {"FrW", "", 1, { } }, // Free Wolf
\r
242 {"FrB", "", 1, { } }, // Free Bear
\r
243 {"BT", "", 1, { X,0,0,X,0,X,0,0 } }, // Bat
\r
244 {"", "", 1, { } }, //
\r
245 { NULL } // sentinel
\r
248 PieceDesc taiPieces[] = {
\r
249 {"", "", 1, { } }, // Peacock
\r
250 {"", "", 1, { } }, // Vermillion Sparrow
\r
251 {"", "", 1, { } }, // Turtle Snake
\r
252 {"", "", 1, { } }, // Silver Hare
\r
253 {"", "", 1, { } }, // Golden Deer
\r
254 {"", "", 1, { } }, //
\r
255 {"", "", 1, { } }, //
\r
256 {"", "", 1, { } }, //
\r
257 { NULL } // sentinel
\r
260 PieceDesc taikyokuPieces[] = {
\r
261 {"", "", 1, { } }, //
\r
262 { NULL } // sentinel
\r
265 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
266 "/............/............/"
\r
267 "...: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
268 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
269 "/PPPPPPPPPPPPPPP/....:GB.....:GB..../.............../.............../.............../....:gb.....:gb..../ppppppppppppppp/"
\r
270 "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
273 int boardWidth, boardFiles, boardRanks, zoneDepth; // board sizes
\r
274 char *name; // WinBoard name
\r
275 char *array; // initial position
\r
278 VariantDesc variants[] = {
\r
279 { 24, 12, 12, 4, "chu", chuArray }, // Chu
\r
280 { 30, 15, 15, 5, "dai", daiArray }, // Dai
\r
281 { 32, 16, 16, 5, "tenjiku", chuArray }, // Tenjiku
\r
282 { 34, 17, 17, 0, "dada", chuArray }, // Dai Dai
\r
283 { 38, 19, 19, 0, "maka", chuArray }, // Maka Dai Dai
\r
284 { 50, 25, 25, 0, "tai", chuArray }, // Tai
\r
285 { 40, 36, 36, 0, "kyoku", chuArray } // Taikyoku
\r
292 Vector direction[] = { // clockwise!
\r
302 { 2, 1}, // Knight jumps
\r
312 int epList[96], ep2List[96], toList[96], reverse[96]; // decoding tables for double and triple moves
\r
313 int kingStep[10], knightStep[10]; // raw tables for step vectors (indexed as -1 .. 8)
\r
314 #define kStep (kingStep+1)
\r
315 #define nStep (knightStep+1)
\r
317 int attackMask[8] = { // indicate which bits in attack-map item are used to count attacks from which direction
\r
328 int rayMask[8] = { // indicate which bits in attack-map item are used to count attacks from which direction
\r
339 int one[] = { // 1 in the bit fields for the various directions
\r
348 0100000000 // marks knight jumps
\r
351 // Main Data structures
\r
354 // Interleaved lists for black and white, white taking the odd slots
\r
355 // Pieces in general have two entries: one for the basic, and one for the promoted version
\r
356 // The unused one gets its position set to the invalid square number ABSENT
\r
357 // The list is sorted by piece value, most valuable pieces first
\r
358 // When a piece is captured in the search, both its versions are marked ABSENT
\r
359 // In the root the list is packed to eliminate all captured pieces
\r
360 // The list contains a table for how far the piece moves in each direction
\r
361 // Range encoding: -3 = full Lion, -2 = on-ray Lion, -1 = plain jump, 0 = none, 1 = step, >1 = (limited) range
\r
363 // A table in board format, containing pairs of consecutive integers for each square (indexed as 2*sqr and 2*sqr+1)
\r
364 // The first integer contains info on black attacks to the square, the second similarly for white attacks
\r
365 // Each integer contains eight 3-bit fields, which count the number of attacks on it with moves in a particular direction
\r
366 // (If there are attacks by range-jumpers, the 3-bit count is increased by 2 over the actual value)
\r
368 // The board has twice as many files as actually used, in 0x88 fashion
\r
369 // The used squares hold the piece numbers (for use as index in the piece list)
\r
370 // Unused squares are set to the invalid piece number EDGE
\r
371 // There are also 3 guard ranks of EDGE on each side
\r
373 // Moves are encoded as 11-bit from-square and to-square numbers packed in the low bits of an int
\r
374 // Special moves (like Lion double moves) are encoded by off-board to-squares above a certain value
\r
375 // Promotions are indicated by bit 22
\r
377 // Entries of 16 bytes, holding a 32-bit signature, 16-bit lower- and upper-bound scores,
\r
378 // 8-bit draft of each of those scores, an age counter that stores the search number of the last access.
\r
379 // The hash key is derived as the XOR of the products pieceKey[piece]*squareKey[square].
\r
381 // the promoBoard contains one byte with flags for each square, to indicate for each side whether the square
\r
382 // is in the promotion zone (twice), on the last two ranks, or on the last rank
\r
383 // the promoFlag field in the piece list can select which bits of this are tested, to see if it
\r
384 // (1) can promote (2) can defer when the to-square is on last rank, last two ranks, or anywhere.
\r
385 // Pawns normally can't defer anywhere, but if the user defers with them, their promoFlag is set to promote on last rank only
\r
393 signed char range[8];
\r
398 } PieceInfo; // piece-list entry
\r
400 int last[2], royal[2];
\r
401 PieceInfo p[NPIECES]; // piece list
\r
412 } HashEntry; // hash-table entry
\r
414 // Some global variables that control your engine's behavior
\r
418 int resign; // engine-defined option
\r
419 int contemptFactor; // likewise
\r
421 int squareKey[BSIZE];
\r
423 int rawBoard[BSIZE + 11*BHMAX + 6];
\r
424 //int attacks[2*BSIZE]; // attack map
\r
425 int attackMaps[200*BSIZE], *attacks = attackMaps;
\r
426 char distance[2*BSIZE]; // distance table
\r
427 char promoBoard[BSIZE];
\r
428 signed char PST[2*BSIZE];
\r
430 #define board (rawBoard + 6*BHMAX + 3)
\r
431 #define dist (distance + BSIZE)
\r
434 ListLookUp (char *name, PieceDesc *list)
\r
435 { // find piece of given name in list of descriptors
\r
437 while(list->name && strcmp(name, list->name)) i++, list++;
\r
438 return (list->name == NULL ? NULL : list);
\r
442 LookUp (char *name, int var)
\r
443 { // search piece of given name in all lists relevant for given variant
\r
447 desc = ListLookUp(name, daiPieces);
\r
448 if(desc) return desc;
\r
450 return ListLookUp(name, chuPieces);
\r
456 { // remove piece number n from the mentioned side's piece list (and adapt the reference to the displaced pieces!)
\r
458 for(i=stm+2; i<last[stm]; i+=2)
\r
459 if(p[i].promo > n) p[i].promo -= 2;
\r
460 for(i=n; i<last[stm]; i+=2) {
\r
462 if(i+2 == royal[stm]) royal[stm] -= 2; // NB: squeezing out the King moves up Crown Prince to royal[stm]
\r
468 Worse (int a, int b)
\r
469 { // determine if range a not upward compatible with b
\r
470 if(a == b) return 0;
\r
471 if(a >= 0 && b >= 0) return a < b;
\r
472 if(a >= 0) return 1; // a (limited) range can never do the same as a special move
\r
474 case J: return b < J; // any special move is better than a plain jump
\r
475 case D: return b > 2 || b < D;
\r
476 case T: return b > 3 || b < T;
\r
477 case L: return b > 2 || b < D;
\r
478 case F: return b > 3 || b < F || b == T;
\r
479 case S: return b == H || b == T;
\r
480 case H: return b < 0;
\r
481 default: return 1; // a >= 0, so b must be < 0 and can always do something a ranging move cannot do
\r
487 IsUpwardCompatible (signed char *r, signed char *s)
\r
490 for(i=0; i<8; i++) {
\r
491 if(Worse(r[i], s[i])) return 0;
\r
497 Forward (signed char *r)
\r
500 for(i=2; i<7; i++) if(r[i]) return 0;
\r
505 Range (signed char *r)
\r
508 for(i=0; i<8; i++) {
\r
510 if(r[i] < 0) d == r[i] >= L ? 2 : 36;
\r
517 Compactify (int stm)
\r
518 { // remove pieces that are permanently gone (captured or promoted) from one side's piece list
\r
520 for(i=stm+2; i<last[stm]; i+=2) { // first pass: unpromoted pieces
\r
521 if((k = p[i].promo) >= 0 && p[i].pos == ABSENT) { // unpromoted piece no longer there
\r
522 p[k].promo = -2; // orphan promoted version
\r
526 for(i=stm+2; i<last[stm]; i+=2) { // second pass: promoted pieces
\r
527 if((k = p[i].promo) == -2 && p[i].pos == ABSENT) { // orphaned promoted piece not present
\r
534 AddPiece (int stm, PieceDesc *list)
\r
537 for(i=stm+2; i<=last[stm]; i += 2) {
\r
538 if(p[i].value < 10*list->value || p[i].value == 10*list->value && (p[i].promo < 0)) break;
\r
541 for(j=last[stm]; j>i; j-= 2) p[j] = p[j-2];
\r
542 p[i].value = v = 10*list->value;
\r
543 for(j=0; j<8; j++) p[i].range[j] = list->range[j^4*(WHITE-stm)];
\r
544 switch(Range(p[i].range)) {
\r
545 case 1: p[i].pst = BH; break;
\r
546 case 2: p[i].pst = bsize; break;
\r
547 default: p[i].pst = bsize + BH; break;
\r
549 key = (stm == WHITE ? &list->whiteKey : &list->blackKey);
\r
550 if(!*key) *key = ~(myRandom()*myRandom());
\r
551 p[i].pieceKey = *key;
\r
552 p[i].promoFlag = 0;
\r
553 p[i].mobWeight = v > 600 ? 0 : v >= 400 ? 1 : v >= 300 ? 2 : v > 150 ? 3 : v >= 100 ? 2 : 0;
\r
554 for(j=stm+2; j<= last[stm]; j+=2) {
\r
555 if(p[j].promo >= i) p[j].promo += 2;
\r
557 if(royal[stm] >= i) royal[stm] += 2;
\r
558 if(p[i].value == 280) royal[stm] = i;
\r
563 SetUp(char *array, int var)
\r
565 int i, j, n, m, nr, color;
\r
566 char c, *q, name[3];
\r
567 PieceDesc *p1, *p2;
\r
568 last[WHITE] = 1; last[BLACK] = 0;
\r
570 //printf("next rank: %s\n", array);
\r
571 for(j = BW*i; ; j++) {
\r
572 c = name[0] = *array++;
\r
574 if(c == '.') continue;
\r
575 if(c == '/') break;
\r
576 name[1] = name[2] = 0;
\r
577 if(c == ':') name[0] = *array++, name[1] = *array++;
\r
578 if(name[0] >= 'a') {
\r
580 name[0] += 'A' - 'a';
\r
581 if(name[1]) name[1] += 'A' - 'a';
\r
582 } else color = WHITE;
\r
583 p1 = LookUp(name, var);
\r
584 n = AddPiece(color, p1);
\r
586 if(p1->promoted[0]) {
\r
587 p2 = LookUp(p1->promoted, var);
\r
588 m = AddPiece(color, p2);
\r
591 p[n].promoFlag = IsUpwardCompatible(p2->range, p1->range) * DONT_DEFER + CAN_PROMOTE;
\r
592 if(Forward(p1->range)) p[n].promoFlag |= LAST_RANK; // Pieces that only move forward can't defer on last rank
\r
593 if(!strcmp(p1->name, "N")) p[n].promoFlag |= CANT_DEFER; // Knights can't defer on last 2 ranks
\r
594 p[n].promoFlag &= n&1 ? P_WHITE : P_BLACK;
\r
597 } else p[n].promo = -1; // unpromotable piece
\r
598 //printf("piece = %c%-2s %d(%d) %d/%d\n", color ? 'w' : 'b', name, n, m, last[color], last[!color]);
\r
602 for(i=0; i<BH; i++) for(j=0; j<BH; j++) board[BW*i+j] = EMPTY;
\r
603 for(i=WHITE+2; i<=last[WHITE]; i+=2) board[p[i].pos] = i;
\r
604 for(i=BLACK+2; i<=last[BLACK]; i+=2) board[p[i].pos] = i;
\r
605 stm = WHITE; xstm = BLACK;
\r
610 return rand() ^ rand()>>10 ^ rand() << 10 ^ rand() << 20;
\r
618 currentVariant = var;
\r
619 bWidth = variants[var].boardWidth;
\r
620 bHeight = variants[var].boardRanks;
\r
621 zone = variants[var].zoneDepth;
\r
622 bsize = bWidth*bHeight;
\r
623 chuFlag = (var == 0);
\r
625 for(i= -1; i<9; i++) { // board steps in linear coordinates
\r
626 kStep[i] = STEP(direction[i&7].x, direction[i&7].y); // King
\r
627 nStep[i] = STEP(direction[(i&7)+8].x, direction[(i&7)+8].y); // Knight
\r
630 for(i=0; i<8; i++) { // Lion double-move decoding tables
\r
631 for(j=0; j<8; j++) {
\r
632 epList[8*i+j] = kStep[i];
\r
633 toList[8*i+j] = kStep[j] + kStep[i];
\r
634 for(k=0; k<8*i+j; k++)
\r
635 if(epList[k] == toList[8*i+j] && toList[k] == epList[8*i+j])
\r
636 reverse[k] = 8*i+j, reverse[8*i+j] = k;
\r
638 // Lion-Dog triple moves
\r
639 toList[64+i] = 3*kStep[i]; epList[64+i] = kStep[i];
\r
640 toList[72+i] = 3*kStep[i]; epList[72+i] = 2*kStep[i];
\r
641 toList[80+i] = 3*kStep[i]; epList[80+i] = kStep[i]; ep2List[80+i] = 2*kStep[i];
\r
642 toList[88+i] = kStep[i]; epList[88+i] = 2*kStep[i];
\r
645 // fill distance table
\r
646 for(i=0; i<2*BSIZE; i++) {
\r
650 for(j=1; j<BH; j++)
\r
651 dist[j * kStep[i]] = j;
\r
654 for(i=0; i<bsize; i++) squareKey[i] = ~(myRandom()*myRandom());
\r
657 for(i=0; i<BSIZE + 11*BHMAX + 6; i++) rawBoard[i] = EDGE;
\r
660 for(i=0; i<BH; i++) for(j=0; j<BH; j++) {
\r
662 if(i == 0) v |= LAST_RANK & P_BLACK;
\r
663 if(i < 2) v |= CANT_DEFER & P_BLACK;
\r
664 if(i < ZONE) v |= (CAN_PROMOTE | DONT_DEFER) & P_BLACK;
\r
665 if(i >= BH-ZONE) v |= (CAN_PROMOTE | DONT_DEFER) & P_WHITE;
\r
666 if(i >= BH-2) v |= CANT_DEFER & P_WHITE;
\r
667 if(i == BH-1) v |= LAST_RANK & P_WHITE;
\r
668 promoBoard[BW*i + j] = v;
\r
671 // piece-square tables
\r
672 for(i=0; i<BH; i++) for(j=0; j<BH; j++) {
\r
673 int s = BW*i + j, d = BH*(BH-2) - (2*i - BH + 1)*(2*i - BH + 1) - (2*j - BH + 1)*(2*j - BH + 1);
\r
674 PST[BH+s] = d/4 - (i == 0 || i == BH-1 ? 15 : 0) - (j == 0 || j == BH-1 ? 15 : 0);
\r
675 PST[BH*BW+s] = d/6;
\r
676 PST[BH*BW+BH+s] = d/12;
\r
683 NewNonCapture (int x, int y, int promoFlags)
\r
685 if(board[y] != EMPTY) return 1; // edge, capture or own piece
\r
686 //if(flag) printf("# add %c%d%c%d, pf=%d\n", x%BW+'a',x/BW,y%BW+'a',y/BW, promoFlags);
\r
687 if( (promoBoard[x] | promoBoard[y]) & promoFlags) { // piece can promote with this move
\r
688 moveStack[msp++] = moveStack[nonCapts]; // create space for promotion
\r
689 moveStack[nonCapts++] = x<<SQLEN | y | PROMOTE; // push promotion
\r
690 if((promoFlags & promoBoard[y] & (CANT_DEFER | DONT_DEFER | LAST_RANK)) == 0) { // deferral could be a better alternative
\r
691 moveStack[msp++] = x<<SQLEN | y; // push deferral
\r
692 if( (promoBoard[x] & CAN_PROMOTE) == 0 ) { // enters zone
\r
693 moveStack[msp-1] |= DEFER; // flag that promo-suppression takes place after this move
\r
697 moveStack[msp++] = x<<SQLEN | y; // push normal move
\r
698 //if(flag) printf("msp=%d nc=%d\n", msp, nonCapts);
\r
703 NewCapture (int x, int y, int promoFlags)
\r
705 if( (promoBoard[x] | promoBoard[y]) & promoFlags) { // piece can promote with this move
\r
706 moveStack[msp++] = x<<SQLEN | y | PROMOTE; // push promotion
\r
707 if((promoFlags & promoBoard[y] & (CANT_DEFER | DONT_DEFER | LAST_RANK)) == 0) { // deferral could be a better alternative
\r
708 moveStack[msp++] = x<<SQLEN | y; // push deferral
\r
709 if( (promoBoard[x] & CAN_PROMOTE) == 0 ) { // enters zone
\r
710 moveStack[msp-1] |= DEFER; // flag that promo-suppression takes place after this move
\r
714 moveStack[msp++] = x<<SQLEN | y; // push normal move
\r
719 GenNonCapts (int promoSuppress)
\r
721 int i, j, nullMove = ABSENT;
\r
722 for(i=stm+2; i<=last[stm]; i+=2) {
\r
723 int x = p[i].pos, pFlag = p[i].promoFlag;
\r
724 if(x == ABSENT) continue;
\r
725 if(x == promoSuppress && chuFlag) pFlag = 0;
\r
726 for(j=0; j<8; j++) {
\r
727 int y, v = kStep[j], r = p[i].range[j];
\r
728 if(r < 0) { // jumping piece, special treatment
\r
729 if(r == N) { // pure Knightm do off-ray jump
\r
730 NewNonCapture(x, x + nStep[j], pFlag);
\r
732 if(r >= S) { // in any case, do a jump of 2
\r
733 NewNonCapture(x, x + 2*v, pFlag);
\r
734 if(r < J) { // Lion power, also single step
\r
735 if(!NewNonCapture(x, x + v, pFlag)) nullMove = x;
\r
736 if(r == L) { // true Lion, also Knight jump
\r
738 NewNonCapture(x, x + v, pFlag);
\r
746 if(NewNonCapture(x, y+=v, pFlag)) break;
\r
753 report (int x, int y, int i)
\r
758 MapOneColor (int start, int last, int *map)
\r
760 int i, j, k, totMob = 0;
\r
761 for(i=start+2; i<=last; i+=2) {
\r
763 if(p[i].pos == ABSENT) continue;
\r
764 for(j=0; j<8; j++) {
\r
765 int x = p[i].pos, v = kStep[j], r = p[i].range[j];
\r
766 if(r < 0) { // jumping piece, special treatment
\r
769 if(board[x] != EMPTY && board[x] != EDGE)
\r
770 map[2*x + start] += one[8];
\r
772 if(r >= S) { // in any case, do a jump of 2
\r
773 if(board[x + 2*v] != EMPTY && board[x + 2*v] != EDGE)
\r
774 map[2*(x + 2*v) + start] += one[j], mob += (board[x + 2*v] ^ start) & 1;
\r
775 if(r < J) { // Lion power, also single step
\r
776 if(board[x + v] != EMPTY && board[x + v] != EDGE)
\r
777 map[2*(x + v) + start] += one[j];
\r
778 if(r == T) { // Lion Dog, also jump of 3
\r
779 if(board[x + 3*v] != EMPTY && board[x + 3*v] != EDGE)
\r
780 map[2*(x + 3*v) + start] += one[j];
\r
782 if(r <= L) { // true Lion, also Knight jump
\r
783 if(r < L) { // Lion plus (limited) range
\r
785 r = (r == S ? 36 : 3);
\r
787 if(board[y+=v] == EDGE) break;
\r
788 if(board[y] != EMPTY) {
\r
789 if(n > 2) map[2*y + start] += one[j]; // outside Lion range
\r
795 if(board[x + v] != EMPTY && board[x + v] != EDGE)
\r
796 map[2*(x + v) + start] += one[8];
\r
803 if(board[x+=v] != EMPTY) {
\r
804 mob += dist[x-v-p[i].pos];
\r
805 if(board[x] != EDGE) map[2*x + start] += one[j], mob += (board[x] ^ start) & 1;
\r
810 totMob += mob * p[i].mobWeight;
\r
812 if(!level) printf("# mobility %d = %d\n", start, totMob);
\r
817 MapFromScratch (int *map)
\r
820 for(i=0; i<2*bsize; i++) map[i] = 0;
\r
821 mobilityScore = MapOneColor(1, last[WHITE], map);
\r
822 mobilityScore -= MapOneColor(0, last[BLACK], map);
\r
826 Connect (int sqr, int piece, int dir)
\r
827 { // scan to both sides along ray to elongate attacks from there, and remove our own attacks on there, if needed
\r
828 int x, step = kStep[dir], r1 = p[piece].range[dir], r2 = p[piece].range[dir+4], piece1, piece2;
\r
829 int d1, d2, r, y, c;
\r
831 if((attacks[2*sqr] + attacks[2*sqr+1]) & attackMask[dir]) { // there are incoming attack(s) from 'behind'
\r
833 while(board[x-=step] == EMPTY); // in any case, scan to attacker, to see where / what it is
\r
834 d1 = dist[x-sqr]; piece1 = board[x];
\r
835 attacks[2*x + stm] -= -(d1 <= r2) & one[dir+4]; // remove our attack on it if in-range
\r
836 if((attacks[2*sqr] + attacks[2*sqr+1]) & attackMask[dir+4]) { // there are also incoming attack(s) from 'ahead'
\r
839 while(board[y+=step] == EMPTY); // also always scan to that one to see what it is
\r
840 d2 = dist[y-sqr]; piece2 = board[y];
\r
841 attacks[2*y+stm] -= -(d2 <= r1) & one[dir]; // remove our attack on it if in-range
\r
842 // we have two pieces now shooting at each other. See how far they get.
\r
843 if(d1 + d2 <= (r1 = p[piece1].range[dir])) { // 1 hits 2
\r
844 attacks[2*y + (piece1 & WHITE)] += one[dir]; // count attack
\r
845 UPDATE_MOBILITY(piece1, d2);
\r
846 } else UPDATE_MOBILITY(piece1, r1 - d1); // does not connect, but could still gain mobility
\r
847 if(d1 + d2 <= (r2 = p[piece2].range[dir+4])) { // 2 hits 1
\r
848 attacks[2*x + (piece2 & WHITE)] += one[dir+4]; // count attack
\r
849 UPDATE_MOBILITY(piece2, d1);
\r
850 } else UPDATE_MOBILITY(piece2, r2 - d2); // does not connect, but could still gain mobility
\r
851 // if r1 or r2<0, moves typically jump, and thus cannot be unblocked. Exceptions are FF and BS distant moves.
\r
852 // test for d1+d2 > 2 && rN == F && d== 3 or rN == S
\r
854 } else { // we were only attacked from behind
\r
856 r = (r2 = p[piece1].range[dir]) - d1;
\r
857 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
858 // for now, forget jumpers
\r
862 if(board[y+=step] != EMPTY) {
\r
863 d2 = dist[y-sqr]; piece2 = board[y];
\r
864 if(piece2 != EDGE) { // extended move hits a piece
\r
865 attacks[2*y + (piece1 & WHITE)] += one[dir]; // count attack
\r
866 attacks[2*y + stm] -= -(d2 <= r1) & one[dir]; // remove our own attack on it, if in-range
\r
868 UPDATE_MOBILITY(piece1, d2); // count extra mobility even if we hit edge
\r
871 // we hit nothing with the extended move of the attacker behind us.
\r
872 UPDATE_MOBILITY(piece1, r2 - d1);
\r
873 r = r1 - r2 + d1; // extra squares covered by mover
\r
875 if(board[y+=step] != EMPTY) {
\r
876 d2 = dist[y-sqr]; piece2 = board[y];
\r
877 if(piece2 != EDGE) { // extended move hits a piece
\r
878 attacks[2*y + stm] -= one[dir]; // count attack
\r
883 // if r2<0 we should again test for F and S moves
\r
885 } else // no incoming attack from behind
\r
886 if(c = (attacks[2*sqr] + attacks[2*sqr+1]) & attackMask[dir+4]) { // but incoming attack(s) from 'ahead'
\r
888 y = sqr; while(board[y+=step]); // locate attacker
\r
889 d2 = dist[y-sqr]; piece2 = board[y];
\r
890 attacks[2*y + stm] -= -(d2 <= r1) & one[dir]; // remove our attack on it if in-range
\r
891 r = (r1 = p[piece1].range[dir]) - d2;
\r
892 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
893 // for now, forget jumpers
\r
897 if(board[x-=step] != EMPTY) {
\r
898 d1 = dist[x-sqr]; piece1 = board[x];
\r
899 if(piece1 != EDGE) { // extended move hits a piece
\r
900 attacks[2*x + (piece2 & WHITE)] += one[dir+4]; // count attack
\r
901 attacks[2*x + stm] -= -(d1 <= r2) & one[dir+4]; // remove our own attack on it, if in-range
\r
903 UPDATE_MOBILITY(piece2, d1); // count extra mobility even if we hit edge
\r
906 // we hit nothing with the extended move of the attacker behind us.
\r
907 UPDATE_MOBILITY(piece2, r2 - d1);
\r
908 r = r2 - r1 + d2; // extra squares covered by mover
\r
910 if(board[x-=step] != EMPTY) {
\r
911 d1 = dist[x-sqr]; piece1 = board[x];
\r
912 if(piece1 != EDGE) { // extended move hits a piece
\r
913 attacks[2*x + stm] -= one[dir+4]; // count attack
\r
918 } else { // no incoming attacks from either side. Only delete attacks of mover on others
\r
922 if(board[x+=step] != EMPTY) { // piece found that we attacked
\r
923 attacks[2*x + stm] -= one[dir]; // decrement attacks along that direction
\r
929 if(board[x-=step] != EMPTY) { // piece found that we attacked
\r
930 attacks[2*x + stm] -= one[dir+4]; // decrement attacks along opposite direction
\r
939 { // test if move with range r reaches over (un-obstructed) distance d
\r
940 if(r < 0) switch(r) {
\r
941 case J: return (d == 2);
\r
943 case L: return (d <= 2);
\r
945 case F: return (d <= 3);
\r
948 } else return (d <= r);
\r
949 return 0; // not reached
\r
953 Disconnect (int sqr, int piece, int dir)
\r
955 int x = sqr, step = kStep[dir], piece1, piece2, d1, d2, r1, r2, y;
\r
956 while( board[x+=step] == EMPTY );
\r
958 if(piece1 != EDGE) { // x has hit a piece
\r
960 r1 = p[piece1].range[dir+4];
\r
961 y = sqr; while( board[y-=step] == EMPTY );
\r
963 if(piece2 != EDGE) { // both ends of the ray hit a piece
\r
965 r2 = p[piece2].range[dir];
\r
966 if(r1 >= d1) { // piece1 hits us
\r
967 attacks[2*sqr + (piece1 & WHITE)] += one[dir+4];
\r
968 if(r1 >= d1 + d2) // was hitting piece2 before, now blocked
\r
969 attacks[2*y + (piece1 & WHITE)] -= one[dir+4];
\r
971 if(r2 >= d2) { // piece2 hits us
\r
972 attacks[2*sqr + (piece2 & WHITE)] += one[dir];
\r
973 if(r2 >= d1 + d2) // was hitting piece1 before, now blocked
\r
974 attacks[2*x + (piece2 & WHITE)] -= one[dir];
\r
976 if( Hit(p[piece].range[dir], d1) )
\r
977 attacks[2*sqr + stm] += one[dir];
\r
978 if( Hit(p[piece].range[dir+4], d2) )
\r
979 attacks[2*sqr + stm] += one[dir+4];
\r
983 x = sqr; while( board[x-=step] == EMPTY );
\r
985 if(piece1 == EDGE) return; // ray empty on both sides
\r
987 r1 = p[piece1].range[dir];
\r
990 // we only get here if one side looks to the board edge
\r
991 if(r1 >= d1) // piece1 hits us
\r
992 attacks[2*sqr + (piece1 & WHITE)] += one[dir^4];
\r
993 if( Hit(p[piece].range[dir], d1) )
\r
994 attacks[2*sqr + stm] += one[dir];
\r
999 { // determines attacks on square and blocking when a piece lands on an empty square
\r
1001 for(i=0; i<4; i++) {
\r
1002 Disconnect(sqr, board[sqr], i);
\r
1007 Evacuate (int sqr, int piece)
\r
1008 { // determines change in attacks on neighbors due to unblocking and mover when the mentioned piece vacates the given square
\r
1010 for(i=0; i<4; i++) Connect(sqr, piece, i);
\r
1014 MakeMove(Move m, UndoInfo *u)
\r
1016 int deferred = ABSENT;
\r
1017 // first execute move on board
\r
1018 u->from = m>>SQLEN & SQUARE;
\r
1019 u->to = m & SQUARE;
\r
1020 u->piece = board[u->from];
\r
1021 board[u->from] = EMPTY;
\r
1023 u->revMoveCount = cnt50++;
\r
1024 u->savKeyL = hashKeyL;
\r
1025 u->savKeyH = hashKeyH;
\r
1027 if(m & (PROMOTE | DEFER)) {
\r
1030 u->new = u->piece;
\r
1032 p[u->piece].pos = ABSENT;
\r
1033 u->new = p[u->piece].promo;
\r
1034 u->booty = p[u->new].value - p[u->piece].value;
\r
1035 cnt50 = 0; // promotion irreversible
\r
1037 } else u->new = u->piece;
\r
1039 u->booty += PST[p[u->new].pst + u->to] - PST[p[u->piece].pst + u->from];
\r
1041 if(u->to >= SPECIAL) { // two-step Lion move
\r
1042 // take care of first e.p. victim
\r
1043 u->epSquare = u->from + epList[u->to - SPECIAL]; // decode
\r
1044 u->epVictim = board[u->epSquare]; // remember for takeback
\r
1045 board[u->epSquare] = EMPTY; // and remove
\r
1046 p[u->epVictim].pos = ABSENT;
\r
1047 // now take care of (optional) second e.p. victim
\r
1048 u->ep2Square = u->from + ep2List[u->to - SPECIAL]; // This is the (already evacuated) from-square when there is none!
\r
1049 u->ep2Victim = board[u->ep2Square]; // remember
\r
1050 board[u->ep2Square] = EMPTY; // and remove
\r
1051 p[u->ep2Victim].pos = ABSENT;
\r
1052 // decode the true to-square, and correct difEval and hash key for the e.p. captures
\r
1053 u->to = u->from + toList[u->to - SPECIAL];
\r
1054 u->booty += p[u->ep2Victim].value + PST[p[u->ep2Victim].pst + u->ep2Square];
\r
1055 u->booty += p[u->epVictim].value + PST[p[u->epVictim].pst + u->epSquare];
\r
1056 hashKeyL ^= p[u->epVictim].pieceKey * squareKey[u->epSquare];
\r
1057 hashKeyH ^= p[u->epVictim].pieceKey * squareKey[u->epSquare+BH];
\r
1058 hashKeyL ^= p[u->ep2Victim].pieceKey * squareKey[u->ep2Square];
\r
1059 hashKeyH ^= p[u->ep2Victim].pieceKey * squareKey[u->ep2Square+BH];
\r
1060 if(p[u->piece].value != 1000 && p[u->epVictim].value == 1000) deferred |= PROMOTE; // flag non-Lion x Lion
\r
1061 cnt50 = 0; // double capture irreversible
\r
1062 } else u->epVictim = EMPTY;
\r
1064 u->victim = board[u->to];
\r
1065 p[u->victim].pos = ABSENT;
\r
1066 u->booty += p[u->victim].value + PST[p[u->victim].pst + u->to];
\r
1067 if(u->victim != EMPTY) cnt50 = 0; // capture irreversible
\r
1069 p[u->new].pos = u->to;
\r
1070 board[u->to] = u->new;
\r
1072 hashKeyL ^= p[u->new].pieceKey * squareKey[u->to]
\r
1073 ^ p[u->piece].pieceKey * squareKey[u->from]
\r
1074 ^ p[u->victim].pieceKey * squareKey[u->to];
\r
1075 hashKeyH ^= p[u->new].pieceKey * squareKey[u->to+BH]
\r
1076 ^ p[u->piece].pieceKey * squareKey[u->from+BH]
\r
1077 ^ p[u->victim].pieceKey * squareKey[u->to+BH];
\r
1083 UnMake(UndoInfo *u)
\r
1085 if(u->epVictim) { // put Lion victim of first leg back
\r
1086 p[u->ep2Victim].pos = u->ep2Square;
\r
1087 board[u->ep2Square] = u->ep2Victim;
\r
1088 p[u->epVictim].pos = u->epSquare;
\r
1089 board[u->epSquare] = u->epVictim;
\r
1092 p[u->victim].pos = u->to;
\r
1093 board[u->to] = u->victim; // can be EMPTY
\r
1095 p[u->new].pos = ABSENT;
\r
1096 p[u->piece].pos = u->from; // this can be the same as above
\r
1097 board[u->from] = u->piece;
\r
1099 cnt50 = u->revMoveCount;
\r
1100 hashKeyL = u->savKeyL;
\r
1101 hashKeyH = u->savKeyH;
\r
1105 GenCapts(int sqr, int victimValue)
\r
1106 { // generate all moves that capture the piece on the given square
\r
1107 int i, range, att = attacks[2*sqr + stm];
\r
1108 //printf("GenCapts(%d,%d)\n",sqr,victimValue);
\r
1109 for(i=0; i<8; i++) { // try all rays
\r
1111 if(att & attackMask[i]) { // attacked by move in this direction
\r
1112 v = -kStep[i]; x = sqr;
\r
1113 while( board[x+=v] == EMPTY ); // scan towards source until we encounter a 'stop'
\r
1114 //printf("stop @ %c%d (dir %d)\n",x%BW+'a',x/BW,i);
\r
1115 if((board[x] & TYPE) == stm) { // stop is ours
\r
1116 int attacker = board[x], d = dist[x-sqr], r = p[attacker].range[i];
\r
1117 //printf("attacker %d, range %d, dist %d\n", attacker, r, d);
\r
1118 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
1119 NewCapture(x, sqr + victimValue - SORTKEY(attacker), p[attacker].promoFlag);
\r
1121 if(!(att & attackMask[i])) continue;
\r
1122 } else if(r < 0) goto lions; // test for special-move attacks by thiis stop
\r
1124 // we get here when the first stop on the ray is an opponent, or a normal mover which did not range fare enough
\r
1125 while(board[x+=v] == EMPTY); // next stop
\r
1127 if((board[x] & TYPE) == stm) { // second stop is ours
\r
1128 int attacker = board[x], d = dist[x-sqr], r = p[attacker].range[i];
\r
1129 if(r < 0) { // stop has non-standard moves
\r
1130 switch(p[attacker].range[i]) { // figure out what he can do (including multi-captures, which causes the complexity)
\r
1131 case F: // Lion power + 3-step (as in FF)
\r
1132 case S: // Lion power + ranging (as in BS)
\r
1135 NewCapture(x, sqr + victimValue - SORTKEY(attacker), p[attacker].promoFlag);
\r
1137 // now the multi-captures of designated victim together with lower-valued piece
\r
1138 if(d == 2) { // primary victim on second ring; look for victims to take in passing
\r
1139 if((board[sqr+v] & TYPE) == xstm && board[sqr+v] > board[sqr])
\r
1140 NewCapture(x, SPECIAL + 9*i + victimValue - SORTKEY(attacker), p[attacker].promoFlag);
\r
1141 if((i&1) == 0) { // orthogonal: two extra bent paths
\r
1143 if((board[x+v] & TYPE) == xstm && board[x+v] > board[sqr])
\r
1144 NewCapture(x, SPECIAL + 8*(i-1&7) + (i+1&7) + victimValue - SORTKEY(attacker), p[attacker].promoFlag);
\r
1146 if((board[x+v] & TYPE) == xstm && board[x+v] > board[sqr])
\r
1147 NewCapture(x, SPECIAL + 8*(i+1&7) + (i-1&7) + victimValue - SORTKEY(attacker), p[attacker].promoFlag);
\r
1149 } else { // primary victim on first ring
\r
1151 for(j=0; j<8; j++) { // we can go on in 8 directions after we captured it in passing
\r
1153 if(sqr + v == x || board[sqr+v] == EMPTY) // hit & run; make sure we include igui (attacker is still at x!)
\r
1154 NewCapture(x, SPECIAL + 8*i + j + victimValue, p[attacker].promoFlag);
\r
1155 else if((board[sqr+v] & TYPE) == xstm && board[sqr+v] > board[sqr]) { // double capture
\r
1156 NewCapture(x, SPECIAL + 8*i + j + victimValue, p[attacker].promoFlag); // other victim after primary
\r
1157 if(dist[sqr+v-x] == 1) // other victim also on first ring; reverse order is possible
\r
1158 NewCapture(x, SPECIAL + reverse[8*i + j] + victimValue, p[attacker].promoFlag);
\r
1163 case D: // linear Lion move (as in HF, SE)
\r
1165 NewCapture(x, sqr + victimValue - SORTKEY(attacker), p[attacker].promoFlag);
\r
1167 if(d == 2) { // check if we can take intermediate with it
\r
1168 if((board[x-v] & TYPE) == xstm && board[x-v] > board[sqr])
\r
1169 NewCapture(x, SPECIAL + 9*i + victimValue - SORTKEY(attacker), p[attacker].promoFlag); // e.p.
\r
1170 } else { // d=1; can move on to second, or move back for igui
\r
1171 NewCapture(x, SPECIAL + 8*i + (i^4) + victimValue, p[attacker].promoFlag); // igui
\r
1172 if(board[sqr-v] == EMPTY || (board[sqr-v] & TYPE) == xstm && board[sqr-v] > board[sqr])
\r
1173 NewCapture(x, SPECIAL + 9*i + victimValue - SORTKEY(attacker), p[attacker].promoFlag); // hit and run
\r
1176 case J: // plain jump (as in KY, PH)
\r
1178 NewCapture(x, sqr + victimValue - SORTKEY(attacker), p[attacker].promoFlag);
\r
1182 //printf("mask[%d] = %o\n", i, att);
\r
1183 if((att & attackMask[i]) == 0) continue; // no other attackers, so done with this direction
\r
1185 // now we get to the hairy part: there are other attackers than the stop, apparently jumping over it
\r
1188 // off-ray attacks
\r
1189 if(att & 0700000000) { // Knight attack
\r
1190 for(i=0; i<8; i++) { // scan all knight jumps to locate source
\r
1191 int x = sqr - nStep[i], attacker = board[x];
\r
1192 if(attacker == EMPTY || (attacker & TYPE) != stm) continue;
\r
1193 if(p[attacker].range[i] <= N && p[attacker].range[i] >= S) { // has Knight jump in our direction
\r
1194 NewCapture(x, sqr + victimValue, p[attacker].promoFlag); // plain jump (as in N)
\r
1195 if(p[attacker].range[i] < N) { // Lion power; generate double captures over two possible intermediates
\r
1196 int v = kStep[i]; // leftish path
\r
1197 if((board[x+v] & TYPE) == xstm && board[x+v] > board[sqr])
\r
1198 NewCapture(x, SPECIAL + 8*i + (i+1&7) + victimValue, p[attacker].promoFlag);
\r
1199 v = kStep[i+1]; // rightish path
\r
1200 if((board[x+v] & TYPE) == xstm && board[x+v] > board[sqr])
\r
1201 NewCapture(x, SPECIAL + 8*(i+1&7) + i + victimValue, p[attacker].promoFlag);
\r
1211 return (stm ? mobilityScore : -mobilityScore);
\r
1215 Search (int alpha, int beta, int difEval, int depth, int oldPromo, int promoSuppress)
\r
1217 int i, j, k, firstMove, oldMSP = msp, curMove, sorted, bad, phase, king, iterDep, replyDep, nextVictim, to, defer, dubious, bestMoveNr;
\r
1219 int score, bestScore, curEval, iterAlpha;
\r
1220 Move move, nullMove;
\r
1222 if(PATH) printf("search(%d) %d-%d eval=%d, stm=%d\n",depth,alpha,beta,difEval,stm),fflush(stdout);
\r
1223 xstm = stm ^ WHITE;
\r
1224 //printf("map made\n");fflush(stdout);
\r
1226 k = p[king=royal[xstm]].pos;
\r
1227 if( k != ABSENT) {
\r
1228 if(attacks[2*k + stm]) {
\r
1229 if( p[king + 2].pos == ABSENT ) return INF; // we have an attack on his only King
\r
1231 } else { // he has no king! Test for attacks on Crown Prince
\r
1232 k = p[king + 2].pos;
\r
1233 if(attacks[2*k + stm]) return INF; // we have attack on Crown Prince
\r
1235 //printf("King safe\n");fflush(stdout);
\r
1236 // EVALUATION & WINDOW SHIFT
\r
1237 curEval = difEval + Evaluate();
\r
1238 alpha -= (alpha < curEval);
\r
1239 beta -= (beta <= curEval);
\r
1242 pv[pvPtr++] = 0; // start empty PV, directly behind PV of parent
\r
1244 firstMove = curMove = sorted = msp += 20; // leave 20 empty slots in front of move list
\r
1245 phase = 0; iterDep=1; replyDep = (depth < 1 ? depth : 1) - 1;
\r
1247 if(flag && depth>= 0) printf("iter %d:%d\n", depth,iterDep),fflush(stdout);
\r
1248 iterAlpha = alpha; bestScore = -INF; bestMoveNr = 0;
\r
1249 for(curMove = firstMove; ; curMove++) { // loop over moves
\r
1250 if(flag && depth>= 0) printf("phase=%d: first/curr/last = %d / %d / %d\n", phase, firstMove, curMove, msp);fflush(stdout);
\r
1252 if(curMove >= msp) { // we ran out of moves; generate some new
\r
1254 case 0: // null move
\r
1256 bestScore = curEval;
\r
1257 if(bestScore >= beta || depth < -1) goto cutoff;
\r
1260 if(curEval >= beta) {
\r
1262 score = -Search(-beta, -iterAlpha, difEval, depth-3, promoSuppress & SQUARE, ABSENT);
\r
1264 if(score >= beta) { msp = oldMSP; return score + (score < curEval); }
\r
1268 case 1: // hash move
\r
1270 case 2: // capture-gen init
\r
1271 nextVictim = xstm;
\r
1273 case 3: // generate captures
\r
1274 while(nextVictim < last[xstm]) { // more victims exist
\r
1275 int group, to = p[nextVictim += 2].pos; // take next
\r
1276 if(to == ABSENT) continue; // ignore if absent
\r
1277 if(!attacks[2*to + stm]) continue; // skip if not attacked
\r
1278 group = p[nextVictim].value; // remember value of this found victim
\r
1280 while(nextVictim < last[xstm] && p[nextVictim+2].value == group) { // more victims of same value exist
\r
1281 to = p[nextVictim += 2].pos; // take next
\r
1282 if(to == ABSENT) continue; // ignore if absent
\r
1283 if(!attacks[2*to + stm]) continue; // skip if not attacked
\r
1286 //printf("captures on %d generated, msp=%d\n", nextVictim, msp);
\r
1289 phase = 4; // out of victims: all captures generated
\r
1290 case 4: // dubious captures
\r
1292 while( dubious < framePtr + 250 ) // add dubious captures back to move stack
\r
1293 moveStack[msp++] = moveStack[dubious++];
\r
1294 if(curMove != msp) break;
\r
1297 case 5: // killers
\r
1298 if(depth <= 0) goto cutoff;
\r
1300 case 6: // non-captures
\r
1302 nullMove = GenNonCapts(oldPromo);
\r
1304 sorted = msp; // do not sort noncapts
\r
1306 case 7: // bad captures
\r
1307 case 8: // PV null move
\r
1309 if(nullMove != ABSENT) {
\r
1310 moveStack[msp++] = nullMove + (nullMove << SQLEN) | DEFER; // kludge: setting DEFER guarantees != 0, and has no effect
\r
1312 printf("# %d. sqr = %08x null = %08x\n", msp, nullMove, moveStack[msp-1]);
\r
1318 // MOVE EXTRACTION
\r
1320 if(flag & depth >= 0) printf("%2d:%d extract %d/%d\n", depth, iterDep, curMove, msp);
\r
1321 if(curMove < sorted) {
\r
1322 move = moveStack[sorted=j=curMove];
\r
1323 for(i=curMove+1; i<msp; i++)
\r
1324 if(moveStack[i] > move) move = moveStack[j=i]; // search move with highest priority
\r
1325 moveStack[j] = moveStack[curMove]; moveStack[curMove] = move; // swap highest-priority move to front of remaining
\r
1326 if(move == 0) { msp = curMove--; continue; } // all remaining moves must be 0; clip off move list
\r
1328 move = moveStack[curMove];
\r
1329 if(move == 0) continue; // skip invalidated move
\r
1331 if(flag & depth >= 0) printf("%2d:%d found %d/%d\n", depth, iterDep, curMove, msp);
\r
1335 defer = MakeMove(move, &tb);
\r
1337 for(i=2; i<=cnt50; i+=2) if(repStack[level-i+200] == hashKeyH) {
\r
1338 moveStack[curMove] = 0; // erase forbidden move
\r
1339 score = -INF; goto repetition;
\r
1341 repStack[level+200] = hashKeyH;
\r
1343 path[level++] = move;
\r
1344 attacks += 2*bsize;
\r
1345 MapFromScratch(attacks); // for as long as incremental update does not work.
\r
1346 if(PATH) pmap(attacks, stm);
\r
1347 if(chuFlag && p[tb.victim].value == 1000) { // verify legality of Lion capture in Chu Shogi
\r
1349 if(p[tb.piece].value == 1000) { // Ln x Ln: can make Ln 'vulnarable' (if distant and not through intemediate > GB)
\r
1350 if(dist[tb.from-tb.to] != 1 && attacks[2*tb.to + stm] && p[tb.epVictim].value <= 50)
\r
1351 score = -INF; // our Lion is indeed made vulnerable and can be recaptured
\r
1352 } else { // other x Ln
\r
1353 if(promoSuppress & PROMOTE) score = -INF; // non-Lion captures Lion after opponent did same
\r
1354 defer |= PROMOTE; // if we started, flag he cannot do it in reply
\r
1356 if(score == -INF) { moveStack[curMove] = 0; goto abortMove; } // zap illegal moves
\r
1359 score = -Search(-beta, -iterAlpha, -difEval - tb.booty, replyDep, promoSuppress & ~PROMOTE, defer);
\r
1364 attacks -= 2*bsize;
\r
1368 xstm = stm; stm ^= WHITE;
\r
1370 if(PATH) printf("%d:%2d:%d %3d %6x %-10s %6d %6d\n", level, depth, iterDep, curMove, moveStack[curMove], MoveToText(moveStack[curMove], 0), score, bestScore);
\r
1372 // ALPHA-BETA STUFF
\r
1373 if(score > bestScore) {
\r
1374 bestScore = score; bestMoveNr = curMove;
\r
1375 if(score > iterAlpha) {
\r
1376 iterAlpha = score;
\r
1377 if(curMove < firstMove + 5) { // if not too much work, sort move to front
\r
1379 for(i=curMove; i>firstMove; i--) {
\r
1380 moveStack[i] = moveStack[i-1];
\r
1382 moveStack[firstMove] = move;
\r
1383 } else { // late move appended in front of list, and leaves hole in list
\r
1384 moveStack[--firstMove] = move;
\r
1385 moveStack[curMove] = 0;
\r
1387 bestMoveNr = firstMove;
\r
1388 if(score >= beta) { // beta cutoff
\r
1393 for(pvPtr = myPV+1; pv[pvPtr++] = pv[i++]; ); // copy daughter PV
\r
1394 pv[myPV] = move; // behind our move (pvPtr left at end of copy)
\r
1402 if(!level) { // root node
\r
1403 if(postThinking > 0) {
\r
1404 int i; // WB thinking output
\r
1405 printf("%d %d %d %d", iterDep, bestScore, (GetTickCount() - startTime)/10, nodes);
\r
1406 for(i=0; pv[i]; i++) printf(" %s", MoveToText(pv[i], 0));
\r
1407 printf("\n"); fflush(stdout);
\r
1409 if(GetTickCount() - startTime > tlim1) break; // do not start iteration we can (most likely) not finish
\r
1411 replyDep = iterDep;
\r
1412 } while(++iterDep <= depth); // next depth
\r
1414 retFirst = firstMove;
\r
1415 msp = oldMSP; // pop move list
\r
1416 pvPtr = myPV; // pop PV
\r
1417 retMove = bestMoveNr ? moveStack[bestMoveNr] : 0;
\r
1418 if(flag && depth >= 0) printf("return %d: %d %d\n", depth, bestScore, curEval);
\r
1419 return bestScore + (bestScore < curEval);
\r
1426 for(i=0; i<182; i++) {
\r
1427 printf("%3d. %3d %3d %4d %02x ", i, p[i].value, p[i].promo, p[i].pos, p[i].promoFlag&255);
\r
1428 for(j=0; j<8; j++) printf(" %2d", p[i].range[j]);
\r
1431 printf("last: %d / %d\nroyal %d / %d\n", last[WHITE], last[BLACK], royal[WHITE], royal[BLACK]);
\r
1438 for(i=BH+2; i>-4; i--) {
\r
1440 for(j=-3; j<BH+3; j++) printf("%4d", b[BW*i+j]);
\r
1446 pbytes (unsigned char *b)
\r
1449 for(i=BH-1; i>=0; i--) {
\r
1450 for(j=0; j<BH; j++) printf("%3x", b[BW*i+j]);
\r
1456 pmap (int *m, int col)
\r
1459 for(i=BH-1; i>=0; i--) {
\r
1461 for(j=0; j<BH; j++) printf("%10o", m[2*(BW*i+j)+col]);
\r
1467 pmoves(int start, int end)
\r
1470 printf("# move stack from %d to %d\n", start, end);
\r
1471 for(i=start; i<end; i++) {
\r
1473 f = m>>SQLEN & SQUARE;
\r
1475 printf("# %3d. %08x %3d-%3d %s\n", i, m, f, t, MoveToText(m, 0));
\r
1479 /********************************************************/
\r
1480 /* Example of a WinBoard-protocol driver, by H.G.Muller */
\r
1481 /********************************************************/
\r
1483 #include <stdio.h>
\r
1485 // four different constants, with values for WHITE and BLACK that suit your engine
\r
1489 // some value that cannot occur as a valid move
\r
1492 // some parameter of your engine
\r
1493 #define MAXMOVES 500 /* maximum game length */
\r
1494 #define MAXPLY 60 /* maximum search depth */
\r
1500 #define DEFAULT_FEN ""
\r
1502 typedef Move MOVE;
\r
1504 int moveNr; // part of game state; incremented by MakeMove
\r
1505 MOVE gameMove[MAXMOVES]; // holds the game history
\r
1507 // Some routines your engine should have to do the various essential things
\r
1508 int MakeMove2(int stm, MOVE move); // performs move, and returns new side to move
\r
1509 void UnMake2(MOVE move); // unmakes the move;
\r
1510 int Setup2(char *fen); // sets up the position from the given FEN, and returns the new side to move
\r
1511 void SetMemorySize(int n); // if n is different from last time, resize all tables to make memory usage below n MB
\r
1512 char *MoveToText(MOVE move, int m); // converts the move from your internal format to text like e2e2, e1g1, a7a8q.
\r
1513 MOVE ParseMove(char *moveText); // converts a long-algebraic text move to your internal move format
\r
1514 int SearchBestMove(int stm, int timeLeft, int mps, int timeControl, int inc, int timePerMove, MOVE *move, MOVE *ponderMove);
\r
1515 void PonderUntilInput(int stm); // Search current position for stm, deepening forever until there is input.
\r
1517 UndoInfo undoInfo;
\r
1518 int sup0, sup1, sup2; // promo suppression squares
\r
1519 int lastLift, lastPut;
\r
1522 MakeMove2 (int stm, MOVE move)
\r
1525 sup0 = sup1; sup1 = sup2;
\r
1526 sup2 = MakeMove(move, &undoInfo);
\r
1527 rootEval = -rootEval - undoInfo.booty;
\r
1528 for(i=0; i<200; i++) repStack[i] = repStack[i+1];
\r
1529 repStack[199] = hashKeyH;
\r
1530 printf("# makemove %08x %c%d %c%d\n", move, sup1%BW+'a', sup1/BW, sup2%BW+'a', sup2/BW);
\r
1531 return stm ^ WHITE;
\r
1535 UnMake2 (MOVE move)
\r
1538 rootEval = -rootEval - undoInfo.booty;
\r
1539 UnMake(&undoInfo);
\r
1540 for(i=200; i>0; i--) repStack[i] = repStack[i-1];
\r
1541 sup2 = sup1; sup1 = sup0;
\r
1545 Setup2 (char *fen)
\r
1547 SetUp(variants[currentVariant].array, currentVariant);
\r
1548 sup0 = sup1 = sup2 = ABSENT;
\r
1549 rootEval = cnt50 = hashKeyH = hashKeyL = 0;
\r
1554 SetMemorySize (int n)
\r
1559 MoveToText (MOVE move, int multiLine)
\r
1561 static char buf[50];
\r
1562 int f = move>>SQLEN & SQUARE, g = f, t = move & SQUARE;
\r
1563 if(f == t) { sprintf(buf, "@@@@"); return buf; } // null-move notation in WB protocol
\r
1565 if(t >= SPECIAL) { // kludgy! Print as side effect non-standard WB command to remove victims from double-capture (breaks hint command!)
\r
1566 int e = f + epList[t - SPECIAL];
\r
1567 // printf("take %c%d\n", e%BW+'a', e/BW+ONE);
\r
1568 sprintf(buf, "%c%d%c%d,", f%BW+'a', f/BW+ONE, e%BW+'a', e/BW+ONE); f = e;
\r
1569 if(multiLine) printf("move %s\n", buf), buf[0] = '\0';
\r
1570 if(ep2List[t - SPECIAL]) {
\r
1571 e = g + ep2List[t - SPECIAL];
\r
1572 // printf("take %c%d\n", e%BW+'a', e/BW+ONE);
\r
1573 sprintf(buf+strlen(buf), "%c%d%c%d,", f%BW+'a', f/BW+ONE, e%BW+'a', e/BW+ONE); f = e;
\r
1574 if(multiLine) printf("move %s\n", buf), buf[0] = '\0';
\r
1576 t = g + toList[t - SPECIAL];
\r
1578 sprintf(buf+strlen(buf), "%c%d%c%d%s", f%BW+'a', f/BW+ONE, t%BW+'a', t/BW+ONE, move & PROMOTE ? "+" : "");
\r
1583 ReadSquare (char *p, int *sqr)
\r
1587 r = atoi(p + 1) - ONE;
\r
1589 return 2 + (r > 9);
\r
1593 ParseMove (char *moveText)
\r
1595 int i, j, f, t, t2, e, ret, deferred=0;
\r
1596 char c = moveText[0];
\r
1597 moveText += ReadSquare(moveText, &f);
\r
1598 moveText += ReadSquare(moveText, &t); t2 = t;
\r
1599 if(*moveText == ',') {
\r
1601 moveText += ReadSquare(moveText, &e);
\r
1602 if(e != t) return INVALID; // must continue with same piece
\r
1604 moveText += ReadSquare(moveText, &t);
\r
1605 for(i=0; i<8; i++) if(f + kStep[i] == e) break;
\r
1606 if(i >= 8) return INVALID; // this rejects Lion Dog 2+1 and 2-1 moves!
\r
1607 for(j=0; j<8; j++) if(e + kStep[j] == t) break;
\r
1608 if(j >= 8) return INVALID; // this rejects Lion Dog 1+2 moves!
\r
1609 t2 = SPECIAL + 8*i + j;
\r
1611 ret = f<<SQLEN | t2;
\r
1612 if(*moveText == '+') ret |= PROMOTE;
\r
1613 printf("# suppress = %c%d\n", sup1%BW+'a', sup1/BW);
\r
1614 MapFromScratch(attacks);
\r
1616 Search(-INF-1, INF+1, 0, 1, sup1, sup2);
\r
1618 for(i=retFirst; i<retMSP; i++) {
\r
1619 if(moveStack[i] == INVALID) continue;
\r
1620 if(c == '@' && (moveStack[i] & SQUARE) == (moveStack[i] >> SQLEN & SQUARE)) break; // any null move matches @@@@
\r
1621 if((moveStack[i] & (PROMOTE | DEFER-1)) == ret) break;
\r
1622 if((moveStack[i] & DEFER-1) == ret) deferred = i; // promoted version of entered non-promotion is legal
\r
1624 if(i>=retMSP) { // no exact match
\r
1625 if(deferred) { // but maybe non-sensical deferral
\r
1626 int flags = p[board[f]].promoFlag;
\r
1627 printf("# deferral of %d\n", deferred);
\r
1628 i = deferred; // in any case we take that move
\r
1629 if(!(flags & promoBoard[t] & (CANT_DEFER | LAST_RANK))) { // but change it into a deferral if that is allowed
\r
1630 moveStack[i] &= ~PROMOTE;
\r
1631 if(p[board[f]].value == 40) p[board[f]].promoFlag &= LAST_RANK; else
\r
1632 if(!(flags & promoBoard[f])) moveStack[i] |= DEFER; // came from outside zone, so essential deferral
\r
1636 for(i=retFirst; i<retMSP; i++) printf("# %d. %08x %08x %s\n", i-20, moveStack[i], ret, MoveToText(moveStack[i], 0));
\r
1638 return (i >= retMSP ? INVALID : moveStack[i]);
\r
1642 Highlight(char *coords)
\r
1644 int i, j, n, sqr, cnt=0;
\r
1645 char b[BSIZE], buf[2000], *q;
\r
1646 for(i=0; i<bsize; i++) b[i] = 0;
\r
1647 ReadSquare(coords, &sqr);
\r
1648 MapFromScratch(attacks);
\r
1651 Search(-INF-1, INF+1, 0, 1, sup1, sup2);
\r
1654 for(i=retFirst; i<retMSP; i++) {
\r
1655 if(sqr == (moveStack[i]>>SQLEN & SQUARE)) {
\r
1656 int t = moveStack[i] & SQUARE;
\r
1657 if(t >= SPECIAL) continue;
\r
1658 b[t] = (board[t] == EMPTY ? 'Y' : 'R'); cnt++;
\r
1661 if(!cnt) { // no moves from given square
\r
1662 if(sqr != lastPut) return; // refrain from sending empty FEN
\r
1663 // we lifted a piece for second leg of move
\r
1664 for(i=retFirst; i<retMSP; i++) {
\r
1665 if(lastLift == (moveStack[i]>>SQLEN & SQUARE)) {
\r
1666 int e, t = moveStack[i] & SQUARE;
\r
1667 if(t < SPECIAL) continue; // only special moves
\r
1668 e = lastLift + epList[t - SPECIAL]; // decode
\r
1669 t = lastLift + toList[t - SPECIAL];
\r
1670 if(e != sqr) continue;
\r
1671 b[t] = (board[t] == EMPTY ? 'Y' : 'R'); cnt++;
\r
1675 } else lastLift = sqr; // remember
\r
1678 for(i=BH-1; i>=0; i--) {
\r
1679 for(j=0; j<BH; j++) {
\r
1681 if(b[n]) *q++ = b[n]; else {
\r
1682 if(q > buf && q[-1] <= '9' && q[-1] >= '0') {
\r
1685 if(q > buf+1 && q[-2] <= '9' && q[-2] >= '0') q[-2]++; else q[-1] = '1', *q++ = '0';
\r
1687 } else *q++ = '1';
\r
1693 printf("highlight %s\n", buf);
\r
1697 SearchBestMove (int stm, int timeLeft, int mps, int timeControl, int inc, int timePerMove, MOVE *move, MOVE *ponderMove)
\r
1699 int score, targetTime, movesLeft = 50;
\r
1700 if(mps) movesLeft = mps - (moveNr>>1)%mps;
\r
1701 targetTime = timeLeft*10 / (movesLeft + 2) + 1000 * inc;
\r
1702 if(timePerMove > 0) targetTime = timeLeft * 5;
\r
1703 startTime = GetTickCount();
\r
1704 tlim1 = 0.2*targetTime;
\r
1705 tlim2 = 1.9*targetTime;
\r
1707 MapFromScratch(attacks);
\r
1708 score = Search(-INF-1, INF+1, rootEval, 20, sup1, sup2);
\r
1710 *ponderMove = INVALID;
\r
1715 PonderUntilInput (int stm)
\r
1719 int TakeBack(int n)
\r
1720 { // reset the game and then replay it to the desired point
\r
1722 stm = Setup2(NULL);
\r
1723 printf("# setup done");fflush(stdout);
\r
1724 last = moveNr - n; if(last < 0) last = 0;
\r
1725 for(moveNr=0; moveNr<last; moveNr++) stm = MakeMove2(stm, gameMove[moveNr]),printf("make %2d: %x\n", moveNr, gameMove[moveNr]);
\r
1729 void PrintResult(int stm, int score)
\r
1731 if(score == 0) printf("1/2-1/2\n");
\r
1732 if(score > 0 && stm == WHITE || score < 0 && stm == BLACK) printf("1-0\n");
\r
1733 else printf("0-1\n");
\r
1738 int engineSide=NONE; // side played by engine
\r
1739 int timeLeft; // timeleft on engine's clock
\r
1740 int mps, timeControl, inc, timePerMove; // time-control parameters, to be used by Search
\r
1741 int maxDepth; // used by search
\r
1742 MOVE move, ponderMove;
\r
1744 char inBuf[8000], command[80];
\r
1747 SetUp(chuArray, 0);
\r
1750 // pbytes(promoBoard);
\r
1751 // Search(-INF, INF, 0, 1, ABSENT, ABSENT);
\r
1752 // pmoves(20, retMSP);
\r
1753 //MapFromScratch(attacks);
\r
1754 // MapOneColor(1, last[WHITE], attacks);
\r
1756 while(1) { // infinite loop
\r
1758 fflush(stdout); // make sure everything is printed before we do something that might take time
\r
1760 if(stm == engineSide) { // if it is the engine's turn to move, set it thinking, and let it move
\r
1762 score = SearchBestMove(stm, timeLeft, mps, timeControl, inc, timePerMove, &move, &ponderMove);
\r
1764 if(move == INVALID) { // game apparently ended
\r
1765 engineSide = NONE; // so stop playing
\r
1766 PrintResult(stm, score);
\r
1768 stm = MakeMove2(stm, move); // assumes MakeMove returns new side to move
\r
1769 gameMove[moveNr++] = move; // remember game
\r
1770 printf("move %s\n", MoveToText(move, 1));
\r
1774 fflush(stdout); // make sure everything is printed before we do something that might take time
\r
1776 // now it is not our turn (anymore)
\r
1777 if(engineSide == ANALYZE) { // in analysis, we always ponder the position
\r
1778 PonderUntilInput(stm);
\r
1780 if(engineSide != NONE && ponder == ON && moveNr != 0) { // ponder while waiting for input
\r
1781 if(ponderMove == INVALID) { // if we have no move to ponder on, ponder the position
\r
1782 PonderUntilInput(stm);
\r
1784 int newStm = MakeMove2(stm, ponderMove);
\r
1785 PonderUntilInput(newStm);
\r
1786 UnMake2(ponderMove);
\r
1791 // wait for input, and read it until we have collected a complete line
\r
1792 for(i = 0; (inBuf[i] = getchar()) != '\n'; i++);
\r
1795 pmoves(retFirst, retMSP);
\r
1797 // extract the first word
\r
1798 sscanf(inBuf, "%s", command);
\r
1799 printf("in: %s\n", command);
\r
1801 // recognize the command,and execute it
\r
1802 if(!strcmp(command, "quit")) { break; } // breaks out of infinite loop
\r
1803 if(!strcmp(command, "force")) { engineSide = NONE; continue; }
\r
1804 if(!strcmp(command, "analyze")) { engineSide = ANALYZE; continue; }
\r
1805 if(!strcmp(command, "exit")) { engineSide = NONE; continue; }
\r
1806 if(!strcmp(command, "otim")) { goto noPonder; } // do not start pondering after receiving time commands, as move will follow immediately
\r
1807 if(!strcmp(command, "time")) { sscanf(inBuf, "time %d", &timeLeft); goto noPonder; }
\r
1808 if(!strcmp(command, "level")) {
\r
1810 sscanf(inBuf, "level %d %d %d", &mps, &min, &inc) == 3 || // if this does not work, it must be min:sec format
\r
1811 sscanf(inBuf, "level %d %d:%d %d", &mps, &min, &sec, &inc);
\r
1812 timeControl = 60*min + sec; timePerMove = -1;
\r
1815 if(!strcmp(command, "protover")){
\r
1816 printf("feature ping=1 setboard=1 colors=0 usermove=1 memory=1 debug=1 sigint=0 sigterm=0\n");
\r
1817 printf("feature variants=\"chu,dai,12x12+0_fairy\"\n");
\r
1818 printf("feature myname=\"HaChu " VERSION "\" highlight=1\n");
\r
1819 printf("feature option=\"Resign -check 0\"\n"); // example of an engine-defined option
\r
1820 printf("feature option=\"Contempt -spin 0 -200 200\"\n"); // and another one
\r
1821 printf("feature done=1\n");
\r
1824 if(!strcmp(command, "option")) { // setting of engine-define option; find out which
\r
1825 if(sscanf(inBuf+7, "Resign=%d", &resign) == 1) continue;
\r
1826 if(sscanf(inBuf+7, "Contempt=%d", &contemptFactor) == 1) continue;
\r
1829 if(!strcmp(command, "variant")) {
\r
1830 for(i=0; i<2; i++) {
\r
1831 sscanf(inBuf+8, "%s", command);
\r
1832 if(!strcmp(variants[i].name, command)) {
\r
1833 Init(i); stm = Setup2(NULL); break;
\r
1838 if(!strcmp(command, "sd")) { sscanf(inBuf, "sd %d", &maxDepth); continue; }
\r
1839 if(!strcmp(command, "st")) { sscanf(inBuf, "st %d", &timePerMove); continue; }
\r
1840 if(!strcmp(command, "memory")) { SetMemorySize(atoi(inBuf+7)); continue; }
\r
1841 if(!strcmp(command, "ping")) { printf("pong%s", inBuf+4); continue; }
\r
1842 // if(!strcmp(command, "")) { sscanf(inBuf, " %d", &); continue; }
\r
1843 if(!strcmp(command, "new")) { engineSide = BLACK; stm = Setup2(DEFAULT_FEN); maxDepth = MAXPLY; randomize = OFF; continue; }
\r
1844 if(!strcmp(command, "setboard")){ engineSide = NONE; stm = Setup2(inBuf+9); continue; }
\r
1845 if(!strcmp(command, "easy")) { ponder = OFF; continue; }
\r
1846 if(!strcmp(command, "hard")) { ponder = ON; continue; }
\r
1847 if(!strcmp(command, "undo")) { stm = TakeBack(1); continue; }
\r
1848 if(!strcmp(command, "remove")) { stm = TakeBack(2); continue; }
\r
1849 if(!strcmp(command, "go")) { engineSide = stm; continue; }
\r
1850 if(!strcmp(command, "post")) { postThinking = ON; continue; }
\r
1851 if(!strcmp(command, "nopost")) { postThinking = OFF;continue; }
\r
1852 if(!strcmp(command, "random")) { randomize = ON; continue; }
\r
1853 if(!strcmp(command, "hint")) { if(ponderMove != INVALID) printf("Hint: %s\n", MoveToText(ponderMove, 0)); continue; }
\r
1854 if(!strcmp(command, "lift")) { Highlight(inBuf+5); continue; }
\r
1855 if(!strcmp(command, "put")) { ReadSquare(inBuf+4, &lastPut); continue; }
\r
1856 if(!strcmp(command, "book")) { continue; }
\r
1857 // non-standard commands
\r
1858 if(!strcmp(command, "p")) { pboard(board); continue; }
\r
1859 if(!strcmp(command, "w")) { pmap(attacks, WHITE); continue; }
\r
1860 if(!strcmp(command, "b")) { pmap(attacks, BLACK); continue; }
\r
1861 // ignored commands:
\r
1862 if(!strcmp(command, "xboard")) { continue; }
\r
1863 if(!strcmp(command, "computer")){ continue; }
\r
1864 if(!strcmp(command, "name")) { continue; }
\r
1865 if(!strcmp(command, "ics")) { continue; }
\r
1866 if(!strcmp(command, "accepted")){ continue; }
\r
1867 if(!strcmp(command, "rejected")){ continue; }
\r
1868 if(!strcmp(command, "result")) { continue; }
\r
1869 if(!strcmp(command, "hover")) { continue; }
\r
1870 if(!strcmp(command, "")) { continue; }
\r
1871 if(!strcmp(command, "usermove")){
\r
1872 int move = ParseMove(inBuf+9);
\r
1873 if(move == INVALID) printf("Illegal move\n");
\r
1875 stm = MakeMove2(stm, move);
\r
1876 ponderMove = INVALID;
\r
1877 gameMove[moveNr++] = move; // remember game
\r
1881 printf("Error: unknown command\n");
\r