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