X-Git-Url: http://winboard.nl/cgi-bin?a=blobdiff_plain;f=hachu.c;h=a926e65f92fe0e16325ea63aed5d5673298bc5af;hb=44d0328d67fa43295e79fe4fc99c997453d8f450;hp=a81f25fcca8a9494ed18ffaaad11a49b7cf1f0c8;hpb=0eecd39628e105c45f9baf11bcb785315517404b;p=hachu.git diff --git a/hachu.c b/hachu.c index a81f25f..a926e65 100644 --- a/hachu.c +++ b/hachu.c @@ -1941,7 +1941,7 @@ typedef Move MOVE; void SetMemorySize(int n); // if n is different from last time, resize all tables to make memory usage below n MB char *MoveToText(MOVE move, int m); // converts the move from your internal format to text like e2e2, e1g1, a7a8q. MOVE ParseMove(char *moveText); // converts a long-algebraic text move to your internal move format - int SearchBestMove(int stm, int timeLeft, int mps, int timeControl, int inc, int timePerMove, MOVE *move, MOVE *ponderMove); + int SearchBestMove(MOVE *move, MOVE *ponderMove); void PonderUntilInput(int stm); // Search current position for stm, deepening forever until there is input. UndoInfo undoInfo; @@ -2091,7 +2091,7 @@ MapFromScratch(attacks); postThinking--; repCnt = 0; tlim1 = tlim2 = 1e8; msp = 0; Search(-INF-1, INF+1, 0, 1, sup1 & ~PROMOTE, sup2); postThinking++; - listStart = retFirst; listEnd = retMSP; + listStart = retFirst; listEnd = msp = retMSP; } MOVE @@ -2116,7 +2116,7 @@ ParseMove (char *moveText) ret = f<> SQLEN & SQUARE)) break; // any null move matches @@@@ @@ -2157,7 +2157,7 @@ Highlight(char *coords) char b[BSIZE], buf[2000], *q; for(i=0; i>SQLEN & SQUARE)) { int t = moveStack[i] & SQUARE; @@ -2200,31 +2200,42 @@ Highlight(char *coords) printf("highlight %s\n", buf); } -int -SearchBestMove (int stm, int timeLeft, int mps, int timeControl, int inc, int timePerMove, MOVE *move, MOVE *ponderMove) +int timeLeft; // timeleft on engine's clock +int mps, timeControl, inc, timePerMove; // time-control parameters, to be used by Search +char inBuf[8000], command[80], ponderMoveText[20]; + +void +SetSearchTimes (int timeLeft) { - int score, targetTime, movesLeft = BW*BH/4 + 20; + int targetTime, movesLeft = BW*BH/4 + 20; if(mps) movesLeft = mps - (moveNr>>1)%mps; - targetTime = timeLeft*10 / (movesLeft + 2) + 1000 * inc; + targetTime = (timeLeft - 1000*inc) / (movesLeft + 2) + 1000 * inc; if(moveNr < 30) targetTime *= 0.5 + moveNr/60.; // speedup in opening - if(timePerMove > 0) targetTime = timeLeft * 5; - startTime = GetTickCount(); + if(timePerMove > 0) targetTime = 0.5*timeLeft, movesLeft = 1; tlim1 = 0.2*targetTime; tlim2 = 1.9*targetTime; +} + +int +SearchBestMove (MOVE *move, MOVE *ponderMove) +{ + int score; + startTime = GetTickCount(); nodes = 0; MapFromScratch(attacks); retMove = INVALID; repCnt = 0; score = Search(-INF-1, INF+1, rootEval, maxDepth, sup1, sup2); *move = retMove; - *ponderMove = INVALID; + *ponderMove = pv[1]; return score; } + void PonderUntilInput (int stm) { } - + int TakeBack(int n) { // reset the game and then replay it to the desired point int last, stm; @@ -2244,14 +2255,30 @@ printf("# setup done");fflush(stdout); else printf("0-1%s\n", tail); } + void GetLine(int root) + { + int i, c; + while(1) { + // wait for input, and read it until we have collected a complete line + for(i = 0; (inBuf[i] = c = getchar()) != '\n'; i++) if(c == EOF || i>7997) exit(0); + inBuf[i+1] = 0; + + // extract the first word + sscanf(inBuf, "%s", command); + if(!strcmp(command, "otim")) { continue; } // do not start pondering after receiving time commands, as move will follow immediately + if(!strcmp(command, "time")) { sscanf(inBuf, "time %d", &timeLeft); continue; } + if(!strcmp(command, "put")) { ReadSquare(inBuf+4, &lastPut); continue; } // ditto + if(!strcmp(command, ".")) { inBuf[0] = 0; return; } // ignore for now + if(!strcmp(command, "lift")) { inBuf[0] = 0; Highlight(inBuf+5); return; } // treat here + return; + } + } + main() { 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 - MOVE move, ponderMove; + MOVE move; int i, score, curVarNr; - char inBuf[8000], command[80]; Init(V_CHU); // Chu @@ -2259,10 +2286,12 @@ printf("# setup done");fflush(stdout); fflush(stdout); // make sure everything is printed before we do something that might take time + if(listEnd == 0) ListMoves(); // always maintain a list of legal moves in root position + 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); + score = SearchBestMove(&move, &ponderMove); if(move == INVALID) { // game apparently ended int kcapt = 0, xstm = stm ^ WHITE, king, k = p[king=royal[xstm]].pos; @@ -2285,6 +2314,7 @@ pboard(board); stm = MakeMove2(stm, move); // assumes MakeMove returns new side to move gameMove[moveNr++] = move; // remember game printf("move %s\n", MoveToText(move, 1)); + listEnd = 0; } } @@ -2304,22 +2334,14 @@ pboard(board); } } - noPonder: - // 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; - - // extract the first word - sscanf(inBuf, "%s", command); -printf("in: %s\n", command); + fflush(stdout); // make sure everything is printed before we do something that might take time + if(!*inBuf) GetLine(1); // takes care of time and otim commands // recognize the command,and execute it if(!strcmp(command, "quit")) { break; } // breaks out of infinite loop if(!strcmp(command, "force")) { engineSide = NONE; continue; } if(!strcmp(command, "analyze")) { engineSide = ANALYZE; continue; } if(!strcmp(command, "exit")) { engineSide = NONE; continue; } - if(!strcmp(command, "otim")) { goto noPonder; } // do not start pondering after receiving time commands, as move will follow immediately - if(!strcmp(command, "time")) { sscanf(inBuf, "time %d", &timeLeft); goto noPonder; } if(!strcmp(command, "level")) { int min, sec=0; sscanf(inBuf, "level %d %d %d", &mps, &min, &inc) == 3 || // if this does not work, it must be min:sec format @@ -2353,8 +2375,6 @@ printf("in: %s\n", command); if(!strcmp(command, "nopost")) { postThinking = OFF;continue; } if(!strcmp(command, "random")) { randomize = ON; continue; } if(!strcmp(command, "hint")) { if(ponderMove != INVALID) printf("Hint: %s\n", MoveToText(ponderMove, 0)); continue; } - if(!strcmp(command, "lift")) { Highlight(inBuf+5); continue; } - if(!strcmp(command, "put")) { ReadSquare(inBuf+4, &lastPut); continue; } if(!strcmp(command, "book")) { continue; } // non-standard commands if(!strcmp(command, "p")) { pboard(board); continue; } @@ -2380,11 +2400,12 @@ pboard(board); if(comp) PrintResult(stm, -INF); // against computer: claim } else { stm = MakeMove2(stm, move); - ponderMove = INVALID; + ponderMove = INVALID; listEnd = 0; gameMove[moveNr++] = move; // remember game } continue; } + listEnd = 0; if(!strcmp(command, "new")) { engineSide = BLACK; Init(V_CHESS); stm = Setup2(NULL); maxDepth = MAXPLY; randomize = OFF; curVarNr = comp = 0; continue;