X-Git-Url: http://winboard.nl/cgi-bin?a=blobdiff_plain;f=hachu.c;h=ad2904e2c4e239147f738e8975b33b2c57e5d918;hb=fbdf26b9d971d08e990b906d65e27bc463571212;hp=68c0a6cbf44fced74853321ee8a31648cf40bd0d;hpb=0123828837dde5378db597e9712e3c510a7abf1b;p=hachu.git diff --git a/hachu.c b/hachu.c index 68c0a6c..ad2904e 100644 --- a/hachu.c +++ b/hachu.c @@ -91,11 +91,13 @@ typedef struct { char fireMask; } UndoInfo; -char *array; +char *array, fenArray[4000], *reason; int bWidth, bHeight, bsize, zone, currentVariant; int stm, xstm, hashKeyH, hashKeyL, framePtr, msp, nonCapts, rootEval, retMSP, retFirst, retDep, pvPtr, level, cnt50, chuFlag=1, tenFlag, mobilityScore; -int nodes, startTime, tlim1, tlim2; -Move retMove, moveStack[10000], path[100], repStack[300], pv[1000]; +int nodes, startTime, tlim1, tlim2, repCnt, comp; +Move retMove, moveStack[10000], path[100], repStack[300], pv[1000], repeatMove[300]; + + int maxDepth; // used by search #define X 36 /* slider */ #define R 37 /* jump capture */ @@ -675,6 +677,8 @@ SetUp(char *array, int var) for(i=0; ; i++) { //printf("next rank: %s\n", array); for(j = BW*i; ; j++) { + int pflag=0; + if(*array == '+') pflag++, array++; c = name[0] = *array++; if(!c) goto eos; if(c == '.') continue; @@ -687,9 +691,10 @@ SetUp(char *array, int var) if(name[1]) name[1] += 'A' - 'a'; } else color = WHITE; p1 = LookUp(name, var); + if(pflag && p1->promoted) p1 = LookUp(p1->promoted, var); // use promoted piece instead n = AddPiece(color, p1); p[n].pos = j; - if(p1->promoted[0]) { + if(p1->promoted[0] && !pflag) { p2 = LookUp(p1->promoted, var); m = AddPiece(color, p2); if(m <= n) n += 2; @@ -789,7 +794,8 @@ Init (int var) // piece-square tables for(i=0; i= S) { // has Knight jump in our direction + if(p[attacker].range[i] <= L && p[attacker].range[i] >= S || p[attacker].range[i] == N) { // has Knight jump in our direction NewCapture(x, sqr + victimValue, p[attacker].promoFlag); // plain jump (as in N) if(p[attacker].range[i] < N) { // Lion power; generate double captures over two possible intermediates int v = kStep[i]; // leftish path @@ -1522,7 +1542,7 @@ if(PATH) /*pboard(board),pmap(attacks, BLACK),*/printf("search(%d) {%d,%d} eval= nodes++; pv[pvPtr++] = 0; // start empty PV, directly behind PV of parent - firstMove = curMove = sorted = msp += 20; // leave 20 empty slots in front of move list + firstMove = curMove = sorted = msp += 50; // leave 50 empty slots in front of move list tb.fireMask = phase = 0; iterDep=1; replyDep = (depth < 1 ? depth : 1) - 1; do { if(flag && depth>= 0) printf("iter %d:%d\n", depth,iterDep),fflush(stdout); @@ -1540,7 +1560,7 @@ if(flag && depth>= 0) printf("phase=%d: first/curr/last = %d / %d / %d\n", phase #if 0 if(curEval >= beta) { stm ^= WHITE; - score = -Search(-beta, -iterAlpha, difEval, depth-3, promoSuppress & SQUARE, ABSENT); + score = -Search(-beta, -iterAlpha, -difEval, depth-3, promoSuppress & SQUARE, ABSENT); stm ^= WHITE; if(score >= beta) { msp = oldMSP; retDep += 3; return score + (score < curEval); } } @@ -1611,7 +1631,7 @@ if(PATH) printf("%d:%2d:%2d msp=%d\n",level,depth,iterDep,msp); // MOVE EXTRACTION extractMove: if(flag & depth >= 0) printf("%2d:%d extract %d/%d\n", depth, iterDep, curMove, msp); - if(curMove < sorted) { + if(curMove > sorted) { move = moveStack[sorted=j=curMove]; for(i=curMove+1; i move) move = moveStack[j=i]; // search move with highest priority @@ -1630,7 +1650,8 @@ if(flag & depth >= 0) printf("%2d:%d found %d/%d %08x %s\n", depth, iterDep, cur if(flag & depth >= 0) printf("%2d:%d made %d/%d %s\n", depth, iterDep, curMove, msp, MoveToText(moveStack[curMove], 0)); for(i=2; i<=cnt50; i+=2) if(repStack[level-i+200] == hashKeyH) { moveStack[curMove] = 0; // erase forbidden move - score = -INF; goto repetition; + if(!level) repeatMove[repCnt++] = move & 0xFFFFFF; // remember outlawed move + score = -INF; moveStack[curMove] = 0; goto repetition; } repStack[level+200] = hashKeyH; @@ -1649,7 +1670,11 @@ MapFromScratch(attacks); // for as long as incremental update does not work. if(promoSuppress & PROMOTE) score = -INF; // non-Lion captures Lion after opponent did same defer |= PROMOTE; // if we started, flag he cannot do it in reply } - if(score == -INF) { moveStack[curMove] = 0; goto abortMove; } // zap illegal moves + if(score == -INF) { + if(level == 1) repeatMove[repCnt++] = move & 0xFFFFFF | (p[tb.piece].value == 10*LVAL ? 3<<24 : 1 << 24); + moveStack[curMove] = 0; // zap illegal moves + goto abortMove; + } } #if 1 score = -Search(-beta, -iterAlpha, -difEval - tb.booty, replyDep, promoSuppress & ~PROMOTE, defer); @@ -1702,7 +1727,9 @@ if(PATH) printf("%d:%2d:%d %3d %6x %-10s %6d %6d\n", level, depth, iterDep, curM int i; // WB thinking output printf("%d %d %d %d", iterDep, bestScore, (GetTickCount() - startTime)/10, nodes); for(i=0; pv[i]; i++) printf(" %s", MoveToText(pv[i], 0)); - printf("\n"); fflush(stdout); + if(iterDep == 1) printf(" { root eval = %4.2f dif = %4.2f; abs = %4.2f}", curEval/100., difEval/100., PSTest()/100.); + printf("\n"); + fflush(stdout); } if(GetTickCount() - startTime > tlim1) break; // do not start iteration we can (most likely) not finish } @@ -1789,8 +1816,8 @@ pmoves(int start, int end) #define INVALID 0 // some parameter of your engine - #define MAXMOVES 500 /* maximum game length */ - #define MAXPLY 60 /* maximum search depth */ + #define MAXMOVES 2000 /* maximum game length */ + #define MAXPLY 20 /* maximum search depth */ #define OFF 0 #define ON 1 @@ -1841,13 +1868,55 @@ UnMake2 (MOVE move) sup2 = sup1; sup1 = sup0; } +char fenNames[] = "RV....DKDEFL..DHGB......SMLNKN..FK....BT..VM..PH...."; // pairs of char +char fenPromo[] = "WLDHSMSECPB R HFDE....WHFB..LNG ..DKVMFS..FO..FK...."; // pairs of char + +char * +Convert (char *fen) +{ + char *p = fenArray, *q, *rows[36], tmp[4000]; + int n=0; + printf("# convert FEN '%s'\n", fen); + q = strchr(fen, ' '); if(q) *q = 0; q = fen; + do { rows[n++] = q; q = strchr(q, '/'); if(!q) break; *q++ = 0; } while(1); + *tmp = 0; + while(--n >= 0) { strcat(tmp, rows[n]); if(n) strcat(tmp, "/"); } + fen = tmp; + printf("# flipped FEN '%s'\n", fen); + while(*fen) { + if(*fen == ' ') { *p = 0; break; } + if(n=atoi(fen)) fen++; // digits read + if(n > 9) fen++; // double digit + while(n-- > 0) *p++ = '.'; // expand to empty squares + if(isalpha(*fen)) { + char *table = fenNames; + n = *fen > 'Z' ? 'a' - 'A' : 0; + if(table[2* (*fen - 'A' - n)] == '.') *p++ = *fen; else { + *p++ = ':'; + *p++ = table[2* (*fen - 'A' - n)] + n; + *p++ = table[2* (*fen - 'A' - n)+1] + n; + } + } else *p++ = *fen; + fen++; + } + printf("# converted FEN '%s'\n", fenArray); + return fenArray; +} + int Setup2 (char *fen) { + int stm = WHITE; + if(fen) { + char *q = strchr(fen, '\n'); + if(q) *q = 0; + if(q = strchr(fen, ' ')) stm = (q[1] == 'b' ? BLACK : WHITE); // fen contains color field + if(strchr(fen, '.') || strchr(fen, ':')) array = fen; else array = Convert(fen); + } SetUp(array, currentVariant); sup0 = sup1 = sup2 = ABSENT; - rootEval = cnt50 = hashKeyH = hashKeyL = 0; - return WHITE; + rootEval = cnt50 = hashKeyH = hashKeyL = moveNr = 0; + return stm; } void @@ -1912,7 +1981,7 @@ ParseMove (char *moveText) if(*moveText == '+') ret |= PROMOTE; printf("# suppress = %c%d\n", sup1%BW+'a', sup1/BW); MapFromScratch(attacks); - postThinking--; + postThinking--; repCnt = 0; Search(-INF-1, INF+1, 0, 1, sup1 & ~PROMOTE, sup2); postThinking++; for(i=retFirst; i= retMSP) - for(i=retFirst; i= retMSP) { + for(i=retFirst; i= retMSP ? INVALID : moveStack[i]); } @@ -1949,7 +2027,7 @@ MapFromScratch(attacks); //pmap(attacks, WHITE); //pmap(attacks, BLACK); //flag=1; - postThinking--; + postThinking--; repCnt = 0; Search(-INF-1, INF+1, 0, 1, sup1 & ~PROMOTE, sup2); postThinking++; flag=0; @@ -1998,16 +2076,18 @@ flag=0; int SearchBestMove (int stm, int timeLeft, int mps, int timeControl, int inc, int timePerMove, MOVE *move, MOVE *ponderMove) { - int score, targetTime, movesLeft = 50; + int score, targetTime, movesLeft = BW*BH/4 + 20; if(mps) movesLeft = mps - (moveNr>>1)%mps; targetTime = timeLeft*10 / (movesLeft + 2) + 1000 * inc; + if(moveNr < 30) targetTime *= 0.5 + moveNr/60.; // speedup in opening if(timePerMove > 0) targetTime = timeLeft * 5; startTime = GetTickCount(); tlim1 = 0.2*targetTime; tlim2 = 1.9*targetTime; nodes = 0; MapFromScratch(attacks); - score = Search(-INF-1, INF+1, rootEval, 20, sup1, sup2); + retMove = INVALID; repCnt = 0; + score = Search(-INF-1, INF+1, rootEval, maxDepth, sup1, sup2); *move = retMove; *ponderMove = INVALID; return score; @@ -2030,9 +2110,11 @@ printf("# setup done");fflush(stdout); void PrintResult(int stm, int score) { - if(score == 0) printf("1/2-1/2\n"); - if(score > 0 && stm == WHITE || score < 0 && stm == BLACK) printf("1-0\n"); - else printf("0-1\n"); + char tail[100]; + if(reason) sprintf(tail, " {%s}", reason); else *tail = 0; + if(score == 0) printf("1/2-1/2%s\n", tail); + if(score > 0 && stm == WHITE || score < 0 && stm == BLACK) printf("1-0%s\n", tail); + else printf("0-1%s\n", tail); } main() @@ -2040,7 +2122,6 @@ printf("# setup done");fflush(stdout); int engineSide=NONE; // side played by engine int timeLeft; // timeleft on engine's clock int mps, timeControl, inc, timePerMove; // time-control parameters, to be used by Search - int maxDepth; // used by search MOVE move, ponderMove; int i, score; char inBuf[8000], command[80]; @@ -2056,6 +2137,20 @@ printf("# setup done");fflush(stdout); score = SearchBestMove(stm, timeLeft, mps, timeControl, inc, timePerMove, &move, &ponderMove); if(move == INVALID) { // game apparently ended + int kcapt = 0, xstm = stm ^ WHITE, king, k = p[king=royal[xstm]].pos; + if( k != ABSENT) { // test if King capture possible + if(attacks[2*k + stm]) { + if( p[king + 2].pos == ABSENT ) kcapt = 1; // we have an attack on his only King + } + } else { // he has no king! Test for attacks on Crown Prince + k = p[king + 2].pos; + if(attacks[2*k + stm]) kcapt = 1; // we have attack on Crown Prince + } + if(kcapt) { // print King capture before claiming + GenCapts(k, 0); + printf("move %s\n", MoveToText(moveStack[msp-1], 1)); + reason = "king capture"; + } else reason = "resign"; engineSide = NONE; // so stop playing PrintResult(stm, score); } else { @@ -2135,7 +2230,10 @@ printf("var %d\n",i); if(!strcmp(command, "memory")) { SetMemorySize(atoi(inBuf+7)); continue; } if(!strcmp(command, "ping")) { printf("pong%s", inBuf+4); continue; } // if(!strcmp(command, "")) { sscanf(inBuf, " %d", &); continue; } - if(!strcmp(command, "new")) { engineSide = BLACK; Init(V_CHESS); stm = Setup2(DEFAULT_FEN); maxDepth = MAXPLY; randomize = OFF; continue; } + if(!strcmp(command, "new")) { + engineSide = BLACK; Init(V_CHESS); stm = Setup2(DEFAULT_FEN); maxDepth = MAXPLY; randomize = OFF; comp = 0; + continue; + } if(!strcmp(command, "setboard")){ engineSide = NONE; stm = Setup2(inBuf+9); continue; } if(!strcmp(command, "easy")) { ponder = OFF; continue; } if(!strcmp(command, "hard")) { ponder = ON; continue; } @@ -2157,7 +2255,7 @@ printf("var %d\n",i); if(!strcmp(command, "l")) { pplist(); continue; } // ignored commands: if(!strcmp(command, "xboard")) { continue; } - if(!strcmp(command, "computer")){ continue; } + if(!strcmp(command, "computer")){ comp = 1; continue; } if(!strcmp(command, "name")) { continue; } if(!strcmp(command, "ics")) { continue; } if(!strcmp(command, "accepted")){ continue; } @@ -2167,8 +2265,10 @@ printf("var %d\n",i); if(!strcmp(command, "")) { continue; } if(!strcmp(command, "usermove")){ int move = ParseMove(inBuf+9); - if(move == INVALID) printf("Illegal move\n"); - else { + if(move == INVALID) { + if(reason) printf("Illegal move {%s}\n", reason); else printf("%s\n", reason="Illegal move"); + if(comp) PrintResult(stm, -INF); // against computer: claim + } else { stm = MakeMove2(stm, move); ponderMove = INVALID; gameMove[moveNr++] = move; // remember game