Add Cambodian Chess
[fairymax.git] / fairymax.c
index 2c16397..98ff2cf 100644 (file)
@@ -18,7 +18,7 @@
      /*****************************************************************/\r
 \r
 #define MULTIPATH\r
-#define VERSION "4.8O"\r
+#define VERSION "4.8Q"\r
 \r
 #include <stdio.h>\r
 #include <stdlib.h>\r
@@ -68,10 +68,10 @@ int StartKey;
 #endif\r
 \r
 /* make unique integer from engine move representation */\r
-#define PACK_MOVE 256*K + L;\r
+#define PACK_MOVE 256*K + L + (PromPiece << 16);\r
 \r
 /* convert intger argument back to engine move representation */\r
-#define UNPACK_MOVE(A) K = (A)>>8 & 255; L = (A) & 255;\r
+#define UNPACK_MOVE(A) K = (A)>>8 & 255; L = (A) & 255; PromPiece = (A)>>16 & 255;\r
 \r
 /* Global variables visible to engine. Normally they */\r
 /* would be replaced by the names under which these  */\r
@@ -93,7 +93,7 @@ int Resign;
 int Threshold = 800;\r
 int Score;\r
 int makruk;\r
-int prom;\r
+int prom, pm, gating;\r
 char piecename[32], piecetype[32], blacktype[32];\r
 char selectedFairy[80];\r
 char *inifile = INI_FILE;\r
@@ -111,7 +111,7 @@ int GamePtr, HistPtr;
 int U=(1<<23)-1;\r
 struct _ {int K,V;char X,Y,D,F;} *A;           /* hash table, 16M+8 entries*/\r
 \r
-int M=136,S=128,I=8e3,Q,O,K,N,j,R,J,Z,LL,      /* M=0x88                   */\r
+int M=136,S=128,I=8e3,Q,O,K,N,j,R,J,Z,LL,GT,   /* M=0x88                   */\r
 BW,BH,sh,\r
 w[16]={0,2,2,-1,7,8,12,23,7,5},                /* relative piece values    */\r
 o[256],\r
