Fix alpha bug, promotion bug, highlighting and move printing bugs
[hachu.git] / hachu.c
diff --git a/hachu.c b/hachu.c
index 14db902..1ea7853 100644 (file)
--- a/hachu.c
+++ b/hachu.c
@@ -1184,7 +1184,7 @@ Search (int alpha, int beta, int difEval, int depth, int oldPromo, int promoSupp
 {\r
   int i, j, k, firstMove, oldMSP = msp, curMove, sorted, bad, phase, king, iterDep, replyDep, nextVictim, to, defer, dubious, bestMoveNr;\r
   int savHashL = hashKeyL, savHashH = hashKeyH;\r
-  int score, bestScore, curEval;\r
+  int score, bestScore, curEval, iterAlpha;\r
   Move move, nullMove;\r
   UndoInfo tb;\r
 if(PATH) printf("search(%d) %d-%d eval=%d, stm=%d\n",depth,alpha,beta,difEval,stm),fflush(stdout);\r
@@ -1206,11 +1206,11 @@ if(PATH) printf("search(%d) %d-%d eval=%d, stm=%d\n",depth,alpha,beta,difEval,st
   alpha -= (alpha < curEval);\r
   beta  -= (beta <= curEval);\r
 \r
-  firstMove = curMove = sorted = nonCapts = msp += 20; // leave 20 empty slots in front of move list\r
+  firstMove = curMove = sorted = msp += 20; // leave 20 empty slots in front of move list\r
   phase = 0; iterDep=1; replyDep = (depth < 1 ? depth : 1) - 1;\r
   do {\r
 if(flag && depth>= 0) printf("iter %d:%d\n", depth,iterDep),fflush(stdout);\r
-    bestScore = -INF; bestMoveNr = 0;\r
+    iterAlpha = alpha; bestScore = -INF; bestMoveNr = 0;\r
     for(curMove = firstMove; ; curMove++) { // loop over moves\r
 if(flag && depth>= 0) printf("phase=%d: first/curr/last = %d / %d / %d\n", phase, firstMove, curMove, msp);fflush(stdout);\r
       // MOVE SOURCE\r
@@ -1224,7 +1224,7 @@ if(flag && depth>= 0) printf("phase=%d: first/curr/last = %d / %d / %d\n", phase
 #if 0\r
            if(curEval >= beta) {\r
              stm ^= WHITE;\r
-             score = -Search(-beta, -alpha, difEval, depth-3, promoSuppress & SQUARE, ABSENT);\r
+             score = -Search(-beta, -iterAlpha, difEval, depth-3, promoSuppress & SQUARE, ABSENT);\r
              stm ^= WHITE;\r
              if(score >= beta) { msp = oldMSP; return score + (score < curEval); }\r
            }\r
@@ -1263,6 +1263,7 @@ if(flag && depth>= 0) printf("phase=%d: first/curr/last = %d / %d / %d\n", phase
            if(depth <= 0) goto cutoff;\r
            phase = 6;\r
          case 6: // non-captures\r
+           nonCapts = msp;\r
            nullMove = GenNonCapts(oldPromo);\r
            phase = 7;\r
            sorted = msp; // do not sort noncapts\r
@@ -1307,7 +1308,7 @@ if(PATH) pmap(attacks, stm);
         if(score == -INF) { moveStack[curMove] = 0; goto abortMove; }\r
       }\r
 #if 1\r
-      score = -Search(-beta, -alpha, -difEval - tb.booty, replyDep, promoSuppress & ~PROMOTE, defer);\r
+      score = -Search(-beta, -iterAlpha, -difEval - tb.booty, replyDep, promoSuppress & ~PROMOTE, defer);\r
 #else\r
       score = 0;\r
 #endif\r
@@ -1325,8 +1326,8 @@ if(PATH) printf("%d:%2d:%d %3d %6x %-10s %6d %6d\n", level, depth, iterDep, curM
       // ALPHA-BETA STUFF\r
       if(score > bestScore) {\r
        bestScore = score; bestMoveNr = curMove;\r
-       if(score > alpha) {\r
-         alpha = score;\r
+       if(score > iterAlpha) {\r
+         iterAlpha = score;\r
          if(curMove < firstMove + 5) { // if not too much work, sort move to front\r
            int i;\r
            for(i=curMove; i>firstMove; i--) {\r
@@ -1554,11 +1555,11 @@ MapFromScratch(attacks);
       if(!(flags & promoBoard[t] & (CANT_DEFER | LAST_RANK))) { // but change it into a deferral if that is allowed\r
        moveStack[i] &= ~PROMOTE;\r
        if(p[board[f]].value == 40) p[board[f]].promoFlag &= LAST_RANK; else\r
-       if(!(flags & promoBoard[t])) moveStack[i] |= DEFER; // came from outside zone, so essential deferral\r
+       if(!(flags & promoBoard[f])) moveStack[i] |= DEFER; // came from outside zone, so essential deferral\r
       }\r
     }\r
     if(i >= retMSP)\r
-      for(i=20; i<retMSP; i++) printf("# %d. %08x %08x %s\n", i-20, moveStack[i], ret, MoveToText(moveStack[i], 0));\r
+      for(i=retFirst; i<retMSP; i++) printf("# %d. %08x %08x %s\n", i-20, moveStack[i], ret, MoveToText(moveStack[i], 0));\r
   }\r
   return (i >= retMSP ? INVALID : moveStack[i]);\r
 }\r
@@ -1584,7 +1585,7 @@ flag=0;
   if(!cnt) { // no moves from given square\r
     if(sqr != lastPut) return; // refrain from sending empty FEN\r
     // we lifted a piece for second leg of move\r
-    for(i=20; i<retMSP; i++) {\r
+    for(i=retFirst; i<retMSP; i++) {\r
       if(lastLift == (moveStack[i]>>SQLEN & SQUARE)) {\r
        int e, t = moveStack[i] & SQUARE;\r
        if(t < SPECIAL) continue; // only special moves\r