X-Git-Url: http://winboard.nl/cgi-bin?a=blobdiff_plain;f=hachu.c;h=8a92ee5e46908bb8df4b24b0d2b9991430db1037;hb=5ec3e1a2db2f2cdbb5b2613294b61b77b8f4c564;hp=fbca937f0c2fef02360da88acb025603162411d9;hpb=e7217ebb6cd52f5f05a7db5b18db16a28ea0f2da;p=hachu.git diff --git a/hachu.c b/hachu.c index fbca937..8a92ee5 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,11 @@ SetUp(char *array, int var) if(name[1]) name[1] += 'A' - 'a'; } else color = WHITE; p1 = LookUp(name, var); + if(!p1) printf("tellusererror Unknown piece '%s' in setup\n", name), exit(-1); + 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 +795,8 @@ Init (int var) // piece-square tables for(i=0; i= S && p[attacker].range[i] != J) { // 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 +1543,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 +1561,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 +1632,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 +1651,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 +1671,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 +1728,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,14 +1817,12 @@ 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 -#define DEFAULT_FEN "" - typedef Move MOVE; int moveNr; // part of game state; incremented by MakeMove @@ -1841,13 +1867,56 @@ 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(currentVariant == V_CHESS && *fen - 'A' - n == 'N' // In Chess N is Knight, not Lion + || 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) { // no exact match if(deferred) { // but maybe non-sensical deferral int flags = p[board[f]].promoFlag; @@ -1932,8 +2002,17 @@ printf("# deferral of %d\n", deferred); if(!(flags & promoBoard[f])) moveStack[i] |= DEFER; // came from outside zone, so essential deferral } } - if(i >= retMSP) - for(i=retFirst; i= retMSP) { + for(i=retFirst; i= retMSP ? INVALID : moveStack[i]); } @@ -1949,7 +2028,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,17 +2077,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); - retMove = INVALID; - 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; @@ -2022,7 +2102,7 @@ PonderUntilInput (int stm) int TakeBack(int n) { // reset the game and then replay it to the desired point int last, stm; - stm = Setup2(NULL); + Init(currentVariant); stm = Setup2(NULL); printf("# setup done");fflush(stdout); last = moveNr - n; if(last < 0) last = 0; for(moveNr=0; moveNr 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() @@ -2041,7 +2123,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]; @@ -2054,6 +2135,7 @@ printf("# setup done");fflush(stdout); if(stm == engineSide) { // if it is the engine's turn to move, set it thinking, and let it move +pboard(board); score = SearchBestMove(stm, timeLeft, mps, timeControl, inc, timePerMove, &move, &ponderMove); if(move == INVALID) { // game apparently ended @@ -2069,7 +2151,8 @@ printf("# setup done");fflush(stdout); 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 { @@ -2099,8 +2182,6 @@ printf("# setup done");fflush(stdout); // wait for input, and read it until we have collected a complete line for(i = 0; (inBuf[i] = getchar()) != '\n'; i++); inBuf[i+1] = 0; -pboard(board); -pmoves(retFirst, retMSP); // extract the first word sscanf(inBuf, "%s", command); @@ -2138,7 +2219,6 @@ printf("in: %s\n", command); for(i=0; i<5; i++) { sscanf(inBuf+8, "%s", command); if(!strcmp(variants[i].name, command)) { -printf("var %d\n",i); Init(i); stm = Setup2(NULL); break; } } @@ -2149,8 +2229,11 @@ 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, "setboard")){ engineSide = NONE; stm = Setup2(inBuf+9); continue; } + if(!strcmp(command, "new")) { + engineSide = BLACK; Init(V_CHESS); stm = Setup2(NULL); maxDepth = MAXPLY; randomize = OFF; comp = 0; + continue; + } + if(!strcmp(command, "setboard")){ engineSide = NONE; Init(currentVariant); stm = Setup2(inBuf+9); continue; } if(!strcmp(command, "easy")) { ponder = OFF; continue; } if(!strcmp(command, "hard")) { ponder = ON; continue; } if(!strcmp(command, "undo")) { stm = TakeBack(1); continue; } @@ -2171,7 +2254,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; } @@ -2181,8 +2264,11 @@ 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 { +pboard(board); + 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