int sBook::AddLineToGuideBook(sPosition *p, char *ptr) { char token[512]; UNDO u[1]; int move; int freq; int flagIsProblem = 0; SetPosition(p, START_POS); for (;;) { ptr = Parser.ParseToken(ptr, token); if (*token == '\0') break; move = StrToMove(p, token); if (IsLegal(p, move)) { // apply move frequency modifiers freq = 1; if (strstr(token, "?")) freq = -100; if (strstr(token, "!")) freq = +100; if (strstr(token, "??")) freq = -4900; if (strstr(token, "!!")) freq = +900; AddMoveToGuideBook( GetBookHash(p), move, freq); Manipulator.DoMove(p, move, u); } else { flagIsProblem = 1; break; }; if (p->reversibleMoves == 0) p->head = 0; } return flagIsProblem; }
void ParseMoves(POS *p, char *ptr) { char token[180]; UNDO u[1]; for (;;) { // Get next move to parse ptr = ParseToken(ptr, token); // No more moves! if (*token == '\0') break; p->DoMove(StrToMove(p, token), u); // We won't be taking back moves beyond this point: if (p->rev_moves == 0) p->head = 0; } }
int sBook::GetPolyglotMove(sPosition *p, int printOutput) { int bestMove = 0; int bestScore = 0; int maxWeight = 0; int sumOfWeights = 0; int pos; polyglot_move entry[1]; int move; int score; int values[100]; U64 key = GetPolyglotKey(p); char moveString[6]; nOfChoices = 0; if (bookFile != NULL && bookSize != 0) { srand(Timer.GetMS() ); for (pos = FindPos(key); pos < bookSize; pos++) { ReadEntry(entry,pos); if (entry->key != key) break; move = entry->move; score = entry->weight; // ugly hack to convert polyglot move to a real one int fsq = Tsq(move); int tsq = Fsq(move); // correction for castling moves if (fsq == E1 && tsq == H1 && p->kingSquare[WHITE] == E1) tsq = G1; if (fsq == E8 && tsq == H8 && p->kingSquare[BLACK] == E8) tsq = G8; if (fsq == E1 && tsq == A1 && p->kingSquare[WHITE] == E1) tsq = C1; if (fsq == E8 && tsq == A8 && p->kingSquare[BLACK] == E8) tsq = C8; // now we want to get a move with full data, not only from and to squares int realMove = (tsq << 6) | fsq; MoveToStr(realMove, moveString); realMove = StrToMove(p, moveString); if (maxWeight < score) maxWeight = score; sumOfWeights += score; moves[nOfChoices] = realMove; values[nOfChoices] = score; nOfChoices++; } // pick a move, filtering out those with significantly lower weight for (int i = 0; i<nOfChoices; i++) { // report about possible choices and rejected moves if (values[i] > 1 || maxWeight == 1) { if (printOutput) { printf("info string "); PrintMove(moves[i]); printf(" %d %%", (values[i] * 100) / sumOfWeights ); if (IsInfrequent(values[i], maxWeight)) printf(" infrequent "); } } // shall we pick this move? if (!IsInfrequent(values[i], maxWeight)) { bestScore += values[i]; if (my_random(bestScore) < values[i]) bestMove = moves[i]; } printf("\n"); } } //if (printOutput) PrintMissingMoves(p); return bestMove; }