inline void TextIO::safeSetPiece(Position& pos, int col, int row, int p) { if (col > 7) throw ChessParseError("Too many columns"); if ((p == Piece::WPAWN) || (p == Piece::BPAWN)) if ((row == 0) || (row == 7)) throw ChessParseError("Pawn on first/last rank"); pos.setPiece(Position::getSquare(col, row), p); }
void TUIGame::handleTestSuite(const std::string& cmd) { std::ifstream fr; int lineNo = -1; try { size_t idx = cmd.find_first_of(' '); if (idx == cmd.npos) return; std::string filename(cmd.substr(0, idx)); std::string timeStr(cmd.substr(idx + 1)); int timeLimit; if (!str2Num(timeStr, timeLimit)) { std::cout << "Error parsing number: " << timeStr << std::endl; return; } // std::cout << "file:" << filename << " time:" << timeStr << " (" << timeLimit << ")" << std::endl; fr.open(filename.c_str()); Player& pl = whitePlayer->isHumanPlayer() ? *blackPlayer : *whitePlayer; if (pl.isHumanPlayer()) { std::cout << "No computer player available" << std::endl; return; } ComputerPlayer& cp = static_cast<ComputerPlayer&>(pl); int numRight = 0; int numTotal = 0; std::string line; lineNo = 0; while (getline(fr, line).good()) { lineNo++; if (startsWith(line, "#") || (line.length() == 0)) continue; size_t idx1 = line.find(" bm "); if (idx1 == line.npos) { std::cout << "Parse error, line:" << lineNo << std::endl; return; } std::string fen = line.substr(0, idx1); size_t idx2 = line.find(";", idx1); if (idx2 == line.npos) { std::cout << "Parse error, line:" << lineNo << std::endl; return; } std::string bm = line.substr(idx1+4, idx2 - (idx1+4)); // std::cout << "Line " << std::setw(3) << lineNo << ": fen:" << fen << " bm:" << bm << std::endl; Position testPos = TextIO::readFEN(fen); cp.clearTT(); std::pair<Move, std::string> ret = cp.searchPosition(testPos, timeLimit); Move sm = ret.first; std::string PV = ret.second; Move m(sm); std::vector<std::string> answers; splitString(bm, answers); bool correct = false; for (size_t i = 0; i < answers.size(); i++) { const std::string& a = answers[i]; Move am(TextIO::stringToMove(testPos, a)); if (am.isEmpty()) throw ChessParseError("Invalid move " + a); if (am.equals(m)) { correct = true; break; } } if (correct) numRight++; numTotal++; std::cout << std::setw(3) << lineNo << ' ' << std::setw(6) << TextIO::moveToString(testPos, sm, false) << ' ' << std::setw(6) << sm.score() << ' ' << (correct ? 1 : 0) << ' ' << std::setw(3) << numRight << '/' << std::setw(3) << numTotal << ' ' << bm << " : " << PV << std::endl; } fr.close(); } catch (const std::ifstream::failure& ex) { std::cout << "IO error: " << ex.what() << std::endl; } catch (const ChessParseError& cpe) { std::cout << "Parse error, line " << lineNo << ": " << cpe.what() << std::endl; } }