Implement parsing and printing of Lion Dog moves
[hachu.git] / hachu.c
diff --git a/hachu.c b/hachu.c
index 5602fe6..5182d30 100644 (file)
--- a/hachu.c
+++ b/hachu.c
@@ -190,7 +190,8 @@ Move retMove, moveStack[20000], path[100], repStack[300], pv[1000], repeatMove[3
 #define FVAL 5000 /* piece value of Fire Demon. Used in code for recognizing moves with it and do burns */\r
 \r
 PieceDesc chuPieces[] = {\r
-  {"LN", "",  LVAL, { L,L,L,L,L,L,L,L }, 4 }, // lion\r
+  {"LN", "",  LVAL, { T,T,T,T,T,T,T,T }, 4 }, // lion\r
+//  {"LN", "",  LVAL, { L,L,L,L,L,L,L,L }, 4 }, // lion\r
   {"FK", "",   600, { X,X,X,X,X,X,X,X }, 4 }, // free king\r
   {"SE", "",   550, { X,D,X,X,X,X,X,D }, 4 }, // soaring eagle\r
   {"HF", "",   500, { D,X,X,X,X,X,X,X }, 4 }, // horned falcon\r
@@ -990,7 +991,7 @@ Init (int var)
     // Lion-Dog triple moves\r
     toList[64+i] = 3*kStep[i]; epList[64+i] =   kStep[i];\r
     toList[72+i] = 3*kStep[i]; epList[72+i] = 2*kStep[i];\r
-    toList[80+i] = 3*kStep[i]; epList[80+i] =   kStep[i]; ep2List[80+i] = 2*kStep[i];\r
+    toList[80+i] = 3*kStep[i]; epList[80+i] = 2*kStep[i]; ep2List[80+i] = kStep[i];\r
     toList[88+i] =   kStep[i]; epList[88+i] = 2*kStep[i];\r
   }\r
 \r
@@ -1208,7 +1209,7 @@ GenNonCapts (int promoSuppress)
              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
+           } 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
@@ -1583,6 +1584,42 @@ 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
@@ -2374,7 +2411,7 @@ UnMake2 (MOVE move)
   sup2 = sup1; sup1 = sup0;\r
 }\r
 \r
-char fenNames[] = "RV....DKDEFL..DHGB......SMLNKN..FK....BT..VMSWPH..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
@@ -2460,15 +2497,15 @@ MoveToText (MOVE move, int multiLine)
   if(t >= SPECIAL) {\r
    if(t < CASTLE) { // castling is printed as a single move, implying its side effects\r
     int e = f + epList[t - SPECIAL];\r
-//    printf("take %c%d\n", e%BW+'a', e/BW+ONE);\r
-    sprintf(buf, "%c%d%c%d,", f%BW+'a', f/BW+ONE, e%BW+'a', e/BW+ONE); f = e;\r
-    if(multiLine) printf("move %s\n", buf), buf[0] = '\0';\r
     if(ep2List[t - SPECIAL]) {\r
-      e = g + ep2List[t - SPECIAL];\r
+      int e2 = f + ep2List[t - SPECIAL];\r
 //      printf("take %c%d\n", e%BW+'a', e/BW+ONE);\r
-      sprintf(buf+strlen(buf), "%c%d%c%d,", f%BW+'a', f/BW+ONE, e%BW+'a', e/BW+ONE); f = e;\r
-    if(multiLine) printf("move %s\n", buf), buf[0] = '\0';\r
+      sprintf(buf+strlen(buf), "%c%d%c%d,", f%BW+'a', f/BW+ONE, e2%BW+'a', e2/BW+ONE); f = e2;\r
+      if(multiLine) printf("move %s\n", buf), buf[0] = '\0';\r
     }\r
+//    printf("take %c%d\n", e%BW+'a', e/BW+ONE);\r
+    sprintf(buf, "%c%d%c%d,", f%BW+'a', f/BW+ONE, e%BW+'a', e/BW+ONE); f = e;\r
+    if(multiLine) printf("move %s\n", buf), buf[0] = '\0';\r
    }\r
     t = g + toList[t - SPECIAL];\r
   }\r
@@ -2518,10 +2555,27 @@ ParseMove (char *moveText)
     e = t;\r
     moveText += ReadSquare(moveText, &t);\r
     for(i=0; i<8; i++) if(f + kStep[i] == e) break;\r
-    if(i >= 8) return INVALID; // this rejects Lion Dog 2+1 and 2-1 moves!\r
-    for(j=0; j<8; j++) if(e + kStep[j] == t) break;\r
-    if(j >= 8) return INVALID; // this rejects Lion Dog 1+2 moves!\r
-    t2 = SPECIAL + 8*i + j;\r
+    if(i >= 8) { // first leg not King step. Try Lion Dog 2+1 or 2-1\r
+      for(i=0; i<8; i++) if(f + 2*kStep[i] == e) break;\r
+      if(i >= 8) return INVALID; // not even that\r
+      if(f + 3*kStep[i] == t)    t2 = SPECIAL + 72 + i; // 2+1\r
+      else if(f + kStep[i] == t) t2 = SPECIAL + 88 + i; // 2-1\r
+      else return INVALID;\r
+    } else if(f + 3*kStep[i] == t) { // Lion Dog 1+2 move\r
+      t2 = SPECIAL + 64 + i;\r
+    } else if(*moveText == ',') { // 3rd leg follows!\r
+      if(f + 2*kStep[i] != t) return INVALID; // 3-leg moves must be linear!\r
+      moveText += ReadSquare(moveText, &e);\r
+      if(e != t) return INVALID; // must again continue with same piece\r
+      moveText += ReadSquare(moveText, &t);\r
+      if(f + 3*kStep[i] == t)    t2 = SPECIAL + 80 + i; // 1+1+1\r
+      else if(f + kStep[i] == t) t2 = SPECIAL + 88 + i; // 2-1 entered as 1+1-1\r
+      else return INVALID;\r
+    } else {\r
+      for(j=0; j<8; j++) if(e + kStep[j] == t) break;\r
+      if(j >= 8) return INVALID; // this rejects Lion Dog 1+2 moves!\r
+      t2 = SPECIAL + 8*i + j;\r
+    }\r
   } else if(chessFlag && board[f] != EMPTY && p[board[f]].value == pVal && board[t] == EMPTY) { // Pawn to empty, could be e.p.\r
       if(t == f + BW + 1) t2 = SPECIAL + 16; else\r
       if(t == f + BW - 1) t2 = SPECIAL + 48; else\r
@@ -2599,9 +2653,13 @@ Highlight(char *coords)
        int e, t = moveStack[i] & SQUARE;\r
        if(t < SPECIAL) continue; // only special moves\r
        e = lastLift + epList[t - SPECIAL]; // decode\r
+       if(sqr == lastLift + ep2List[t - SPECIAL]) { // second leg of 3-leg move\r
+         b[e] = 'C'; cnt++;\r
+         continue;\r
+       }\r
        t = lastLift + toList[t - SPECIAL];\r
        if(e != sqr) continue;\r
-       b[t] = (!boardCopy[t] ? 'Y' : 'R'); cnt++;\r
+       if(!b[t]) b[t] = (!boardCopy[t] ? 'Y' : 'R'); cnt++;\r
       }\r
     }\r
     if(!cnt) return;\r