1 /***************************************************************************/
\r
3 /* Version of the sub-2KB (source) micro-Max Chess program, fused to a */
\r
4 /* generic WinBoard interface, loading its move-generator tables from file */
\r
5 /***************************************************************************/
\r
7 /*****************************************************************/
\r
8 /* LICENCE NOTIFICATION */
\r
9 /* Fairy-Max 4.8 is free software, and you have my permission do */
\r
10 /* with it whatever you want, whether it is commercial or not. */
\r
11 /* Note, however, that Fairy-Max can easily be configured through*/
\r
12 /* its fmax.ini file to play Chess variants that are legally pro-*/
\r
13 /* tected by patents, and that to do so would also require per- */
\r
14 /* mission of the holders of such patents. No guarantees are */
\r
15 /* given that Fairy-Max does anything in particular, or that it */
\r
16 /* would not wreck the hardware it runs on, and running it is */
\r
17 /* entirely for your own risk. H.G,Muller, author of Fairy-Max */
\r
18 /*****************************************************************/
\r
21 #define VERSION "4.8M"
\r
30 #define INI_FILE "fmax.ini"
\r
34 # include <windows.h>
\r
36 # include <sys/time.h>
\r
37 int GetTickCount() // with thanks to Tord
\r
39 gettimeofday(&t, NULL);
\r
40 return t.tv_sec*1000 + t.tv_usec/1000;
\r
52 /* The following macros indicate the differences between Fairy-Max and its */
\r
53 /* dedicated Shatranj derivative ShaMax so that these can now be compiled */
\r
54 /* from the same unified source file. */
\r
55 /* Compile with gcc option -DSHATRANJ to build ShaMax. */
\r
59 # define NAME "ShaMax"
\r
60 # define SHAMAX(x) x
\r
65 # define NAME "Fairy-Max"
\r
70 /* make unique integer from engine move representation */
\r
71 #define PACK_MOVE 256*K + L;
\r
73 /* convert intger argument back to engine move representation */
\r
74 #define UNPACK_MOVE(A) K = (A)>>8 & 255; L = (A) & 255;
\r
76 /* Global variables visible to engine. Normally they */
\r
77 /* would be replaced by the names under which these */
\r
78 /* are known to your engine, so that they can be */
\r
79 /* manipulated directly by the interface. */
\r
93 int Threshold = 800;
\r
95 char piecename[32], piecetype[32];
\r
96 char *inifile = INI_FILE;
\r
98 int Ticks, tlim, Setup, SetupQ;
\r
100 int GameHistory[1024];
\r
101 char HistoryBoards[1024][STATE], setupPosition[131];
\r
102 int GamePtr, HistPtr;
\r
105 #define K(A,B) *(int*)(T+A+S*(B&31))
\r
106 #define J(A) K(y+A,b[y])-K(x+A,u)-K(H+A,t)
\r
109 struct _ {int K,V;char X,Y,D,F;} *A; /* hash table, 16M+8 entries*/
\r
111 int M=136,S=128,I=8e3,Q,O,K,N,j,R,J,Z,LL, /* M=0x88 */
\r
113 w[16]={0,2,2,-1,7,8,12,23,7,5}, /* relative piece values */
\r
115 oo[32], /* initial piece setup */
\r
117 od[16]; /* 1st dir. in o[] per piece*/
\r
119 signed char L,pl[17],
\r
120 b[513], /* board: 16x8+dummy, + PST */
\r
121 T[4104], /* hash translation table */
\r
123 n[]=".*XKNBRQEWFMACHG?x+knbrqewfmachg"; /* piece symbols on printout*/
\r
127 i=-1;W(++i<128)printf(" %c",(i&15)==BW&&(i+=15-BW)?10:n[b[i]&31]);
\r
131 D(k,q,l,e,E,z,n) /* recursive minimax search, k=moving side, n=depth*/
\r
132 int k,q,l,e,E,z,n; /* (q,l)=window, e=current eval. score, E=e.p. sqr.*/
\r
133 { /* e=score, z=prev.dest; J,Z=hashkeys; return score*/
\r
134 int j,r,m,v,d,h,i,F,G,P,V,f=J,g=Z,C,s,flag,FF;
\r
135 signed char t,p,u,x,y,X,Y,H,B;
\r
136 struct _*a=A+(J+(k+S)*E&U-1); /* lookup pos. in hash table*/
\r
137 q-=q<e;l-=l<=e; /* adj. window: delay bonus */
\r
138 d=a->D;m=a->V;X=a->F;Y=a->Y; /* resume at stored depth */
\r
139 if(a->K-Z|z&S | /* miss: other pos. or empty*/
\r
140 !(m<=q|X&8&&m>=l|X&S)) /* or window incompatible */
\r
141 d=Y=0; /* start iter. from scratch */
\r
142 X=a->X; /* start at best-move hint */
\r
143 W(d++<n||d<3|| /*** min depth = 2 iterative deepening loop */
\r
144 z&S&&K==I&&(GetTickCount()-Ticks<tlim&d<=MaxDepth|| /* root: deepen upto time */
\r
145 (K=X,L=Y&~S,Score=m,d=3))) /* time's up: go do best */
\r
146 {x=B=X; /* start scan at prev. best */
\r
147 h=Y&S; /* request try noncastl. 1st*/
\r
148 P=d>2&&l+I?D(16-k,-l,1-l,-e,2*S,2*S,d-3):I; /* search null move */
\r
149 m=-P<l|R<5?d-2?-I:e:-P; /*** prune if > beta unconsidered:static eval */
\r
150 SHAMAX( if(pl[k]<=1&pl[16-k]>1)m=I-1; ) /* bare king loses */
\r
151 N++; /* node count (for timing) */
\r
152 do{u=b[x]; /* scan board looking for */
\r
153 if(u&&(u&16)==k) /* own piece (inefficient!)*/
\r
154 {r=p=u&15; /* p = piece type (set r>0) */
\r
155 j=od[p]; /* first step vector f.piece*/
\r
156 W(r=o[++j]) /* loop over directions o[] */
\r
157 {A: /* resume normal after best */
\r
158 flag=h?3:of[j]; /* move modes (for fairies) */
\r
159 y=x;F=FF=G=S; /* (x,y)=move, (F,G)=castl.R*/
\r
160 do{ /* y traverses ray, or: */
\r
161 H=y=h?Y^h:y+r; /* sneak in prev. best move */
\r
162 if(flag&1<<8)H=y=(y&15)>13?y+BW:(y&15)>=BW?y-BW:y; /* cylinder board */
\r
163 if(y&S|(y&15)>=BW)break; /* board edge hit */
\r
165 if(flag&1<<9) /* if multipath move */
\r
166 {t=flag>>12; /* get dir. stepped twice */
\r
167 if(b[x+t]){if(b[y-2*t]|b[y-t])break;}else
\r
168 if(b[x+2*t]&&b[y-t])break; /* test if empty path exists*/
\r
171 m=E<16|(E^112)<16&&flag&1&y-E<2&E-y<2?I:m; /* bad castling */
\r
172 if(p<3&y==E)H=z&127; /* shift capt.sqr. H if e.p.*/
\r
174 if(flag&1+!t) /* mode (capt/nonc) allowed?*/
\r
175 {if(t&&(t&16)==k)break; /* capture own */
\r
176 i=w[t&15]+((t&192)>>sh); /* value of capt. piece t */
\r
177 if(i<0)m=I,d=98; /* K capture */
\r
178 if(m>=l&d>1)goto C; /* abort on fail high */
\r
179 v=d-1?e:i-p; /*** MVV/LVA scoring if d=1**/
\r
180 if(d-!t>1) /*** all captures if d=2 ***/
\r
181 {v=centr[p]?b[x+257]-b[y+257]:0; /* center positional pts. */
\r
182 b[G]=b[H]=b[x]=0;b[y]=u|32; /* do move, set non-virgin */
\r
183 if(!(G&S))b[FF]=k+6,v+=50; /* castling: put R & score */
\r
184 v-=w[p]>0|R<EG?0:20; /*** freeze K in mid-game ***/
\r
185 if(p<3) /* pawns: */
\r
186 {v-=9*((x-2&M||b[x-2]-u)+ /* structure, undefended */
\r
187 (x+2&M||b[x+2]-u) /* squares plus bias */
\r
188 +(w[b[x^16]&15]<0)) /*** cling to magnetic K ***/
\r
189 +(R-76>>2); /* end-game Pawn-push bonus */
\r
190 b[y]+=V=y+r+1&S?647-p:2*(u&y+16&32); /* upgrade P or convert to Q*/
\r
191 V>>=sh; /* for Shatranj promo to F */
\r
192 i+=V; /* promotion / passer bonus */
\r
193 } if(z&S && GamePtr<6) v+=(rand()>>10&31)-16;
\r
194 J+=J(0);Z+=J(4)+G-S;
\r
195 SHAMAX( pl[k]-=!!t; ) /* count pieces per side */
\r
196 v+=e+i;V=m>q?m:q; /*** new eval & alpha ****/
\r
197 C=d-1-(d>5&p>2&!t&!h); /* nw depth, reduce non-cpt.*/
\r
198 C=R<EG|P-I|d<3||t&&p-3?C:d; /* extend 1 ply if in-check */
\r
200 s=C>2|v>V?-D(16-k,-l,-V,-v,/*** futility, recursive eval. of reply */
\r
202 W(s>q&++C<d); v=s; /* no fail:re-srch unreduced*/
\r
203 if(z&S&&K-I) /* move pending: check legal*/
\r
204 {if(v+I&&x==K&y==L) /* if move found */
\r
206 if(b[y]-u&15)b[y]-=PromPiece, /* under-promotion, correct */
\r
207 J+=PromPiece; /* piece & invalidate hash */
\r
208 a->D=99;a->V=0; /* lock game in hash as draw*/
\r
209 R-=i/FAC; /*** total captd material ***/
\r
210 Fifty = t|p<3?0:Fifty+1;
\r
211 return l;} /* & not in check, signal */
\r
212 v=m; /* (prevent fail-lows on */
\r
213 } /* K-capt. replies) */
\r
215 SHAMAX( pl[k]+=!!t; )
\r
216 b[G]=k+6;b[FF]=b[y]=0;b[x]=u;b[H]=t; /* undo move,G can be dummy */
\r
217 } /* if non-castling */
\r
218 if(v>m) /* new best, update max,best*/
\r
219 m=v,X=x,Y=y|S&F; /* mark non-double with S */
\r
220 if(h){h=0;goto A;} /* redo after doing old best*/
\r
223 if(flag&15^4|u&32|| /* no double or moved before*/
\r
224 p>2&& /* no P & no lateral K move,*/
\r
225 (b[G=r<0?x&~15:BW-1|x&112]-k-6 /* no virgin R in corner G, */
\r
226 ||b[G^1]|b[G^2]|b[FF=r<0?G+3:G-2]) /* no 2 empty sq. next to R */
\r
227 )t+=flag&4; /* fake capt. for nonsliding*/
\r
228 else F=y; /* enable e.p. */
\r
229 if(s&&flag&8)t=0,flag^=flag>>4&15; /* hoppers go to next phase */
\r
230 if(!(flag&S)) /* zig-zag piece? */
\r
231 r^=flag>>12,flag^=flag>>4&15; /* alternate vector & mode */
\r
232 }W(!t); /* if not capt. continue ray*/
\r
234 if((++x&15)>=BW)x=x+16&112; /* next sqr. of board, wrap */
\r
236 C:FMAX( m=m+I|P==I?m:(X=Y=0); ) /* if stalemate, draw-score */
\r
237 if(a->D<99) /* protect game history */
\r
238 a->K=Z,a->V=m,a->D=d,a->X=X, /* always store in hash tab */
\r
239 a->F=8*(m>q)|S*(m<l),a->Y=Y; /* move, type (bound/exact),*/
\r
241 printf("%2d ",d-2);
\r
243 printf("%8d %10d %c%c%c%c\n",(GetTickCount()-Ticks)/10,N,
\r
244 'a'+(X&15),'8'-(X>>4),'a'+(Y&15),'8'-(Y>>4&7)),fflush(stdout);
\r
245 }} /* encoded in X S,8 bits */
\r
246 if(z&4*S)K=X,L=Y&~S;
\r
247 return m+=m<e; /* delayed-loss bonus */
\r
251 /* Generic main() for Winboard-compatible engine */
\r
252 /* (Inspired by TSCP) */
\r
253 /* Author: H.G. Muller */
\r
255 /* The engine is invoked through the following */
\r
256 /* subroutines, that can draw on the global vaiables */
\r
257 /* that are maintained by the interface: */
\r
258 /* Side side to move */
\r
259 /* Move move input to or output from engine */
\r
260 /* PromPiece requested piece on promotion move */
\r
261 /* TimeLeft ms left to next time control */
\r
262 /* MovesLeft nr of moves to play within TimeLeft */
\r
263 /* MaxDepth search-depth limit in ply */
\r
264 /* Post boolean to invite engine babble */
\r
266 /* InitEngine() progran start-up initialization */
\r
267 /* InitGame() initialization to start new game */
\r
268 /* (sets Side, but not time control) */
\r
269 /* Think() think up move from current position */
\r
270 /* (leaves move in Move, can be invalid */
\r
271 /* if position is check- or stalemate) */
\r
272 /* DoMove() perform the move in Move */
\r
273 /* (togglese Side) */
\r
274 /* ReadMove() convert input move to engine format */
\r
275 /* PrintMove() print Move on standard output */
\r
276 /* Legal() check Move for legality */
\r
277 /* ClearBoard() make board empty */
\r
278 /* PutPiece() put a piece on the board */
\r
280 /* define this to the codes used in your engine, */
\r
281 /* if the engine hasn't defined it already. */
\r
283 int PrintResult(int s)
\r
285 int i, j, k, cnt=0;
\r
287 /* search last 50 states with this stm for third repeat */
\r
288 for(j=2; j<=100 && j <= HistPtr; j+=2)
\r
290 for(k=0; k<STATE; k++)
\r
291 if(HistoryBoards[HistPtr][k] !=
\r
292 HistoryBoards[HistPtr-j&1023][k] )
\r
295 /* is the same, count it */
\r
296 if(++cnt > 1) /* third repeat */
\r
298 printf("1/2-1/2 {Draw by repetition}\n");
\r
304 cnt = D(s,-I,I,Q,O,LL|4*S,3);
\r
305 if(cnt>-I+1 && K==0 && L==0) {
\r
306 printf("1/2-1/2 {Stalemate}\n");
\r
311 printf("0-1 {Black mates}\n");
\r
313 printf("1-0 {White mates}\n");
\r
317 printf("1/2-1/2 {Draw by fifty move rule}\n");
\r
321 if(pl[s]==1 && pl[16-s]==1) {
\r
322 printf("1/2-1/2 {Insufficient mating material}\n");
\r
325 if(pl[s]<=1 && pl[16-s]>1) {
\r
327 printf("0-1 {Bare King}\n");
\r
329 printf("1-0 {Bare King}\n");
\r
341 N=32*S+7;W(N-->S+3)T[N]=rand()>>9;
\r
342 srand(GetTickCount());
\r
349 for(i=0;i<16*BH;i++)b[i]=0;
\r
351 {b[K]=oo[K+16]+16;b[K+112]=oo[K];b[K+16]=18;b[K+96]=1; /* initial board setup*/
\r
352 L=8;W(L--)b[16*L+K+257]=(K-BW/2)*(K-BW/2)+(L-3.5)*(L-3.5); /* center-pts table */
\r
353 } /*(in unused half b[])*/
\r
354 Side = WHITE; Q=0; O=S;
\r
356 for(i=0; i<BW; i++) if(i!=3) R += (w[oo[i]]/FAC) + (w[oo[i+16]]/FAC);
\r
357 UnderProm = -1; pl[WHITE] = pl[BLACK] = 2*BW;
\r
360 void CopyBoard(int s)
\r
362 int i, j, k, cnt=0;
\r
364 /* copy game representation of engine to HistoryBoard */
\r
365 /* don't forget castling rights and e.p. state! */
\r
366 for(i=0; i<BH; i++)
\r
367 for(j=0; j<BW; j++) /* board squares */
\r
368 HistoryBoards[s][BW*i+j] = b[16*i+j]|64*(16*i+j==O);
\r
371 void PrintVariants()
\r
373 int i, j, count=0; char c, buf[80];
\r
376 f = fopen(INI_FILE, "r");
\r
377 if(f==NULL) return;
\r
379 /* search for game names in definition file */
\r
381 while(fscanf(f, "Game: %s", buf) != 1 && c != EOF)
\r
382 while((c = fgetc(f)) != EOF && c != '\n');
\r
383 if(c == EOF) break;
\r
384 if(count++) printf(",");
\r
391 void PrintOptions()
\r
393 printf("feature option=\"Resign -check %d\"\n", Resign);
\r
394 printf("feature option=\"Resign Threshold -spin %d 200 1200\"\n", Threshold);
\r
395 printf("feature option=\"Ini File -file %s\"\n", inifile);
\r
396 printf("feature option=\"Playing Style ;-) -combo Brilliant /// *Brave /// Beautiful\"\n");
\r
397 printf("feature option=\"Dummy Slider Example -slider 20 0 100\"\n");
\r
398 printf("feature option=\"Dummy String Example -file happy birthday!\"\n");
\r
399 printf("feature option=\"Dummy Path Example -path .\"\n");
\r
400 printf("feature option=\"Clear Hash -button\"\n");
\r
401 printf("feature done=1\n");
\r
404 int LoadGame(char *name)
\r
406 int i, j, count=0; char c, buf[80];
\r
407 static int currentVariant;
\r
410 f = fopen(inifile, "r");
\r
412 { printf("telluser piece-desription file '%s' not found\n", inifile);
\r
415 if(fscanf(f, "version 4.8(%c)", &c)!=1 || c != 'w')
\r
416 { printf("telluser incompatible fmax.ini file\n"); exit(0); }
\r
419 { /* search for game name in definition file */
\r
420 while(fscanf(f, "Game: %s", buf)!=1 || strcmp(name, buf) ) {
\r
421 while((c = fgetc(f)) != EOF && c != '\n');
\r
424 printf("telluser variant %s not supported\n", name);
\r
426 return; /* keep old settings */
\r
429 currentVariant = count;
\r
432 /* We have found variant, or if none specified, are at beginning of file */
\r
433 if(fscanf(f, "%dx%d", &BW, &BH)!=2 || BW>12 || BH!=8)
\r
434 { printf("telluser unsupported board size %dx%d\n",BW,BH); exit(0); }
\r
436 for(i=0; i<BW; i++) fscanf(f, "%d", oo+i);
\r
437 for(i=0; i<BW; i++) fscanf(f, "%d", oo+i+16);
\r
438 for(i= 0; i<=U; i++)
\r
439 A[i].K = A[i].D = A[i].X = A[i].Y = A[i].F = 0; /* clear hash */
\r
440 for(i=0; i<32; i++) piecetype[i] = 0;
\r
443 while(fscanf(f, "%d,%x", o+j, of+j)==2 ||
\r
444 fscanf(f,"%c:%d",&c, w+i+1)==2)
\r
446 { od[++i]=j; centr[i] = c>='a';
\r
447 piecetype[c&31]=i; piecename[i]=c&31;
\r
450 /* printf("# c='%c' i=%d od[i]=%d j=%d (%3d,%8x)\n",c?c:' ',i,od[i],j,o[j-1],of[j-1]); /**/
\r
451 c=0; if(i>15 || j>255) break;
\r
455 sh = w[7] < 250 ? 3 : 0;
\r
458 int main(int argc, char **argv)
\r
460 int Computer, MaxTime, MaxMoves, TimeInc, sec, i, j;
\r
461 char line[256], command[256], c, cc;
\r
465 if(argc>1 && sscanf(argv[1], "%d", &m)==1)
\r
466 { U = (1<<m)-1; argc--; argv++; }
\r
467 A = (struct _ *) calloc(U+1, sizeof(struct _));
\r
468 if(argc>1) inifile = argv[1];
\r
470 signal(SIGINT, SIG_IGN);
\r
471 printf("tellics say " NAME " " VERSION "\n");
\r
472 printf("tellics say by H.G. Muller\n");
\r
473 printf("tellics say Gothic Chess is protected by U.S. patent #6,481,716 by Ed Trice.\n");
\r
474 printf("tellics say Falcon Chess is protected by U.S. patent #5,690,334 by George W. Duke\n");
\r
479 MaxTime = 10000; /* 10 sec */
\r
480 MaxDepth = 30; /* maximum depth of your search */
\r
484 if (Side == Computer) {
\r
485 /* think up & do move, measure time used */
\r
486 /* it is the responsibility of the engine */
\r
487 /* to control its search time based on */
\r
488 /* MovesLeft, TimeLeft, MaxMoves, TimeInc */
\r
489 /* Next 'MovesLeft' moves have to be done */
\r
490 /* within TimeLeft+(MovesLeft-1)*TimeInc */
\r
491 /* If MovesLeft<0 all remaining moves of */
\r
492 /* the game have to be done in this time. */
\r
493 /* If MaxMoves=1 any leftover time is lost*/
\r
494 Ticks = GetTickCount();
\r
495 m = MovesLeft<=0 ? 40 : MovesLeft;
\r
496 tlim = (0.6-0.06*(BW-8))*(TimeLeft+(m-1)*TimeInc)/(m+7);
\r
497 if(tlim>TimeLeft/15) tlim = TimeLeft/15;
\r
498 PromPiece = 0; /* Always promote to Queen ourselves */
\r
500 if (D(Side,-I,I,Q,O,LL|S,3)==I) {
\r
501 Side ^= BLACK^WHITE;
\r
502 if(UnderProm>=0 && UnderProm != L)
\r
503 { printf("tellics I hate under-promotions!\n");
\r
504 printf("resign { underpromotion } \n");
\r
507 } else UnderProm = -1;
\r
509 printf("%c%c%c%c",'a'+(K&15),'0'+BH-(K>>4),
\r
510 'a'+(L&15),'0'+BH-(L>>4));
\r
512 m = GetTickCount() - Ticks;
\r
514 /* time-control accounting */
\r
516 TimeLeft += TimeInc;
\r
517 if(--MovesLeft == 0) {
\r
518 MovesLeft = MaxMoves;
\r
520 TimeLeft = MaxTime;
\r
521 else TimeLeft += MaxTime;
\r
524 GameHistory[GamePtr++] = PACK_MOVE;
\r
525 CopyBoard(HistPtr=HistPtr+1&1023);
\r
526 if(Resign && Score <= -Threshold) {
\r
527 printf("resign\n"); Computer=EMPTY;
\r
528 } else if(PrintResult(Side))
\r
531 if(!PrintResult(Side))
\r
532 printf("resign { refuses own move }\n");
\r
537 if (!fgets(line, 256, stdin))
\r
539 if (line[0] == '\n')
\r
541 sscanf(line, "%s", command);
\r
542 if (!strcmp(command, "xboard"))
\r
544 if (!strcmp(command, "protover")) {
\r
545 printf("feature myname=\"" NAME " " VERSION "\"\n");
\r
546 printf("feature memory=1\n");
\r
547 printf("feature setboard=0 ping=1 done=0\n");
\r
548 printf("feature variants=\"");
\r
554 if (!strcmp(command, "ping")) { int nr=0;
\r
555 sscanf(line, "ping %d", &nr);
\r
556 printf("pong %d\n", nr);
\r
559 if (!strcmp(command, "p")) {
\r
563 if (!strcmp(command, "memory")) {
\r
565 sscanf(line+6, "%d", &mem); mem = (mem*1024*1024)/12; // max nr of hash entries
\r
566 mask = 0x7FFFFFFF; while(mask > mem) mask >>= 1;
\r
569 A = (struct _ *) calloc(U+1, sizeof(struct _));
\r
573 if (!strcmp(command, "new")) {
\r
574 /* start new game */
\r
575 LoadGame("normal");
\r
577 GamePtr = Setup = 0;
\r
581 TimeLeft = MaxTime;
\r
582 MovesLeft = MaxMoves;
\r
583 for(nr=0; nr<1024; nr++)
\r
584 for(m=0; m<STATE; m++)
\r
585 HistoryBoards[nr][m] = 0;
\r
588 if (!strcmp(command, "quit"))
\r
591 if (!strcmp(command, "force")) {
\r
592 /* computer plays neither */
\r
596 if (!strcmp(command, "white")) {
\r
597 /* set white to move in current position */
\r
602 if (!strcmp(command, "black")) {
\r
603 /* set blck to move in current position */
\r
608 if (!strcmp(command, "st")) {
\r
609 /* move-on-the-bell mode */
\r
610 /* indicated by MaxMoves = 1 */
\r
611 sscanf(line, "st %d", &MaxTime);
\r
612 MovesLeft = MaxMoves = 1;
\r
613 TimeLeft = MaxTime *= 1000;
\r
617 if (!strcmp(command, "sd")) {
\r
618 /* set depth limit (remains in force */
\r
619 /* until next 'sd n' command) */
\r
620 sscanf(line, "sd %d", &MaxDepth);
\r
621 MaxDepth += 2; /* QS depth */
\r
624 if (!strcmp(command, "level")) {
\r
625 /* normal or blitz time control */
\r
627 if(sscanf(line, "level %d %d %d",
\r
628 &MaxMoves, &MaxTime, &TimeInc)!=3 &&
\r
629 sscanf(line, "level %d %d:%d %d",
\r
630 &MaxMoves, &MaxTime, &sec, &TimeInc)!=4)
\r
632 MovesLeft = MaxMoves;
\r
633 TimeLeft = MaxTime = 60000*MaxTime + 1000*sec;
\r
637 if (!strcmp(command, "time")) {
\r
638 /* set time left on clock */
\r
639 sscanf(line, "time %d", &TimeLeft);
\r
640 TimeLeft *= 10; /* centi-sec to ms */
\r
643 if (!strcmp(command, "otim")) {
\r
644 /* opponent's time (not kept, so ignore) */
\r
647 if (!strcmp(command, "easy")) {
\r
650 if (!strcmp(command, "hard")) {
\r
653 if (!strcmp(command, "accepted")) {
\r
656 if (!strcmp(command, "rejected")) {
\r
659 if (!strcmp(command, "random")) {
\r
662 if (!strcmp(command, "option")) {
\r
663 int i; static char filename[80];
\r
664 if(sscanf(line+7, "Resign=%d", &Resign) == 1) continue;
\r
665 if(sscanf(line+7, "Resign Threshold=%d", &Threshold) == 1) continue;
\r
666 if(sscanf(line+7, "Ini File=%s", filename) == 1) {
\r
667 inifile = filename; continue;
\r
669 if(sscanf(line+7, "Clear Hash") == 1) for(i=0; i<U; i++) A->K = 0;
\r
672 if (!strcmp(command, "go")) {
\r
673 /* set computer to play current side to move */
\r
675 MovesLeft = -(GamePtr+(Side==WHITE)>>1);
\r
676 while(MaxMoves>0 && MovesLeft<=0)
\r
677 MovesLeft += MaxMoves;
\r
680 if (!strcmp(command, "hint")) {
\r
681 Ticks = GetTickCount(); tlim = 1000;
\r
682 D(Side,-I,I,Q,O,LL|4*S,6);
\r
686 printf("%c%c%c%c",'a'+(K&7),'8'-(K>>4),
\r
687 'a'+(L&7),'8'-(L>>4));
\r
691 if (!strcmp(command, "undo") && (nr=1) ||
\r
692 !strcmp(command, "remove") && (nr=2) ) {
\r
693 /* 'take back' moves by replaying game */
\r
694 /* from history until desired ply */
\r
695 if (GamePtr - nr < 0)
\r
698 HistPtr -= nr; /* erase history boards */
\r
700 for(m=0; m<STATE; m++)
\r
701 HistoryBoards[HistPtr+nr+1&1023][m] = 0;
\r
704 for(i=0; i<128; i++) b[i] = setupPosition[i];
\r
705 Side = setupPosition[128]; Q = SetupQ;
\r
706 pl[WHITE] = setupPosition[129];
\r
707 pl[BLACK] = setupPosition[130];
\r
709 for(i=0; i<=U; i++) A[i].D = A[i].K = 0; // clear hash table
\r
710 for(nr=0; nr<GamePtr; nr++) {
\r
711 UNPACK_MOVE(GameHistory[nr]);
\r
712 D(Side,-I,I,Q,O,LL|S,3);
\r
713 Side ^= BLACK^WHITE;
\r
717 if (!strcmp(command, "post")) {
\r
721 if (!strcmp(command, "nopost")) {
\r
725 if (!strcmp(command, "variant")) {
\r
726 sscanf(line, "variant %s", command);
\r
728 InitGame(); Setup = 0;
\r
731 if (!strcmp(command, "edit")) {
\r
732 int color = WHITE, p;
\r
734 while(fgets(line, 256, stdin)) {
\r
738 for(i=0; i<128; i++) b[i]=0;
\r
740 pl[WHITE]=pl[BLACK]=0;
\r
744 color = WHITE+BLACK - color;
\r
748 if( m >= 'A' && m <= 'Z' && piecetype[m&31]
\r
749 && line[1] >= 'a' && line[1] <= 'a'+BW-1
\r
750 && line[2] >= '1' && line[2] <= '0'+BH) {
\r
751 m = line[1]-16*line[2]+799;
\r
752 switch(p = piecetype[line[0]&31])
\r
757 b[m]=(m&0x70)==0x60?1:33,
\r
759 else b[m]=(m&0x70)==0x10?18:50,
\r
762 case 3: // can castle, normally King
\r
764 if(m==BW>>1 && color==BLACK ||
\r
765 m==0x70+(BW>>1) && color==WHITE)
\r
768 case 6: // can castle, normally Rook
\r
770 if((m==0x00 || m==BW-1 ) && color==BLACK ||
\r
771 (m==0x70 || m==0x6F+BW) && color==WHITE)
\r
773 Q+=w[6]; R+=w[6]/FAC;
\r
777 Q+=w[p]; R+=w[p]/FAC;
\r
778 case 0: // undefined piece, ignore
\r
781 pl[BLACK+WHITE-color]++;
\r
785 if(Side != color) Q = -Q;
\r
786 GamePtr = HistPtr = 0; Setup = 1; SetupQ = Q; // start anew
\r
787 for(i=0; i<128; i++) setupPosition[i] = b[i]; // remember position
\r
788 setupPosition[128] = Side;
\r
789 setupPosition[129] = pl[WHITE];
\r
790 setupPosition[130] = pl[BLACK];
\r
793 /* command not recognized, assume input move */
\r
794 m = line[0]<'a' | line[0]>='a'+BW | line[1]<'1' | line[1]>='1'+BH |
\r
795 line[2]<'a' | line[2]>='a'+BW | line[3]<'1' | line[3]>='1'+BH;
\r
796 if(line[4] == '\n') line[4] = piecename[7];
\r
797 PromPiece = 7 - piecetype[line[4]&31];
\r
798 if(PromPiece == 7) PromPiece = 0;
\r
799 {char *c=line; K=c[0]-16*c[1]+799;L=c[2]-16*c[3]+799; }
\r
801 /* doesn't have move syntax */
\r
802 printf("Error (unknown command): %s\n", command);
\r
803 else if(D(Side,-I,I,Q,O,LL|S,3)!=I) {
\r
804 /* did have move syntax, but illegal move */
\r
805 printf("Illegal move:%s\n", line);
\r
806 } else { /* legal move, perform it */
\r
807 GameHistory[GamePtr++] = PACK_MOVE;
\r
808 Side ^= BLACK^WHITE;
\r
809 CopyBoard(HistPtr=HistPtr+1&1023);
\r
810 if(PrintResult(Side)) Computer = EMPTY;
\r