Clarify license
[fairymax.git] / fairymax.c
1 /***************************************************************************/\r
2 /*                               Fairy-Max,                                */\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
6 \r
7      /*****************************************************************/\r
8      /*                      LICENCE NOTIFICATION                     */\r
9      /* Fairy-Max 4.8 is free software, released in the public domain */\r
10      /* so that you have my permission do with it whatever you want,  */\r
11      /* whether it is commercial or not, at your own risk. Those that */\r
12      /* are not comfortable with this, can also use or redistribute   */\r
13      /* it under the GNU Public License or the MIT License.           */\r
14      /* Note, however, that Fairy-Max can easily be configured through*/\r
15      /* its fmax.ini file to play Chess variants that are legally pro-*/\r
16      /* tected by patents, and that to do so would also require per-  */\r
17      /* mission of the holders of such patents. No guarantees are     */\r
18      /* given that Fairy-Max does anything in particular, or that it  */\r
19      /* would not wreck the hardware it runs on, and running it is    */\r
20      /* entirely for your own risk.  H.G,Muller, author of Fairy-Max  */\r
21      /*****************************************************************/\r
22 \r
23 #define MULTIPATH\r
24 #define VERSION "4.8V"\r
25 \r
26 #include <stdio.h>\r
27 #include <stdlib.h>\r
28 #include <string.h>\r
29 #include <signal.h>\r
30 #include <time.h>\r
31 \r
32 #ifndef INI_FILE \r
33 #define INI_FILE "fmax.ini"\r
34 #endif\r
35 \r
36 #ifdef WIN32 \r
37 #    include <windows.h>\r
38 #    define CPUtime 1000.*clock\r
39 #else\r
40 #    include <sys/time.h>\r
41 #    include <sys/times.h>\r
42 #    include <unistd.h>\r
43      int GetTickCount() // with thanks to Tord\r
44      {  struct timeval t;\r
45         gettimeofday(&t, NULL);\r
46         return t.tv_sec*1000 + t.tv_usec/1000;\r
47      }\r
48      double CPUtime()\r
49      {  // get CPU time used by process, converted to 'MILLICLOCKS'\r
50         struct tms cpuTimes;\r
51         static int cps = 0;\r
52         if(!cps) cps = sysconf(_SC_CLK_TCK);\r
53         times(&cpuTimes);\r
54         return ((double)(cpuTimes.tms_utime + cpuTimes.tms_stime) * CLOCKS_PER_SEC * 1000)/cps;\r
55      }\r
56 #endif\r
57 \r
58 int StartKey;\r
59 \r
60 #define EMPTY -1\r
61 #define WHITE 0\r
62 #define BLACK 16\r
63 \r
64 #define STATE 128\r
65 \r
66 /* The following macros indicate the differences between Fairy-Max and its */\r
67 /* dedicated Shatranj derivative ShaMax so that these can now be compiled  */\r
68 /* from the same unified source file.                                      */\r
69 /* Compile with gcc option -DSHATRANJ to build ShaMax.                     */\r
70 #ifdef SHATRANJ\r
71 #    define FAC 175\r
72 #    define EG  13\r
73 #    define NAME "ShaMax"\r
74 #    define SHAMAX(x) x\r
75 #    define FMAX(x)\r
76 #else\r
77 #    define FAC 128\r
78 #    define EG  10\r
79 #    define NAME "Fairy-Max"\r
80 #    define SHAMAX(x)\r
81 #    define FMAX(x) x\r
82 #endif\r
83 \r
84 /* make unique integer from engine move representation */\r
85 #define PACK_MOVE 256*K + L + (PromPiece << 16) + (GT<<24);\r
86 \r
87 /* convert intger argument back to engine move representation */\r
88 #define UNPACK_MOVE(A) K = (A)>>8 & 255; L = (A) & 255; PromPiece = (A)>>16 & 255; GT = (A)>>24 & 255;\r
89 \r
90 /* Global variables visible to engine. Normally they */\r
91 /* would be replaced by the names under which these  */\r
92 /* are known to your engine, so that they can be     */\r
93 /* manipulated directly by the interface.            */\r
94 \r
95 int Side;\r
96 int Move;\r
97 int PromPiece;\r
98 int Result;\r
99 int TimeLeft;\r
100 int MovesLeft;\r
101 int MaxDepth;\r
102 int Post;\r
103 int Fifty;\r
104 int GameNr;\r
105 int Resign;\r
106 char Cambodian[80] = "makruk";\r
107 int Threshold = 800;\r
108 int drawMoves = 50;\r
109 int Score;\r
110 int makruk;\r
111 int prom, pm, gating, succession, hill;\r
112 char piecename[32], piecetype[32], blacktype[32];\r
113 char selectedFairy[80];\r
114 char *inifile = INI_FILE;\r
115 char info[999];\r
116 \r
117 int Ticks, tlim, Setup, SetupQ;\r
118 \r
119 int GameHistory[1024];\r
120 char HistoryBoards[1024][STATE], setupPosition[162];\r
121 int GamePtr, HistPtr;\r
122 \r
123 #define W while\r
124 #define K(A,B) *(int*)(T+A+S*(B&31))\r
125 #define J(A) K(y+A,b[y])-K(x+A,u)-K(H+A,t)\r
126 \r
127 int U=(1<<23)-1;\r
128 struct _ {int K,V;char X,Y,D,F;} *A;           /* hash table, 16M+8 entries*/\r
129 \r
130 int M=136,S=128,I=8e3,Q,O,K,N,j,R,J,Z,LL,GT,   /* M=0x88                   */\r
131 BW,BH,sh,\r
132 w[16]={0,2,2,-1,7,8,12,23,7,5},                /* relative piece values    */\r
133 o[256],\r
134 oo[32],                                        /* initial piece setup      */\r
135 of[256],\r
136 od[16];                                        /* 1st dir. in o[] per piece*/\r
137 \r
138 signed char L,pl[32],\r
139 b[513],                                        /* board: 16x8+dummy, + PST */\r
140 T[4104],                                       /* hash translation table   */\r
141 centr[32],\r
142 n[]=".*XKNBRQEWFMACHG?x+knbrqewfmachg";        /* piece symbols on printout*/\r
143 \r
144 int pv[10000],*sp=pv; // triangular array\r
145 int margin;\r
146 \r
147 void pboard()\r
148 {int i;\r
149  i=-1;W(++i<128)printf(" %c",(i&15)==BW&&(i+=15-BW)?10:n[b[i]&31]);\r
150 }\r
151          \r
152 \r
153 D(k,q,l,e,E,z,n)        /* recursive minimax search, k=moving side, n=depth*/\r
154 int k,q,l,e,E,z,n;      /* (q,l)=window, e=current eval. score, E=e.p. sqr.*/\r
155 {                       /* e=score, z=prev.dest; J,Z=hashkeys; return score*/\r
156  int j,r,m,v,d,h,i,F,G,P,V,f=J,g=Z,C,s,flag,FF,*ps=sp,kk=S;\r
157  signed char t,p,u,x,y,X,Y,H,B,gt,rg,rk;\r
158  struct _*a=A+(J+(k+S)*E&U);                   /* lookup pos. in hash table*/\r
159  *sp++=0;\r
160  q-=q<e;l-=l<=e;                               /* adj. window: delay bonus */\r
161  d=a->D;m=a->V;X=a->F;Y=a->Y;                  /* resume at stored depth   */\r
162  if(a->K-Z|z&S  |                              /* miss: other pos. or empty*/\r
163   !(m<=q|X&8&&m>=l|X&S))                       /*   or window incompatible */\r
164   d=Y=0;                                       /* start iter. from scratch */\r
165  X=a->X;                                       /* start at best-move hint  */\r
166  W(d++<n||d<3||              /*** min depth = 2   iterative deepening loop */\r
167    z&S&&K==I&&(GetTickCount()-Ticks<tlim&d<=MaxDepth|| /* root: deepen upto time   */\r
168    (K=X,L=Y&~S,Score=m,d=3)))                  /* time's up: go do best    */\r
169  {x=B=X;                                       /* start scan at prev. best */\r
170   h=Y&S;                                       /* request try noncastl. 1st*/\r
171   P=d>2&&l+I?D(16-k,-l,1-l,-e,2*S,2*S,d-3):I;  /* search null move         */\r
172   m=-P<l|R<5?d-2?-I:e:-P;   /*** prune if > beta  unconsidered:static eval */\r
173   SHAMAX( if(pl[k]<=1&pl[16-k]>1)m=I-1; )      /* bare king loses          */\r
174   N++;                                         /* node count (for timing)  */\r
175   do{u=b[x];                                   /* scan board looking for   */\r
176    if(u&&(u&16)==k)                            /*  own piece (inefficient!)*/\r
177    {r=p=u&15;                                  /* p = piece type (set r>0) */\r
178     if(hill&&w[p]<0&b[385+x])m=I,d=98;         /* King on the hill: we won */\r
179     j=od[p];                                   /* first step vector f.piece*/\r
180     W(r=o[++j])                                /* loop over directions o[] */\r
181     {A:                                        /* resume normal after best */\r
182      flag=h?3:of[j];                           /* move modes (for fairies) */\r
183      y=x;F=FF=G=S;rg=flag>>10&3;               /* (x,y)=move, (F,G)=castl.R*/\r
184      do{                                       /* y traverses ray, or:     */\r
185       H=y=h?Y^h:y+r;                           /* sneak in prev. best move */\r
186       if(flag&1<<8)H=y=(y&15)>13?y+BW:(y&15)>=BW?y-BW:y; /* cylinder board */\r
187       if(y&S|(y&15)>=BW)break;                 /* board edge hit           */\r
188 #ifdef MULTIPATH\r
189       if(flag&1<<9)                            /* if multipath move        */\r
190       {t=flag>>12;                             /* get dir. stepped twice   */\r
191        if(b[x+t]){if(b[y-2*t]|b[y-t])break;}else \r
192        if(b[x+2*t]&&b[y-t])break;              /* test if empty path exists*/\r
193       }\r
194 #endif\r
195       m=E<16|(E^112)<16&&flag&1&y-E<2&E-y<2?I:m;      /* bad castling  */\r
196       if(p<3&y==E&flag)H=z&127;                /* shift capt.sqr. H if e.p.*/\r
197       t=b[H];\r
198       if(flag&1+!t)                            /* mode (capt/nonc) allowed?*/\r
199       {if(t&&(t&16)==k)break;                  /* capture own              */\r
200        i=w[t&15]+((t&192)>>sh);                /* value of capt. piece t   */\r
201        if(i<0&&(pl[t&31]<2||                   /* K capture, (of last K),  */\r
202         t>>3&kk!=H&kk!=S||(kk=H,i=-i,0)))m=I,d=98;/* or duple check: cutoff*/\r
203        if(m>=l&d>1)goto C;                     /* abort on fail high       */\r
204        v=d-1?e:i-p;                            /*** MVV/LVA scoring if d=1**/\r
205        if(d-!t>1)                              /*** all captures if d=2  ***/\r
206        {v=gt=0;G:                              /* retry move with gating   */\r
207         v+=centr[p]*(b[x+257]-b[y+257]);       /* center positional pts.   */\r
208         if(G-S)b[FF]=(rk=b[G])|32,v+=50;       /* castling: put R & score  */\r
209         b[G]=b[H]=0;b[x]=gt;b[y]=u|32;         /* do move, set non-virgin  */\r
210         pl[t&31]-=!!t;                         /* updat victim piece count */\r
211         v-=w[p]>0|R<EG?0:20;                   /*** freeze K in mid-game ***/\r
212         if(p<3)                                /* pawns:                   */\r
213         {v-=9*((x-2&M||b[x-2]-u)+              /* structure, undefended    */\r
214                (x+2&M||b[x+2]-u)               /*        squares plus bias */\r
215               +(w[b[x^16]&15]<0))              /*** cling to magnetic K ***/\r
216               +(R-76>>2);                      /* end-game Pawn-push bonus */\r
217          b[y]+=V=y+r+1&S?647-p|pm:2*(u&y+16&32);/*upgrade P or convert to Q*/\r
218          if(V&makruk)b[y]=u|7,V=480;           /* Makruk promotion on 6th  */\r
219          V>>=sh;                               /* for Shatranj promo to F  */\r
220          i+=V;                                 /* promotion / passer bonus */\r
221         } if(z&S && GamePtr<6) v+=(rand()>>10&31)-16;\r
222         J+=J(0);Z+=J(4)+G-S;\r
223         SHAMAX( pl[k]-=!!t; )                  /* count pieces per side    */\r
224         v+=e+i;V=m>q?m:q;                      /*** new eval & alpha    ****/\r
225         if(z&S)V=m-margin>q?m-margin:q;        /* multiPV                  */\r
226         C=d-1-(d>5&p>2&!t&!h);                 /* nw depth, reduce non-cpt.*/\r
227         C=R<EG|P-I|d<3||t&&w[p]>0?C:d;         /* extend 1 ply if in-check */\r
228         do\r
229          s=C>2|v>V?-D(16-k,-l,-V,-v,/*** futility, recursive eval. of reply */\r
230                                      F,y&255,C):v;\r
231         W(s>q&++C<d); v=s;                     /* no fail:re-srch unreduced*/\r
232         if(v>V&v<l){int *p=sp;\r
233          sp=ps+1;\r
234          W(*sp++=*p++);\r
235          *ps=256*x+y;\r
236         }\r
237         if(z&S&&K-I)                           /* move pending: check legal*/\r
238         {if(v+I&&x==K&y==L&gt==GT)             /*   if move found          */\r
239          {Q=-e-i;O=F;LL=L;prom=gt&15;\r
240           if(b[y]-u&15)prom=b[y]-=PromPiece,   /* (under-)promotion:       */\r
241            Q-=abs(w[prom&=15])-w[p]-(6*S>>sh), /*  correct piece & score & */\r
242                        Z+=PromPiece;           /*  invalidate hash         */\r
243           a->D=99;a->V=0;                      /* lock game in hash as draw*/\r
244           R-=i/FAC;                            /*** total captd material ***/\r
245           Fifty = t|p<3?0:Fifty+1;\r
246           if(centr[p]==5)b[257+y]-=5;\r
247           sp=ps;\r
248                      return l;}                /*   & not in check, signal */\r
249          v=m;                                  /* (prevent fail-lows on    */\r
250         }                                      /*   K-capt. replies)       */\r
251         SHAMAX( pl[k]+=!!t; ) pl[t&31]+=!!t;\r
252         b[G]=rk;b[FF]=b[y]=0;b[x]=u;b[H]=t;    /* undo move,G can be dummy */\r
253        }                                       /*          if non-castling */\r
254        if(z&S&&K==I&d>2&v>V&v<l){int *p=ps;char X,Y;\r
255         if(Post){\r
256          printf("%2d ",d-2);\r
257          printf("%6d ",v);\r
258          printf("%8d %10d",(GetTickCount()-Ticks)/10,N);\r
259          while(*p){X=*p>>8;Y=*p++;\r
260          printf(" %c%c%c%c",'a'+(X&15),'8'-(X>>4),'a'+(Y&15),'8'-(Y>>4&7));}\r
261          printf("\n");fflush(stdout);\r
262         }GT=gt;                                /* In root, remember gated  */\r
263        }\r
264        if(v>m)                                 /* new best, update max,best*/\r
265         m=v,X=x,Y=y|S&F;                       /* mark non-double with S   */\r
266        if(gating&&!(u&32)&&p>2&&d-!t>1){       /* virgin non-Pawn: gate    */\r
267         pl[(gt|=k+40)-27]++;                   /* prev. gated back in hand */\r
268         if(m>=l)goto C;                        /* loop skips cutoff :-(    */\r
269         W(++gt<k+43)if(pl[gt-27]){             /* look if more to gate     */\r
270          pl[gt-27]--;v=10;goto G;              /* remove from hand & retry */\r
271        }}\r
272        J=f;Z=g;\r
273        if(h){h=0;goto A;}                      /* redo after doing old best*/\r
274       }\r
275       s=t&&2&~rg|~t&16^k;v=r^flag>>12;         /* platform & toggled vector*/\r
276       if(flag&15^4|u&32||                      /* no double or moved before*/\r
277          p>2&!(flag&S)&&                       /* no P & no virgin jump,   */\r
278          ((b[G=r<0?x&~15:BW-1|x&112]^32)<33    /* no virgin R in corner G, */\r
279          ||b[G^1]|b[G^2]|b[FF=y+v-r])          /* no 2 empty sq. next to R */\r
280         )t+=flag&4;                            /* fake capt. for nonsliding*/\r
281       else if(flag&64)t=flag&128?0:t,flag&=63;else F=y; /* enable e.p.     */\r
282       if(s&&flag&8&&!(y=rg&1?y-r:y,t=0)        /* hoppers go to next phase */\r
283          ||!(flag&S)&&!rg--)                   /* zig-zag piece? (w. delay)*/\r
284        r=v,flag^=flag>>4&15;                   /* alternate vector & mode  */\r
285      }W(!t);                                   /* if not capt. continue ray*/\r
286    }}\r
287    if((++x&15)>=BW)x=x+16&112;                 /* next sqr. of board, wrap */\r
288   }W(x-B);           \r
289 C:FMAX( m=m+I|P==I?m:(X=Y=0); )                /* if stalemate, draw-score */\r
290   if(a->D<99)                                  /* protect game history     */\r
291    a->K=Z,a->V=m,a->D=d,a->X=X,                /* always store in hash tab */\r
292    a->F=8*(m>q)|S*(m<l),a->Y=Y;                /* move, type (bound/exact),*/\r
293   }                                            /*    encoded in X S,8 bits */\r
294 if(z&4*S)K=X,L=Y&~S;\r
295  sp=ps;\r
296  return m+=m<e;                                /* delayed-loss bonus       */\r
297 }\r
298 \r
299 \r
300 /* Generic main() for Winboard-compatible engine     */\r
301 /* (Inspired by TSCP)                                */\r
302 /* Author: H.G. Muller                               */\r
303 \r
304 /* The engine is invoked through the following       */\r
305 /* subroutines, that can draw on the global vaiables */\r
306 /* that are maintained by the interface:             */\r
307 /* Side         side to move                         */\r
308 /* Move         move input to or output from engine  */\r
309 /* PromPiece    requested piece on promotion move    */\r
310 /* TimeLeft     ms left to next time control         */\r
311 /* MovesLeft    nr of moves to play within TimeLeft  */\r
312 /* MaxDepth     search-depth limit in ply            */\r
313 /* Post         boolean to invite engine babble      */\r
314 \r
315 /* InitEngine() progran start-up initialization      */\r
316 /* InitGame()   initialization to start new game     */\r
317 /*              (sets Side, but not time control)    */\r
318 /* Think()      think up move from current position  */\r
319 /*              (leaves move in Move, can be invalid */\r
320 /*               if position is check- or stalemate) */\r
321 /* DoMove()     perform the move in Move             */\r
322 /*              (togglese Side)                      */\r
323 /* ReadMove()   convert input move to engine format  */\r
324 /* PrintMove()  print Move on standard output        */\r
325 /* Legal()      check Move for legality              */\r
326 /* ClearBoard() make board empty                     */\r
327 /* PutPiece()   put a piece on the board             */\r
328 \r
329 /* define this to the codes used in your engine,     */\r
330 /* if the engine hasn't defined it already.          */\r
331 \r
332 int PrintResult(int s)\r
333 {\r
334         int j, k, cnt=0;\r
335 \r
336         /* search last 50 states with this stm for third repeat */\r
337         for(j=2; j<=100 && j <= HistPtr; j+=2)\r
338         {\r
339             for(k=0; k<STATE; k++)\r
340                 if(HistoryBoards[HistPtr][k] !=\r
341                    HistoryBoards[HistPtr-j&1023][k] )\r
342                    {\r
343                      goto differs;}\r
344             /* is the same, count it */\r
345             if(++cnt > 1) /* third repeat */\r
346             {\r
347                 printf("1/2-1/2 {Draw by repetition}\n");\r
348                 return 1;\r
349             }\r
350           differs: ;\r
351         }\r
352         K=I;\r
353         cnt = D(s,-I,I,Q,O,LL|4*S,3);\r
354 #ifdef SHATRANJ\r
355         if(pl[s]==1 && pl[16-s]==1) {\r
356                 printf("1/2-1/2 {Insufficient mating material}\n");\r
357                 return 4;\r
358         }\r
359         if(pl[s]<=1 && pl[16-s]>1) {\r
360                 if (s == BLACK)\r
361                         printf("0-1 {Bare King}\n");\r
362                 else\r
363                         printf("1-0 {Bare King}\n");\r
364                 return 5;\r
365         }\r
366 #else\r
367         if(cnt>-I+1 && K==0 && L==0) {\r
368                 printf("1/2-1/2 {Stalemate}\n");\r
369                 return 2;\r
370         }\r
371 #endif\r
372         if(cnt==-I+1) {\r
373                 if (s == WHITE)\r
374                         printf("0-1 {Black mates}\n");\r
375                 else {\r
376                         if(succession) { // suppress loss claim if black might be able to replace its King by promotion\r
377                             for(j=0;j<BW;j++)if((b[j+96]&31)==18)return 0;\r
378                         }\r
379                         printf("1-0 {White mates}\n");\r
380                 }\r
381                 return 3;\r
382         }\r
383         if(Fifty >= 2*drawMoves) {\r
384                 printf("1/2-1/2 {Draw by fifty move rule}\n");\r
385                 return 4;\r
386         }\r
387         return 0;\r
388 }\r
389 \r
390 \r
391 void InitEngine()\r
392 {\r
393  N=32*S+7;W(N-->S+3)T[N]=rand()>>9;\r
394  srand(GetTickCount());\r
395 }\r
396 \r
397 void InitGame()\r
398 {\r
399  int i,k=0;\r
400 \r
401  Side = WHITE; Q=0; O=S;\r
402  Fifty = 0; R = 0;\r
403  for(i=0;i<16*BH;i++)b[i]=0;\r
404  for(i=0;i<32;i++)pl[i]=0;\r
405  K=BW;W(K--)\r
406  {b[K]=oo[K+16]+16;b[K+112]=oo[K];b[K+16+16*!!makruk]=18;b[K+96-16*!!makruk]=1; /* initial board setup*/\r
407   pl[oo[K+16]+16]++;pl[oo[K]]++;pl[18]++;pl[1]++;\r
408   if(w[oo[K+16]+16] == -1)pl[oo[K+16]+16]=1;\r
409   if(w[oo[K]] == -1)pl[oo[K]]=1;\r
410   L=8;W(L--)b[16*L+K+257]=(K-BW/2+hill/2.)*(K-BW/2+hill/2.)+(L-3.5)*(L-3.5); /* center-pts table   */\r
411  }                                                   /*(in unused half b[])*/\r
412  b[385+16*3+BW/2]=b[385+16*4+BW/2]=b[385+16*3+BW/2-1]=b[385+16*4+BW/2-1]=1; /* hill */\r
413  for(i=0; i<BW; i++) {\r
414   R += abs(w[oo[i]])/FAC + abs(w[oo[i+16]])/FAC;\r
415   Q += abs(w[oo[i]]) - abs(w[oo[i+16]]) + w[1] - w[2];\r
416   if(w[oo[i]] < 0) k = w[oo[i]];\r
417  }\r
418  R -= 2*(-k/FAC);\r
419  pl[WHITE] = pl[BLACK] = 2*BW; \r
420  pm = !pl[BLACK+7] && pl[BLACK+9] && pl[WHITE+7] ? 2 : 0; // Unlike white, black has no 'Q', so promote to 9, which he does have.\r
421  if(gating) pl[14] = pl[15] = pl[30] = pl[31] = 1, R += 2*(w[9]/FAC + w[10]/FAC);\r
422 }\r
423 \r
424 void CopyBoard(int s)\r
425 {\r
426         int i, j;\r
427 \r
428         /* copy game representation of engine to HistoryBoard */\r
429         /* don't forget castling rights and e.p. state!       */\r
430         for(i=0; i<BH; i++)\r
431         for(j=0; j<BW; j++)                 /* board squares  */\r
432             HistoryBoards[s][BW*i+j] = b[16*i+j]|64*(16*i+j==O);\r
433 }\r
434                                          \r
435 void PrintVariants(int combo)\r
436 {\r
437         int count=0, total=0; char c=EOF+1, buf[80];\r
438         FILE *f;\r
439 \r
440         f = fopen(INI_FILE, "r");\r
441         if(f==NULL) return;\r
442 \r
443         /* search for game names in definition file */\r
444         do {\r
445            while(fscanf(f, "Game: %s", buf) != 1 && c != EOF) \r
446                while((c = fgetc(f)) != EOF && c != '\n');\r
447            if(c == EOF) break;\r
448            total++;\r
449            if(*buf < 'a' || combo == (strstr(buf, "fairy/") != buf)) continue;\r
450            if(combo && count == 0) strcpy(selectedFairy, buf);\r
451            if(count++) printf(combo ? " /// " : ",");\r
452            printf("%s", combo ? buf+6 : buf);\r
453         } while(c != EOF);\r
454 \r
455         fclose(f);\r
456         if(!combo && total != count) printf("%sfairy", count ? "," : "");\r
457 }\r
458 \r
459 void PrintOptions()\r
460 {\r
461         printf("feature option=\"Resign -check %d\"\n", Resign);\r
462         printf("feature option=\"Resign Threshold -spin %d 200 1200\"\n", Threshold);\r
463         printf("feature option=\"Claim draw after -spin %d 0 200\"\n", drawMoves);\r
464         printf("feature option=\"Ini File -file %s\"\n", inifile);\r
465         printf("feature option=\"Multi-PV Margin -spin %d 0 1000\"\n", margin);\r
466         printf("feature option=\"Variant fairy selects -combo "); PrintVariants(1); printf("\"\n");\r
467         printf("feature option=\"Makruk rules -combo makruk /// Cambodian /// Ai-wok\"\n");\r
468         printf("feature option=\"Dummy Slider Example -slider 20 0 100\"\n");\r
469         printf("feature option=\"Dummy String Example -string happy birthday!\"\n");\r
470         printf("feature option=\"Dummy Path Example -path .\"\n");\r
471         printf("feature option=\"Info -button\"\n");\r
472         printf("feature option=\"Clear Hash -button\"\n");\r
473         printf("feature done=1\n");\r
474 }\r
475                                          \r
476 void LoadGame(char *name)\r
477 {\r
478         int i, j, ptc=0, count=0; char c, buf[80], pieceToChar[80], parent[80];\r
479         static int currentVariant;\r
480         FILE *f;\r
481 \r
482         f = fopen(inifile, "r");\r
483         if(f==NULL)\r
484         {   printf("telluser piece-description file '%s'  not found\n", inifile);\r
485             exit(0);\r
486         }\r
487         if(fscanf(f, "version 4.8(%c)", &c)!=1 || c != 'w')\r
488         { printf("telluser incompatible fmax.ini file\n"); exit(0); }\r
489 \r
490         gating = succession = 0;\r
491         if(name != NULL)\r
492         {  /* search for game name in definition file */\r
493            if(!strcmp(name, "makruk")) name = Cambodian; else\r
494            if(!strcmp(name, "fairy")) name = selectedFairy;\r
495            gating = !strcmp(name, "seirawan");\r
496            while((ptc=fscanf(f, "Game: %s # %s # %s", buf, pieceToChar, parent))==0 || strcmp(name, buf) ) {\r
497                char *p = info;\r
498                while((c = fgetc(f)) != EOF && c != '\n') *p++ = c;\r
499                if(*info == '/') *p = 0; else *info = 0; // remember last line before Game if it was comment\r
500                count++;\r
501                if(c == EOF) {\r
502                    printf("telluser variant %s not supported\n", name);\r
503                    fclose(f);\r
504                    return; /* keep old settings */\r
505                }\r
506            }\r
507            currentVariant = count;\r
508         }\r
509 \r
510         /* We have found variant, or if none specified, are at beginning of file */\r
511         if(fscanf(f, "%dx%d", &BW, &BH)!=2 || BW>12 || BH!=8)\r
512         { printf("telluser unsupported board size %dx%d\n",BW,BH); exit(0); }\r
513         makruk = 0; if(fscanf(f, "=%d", &i)) makruk=67-i; // new method to indicate deviant zone depth (for now assumes 3)\r
514 \r
515         for(i=0; i<BW; i++) fscanf(f, "%d", oo+i);\r
516         for(i=0; i<BW; i++) fscanf(f, "%d", oo+i+16);\r
517         for(i= 0; i<=U; i++)\r
518             A[i].K = A[i].D = A[i].X = A[i].Y = A[i].F = 0; /* clear hash */\r
519         for(i=0; i<32; i++) piecetype[i] = blacktype[i] = 0;\r
520 \r
521         i=0; j=-1; c=0;\r
522         while(fscanf(f, "%d,%x", o+j, of+j)==2 ||\r
523                                       fscanf(f,"%c:%d",&c, w+i+1)==2)\r
524         {   if(c)\r
525             { od[++i]=j; centr[i] = c>='a';\r
526               blacktype[c&31]=i; piecename[i]=c&31;\r
527               if(piecetype[c&31]==0) piecetype[c&31]=i; // only first\r
528               succession |= w[i] < -4;         // expendable royalty; assume we can promote to it\r
529             }\r
530             j++; o[j]=0;\r
531             /* 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
532             c=0; if(i>15 || j>255) break;\r
533         }\r
534 \r
535         sh = w[7] < 250 ? 3 : 0; hill = (w[3] == -2);\r
536         if(ptc > 1) { // setup board in GUI, by sending it pieceToCharTable and FEN\r
537             if(ptc == 2) printf("setup (%s) ", pieceToChar);\r
538             else printf("setup (%s) %dx%d+0_%s ", pieceToChar, BW, BH, parent);\r
539             for(i=0; i<BW; i++) printf("%c", piecename[oo[i+16]]+'`'); printf("/");\r
540             if(makruk) printf("8/");\r
541             for(i=0; i<BW; i++) printf("%c", piecename[2]+'`'); printf("/");\r
542             for(i=2+!!makruk; i<BH-2-!!makruk; i++) printf("%d/", BW);\r
543             for(i=0; i<BW; i++) printf("%c", piecename[1]+'@'); printf("/");\r
544             if(makruk) printf("8/");\r
545             for(i=0; i<BW; i++) printf("%c", oo[i] ? piecename[oo[i]]+'@' : '1'); printf(" w KQkq - 0 1\n");\r
546         }\r
547         while(fscanf(f, " # %[^\n]", pieceToChar)) printf("piece %s\n", pieceToChar);\r
548         fclose(f);\r
549         if(makruk == 66) makruk = 8; // 66 was kludge to interpret "=1" suffix in asean for setting up 3rd-rank Pawns\r
550 }\r
551 \r
552 int main(int argc, char **argv)\r
553 {\r
554         int Computer, MaxTime, MaxMoves, TimeInc, sec, i;\r
555         char line[256], command[256], c;\r
556         int m, nr;\r
557         double cpuT;\r
558 \r
559         if(argc > 1 && !strcmp(argv[1], "-v")) argc++, argv--, printf("%s\n", VERSION), exit(0);\r
560 \r
561         if(argc>1 && sscanf(argv[1], "%d", &m)==1)\r
562         { U = (1<<m)-1; argc--; argv++; }\r
563         A = (struct _ *) calloc(U+1, sizeof(struct _));\r
564         if(argc>1) inifile = argv[1];\r
565 \r
566         signal(SIGINT, SIG_IGN);\r
567         printf("tellics say     " NAME " " VERSION "\n");\r
568         printf("tellics say     by H.G. Muller\n");\r
569         InitEngine();\r
570         LoadGame(NULL);\r
571         InitGame();\r
572         Computer = EMPTY;\r
573         MaxTime  = 10000;  /* 10 sec */\r
574         MaxDepth = 30;     /* maximum depth of your search */\r
575 \r
576         for (;;) {\r
577                 fflush(stdout);\r
578                 if (Side == Computer) {\r
579                         /* think up & do move, measure time used  */\r
580                         /* it is the responsibility of the engine */\r
581                         /* to control its search time based on    */\r
582                         /* MovesLeft, TimeLeft, MaxMoves, TimeInc */\r
583                         /* Next 'MovesLeft' moves have to be done */\r
584                         /* within TimeLeft+(MovesLeft-1)*TimeInc  */\r
585                         /* If MovesLeft<0 all remaining moves of  */\r
586                         /* the game have to be done in this time. */\r
587                         /* If MaxMoves=1 any leftover time is lost*/\r
588                         Ticks = GetTickCount();\r
589                         cpuT = CPUtime(); printf("# times @ %u\n", Ticks);\r
590                         m = MovesLeft<=0 ? 40 : MovesLeft;\r
591                         tlim = (0.6-0.06*(BW-8))*(TimeLeft+(m-1)*TimeInc)/(m+7);\r
592                         if(tlim>TimeLeft/15) tlim = TimeLeft/15;\r
593                         PromPiece = 0; /* Always promote to Queen ourselves */\r
594                         for(N=K=0;K<S;K++)N+=b[K]?b[K]&16?S:1:0; /* count pieces for detecting bare King */\r
595                         if(w[3]<0&w[4]<0){if(N<2*S)centr[4]=5,Z+=R>4,R=4;if(!(N&S-2))centr[3]=5,Z+=R>4,R=4;}\r
596 printf("# %d+%d pieces, centr = (%d,%d) R=%d\n", N&63, N>>7, centr[3], centr[4], R);\r
597                         N=0;K=I;\r
598                         if(hill) centr[3] = R>20 ? 1 : 22-R;\r
599                         if (D(Side,-I,I,Q,O,LL|S,3)==I) {\r
600                             Side ^= BLACK^WHITE;\r
601                             m = GetTickCount() - Ticks;\r
602                             printf("# times @ %u: real=%d cpu=%1.0f\n", m + Ticks, m,\r
603                                       (CPUtime() - cpuT)/CLOCKS_PER_SEC);\r
604 printf("# promo = %d (%c) GT = %d\n", prom, prom + '`', GT); \r
605                             printf("move ");\r
606                             printf("%c%c%c%c",'a'+(K&15),'0'+BH-(K>>4),\r
607                                           'a'+(L&15),'0'+BH-(L>>4));\r
608                             if(prom)printf("%c",piecename[prom]+'a'-1);\r
609                             printf("\n");\r
610 \r
611                             /* time-control accounting */\r
612                             TimeLeft -= m;\r
613                             TimeLeft += TimeInc;\r
614                             if(--MovesLeft == 0) {\r
615                                 MovesLeft = MaxMoves;\r
616                                 if(MaxMoves == 1)\r
617                                      TimeLeft  = MaxTime;\r
618                                 else TimeLeft += MaxTime;\r
619                             }\r
620 \r
621                             GameHistory[GamePtr++] = PACK_MOVE;\r
622                             CopyBoard(HistPtr=HistPtr+1&1023);\r
623                             if(Resign && Score <= -Threshold) { \r
624                                 printf("resign\n"); Computer=EMPTY;\r
625                             } else if(PrintResult(Side))\r
626                                 Computer = EMPTY;\r
627                         } else {\r
628                             if(!PrintResult(Side))\r
629                                 printf("resign { refuses own move }\n");\r
630                             Computer = EMPTY;\r
631                         }\r
632                         continue;\r
633                 }\r
634                 if (!fgets(line, 256, stdin))\r
635                         return 1;\r
636                 if (line[0] == '\n')\r
637                         continue;\r
638                 sscanf(line, "%s", command);\r
639                 if (!strcmp(command, "xboard"))\r
640                         continue;\r
641                 if (!strcmp(command, "protover")) {\r
642                         printf("feature myname=\"" NAME " " VERSION "\"\n");\r
643                         printf("feature memory=1\n");\r
644                         printf("feature setboard=0 xedit=1 ping=1 done=0\n");\r
645                         printf("feature variants=\"");\r
646                         PrintVariants(0);\r
647                         printf("\"\n");\r
648                         PrintOptions();\r
649                         continue;\r
650                 }\r
651                 if (!strcmp(command, "ping")) { int nr=0;\r
652                         sscanf(line, "ping %d", &nr);\r
653                         printf("pong %d\n", nr);\r
654                         continue;\r
655                 }\r
656                 if (!strcmp(command, "p")) {\r
657                         pboard();\r
658                         continue;\r
659                 }\r
660                 if (!strcmp(command, "memory")) {\r
661                         int mem, mask;\r
662                         sscanf(line+6, "%d", &mem); mem = (mem*1024*1024)/12; // max nr of hash entries\r
663                         mask = 0x7FFFFFFF; while(mask > mem) mask >>= 1;\r
664                         if(mask != U) {\r
665                             free(A); U = mask;\r
666                             A = (struct _ *) calloc(U+1, sizeof(struct _));\r
667                         }\r
668                         continue;\r
669                 }\r
670                 if (!strcmp(command, "new")) {\r
671                         /* start new game */\r
672                         LoadGame("normal");\r
673                         InitGame();\r
674                         GamePtr   = Setup = 0;\r
675                         GameNr++;\r
676                         HistPtr   = 0;\r
677                         Computer  = BLACK;\r
678                         TimeLeft  = MaxTime;\r
679                         MovesLeft = MaxMoves;\r
680                         for(nr=0; nr<1024; nr++)\r
681                             for(m=0; m<STATE; m++)\r
682                                 HistoryBoards[nr][m] = 0;\r
683                         continue;\r
684                 }\r
685                 if (!strcmp(command, "quit"))\r
686                         /* exit engine */\r
687                         return 0;\r
688                 if (!strcmp(command, "force")) {\r
689                         /* computer plays neither */\r
690                         Computer = EMPTY;\r
691                         continue;\r
692                 }\r
693                 if (!strcmp(command, "white")) {\r
694                         /* set white to move in current position */\r
695                         if(Side == BLACK) Q = -Q;\r
696                         Side     = WHITE;\r
697                         Computer = BLACK;\r
698                         continue;\r
699                 }\r
700                 if (!strcmp(command, "black")) {\r
701                         /* set blck to move in current position */\r
702                         if(Side == WHITE) Q = -Q;\r
703                         Side     = BLACK;\r
704                         Computer = WHITE;\r
705                         continue;\r
706                 }\r
707                 if (!strcmp(command, "st")) {\r
708                         /* move-on-the-bell mode     */\r
709                         /* indicated by MaxMoves = 1 */\r
710                         sscanf(line, "st %d", &MaxTime);\r
711                         MovesLeft = MaxMoves = 1;\r
712                         TimeLeft  = MaxTime *= 1000;\r
713                         TimeInc   = 0;\r
714                         continue;\r
715                 }\r
716                 if (!strcmp(command, "sd")) {\r
717                         /* set depth limit (remains in force */\r
718                         /* until next 'sd n' command)        */\r
719                         sscanf(line, "sd %d", &MaxDepth);\r
720                         MaxDepth += 2; /* QS depth */\r
721                         continue;\r
722                 }\r
723                 if (!strcmp(command, "level")) {\r
724                         /* normal or blitz time control */\r
725                         sec = 0;\r
726                         if(sscanf(line, "level %d %d %d",\r
727                                  &MaxMoves, &MaxTime, &TimeInc)!=3 &&\r
728                            sscanf(line, "level %d %d:%d %d",\r
729                                  &MaxMoves, &MaxTime, &sec, &TimeInc)!=4)\r
730                              continue;\r
731                         MovesLeft = MaxMoves;\r
732                         TimeLeft  = MaxTime = 60000*MaxTime + 1000*sec;\r
733                         TimeInc  *= 1000;\r
734                         continue;\r
735                 }\r
736                 if (!strcmp(command, "time")) {\r
737                         /* set time left on clock */\r
738                         sscanf(line, "time %d", &TimeLeft);\r
739                         TimeLeft  *= 10; /* centi-sec to ms */\r
740                         continue;\r
741                 }\r
742                 if (!strcmp(command, "otim")) {\r
743                         /* opponent's time (not kept, so ignore) */\r
744                         continue;\r
745                 }\r
746                 if (!strcmp(command, "easy")) {\r
747                         continue;\r
748                 }\r
749                 if (!strcmp(command, "hard")) {\r
750                         continue;\r
751                 }\r
752                 if (!strcmp(command, "accepted")) {\r
753                         continue;\r
754                 }\r
755                 if (!strcmp(command, "rejected")) {\r
756                         continue;\r
757                 }\r
758                 if (!strcmp(command, "random")) {\r
759                         continue;\r
760                 }\r
761                 if (!strcmp(command, "option")) {\r
762                         int i; static char filename[80];\r
763                         if(sscanf(line+7, "Resign=%d", &Resign) == 1) continue;\r
764                         if(sscanf(line+7, "Resign Threshold=%d", &Threshold) == 1) continue;\r
765                         if(sscanf(line+7, "Ini File=%s", filename) == 1) {\r
766                                 inifile = filename; continue;\r
767                         }\r
768                         if(sscanf(line+7, "Clear Hash%c", &c) == 1) for(i=0; i<=U; i++) A->K = 0;\r
769                         if(sscanf(line+7, "Info%c", &c) == 1) printf("telluser %s\n", info+3);\r
770                         if(sscanf(line+7, "MultiVariation Margin=%d", &margin) == 1) continue;\r
771                         if(sscanf(line+7, "Variant fairy selects=%s", selectedFairy+6) == 1) continue;\r
772                         if(sscanf(line+7, "Makruk rules=%s", Cambodian) == 1) continue;\r
773                         if(sscanf(line+7, "Claim draw after=%d", &drawMoves) == 1) continue;\r
774                         continue;\r
775                 }\r
776                 if (!strcmp(command, "go")) {\r
777                         /* set computer to play current side to move */\r
778                         Computer = Side;\r
779                         MovesLeft = -(GamePtr+(Side==WHITE)>>1);\r
780                         while(MaxMoves>0 && MovesLeft<=0)\r
781                             MovesLeft += MaxMoves;\r
782                         continue;\r
783                 }\r
784                 if (!strcmp(command, "hint")) {\r
785                         Ticks = GetTickCount(); tlim = 1000;\r
786                         D(Side,-I,I,Q,O,LL|4*S,6);\r
787                         if (K==0 && L==0)\r
788                                 continue;\r
789                         printf("Hint: ");\r
790                         printf("%c%c%c%c",'a'+(K&7),'8'-(K>>4),\r
791                                           'a'+(L&7),'8'-(L>>4));\r
792                         printf("\n");\r
793                         continue;\r
794                 }\r
795                 if (!strcmp(command, "undo")   && (nr=1) ||\r
796                     !strcmp(command, "remove") && (nr=2)   ) {\r
797                         /* 'take back' moves by replaying game */\r
798                         /* from history until desired ply      */\r
799                         if (GamePtr - nr < 0)\r
800                                 continue;\r
801                         GamePtr -= nr;\r
802                         HistPtr -= nr;   /* erase history boards */\r
803                         while(nr-- > 0)  \r
804                             for(m=0; m<STATE; m++)\r
805                                 HistoryBoards[HistPtr+nr+1&1023][m] = 0;\r
806                         InitGame();\r
807                         if(Setup) {\r
808                             for(i=0; i<128; i++) b[i] = setupPosition[i];\r
809                             for(i=0; i<32; i++) pl[i] = setupPosition[i+130];\r
810                             Side = setupPosition[128]; Q = SetupQ;\r
811                             R = setupPosition[129];\r
812                         }\r
813                         for(i=0; i<=U; i++) A[i].D = A[i].K = 0; // clear hash table\r
814                         for(nr=0; nr<GamePtr; nr++) {\r
815                             UNPACK_MOVE(GameHistory[nr]);\r
816                             D(Side,-I,I,Q,O,LL|S,3);\r
817                             Side ^= BLACK^WHITE;\r
818                         }\r
819                         continue;\r
820                 }\r
821                 if (!strcmp(command, "post")) {\r
822                         Post = 1;\r
823                         continue;\r
824                 }\r
825                 if (!strcmp(command, "nopost")) {\r
826                         Post = 0;\r
827                         continue;\r
828                 }\r
829                 if (!strcmp(command, "variant")) {\r
830                         sscanf(line, "variant %s", command);\r
831                         LoadGame(command);\r
832                         InitGame(); Setup = 0;\r
833                         continue;\r
834                 }\r
835                 if (!strcmp(command, "edit")) {\r
836                         int color = WHITE, p, r;\r
837 \r
838                         while(fgets(line, 256, stdin)) {\r
839                                 m = line[0];\r
840                                 if(m=='.') break;\r
841                                 if(m=='#') {\r
842                                         for(i=0; i<128; i++) b[i]=0;\r
843                                         for(i=0; i<32; i++) pl[i]=0;\r
844                                         Q=0; R=0; O=S;\r
845                                         pl[WHITE]=pl[BLACK]=0;\r
846                                         continue;\r
847                                 }\r
848                                 if(m=='c') {\r
849                                         color = WHITE+BLACK - color;\r
850                                         Q = -Q;\r
851                                         continue;\r
852                                 }\r
853                                 if( m >= 'A' && m <= 'Z' && piecetype[m&31]) {\r
854                                     p = (color == WHITE ? piecetype : blacktype)[line[0]&31];\r
855                                     if(line[1] == '@') { // stuff holdings\r
856                                         pl[color+p+5] = m = line[2] - '0';\r
857                                         pl[BLACK+WHITE-color]+=m;pl[p+color]+=m;\r
858                                         Q+=m*w[p]; R+=m*(w[p]/FAC);\r
859                                         continue;\r
860                                     } else\r
861                                     if(line[1] >= 'a' && line[1] <= 'a'+BW-1\r
862                                     && line[2] >= '1' && line[2] <= '0'+BH) {\r
863                                         m = line[1]-16*line[2]+799; r = m & 0x70;\r
864                                         switch(p)\r
865                                         {\r
866                                         case 1:\r
867                                         case 2:\r
868                                             if(color==WHITE)\r
869                                                  b[m]=r==0x10?161:r==0x20?97:r==0x60?1:33,\r
870                                                  Q+=w[1]+(r==0x10?S:r==0x20?64:0);\r
871                                             else b[m]=r==0x60?178:r==0x50?114:r==0x10?18:50,\r
872                                                  Q+=w[2]+(r==0x60?S:r==0x50?64:0);\r
873                                             break;\r
874                                         default:\r
875                                             b[m]=p+color+32; // assume non-virgin\r
876                                             if(color==BLACK && m<0x10 && p==oo[m+16] || // but make virgin on original square\r
877                                                color==WHITE && m>0x6F && p==oo[m-0x70]) b[m] -= 32;\r
878                                             if(w[p]<0) { // Royal piece on original square: virgin\r
879                                                 Q-=w[p]; // assume value was flipped to indicate royalty\r
880                                                 if(pl[p+color])R-=w[p]/FAC; // capturable King, add to material\r
881                                             } else { Q+=w[p]; R+=w[p]/FAC; }\r
882                                         case 0: // undefined piece, ignore\r
883                                             break;\r
884                                         }\r
885                                         pl[BLACK+WHITE-color]++;pl[p+color]++;\r
886                                         if(w[p+color] == -1)pl[p+color]=1; // fake we have one if value = -1, to thwart extinction condition\r
887                                         continue;\r
888                                     }\r
889                                 }\r
890                         }\r
891                         if(Side != color) Q = -Q;\r
892                         GamePtr = HistPtr = 0; Setup = 1; SetupQ = Q; // start anew\r
893                         for(i=0; i<128; i++) setupPosition[i] = b[i]; // remember position\r
894                         setupPosition[128] = Side;\r
895                         setupPosition[129] = R;\r
896                         for(i=0; i<32; i++) setupPosition[i+130] = pl[i];\r
897                         Computer = EMPTY; // after edit: force mode!\r
898                         continue;\r
899                 }\r
900                 /* command not recognized, assume input move */\r
901                 m = line[0]<'a' | line[0]>='a'+BW | line[1]<'1' | line[1]>='1'+BH |\r
902                     line[2]<'a' | line[2]>='a'+BW | line[3]<'1' | line[3]>='1'+BH;\r
903                 if(line[4] == '\n') line[4] = 0;\r
904                 GT = (Side == WHITE ? piecetype : blacktype)[line[4]&31];\r
905                 if(GT) PromPiece = (Side == WHITE ? 7 : 7+pm) - GT, GT |= 32 + Side;\r
906                 {char *c=line; K=c[0]-16*c[1]+799;L=c[2]-16*c[3]+799; }\r
907                 if(w[GT&15] == -1 || w[GT&15]%10 == 3) L = S; // spoil move for promotion to King (or when marked non-promoting)\r
908                 if (m & line[1] != '@')\r
909                         /* doesn't have move syntax */\r
910                         printf("Error (unknown command): %s\n", command);\r
911                 else { int i=-1;\r
912                     if(b[L] && (b[L]&16) == Side && w[b[L]&15] < 0) // capture own King: castling\r
913                     { i=K; K = L; L = i>L ? i-1 : i+2; }\r
914                     if(w[GT&15] < -1) pl[GT&31]++, J+=89729; // promotion to royal piece\r
915                     if((b[K]&15) < 3) GT = 0; // Pawn => true promotion rather than gating\r
916                     if(D(Side,-I,I,Q,O,LL|S,3)!=I) {\r
917                         /* did have move syntax, but illegal move */\r
918                         printf("Illegal move:%s\n", line);\r
919                     } else {  /* legal move, perform it */\r
920                         if(i >= 0) b[i]=b[K],b[K]=0; // reverse Seirawan gating\r
921                         GameHistory[GamePtr++] = PACK_MOVE;\r
922                         Side ^= BLACK^WHITE;\r
923                         CopyBoard(HistPtr=HistPtr+1&1023);\r
924                         if(PrintResult(Side)) Computer = EMPTY;\r
925                     }\r
926 \r
927                 }\r
928         }\r
929         return 0;\r
930 }\r