@@ -138,7 +138,7 @@ D(k,q,l,e,E,z,n)        /* recursive minimax search, k=moving side, n=depth*/
 int k,q,l,e,E,z,n;      /* (q,l)=window, e=current eval. score, E=e.p. sqr.*/\r
 {                       /* e=score, z=prev.dest; J,Z=hashkeys; return score*/\r
  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
- signed char t,p,u,x,y,X,Y,H,B;\r
+ signed char t,p,u,x,y,X,Y,H,B,gt;\r
  struct _*a=A+(J+(k+S)*E&U-1);                 /* lookup pos. in hash table*/\r
  *sp++=0;\r
  q-=q<e;l-=l<=e;                               /* adj. window: delay bonus */\r
@@ -181,13 +181,15 @@ int k,q,l,e,E,z,n;      /* (q,l)=window, e=current eval. score, E=e.p. sqr.*/
       if(flag&1+!t)                            /* mode (capt/nonc) allowed?*/\r
       {if(t&&(t&16)==k)break;                  /* capture own              */\r
        i=w[t&15]+((t&192)>>sh);                /* value of capt. piece t   */\r
-       if(i<0&&(pl[t&31]<2||kk!=H&kk!=S||(kk=H,i=-i,0)))m=I,d=98;/* K capture, duple check */\r
+       if(i<0&&(pl[t&31]<2||                   /* K capture, (of last K),  */\r
+        t>>3&kk!=H&kk!=S||(kk=H,i=-i,0)))m=I,d=98;/* or duple check: cutoff*/\r
        if(m>=l&d>1)goto C;                     /* abort on fail high       */\r
        v=d-1?e:i-p;                            /*** MVV/LVA scoring if d=1**/\r
        if(d-!t>1)                              /*** all captures if d=2  ***/\r
-       {v=centr[p]?b[x+257]-b[y+257]:0;        /* center positional pts.   */\r
+       {v=gt=0;G:                              /* retry move with gating   */\r
+        v+=centr[p]?b[x+257]-b[y+257]:0;       /* center positional pts.   */\r
         if(!(G&S))b[FF]=b[G],v+=50;            /* castling: put R & score  */\r
-        b[G]=b[H]=b[x]=0;b[y]=u|32;            /* do move, set non-virgin  */\r
+        b[G]=b[H]=0;b[x]=gt;b[y]=u|32;         /* do move, set non-virgin  */\r
         pl[t&31]-=!!t;                         /* updat victim piece count */\r
         v-=w[p]>0|R<EG?0:20;                   /*** freeze K in mid-game ***/\r
         if(p<3)                                /* pawns:                   */\r
@@ -195,7 +197,7 @@ int k,q,l,e,E,z,n;      /* (q,l)=window, e=current eval. score, E=e.p. sqr.*/
                (x+2&M||b[x+2]-u)               /*        squares plus bias */\r
               +(w[b[x^16]&15]<0))              /*** cling to magnetic K ***/\r
               +(R-76>>2);                      /* end-game Pawn-push bonus */\r
-         b[y]+=V=y+r+1&S?647-p:2*(u&y+16&32);  /* upgrade P or convert to Q*/\r
+         b[y]+=V=y+r+1&S?647-p|pm:2*(u&y+16&32);/*upgrade P or convert to Q*/\r
          if(V&makruk)b[y]=u|7,V=480;           /* Makruk promotion on 6th  */\r
          V>>=sh;                               /* for Shatranj promo to F  */\r
          i+=V;                                 /* promotion / passer bonus */\r
@@ -216,8 +218,8 @@ int k,q,l,e,E,z,n;      /* (q,l)=window, e=current eval. score, E=e.p. sqr.*/
          *ps=256*x+y;\r
         }\r
         if(z&S&&K-I)                           /* move pending: check legal*/\r
-        {if(v+I&&x==K&y==L)                    /*   if move found          */\r
-         {Q=-e-i;O=F;LL=L;prom=0;\r
+        {if(v+I&&x==K&y==L&gt==GT)             /*   if move found          */\r
+         {Q=-e-i;O=F;LL=L;prom=gt;\r
           if(b[y]-u&15)prom=b[y]-=PromPiece,   /* under-promotion, correct */\r
                        J+=PromPiece;           /*  piece & invalidate hash */\r
           a->D=99;a->V=0;                      /* lock game in hash as draw*/\r
@@ -227,25 +229,33 @@ int k,q,l,e,E,z,n;      /* (q,l)=window, e=current eval. score, E=e.p. sqr.*/
                      return l;}                /*   & not in check, signal */\r
          v=m;                                  /* (prevent fail-lows on    */\r
         }                                      /*   K-capt. replies)       */\r
-        J=f;Z=g;\r
         SHAMAX( pl[k]+=!!t; ) pl[t&31]+=!!t;\r
         b[G]=b[FF];b[FF]=b[y]=0;b[x]=u;b[H]=t; /* undo move,G can be dummy */\r
        }                                       /*          if non-castling */\r
-       if(z&S&&Post&K==I&d>2&v>V&v<l){int *p=ps;char X,Y;\r
-        printf("%2d ",d-2);\r
-        printf("%6d ",v);\r
-        printf("%8d %10d",(GetTickCount()-Ticks)/10,N);\r
-        while(*p){X=*p>>8;Y=*p++;\r
-        printf(" %c%c%c%c",'a'+(X&15),'8'-(X>>4),'a'+(Y&15),'8'-(Y>>4&7));}\r
-        printf("\n");fflush(stdout);\r
+       if(z&S&&K==I&d>2&v>V&v<l){int *p=ps;char X,Y;\r
+        if(Post){\r
+         printf("%2d ",d-2);\r
+         printf("%6d ",v);\r
+         printf("%8d %10d",(GetTickCount()-Ticks)/10,N);\r
+         while(*p){X=*p>>8;Y=*p++;\r
+         printf(" %c%c%c%c",'a'+(X&15),'8'-(X>>4),'a'+(Y&15),'8'-(Y>>4&7));}\r
+         printf("\n");fflush(stdout);\r
+        }GT=gt;                                /* In root, remember gated  */\r
        }\r
        if(v>m)                                 /* new best, update max,best*/\r
         m=v,X=x,Y=y|S&F;                       /* mark non-double with S   */\r
+       if(gating&&!(u&32)&&p>2&&d-!t>1){       /* virgin non-Pawn: gate    */\r
+        pl[(gt|=k+40)-27]++;                   /* prev. gated back in hand */\r
+        if(m>=l)goto C;                        /* loop skips cutoff :-(    */\r
+        W(++gt<k+43)if(pl[gt-27]){             /* look if more to gate     */\r
+         pl[gt-27]--;v=10;goto G;              /* remove from hand & retry */\r
+       }}\r
+       J=f;Z=g;\r
        if(h){h=0;goto A;}                      /* redo after doing old best*/\r
       }\r
       s=t;v=r^flag>>12;                        /* calc. alternated vector  */\r
       if(flag&15^4|u&32||                      /* no double or moved before*/\r
-         p>2&&                                 /* no P & no lateral K move,*/\r
+         p>2&!(flag&S)&&                       /* no P & no virgin jump,   */\r
          ((b[G=r<0?x&~15:BW-1|x&112]^32)<33    /* no virgin R in corner G, */\r
          ||b[G^1]|b[G^2]|b[FF=y+v-r])          /* no 2 empty sq. next to R */\r
         )t+=flag&4;                            /* fake capt. for nonsliding*/\r
@@ -365,19 +375,28 @@ InitEngine()
 \r
 InitGame()\r
 {\r
- int i,j;\r
+ int i,j,k=0;\r
 \r
+ Side = WHITE; Q=0; O=S;\r
+ Fifty = 0; R = 0;\r
  for(i=0;i<16*BH;i++)b[i]=0;\r
  for(i=0;i<32;i++)pl[i]=0;\r
  K=BW;W(K--)\r
- {b[K]=oo[K+16]+16;b[K+112]=oo[K];b[K+16]=18;b[K+96]=1; /* initial board setup*/\r
+ {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
   pl[oo[K+16]+16]++;pl[oo[K]]++;pl[18]++;pl[1]++;\r
+  if(w[oo[K+16]+16] == -1)pl[oo[K+16]+16]=1;\r
+  if(w[oo[K]] == -1)pl[oo[K]]=1;\r
   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
  }                                                   /*(in unused half b[])*/\r
- Side = WHITE; Q=0; O=S;\r
- Fifty = 0; R = 0;\r
- for(i=0; i<BW; i++) if(i!=3) R += (w[oo[i]]/FAC) + (w[oo[i+16]]/FAC);\r
- UnderProm = -1; pl[WHITE] = pl[BLACK] = 2*BW;\r
+ for(i=0; i<BW; i++) {\r
+  R += abs(w[oo[i]])/FAC + abs(w[oo[i+16]])/FAC;\r
+  Q += abs(w[oo[i]]) - abs(w[oo[i+16]]) + w[1] - w[2];\r
+  if(w[oo[i]] < 0) k = w[oo[i]];\r
+ }\r
+ R -= 2*(-k/FAC);\r
+ UnderProm = -1; pl[WHITE] = pl[BLACK] = 2*BW; \r
+ 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
+ if(gating) pl[14] = pl[15] = pl[30] = pl[31] = 1, R += 2*(w[9]/FAC + w[10]/FAC);\r
 }\r
 \r
 void CopyBoard(int s)\r
@@ -393,7 +412,7 @@ void CopyBoard(int s)
                                          \r
 void PrintVariants(int combo)\r
 {\r
-        int i, j, count=0, total=0; char c, buf[80];\r
+        int i, j, count=0, total=0; char c=EOF+1, buf[80];\r
         FILE *f;\r
 \r
         f = fopen(INI_FILE, "r");\r
@@ -431,7 +450,7 @@ void PrintOptions()
                                          \r
 int LoadGame(char *name)\r
 {\r
-        int i, j, count=0; char c, buf[80];\r
+        int i, j, ptc, count=0; char c, buf[80], pieceToChar[80];\r
         static int currentVariant;\r
         FILE *f;\r
 \r
@@ -443,10 +462,12 @@ int LoadGame(char *name)
         if(fscanf(f, "version 4.8(%c)", &c)!=1 || c != 'w')\r
         { printf("telluser incompatible fmax.ini file\n"); exit(0); }\r
 \r
+        gating = 0;\r
         if(name != NULL)\r
         {  /* search for game name in definition file */\r
            if(!strcmp(name, "fairy")) name = selectedFairy;\r
-           while(fscanf(f, "Game: %s", buf)!=1 || strcmp(name, buf) ) {\r
+           gating = !strcmp(name, "seirawan");\r
+           while((ptc=fscanf(f, "Game: %s # %s", buf, pieceToChar))==0 || strcmp(name, buf) ) {\r
                while((c = fgetc(f)) != EOF && c != '\n');\r
                count++;\r
                if(c == EOF) {\r
@@ -484,6 +505,14 @@ int LoadGame(char *name)
         fclose(f);\r
        sh = w[7] < 250 ? 3 : 0;\r
        makruk = w[7]==181 ? 64 : 0; // w[7] is used as kludge to enable makruk promotions\r
+        if(name == selectedFairy) {\r
+            printf(ptc == 1 ? "setup " : "setup (%s) ", pieceToChar); // setup board in GUI\r
+            for(i=0; i<BW; i++) printf("%c", piecename[oo[i+16]]+'`'); printf("/");\r
+            for(i=0; i<BW; i++) printf("%c", piecename[2]+'`'); printf("/");\r
+            for(i=2; i<BH-2; i++) printf("%d/", BW);\r
+            for(i=0; i<BW; i++) printf("%c", piecename[1]+'@'); printf("/");\r
+            for(i=0; i<BW; i++) printf("%c", piecename[oo[i]]+'@'); printf(" w KQkq - 0 1\n");\r
+        }\r
 }\r
 \r
 int main(int argc, char **argv)\r
@@ -811,6 +840,7 @@ int main(int argc, char **argv)
                                             break;\r
                                         }\r
                                         pl[BLACK+WHITE-color]++;pl[p+color]++;\r
+                                        if(w[p+color] == -1)pl[p+color]=1; // fake we have one if value = -1, to thwart extinction condition\r
                                         continue;\r
                                 }\r
                         }\r
@@ -820,26 +850,34 @@ int main(int argc, char **argv)
                        setupPosition[128] = Side;\r
                        setupPosition[129] = R;\r
                        for(i=0; i<32; i++) setupPosition[i+130] = pl[i];\r
+                       Computer = EMPTY; // after edit: force mode!\r
                        continue;\r
                }\r
                 /* command not recognized, assume input move */\r
                 m = line[0]<'a' | line[0]>='a'+BW | line[1]<'1' | line[1]>='1'+BH |\r
                     line[2]<'a' | line[2]>='a'+BW | line[3]<'1' | line[3]>='1'+BH;\r
-                if(line[4] == '\n') line[4] = piecename[7];\r
-                PromPiece = 7 - (Side == WHITE ? piecetype : blacktype)[line[4]&31];\r
-                   if(PromPiece == 7) PromPiece = 0;\r
+                if(line[4] == '\n') line[4] = 0;\r
+                GT = (Side == WHITE ? piecetype : blacktype)[line[4]&31];\r
+                if(GT) PromPiece = (Side == WHITE ? 7 : 7+pm) - GT, GT |= 32 + Side;\r
                 {char *c=line; K=c[0]-16*c[1]+799;L=c[2]-16*c[3]+799; }\r
                 if (m)\r
                         /* doesn't have move syntax */\r
                        printf("Error (unknown command): %s\n", command);\r
-                else if(D(Side,-I,I,Q,O,LL|S,3)!=I) {\r
+                else { int i=-1;\r
+                    if(b[L] && (b[L]&16) == Side && w[b[L]&15] < 0) // capture own King: castling\r
+                    { i=K; K = L; L = i>L ? i-1 : i+2; }\r
+                   if(w[GT&15] < -1) pl[GT&31]++, J+=89729; // promotion to royal piece\r
+                    if(b[K]&32) GT = 0; // non-virgin mover => true promotion rather than gating\r
+                    if(D(Side,-I,I,Q,O,LL|S,3)!=I) {\r
                         /* did have move syntax, but illegal move */\r
                         printf("Illegal move:%s\n", line);\r
-                } else {  /* legal move, perform it */\r
+                    } else {  /* legal move, perform it */\r
+                        if(i >= 0) b[i]=b[K],b[K]=0; // reverse Seirawan gating\r
                         GameHistory[GamePtr++] = PACK_MOVE;\r
                         Side ^= BLACK^WHITE;\r
                         CopyBoard(HistPtr=HistPtr+1&1023);\r
                         if(PrintResult(Side)) Computer = EMPTY;\r
+                   }\r
                }\r
        }\r
 }\r