Implement Lion Dog moves
[hachu.git] / hachu.c
diff --git a/hachu.c b/hachu.c
index a0c878b..9cf4f27 100644 (file)
--- a/hachu.c
+++ b/hachu.c
@@ -10,7 +10,7 @@
 // promotions by pieces with Lion power stepping in & out the zone in same turn\r
 // promotion on capture\r
 \r
-#define VERSION "0.20"\r
+#define VERSION "0.21"\r
 \r
 //define PATH level==0 || path[0] == 0x590cb &&  (level==1 || path[1] == 0x4c0c9 && (level == 2 || path[2] == 0x8598ca && (level == 3 /*|| path[3] == 0x3e865 && (level == 4 || path[4] == 0x4b865 && (level == 5))*/)))\r
 #define PATH 0\r
@@ -128,6 +128,7 @@ char *MoveToText(Move move, int m);     // from WB driver
 void pmap(int *m, int col);\r
 void pboard(int *b);\r
 void pbytes(unsigned char *b);\r
+int myRandom();\r
 \r
 typedef struct {\r
   int lock[5];\r
@@ -175,11 +176,12 @@ Move retMove, moveStack[20000], path[100], repStack[300], pv[1000], repeatMove[3
 #define R 37 /* jump capture        */\r
 #define N -1 /* Knight              */\r
 #define J -2 /* jump                */\r
-#define D -3 /* linear double move  */\r
-#define T -4 /* linear triple move  */\r
-#define L -5 /* true Lion move      */\r
-#define F -6 /* Lion + 3-step       */\r
-#define S -7 /* Lion + range        */\r
+#define I -3 /* jump + step         */\r
+#define D -4 /* linear double move  */\r
+#define T -5 /* linear triple move  */\r
+#define L -6 /* true Lion move      */\r
+#define F -7 /* Lion + 3-step       */\r
+#define S -8 /* Lion + range        */\r
 #define H -9 /* hook move           */\r
 #define C -10 /* capture only       */\r
 #define M -11 /* non-capture only   */\r
@@ -248,6 +250,33 @@ PieceDesc daiPieces[] = {
   { NULL }  // sentinel\r
 };\r
 \r
+PieceDesc waPieces[] = {\r
+  {"TE", "",   720, { X,X,1,X,X,X,1,X }, 4 }, // Tenacious Falcon\r
+  {"GS", "",   500, { X,0,X,0,X,0,X,0 }, 4 }, // Gliding Swallow (R)\r
+  {"DE", "",   430, { X,3,1,1,X,1,1,3 }, 3 }, // Cloud Eagle\r
+  {"K",  "",   410, { 1,1,1,1,1,1,1,1 }, 2 }, // Crane King (K)\r
+  {"BT", "",   390, { I,I,0,I,I,I,0,I }, 4 }, // Treacherous Fox\r
+  {"FL", "TE", 380, { 1,X,0,X,0,X,0,X }, 4 }, // Flying Falcon\r
+  {"FS", "",   290, { X,1,1,0,X,0,1,1 }, 3 }, // Raiding Falcon\r
+  {"S",  "GS", 260, { 1,0,X,0,1,0,X,0 }, 6 }, // Swallow's Wing (SM)\r
+  {"PO", "",   260, { 1,1,1,1,1,1,1,1 }, 2 }, // Plodding Ox (K)\r
+  {"R",  "BT", 260, { X,1,0,1,1,1,0,1 }, 2 }, // Running Rabit\r
+  {"B",  "",   240, { 1,1,1,1,0,1,1,1 }, 2 }, // Roaming Boar\r
+  {"HH", "",   220, { N,0,0,N,N,0,0,N }, 1 }, // Heavenly Horse\r
+  {"EW", "PO", 220, { 1,1,1,0,1,0,1,1 }, 2 }, // Violent Wolf (G)\r
+  {"VM", "B",  200, { 1,1,0,1,0,1,0,1 }, 2 }, // Violent Stag (S)\r
+  {"G",  "S",  190, { 1,1,0,0,1,0,0,1 }, 2 }, // Flying Goose (C)\r
+  {"SM", "VM", 175, { 1,1,0,0,1,0,0,1 }, 2 }, // Climbing Monkey (C)\r
+  {"DH", "HH", 170, { X,0,0,0,2,0,0,0 }, 1 }, // Liberated Horse\r
+  {"DK", "EW", 150, { 0,1,1,0,1,0,1,1 }, 2 }, // Blind Dog\r
+  {"PH", "PO", 150, { X,0,0,0,0,0,0,0 }, 1 }, // Oxcart (L)\r
+  {"L",  "FS", 130, { 0,1,1,0,0,0,1,1 }, 2 }, // Flying Cock\r
+  {"KN", "DE", 115, { 1,0,0,1,0,1,0,0 }, 2 }, // Swooping Owl\r
+  {"C",  "FL", 105, { 1,0,0,1,0,1,0,0 }, 2 }, // Strutting Crow\r
+  {"P",  "EW",  80, { 1,0,0,0,0,0,0,0 }, 2 }, // Sparrow Pawn (P)\r
+  { NULL }  // sentinel\r
+};\r
+\r
 PieceDesc ddPieces[] = {\r
   {"LO", "",   10, { 1,H,1,H,1,H,1,H } }, // Long-Nosed Goblin\r
   {"OK", "LO", 10, { 2,1,2,0,2,0,2,1 } }, // Old Kite\r
@@ -442,6 +471,9 @@ char tenArray[] = "LN:FLICSGK:DEGSCI:FLNL/:RV.:CS:CS.:BT:KN:LN:FK:PH:BT.:CS:CS.:
                  "....d......d..../pppppppppppppppp/:sm:vmr:hf:se:bg:rg:vg:gg:rg:bg:se:hfr:vm:sm/"\r
                  ":ss:vsb:dh:dk:wb:fi:fe:lh:fi:wb:dk:dhb:vs:ss/:rv.:cs:cs.:bt:ph:fk:ln:kn:bt.:cs:cs.:rv/ln:flicsg:dekgsci:flnl";\r
 char shoArray[] = "LNSGKGSNL/.B..:DE..R./PPPPPPPPP/........./........./........./ppppppppp/.r..:de..b./lnsgkgsnl";\r
+char waArray[] = ":PH:DKCG:EWK:VML:KN:SM:DH/.:FL...S...:DE./PPP:BTPPPRPPP/...P...P..."\r
+                "/.........../.........../..........."\r
+                "/...p...p.../ppprppp:btppp/.:de...s...:fl./:dh:sm:knl:vmk:ewgc:dk:ph";\r
 char chessArray[] = "RNB:FKKBNR/PPPPPPPP/......../......../......../......../pppppppp/rnb:fkkbnr";\r
 char lionArray[]  = "R:LNB:FKKBNR/PPPPPPPP/......../......../......../......../pppppppp/r:lnb:fkkbnr";\r
 char shatArray[]= "RNBK:FKBNR/PPPPPPPP/......../......../......../......../pppppppp/rnbk:fkbnr";\r
@@ -453,7 +485,7 @@ typedef struct {
   char *array; // initial position\r
 } VariantDesc;\r
 \r
-typedef enum { V_CHESS, V_SHO, V_CHU, V_DAI, V_DADA, V_MAKA, V_TAI, V_KYOKU, V_TENJIKU, V_SHATRANJ, V_MAKRUK, V_LION } Variant;\r
+typedef enum { V_CHESS, V_SHO, V_CHU, V_DAI, V_DADA, V_MAKA, V_TAI, V_KYOKU, V_TENJIKU, V_SHATRANJ, V_MAKRUK, V_LION, V_WA } Variant;\r
 \r
 #define SAME (-1)\r
 \r
@@ -467,6 +499,7 @@ VariantDesc variants[] = {
   { 16,  8,  8, 1, V_SHATRANJ,"shatranj",shatArray}, // Shatranj\r
   { 16,  8,  8, 3, V_MAKRUK,  "makruk",  thaiArray}, // Makruk\r
   { 16,  8,  8, 1, V_LION,    "lion",    lionArray}, // Mighty Lion\r
+  { 22, 11, 11, 3, V_WA,      "washogi", waArray},   // Wa\r
 \r
   { 0, 0, 0, 0, 0 }, // sentinel\r
   { 34, 17, 17, 0, V_DADA,    "dada",    chuArray }, // Dai Dai\r
@@ -621,7 +654,7 @@ int rawBoard[BSIZE + 11*BHMAX + 6];
 int attackMaps[200*BSIZE], *attacks = attackMaps;\r
 char distance[2*BSIZE]; // distance table\r
 char promoBoard[BSIZE]; // flags to indicate promotion zones\r
-char rawFire[BSIZE+2*BWMAX]; // flags to indicate squares controlled by Fire Demons\r
+unsigned char rawFire[BSIZE+2*BWMAX]; // flags to indicate squares controlled by Fire Demons\r
 signed char PST[7*BSIZE];\r
 \r
 #define board     (rawBoard + 6*BHMAX + 3)\r
@@ -659,6 +692,8 @@ LookUp (char *name, int var)
       return ListLookUp(name, makrukPieces);\r
     case V_LION: // Mighty Lion\r
       return ListLookUp(name, lionPieces);\r
+    case V_WA: // Wa\r
+      return ListLookUp(name, waPieces);\r
   }\r
   return NULL;\r
 }\r
@@ -708,7 +743,6 @@ Lance (signed char *r)
 int\r
 EasyProm (signed char *r)\r
 {\r
-  int i;\r
   if(r[0] == X) return 30 + PROMO*((unsigned int)(r[1] | r[2] | r[3] | r[5] | r[6] | r[7]) <= 1);\r
   if(r[1] == X || r[7] == X) return 30 + PROMO/2;\r
   return 0;\r
@@ -763,7 +797,7 @@ StackMultis (int col)
 void\r
 Compactify (int stm)\r
 { // remove pieces that are permanently gone (captured or promoted) from one side's piece list\r
-  int i, j, k;\r
+  int i, k;\r
   for(i=stm+2; i<=last[stm]; i+=2) { // first pass: unpromoted pieces\r
     if((k = p[i].promo) >= 0 && p[i].pos == ABSENT) { // unpromoted piece no longer there\r
       p[k].promo = -2; // orphan promoted version\r
@@ -809,16 +843,16 @@ AddPiece (int stm, PieceDesc *list)
   }\r
   if(royal[stm] >= i) royal[stm] += 2;\r
   if(kylin[stm] >= i) kylin[stm] += 2;\r
-  if(p[i].value == (currentVariant == V_SHO ? 410 : 280) ) royal[stm] = i, p[i].pst = 0;\r
+  if(p[i].value == (currentVariant == V_SHO || currentVariant == V_WA ? 410 : 280) ) royal[stm] = i, p[i].pst = 0;\r
   p[i].qval = (currentVariant == V_TENJIKU ? list->ranking : 0); // jump-capture hierarchy\r
   return i;\r
 }\r
 \r
 void\r
-SetUp(char *array, int var)\r
+SetUp (char *array, int var)\r
 {\r
-  int i, j, n, m, nr, color;\r
-  char c, *q, name[3], prince = 0;\r
+  int i, j, n, m, color;\r
+  char c, name[3], prince = 0;\r
   PieceDesc *p1, *p2;\r
   last[WHITE] = 1; last[BLACK] = 0;\r
   royal[WHITE] = royal[BLACK] = 0;\r
@@ -1100,7 +1134,7 @@ char mapStep[] = { 7, 8, 1, -6, -7, -8, -1, 6 };
 char rowMask[] = { 0100, 0140, 0160, 070, 034, 016, 07, 03, 01 };\r
 char rows[9];\r
 \r
-int\r
+void\r
 AreaStep (int from, int x, int flags, int n, int d)\r
 {\r
   int i;\r
@@ -1114,7 +1148,7 @@ AreaStep (int from, int x, int flags, int n, int d)
   }\r
 }\r
 \r
-int\r
+void\r
 AreaMoves (int from, int piece, int range)\r
 {\r
   int i;\r
@@ -1168,14 +1202,14 @@ GenNonCapts (int promoSuppress)
        } else\r
        if(r >= S) { // in any case, do a jump of 2\r
          int occup = NewNonCapture(x, x + 2*v, pFlag);\r
-         if(r < J) { // Lion power, also single step\r
+         if(r < I) { // Lion power, also single step\r
            if(!NewNonCapture(x, x + v, pFlag)) nullMove = x; else occup = 1;\r
            if(r <= L) { // true Lion, also Knight jump\r
              if(!occup & r < L) for(y=x+2*v; !NewNonCapture(x, y+=v, pFlag) && r == S; ); // BS and FF moves\r
              v = nStep[j];\r
              NewNonCapture(x, x + v, pFlag);\r
-           }\r
-         }\r
+           } else if(r == T) NewNonCapture(x, x+3*v, pFlag); // Lion Dog, also triple step\r
+         } else if(r == I) NewNonCapture(x, x + v, pFlag); // also do step\r
        } else\r
        if(r == M) { // FIDE Pawn; check double-move\r
          if(!NewNonCapture(x, x+v, pFlag) && chessFlag && promoBoard[x-v] & LAST_RANK)\r
@@ -1199,7 +1233,7 @@ report (int x, int y, int i)
 int\r
 MapOneColor (int start, int last, int *map)\r
 {\r
-  int i, j, k, totMob = 0;\r
+  int i, j, totMob = 0;\r
   for(i=start+2; i<=last; i+=2) {\r
     int mob = 0;\r
     if(p[i].pos == ABSENT) continue;\r
@@ -1217,6 +1251,7 @@ MapOneColor (int start, int last, int *map)
          if(r < J) { // Lion power, also single step\r
            if(board[x + v] != EMPTY && board[x + v] != EDGE)\r
              map[2*(x + v) + start] += one[j];\r
+           if(r < I) {\r
            if(r == T) { // Lion Dog, also jump of 3\r
              if(board[x + 3*v] != EMPTY && board[x + 3*v] != EDGE)\r
                map[2*(x + 3*v) + start] += one[j];\r
@@ -1237,6 +1272,7 @@ MapOneColor (int start, int last, int *map)
              if(board[x + v] != EMPTY && board[x + v] != EDGE)\r
                map[2*(x + v) + start] += one[8];\r
            }\r
+           }\r
          }\r
        } else\r
        if(r == C) { // FIDE Pawn diagonal\r
@@ -1281,6 +1317,7 @@ MapFromScratch (int *map)
   int i;\r
   for(i=0; i<2*bsize; i++) map[i] = 0;\r
   mobilityScore  = MapOneColor(1, last[WHITE], map);\r
+\r
   mobilityScore -= MapOneColor(0, last[BLACK], map);\r
 }\r
 \r
@@ -1464,13 +1501,13 @@ UnMake(UndoInfo *u)
 }\r
        \r
 void\r
-GenCapts(int sqr, int victimValue)\r
+GenCapts (int sqr, int victimValue)\r
 { // generate all moves that capture the piece on the given square\r
-  int i, range, att = attacks[2*sqr + stm];\r
+  int i, att = attacks[2*sqr + stm];\r
 //printf("GenCapts(%c%d,%d) %08x\n",sqr%BW+'a',sqr/BW,victimValue,att);\r
   if(!att) return; // no attackers at all!\r
   for(i=0; i<8; i++) {               // try all rays\r
-    int x, v, jumper, jcapt=0;\r
+    int x, v, jcapt=0;\r
     if(att & attackMask[i]) {        // attacked by move in this direction\r
       v = -kStep[i]; x = sqr;\r
       while( board[x+=v] == EMPTY ); // scan towards source until we encounter a 'stop'\r
@@ -1546,8 +1583,46 @@ GenCapts(int sqr, int victimValue)
                  NewCapture(x, SPECIAL + 9*i + victimValue - SORTKEY(attacker), p[attacker].promoFlag); // hit and run\r
              }\r
              break;\r
+           case T: // Lion-Dog move (awful!)\r
+             if(d > 3) break;\r
+             NewCapture(x, sqr + victimValue - SORTKEY(attacker), p[attacker].promoFlag);\r
+             att -= one[i];\r
+             if(d == 3) { // check if we can take one or two intermediates (with higher piece index) with it\r
+               if((board[x-v] & TYPE) == xstm && board[x-v] > board[sqr]) {\r
+                 NewCapture(x, SPECIAL + 64 + i + victimValue - SORTKEY(attacker), p[attacker].promoFlag); // e.p. first\r
+                 if((board[x-2*v] & TYPE) == xstm && board[x-2*v] > board[sqr])\r
+                   NewCapture(x, SPECIAL + 80 + i + victimValue - SORTKEY(attacker), p[attacker].promoFlag); // e.p. both\r
+               } else if((board[x-2*v] & TYPE) == xstm && board[x-2*v] > board[sqr])\r
+                 NewCapture(x, SPECIAL + 72 + i + victimValue - SORTKEY(attacker), p[attacker].promoFlag); // e.p. second\r
+             } else if(d == 2) { // check if we can take intermediate with it\r
+               if((board[x-v] & TYPE) == xstm && board[x-v] > board[sqr]) {\r
+                 NewCapture(x, SPECIAL + 9*i + victimValue - SORTKEY(attacker), p[attacker].promoFlag); // e.p. first, stop at 2nd\r
+                 NewCapture(x, SPECIAL + 88 + i + victimValue - SORTKEY(attacker), p[attacker].promoFlag); // shoot 2nd, take 1st\r
+                 if(board[sqr-v] == EMPTY || (board[sqr-v] & TYPE) == xstm && board[sqr-v] > board[sqr])\r
+                   NewCapture(x, SPECIAL + 80 + i + victimValue - SORTKEY(attacker), p[attacker].promoFlag); // e.p. 1st and 2nd\r
+               } else if(board[sqr-v] == EMPTY || (board[sqr-v] & TYPE) == xstm && board[sqr-v] > board[sqr])\r
+                 NewCapture(x, SPECIAL + 72 + i + victimValue - SORTKEY(attacker), p[attacker].promoFlag); // e.p. 2nd\r
+             } else { // d=1; can move on to second, or move back for igui\r
+               NewCapture(x, SPECIAL + 8*i + (i^4) + victimValue, p[attacker].promoFlag); // igui\r
+               if(board[sqr-v] == EMPTY) { // 2nd empty\r
+                 NewCapture(x, SPECIAL + 9*i + victimValue - SORTKEY(attacker), p[attacker].promoFlag); // e.p. 1st and run to 2nd\r
+                 if(board[sqr-2*v] == EMPTY || (board[sqr-2*v] & TYPE) == xstm && board[sqr-2*v] > board[sqr])\r
+                   NewCapture(x, SPECIAL + 72 + i + victimValue - SORTKEY(attacker), p[attacker].promoFlag); // e.p. 1st, end on 3rd\r
+               } else if((board[sqr-v] & TYPE) == stm) { // 2nd own\r
+                 if(board[sqr-2*v] == EMPTY || (board[sqr-2*v] & TYPE) == xstm && board[sqr-2*v] > board[sqr])\r
+                   NewCapture(x, SPECIAL + 72 + i + victimValue - SORTKEY(attacker), p[attacker].promoFlag); // e.p. 1st, end on 3rd\r
+               } else if((board[sqr-v] & TYPE) == xstm && board[sqr-v] > board[sqr]) {\r
+                 NewCapture(x, SPECIAL + 9*i + victimValue - SORTKEY(attacker), p[attacker].promoFlag); // e.p. 1st, capture and stop at 2nd\r
+                 NewCapture(x, SPECIAL + 88 + i + victimValue - SORTKEY(attacker), p[attacker].promoFlag); // shoot 2nd\r
+                 if(board[sqr-2*v] == EMPTY || (board[sqr-2*v] & TYPE) == xstm && board[sqr-2*v] > board[sqr])\r
+                   NewCapture(x, SPECIAL + 80 + i + victimValue - SORTKEY(attacker), p[attacker].promoFlag); // e.p. 1st and 2nd\r
+               }\r
+             }\r
+             break;\r
            case J: // plain jump (as in KY, PH)\r
              if(d != 2) break;\r
+           case I: // jump + step (as in Wa TF)\r
+             if(d > 2) break;\r
              NewCapture(x, sqr + victimValue - SORTKEY(attacker), p[attacker].promoFlag);\r
              att -= one[i];\r
              break;\r
@@ -2230,6 +2305,7 @@ pbytes (unsigned char *b)
   for(i=BH-1; i>=0; i--) {\r
     for(j=0; j<BH; j++) printf("%3x", b[BW*i+j]);\r
     printf("\n");\r
+\r
   }\r
 }\r
 \r
@@ -2334,7 +2410,7 @@ UnMake2 (MOVE move)
   sup2 = sup1; sup1 = sup0;\r
 }\r
 \r
-char fenNames[] = "RV....DKDEFL..DHGB......SMLNKN..FK....BT..VM..PH..LN"; // pairs of char\r
+char fenNames[] = "RV....DKDEFL..DHGB......SMLNKN..FK....BT..VMEWPH..LN"; // pairs of char\r
 char fenPromo[] = "WLDHSMSECPB R HFDE....WHFB..LNG ..DKVMFS..FO..FK...."; // pairs of char\r
 \r
 char *\r
@@ -2440,7 +2516,7 @@ MoveToText (MOVE move, int multiLine)
 int\r
 ReadSquare (char *p, int *sqr)\r
 {\r
-  int i=0, f, r;\r
+  int f, r;\r
   f = p[0] - 'a';\r
   r = atoi(p + 1) - ONE;\r
   *sqr = r*BW + f;\r
@@ -2607,7 +2683,7 @@ printf("# limits %d, %d, %d mode = %d\n", tlim1, tlim2, tlim3, abortFlag);
 int\r
 SearchBestMove (MOVE *move, MOVE *ponderMove)\r
 {\r
-  int score, i;\r
+  int score;\r
 printf("# SearchBestMove\n");\r
   startTime = GetTickCount();\r
   nodes = 0;\r
@@ -2686,6 +2762,7 @@ printf("# ponder hit\n");
       }\r
     }\r
 \r
+    int\r
     main()\r
     {\r
       int engineSide=NONE;                // side played by engine\r
@@ -2863,6 +2940,9 @@ pboard(board);
          }\r
           if(currentVariant == V_SHO)\r
             printf("setup (PNBRLSE..G.+++++++Kpnbrlse..g.+++++++k) 9x9+0_shogi lnsgkgsnl/1r2e2b1/ppppppppp/9/9/9/PPPPPPPPP/1B2E2R1/LNSGKGSNL w 0 1\n");\r
+          if(currentVariant == V_WA)\r
+            printf("setup (P.C.EVLO.WGHDF.TRSXOM.+.+..+++.+++++..+++++Kp.c.evlo.wghdf.trsxom.+.+..+++.+++++..+++++k) 11x11+0_chu "\r
+                                                       "hmolvkwgcdx/1e3s3f1/ppprppptppp/3p3p3/11/11/11/3P3P3/PPPTPPPRPPP/1F3S3E1/XDCGWKVLOMH w 0 1\n");\r
          repStack[199] = hashKeyH, checkStack[199] = 0;\r
           continue;\r
         }\r
@@ -2871,5 +2951,6 @@ pboard(board);
         if(!strcmp(command, "remove"))  { stm = TakeBack(2); continue; }\r
         printf("Error: unknown command\n");\r
       }\r
+      return 0;\r
     }\r
 \r