\r
#define VERSION 0.0\r
\r
-#define PATH level==0 || level==1 && path[0] == 0x55893\r
+//define PATH level==0 || level==1 && path[0] == 0x55893\r
+#define PATH 0\r
\r
#include <stdio.h>\r
+#include <stdlib.h>\r
+#include <string.h>\r
+#include <signal.h>\r
+#include <time.h>\r
+\r
+#ifdef WIN32 \r
+# include <windows.h>\r
+#else\r
+# include <sys/time.h>\r
+ int GetTickCount() // with thanks to Tord\r
+ { struct timeval t;\r
+ gettimeofday(&t, NULL);\r
+ return t.tv_sec*1000 + t.tv_usec/1000;\r
+ }\r
+#endif\r
\r
#define BW 24\r
#define BH 12\r
char *name, *promoted;\r
int value;\r
signed char range[8];\r
+ int whiteKey, blackKey;\r
} PieceDesc;\r
\r
typedef struct {\r
- int from, to, piece, victim, new, booty, epSquare, epVictim, ep2Square, ep2Victim;\r
+ int from, to, piece, victim, new, booty, epSquare, epVictim, ep2Square, ep2Victim, savKeyL, savKeyH;\r
} UndoInfo;\r
\r
-int stm, xstm, hashKeyH, hashKeyL, framePtr, msp, nonCapts, retMSP, retFirst, level, chuFlag=1;\r
-Move retMove, moveStack[10000], path[100];\r
+int stm, xstm, hashKeyH, hashKeyL, framePtr, msp, nonCapts, rootEval, retMSP, retFirst, pvPtr, level, chuFlag=1;\r
+int nodes, startTime, tlim1, tlim2;\r
+Move retMove, moveStack[10000], path[100], pv[1000];\r
\r
#define X 36 /* slider */\r
#define J -1 /* jump */\r
#define board (rawBoard + 6*BW + 3)\r
#define dist (distance + BSIZE)\r
\r
-int LookUp(char *name, PieceDesc *list)\r
+PieceDesc *\r
+LookUp (char *name, PieceDesc *list)\r
{ // find piece of given name in list of descriptors\r
int i=0;\r
while(list->name && strcmp(name, list->name)) i++, list++;\r
- return (list->name == NULL ? -1 : i);\r
+ return (list->name == NULL ? NULL : list);\r
}\r
\r
void\r
}\r
\r
int\r
-AddPiece (int stm, int n, PieceDesc *list)\r
+AddPiece (int stm, PieceDesc *list)\r
{\r
- int i, j;\r
+ int i, j, *key;\r
for(i=stm+2; i<=last[stm]; i += 2) {\r
- if(p[i].value < 10*list[n].value || p[i].value == 10*list[n].value && (p[i].promo < 0)) break;\r
+ if(p[i].value < 10*list->value || p[i].value == 10*list->value && (p[i].promo < 0)) break;\r
}\r
last[stm] += 2;\r
for(j=last[stm]; j>i; j-= 2) p[j] = p[j-2];\r
- p[i].value = 10*list[n].value;\r
- for(j=0; j<8; j++) p[i].range[j] = list[n].range[j^4*(WHITE-stm)];\r
+ p[i].value = 10*list->value;\r
+ for(j=0; j<8; j++) p[i].range[j] = list->range[j^4*(WHITE-stm)];\r
switch(Range(p[i].range)) {\r
case 1: p[i].pst = BH; break;\r
case 2: p[i].pst = BSIZE; break;\r
default: p[i].pst = BSIZE + BH; break;\r
}\r
+ key = (stm == WHITE ? &list->whiteKey : &list->blackKey);\r
+ if(!*key) *key = ~(myRandom()*myRandom());\r
+ p[i].pieceKey = *key;\r
p[i].promoFlag = 0;\r
for(j=stm+2; j<= last[stm]; j+=2) {\r
if(p[j].promo >= i) p[j].promo += 2;\r
void\r
SetUp(char *array, PieceDesc *list)\r
{\r
- int i, j, k, k2, n, m, nr, color;\r
+ int i, j, n, m, nr, color;\r
char c, *q, name[3];\r
+ PieceDesc *p1, *p2;\r
last[WHITE] = 1; last[BLACK] = 0;\r
for(i=0; ; i++) {\r
//printf("next rank: %s\n", array);\r
name[0] += 'A' - 'a';\r
if(name[1]) name[1] += 'A' - 'a';\r
} else color = WHITE;\r
- k = LookUp(name, list);\r
- n = AddPiece(color, k, list);\r
+ p1 = LookUp(name, list);\r
+ n = AddPiece(color, p1);\r
p[n].pos = j;\r
- if(list[k].promoted[0]) {\r
- k2 = LookUp(list[k].promoted, list);\r
- m = AddPiece(color, k2, list);\r
+ if(p1->promoted[0]) {\r
+ p2 = LookUp(p1->promoted, list);\r
+ m = AddPiece(color, p2);\r
if(m <= n) n += 2;\r
p[n].promo = m;\r
- p[n].promoFlag = IsUpwardCompatible(list[k2].range, list[k].range) * DONT_DEFER + CAN_PROMOTE;\r
- if(Forward(list[k].range)) p[n].promoFlag |= LAST_RANK; // Pieces that only move forward can't defer on last rank\r
- if(!strcmp(list[k].name, "N")) p[n].promoFlag |= CANT_DEFER; // Knights can't defer on last 2 ranks\r
+ p[n].promoFlag = IsUpwardCompatible(p2->range, p1->range) * DONT_DEFER + CAN_PROMOTE;\r
+ if(Forward(p1->range)) p[n].promoFlag |= LAST_RANK; // Pieces that only move forward can't defer on last rank\r
+ if(!strcmp(p1->name, "N")) p[n].promoFlag |= CANT_DEFER; // Knights can't defer on last 2 ranks\r
p[n].promoFlag &= n&1 ? P_WHITE : P_BLACK;\r
p[m].promo = -1;\r
p[m].pos = ABSENT;\r
\r
// hash key tables\r
for(i=0; i<BSIZE; i++) squareKey[i] = ~(myRandom()*myRandom());\r
- for(i=0; i<NPIECES; i++) p[i].pieceKey = ~(myRandom()*myRandom());\r
\r
// board edge\r
for(i=0; i<BSIZE + 11*BW + 6; i++) rawBoard[i] = EDGE;\r
}\r
#endif\r
\r
+int flag;\r
+\r
inline int\r
NewNonCapture (int x, int y, int promoFlags)\r
{\r
if(board[y] != EMPTY) return 1; // edge, capture or own piece\r
+//if(flag) printf("# add %c%d%c%d, pf=%d\n", x%BW+'a',x/BW,y%BW+'a',y/BW, promoFlags);\r
if( (promoBoard[x] | promoBoard[y]) & promoFlags) { // piece can promote with this move\r
moveStack[msp++] = moveStack[nonCapts]; // create space for promotion\r
moveStack[nonCapts++] = x<<SQLEN | y | PROMOTE; // push promotion\r
}\r
} else\r
moveStack[msp++] = x<<SQLEN | y; // push normal move\r
+//if(flag) printf("msp=%d nc=%d\n", msp, nonCapts); \r
return 0;\r
}\r
\r
u->piece = board[u->from];\r
board[u->from] = EMPTY;\r
u->booty = 0;\r
+ u->savKeyL = hashKeyL;\r
+ u->savKeyH = hashKeyH;\r
\r
if(m & (PROMOTE | DEFER)) {\r
if(m & DEFER) {\r
p[u->new].pos = ABSENT; \r
p[u->piece].pos = u->from; // this can be the same as above\r
board[u->from] = u->piece;\r
+\r
+ hashKeyL = u->savKeyL;\r
+ hashKeyH = u->savKeyH;\r
}\r
\r
void\r
return 0;\r
}\r
\r
-int flag;\r
-\r
int\r
Search (int alpha, int beta, int difEval, int depth, int oldPromo, int promoSuppress)\r
{\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 myPV = pvPtr;\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
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
+ nodes++;\r
+ pv[pvPtr++] = 0; // start empty PV, directly behind PV of parent\r
+\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
#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
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
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
attacks -= 2*BSIZE;\r
level--;\r
UnMake(&tb);\r
- hashKeyL = savHashL;\r
- hashKeyH = savHashH;\r
xstm = stm; stm ^= WHITE;\r
#if 1\r
if(PATH) printf("%d:%2d:%d %3d %6x %-10s %6d %6d\n", level, depth, iterDep, curMove, moveStack[curMove], MoveToText(moveStack[curMove], 0), score, bestScore);\r
// 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
// update killer\r
goto cutoff;\r
}\r
+ { int i=pvPtr;\r
+ for(pvPtr = myPV+1; pv[pvPtr++] = pv[i++]; ); // copy daughter PV\r
+ pv[myPV] = move; // behind our move (pvPtr left at end of copy)\r
+ }\r
}\r
\r
}\r
#endif\r
} // next move\r
cutoff:\r
+ if(!level) { // root node\r
+ if(GetTickCount() - startTime > tlim1) break; // do not start iteration we can (most likely) not finish\r
+ }\r
replyDep = iterDep;\r
} while(++iterDep <= depth); // next depth\r
retMSP = msp;\r
retFirst = firstMove;\r
- msp = oldMSP;\r
+ msp = oldMSP; // pop move list\r
+ pvPtr = myPV; // pop PV\r
retMove = bestMoveNr ? moveStack[bestMoveNr] : 0;\r
if(flag && depth >= 0) printf("return %d: %d %d\n", depth, bestScore, curEval);\r
return bestScore + (bestScore < curEval);\r
{\r
sup0 = sup1; sup1 = sup2;\r
sup2 = MakeMove(move, &undoInfo);\r
+ rootEval = -rootEval - undoInfo.booty;\r
return stm ^ WHITE;\r
}\r
\r
void\r
UnMake2 (MOVE move)\r
{\r
+ rootEval = -rootEval - undoInfo.booty;\r
UnMake(&undoInfo);\r
sup2 = sup1; sup1 = sup0;\r
}\r
{\r
SetUp(chuArray, chuPieces);\r
sup0 = sup1 = sup2 = ABSENT;\r
+ rootEval = hashKeyH = hashKeyL = 0;\r
return WHITE;\r
}\r
\r
if(i>=retMSP) { // no exact match\r
if(deferred) { // but maybe non-sensical deferral\r
int flags = p[board[f]].promoFlag;\r
+printf("# deferral of %d\n", deferred);\r
i = deferred; // in any case we take that move\r
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
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
int\r
SearchBestMove (int stm, int timeLeft, int mps, int timeControl, int inc, int timePerMove, MOVE *move, MOVE *ponderMove)\r
{\r
- int score;\r
+ int score, targetTime, movesLeft = 50;\r
+ if(mps) movesLeft = mps - (moveNr>>1)%mps;\r
+ targetTime = timeLeft*10 / (movesLeft + 1) + 1000 * inc;\r
+ if(timePerMove > 0) targetTime = timeLeft * 5;\r
+ startTime = GetTickCount();\r
+ tlim1 = 0.3*targetTime;\r
+ tlim2 = 1.9*targetTime;\r
+ nodes = 0;\r
MapFromScratch(attacks);\r
- score = Search(-INF-1, INF+1, 0, 3, sup1, sup2);\r
+ score = Search(-INF-1, INF+1, rootEval, 20, sup1, sup2);\r
*move = retMove;\r
*ponderMove = INVALID;\r
return score;\r