X-Git-Url: http://winboard.nl/cgi-bin?a=blobdiff_plain;f=hachu.c;h=9bc797c02cadae5aa38dc0b1a854dbaf257cf75b;hb=5676d8e01a0b03b248b2108918874b795558d6c0;hp=59fbacf509586fe170a8770e9acc40da4f9d8631;hpb=62077784733af1f94035193b31f3e3379b8a4340;p=hachu.git diff --git a/hachu.c b/hachu.c index 59fbacf..9bc797c 100644 --- a/hachu.c +++ b/hachu.c @@ -11,7 +11,7 @@ // promotions by pieces with Lion power stepping in & out the zone in same turn // promotion on capture -#define VERSION "0.1beta" +#define VERSION "0.4beta" #define PATH level==0 || path[0] == 0xc4028 && (level==1 /*|| path[1] == 0x75967 && (level == 2 || path[2] == 0x3400b && (level == 3))*/) //define PATH 0 @@ -40,7 +40,7 @@ #define BSIZE BWMAX*BHMAX #define ZONE zone -#define ONE (currentVariant == V_SHO || currentVariant == V_CHESS) +#define ONE (currentVariant == V_SHO || currentVariant == V_CHESS || currentVariant == V_SHATRANJ) #define BLACK 0 #define WHITE 1 @@ -91,12 +91,14 @@ typedef struct { char fireMask; } UndoInfo; -char *array, *reason; +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, 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 */ #define N -1 /* Knight */ @@ -324,6 +326,16 @@ PieceDesc chessPieces[] = { { NULL } // sentinel }; +PieceDesc shatranjPieces[] = { + {"Q", "", 15, { 0,1,0,1,0,1,0,1 } }, + {"R", "", 50, { X,0,X,0,X,0,X,0 } }, + {"B", "", 9, { 0,J,0,J,0,J,0,J } }, + {"N", "", 30, { N,N,N,N,N,N,N,N } }, + {"K", "", 28, { 1,1,1,1,1,1,1,1 } }, + {"P", "Q", 8, { M,C,0,0,0,0,0,C } }, + { NULL } // sentinel +}; + char chuArray[] = "L:FLCSGK:DEGSC:FLL/:RV.B.:BT:KN:PH:BT.B.:RV/:SM:VMR:DH:DK:LN:FK:DK:DHR:VM:SM/PPPPPPPPPPPP/...:GB....:GB..." "/............/............/" "...:gb....:gb.../pppppppppppp/:sm:vmr:dh:dk:fk:ln:dk:dhr:vm:sm/:rv.b.:bt:ph:kn:bt.b.:rv/l:flcsg:dekgsc:fll"; @@ -337,6 +349,7 @@ char tenArray[] = "LN:FLICSGK:DEGSCI:FLNL/:RV.:CS:CS.:BT:KN:LN:FK:PH:BT.:CS:CS.: ":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"; char shoArray[] = "LNSGKGSNL/.B..:DE..R./PPPPPPPPP/........./........./........./ppppppppp/.r..:de..b./lnsgkgsnl"; char chessArray[] = "RNBQKBNR/PPPPPPPP/......../......../......../......../pppppppp/rnbqkbnr"; +char shatArray[]= "RNBKQBNR/PPPPPPPP/......../......../......../......../pppppppp/rnbkqbnr"; typedef struct { int boardWidth, boardFiles, boardRanks, zoneDepth, varNr; // board sizes @@ -344,7 +357,7 @@ typedef struct { char *array; // initial position } VariantDesc; -typedef enum { V_CHESS, V_SHO, V_CHU, V_DAI, V_TENJIKU, V_DADA, V_MAKA, V_TAI, V_KYOKU } Variant; +typedef enum { V_CHESS, V_SHO, V_CHU, V_DAI, V_TENJIKU, V_DADA, V_MAKA, V_TAI, V_KYOKU, V_SHATRANJ } Variant; VariantDesc variants[] = { { 16, 8, 8, 1, V_CHESS, "normal", chessArray }, // FIDE @@ -352,6 +365,8 @@ VariantDesc variants[] = { { 24, 12, 12, 4, V_CHU, "chu", chuArray }, // Chu { 30, 15, 15, 5, V_DAI, "dai", daiArray }, // Dai { 32, 16, 16, 5, V_TENJIKU, "tenjiku", tenArray }, // Tenjiku + { 16, 8, 8, 1, V_SHATRANJ,"shatranj",shatArray}, // Shatranj + // { 0, 0, 0, 0, 0 }, // sentinel { 34, 17, 17, 0, V_DADA, "dada", chuArray }, // Dai Dai { 38, 19, 19, 0, V_MAKA, "maka", chuArray }, // Maka Dai Dai @@ -532,6 +547,8 @@ LookUp (char *name, int var) return ListLookUp(name, chuPieces); case V_CHESS: // FIDE return ListLookUp(name, chessPieces); + case V_SHATRANJ: // Shatranj + return ListLookUp(name, shatranjPieces); } return NULL; } @@ -626,6 +643,7 @@ Compactify (int stm) } } for(i=stm+2; ipromoted) 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 +811,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 +1559,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 +1577,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 +1648,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 @@ -1650,7 +1687,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); @@ -1703,7 +1744,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 } @@ -1790,14 +1833,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 @@ -1842,13 +1883,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 || currentVariant == V_SHATRANJ) && *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 @@ -1922,6 +2006,7 @@ MapFromScratch(attacks); if((moveStack[i] & (PROMOTE | DEFER-1)) == ret) break; if((moveStack[i] & DEFER-1) == ret) deferred = i; // promoted version of entered non-promotion is legal } +printf("# moveNr = %d in {%d,%d}\n", i, retFirst, retMSP); if(i>=retMSP) { // no exact match if(deferred) { // but maybe non-sensical deferral int flags = p[board[f]].promoFlag; @@ -1937,7 +2022,8 @@ printf("# deferral of %d\n", deferred); for(i=retFirst; i>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; @@ -2017,7 +2104,7 @@ SearchBestMove (int stm, int timeLeft, int mps, int timeControl, int inc, int ti nodes = 0; MapFromScratch(attacks); retMove = INVALID; repCnt = 0; - score = Search(-INF-1, INF+1, rootEval, 20, sup1, sup2); + score = Search(-INF-1, INF+1, rootEval, maxDepth, sup1, sup2); *move = retMove; *ponderMove = INVALID; return score; @@ -2031,7 +2118,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