LCOV - code coverage report
Current view: top level - Source - opponent.cpp (source / functions) Coverage Total Hit
Test: coverage Lines: 38.7 % 31 12
Test Date: 2026-03-02 16:42:41 Functions: 66.7 % 3 2

            Line data    Source code
       1              : #include "opponent.hpp"
       2              : 
       3              : #include "definition.hpp"
       4              : #include "dynamicConfig.hpp"
       5              : #include "tools.hpp"
       6              : 
       7              : namespace Opponent {
       8              : 
       9              : static std::map<std::string, uint16_t> ratings = {
      10              : {"Stockfish", 3550 }, 
      11              : {"Dragon", 3528 }, 
      12              : {"Berserk", 3491 }, 
      13              : {"Chess", 3488 }, 
      14              : {"Ethereal", 3487 }, 
      15              : {"Igel", 3487 }, 
      16              : {"RubiChess", 3474 }, 
      17              : {"Koivisto", 3470 }, 
      18              : {"Revenge", 3470 }, 
      19              : {"Sjeng", 3461 }, 
      20              : {"SlowChess", 3460 }, 
      21              : {"Clover", 3458 }, 
      22              : {"Rebel", 3457 }, 
      23              : {"Seer", 3450 }, 
      24              : {"RofChade", 3443 }, 
      25              : {"Caissa", 3442 }, 
      26              : {"Minic", 3441 }, 
      27              : {"Viridithas", 3440 }, 
      28              : {"Uralochka", 3431 }, 
      29              : {"Arasan", 3419 }, 
      30              : {"Fire", 3419 }, 
      31              : {"Ginkgo", 3403 }, 
      32              : {"Wasp", 3402 }, 
      33              : {"Velvet", 3399 }, 
      34              : {"Booot", 3396 }, 
      35              : {"Marlin", 3389 }, 
      36              : {"Halogen", 3382 }, 
      37              : {"Houdini", 3382 }, 
      38              : {"Nemorino", 3380 }, 
      39              : {"Marvin", 3374 }, 
      40              : {"Smallbrain", 3374 }, 
      41              : {"Lc0", 3370 }, 
      42              : {"BlackCore", 3365 }, 
      43              : {"Superultra", 3361 }, 
      44              : {"Tucano", 3356 }, 
      45              : {"Rice", 3333 }, 
      46              : {"Fritz", 3328 }, 
      47              : {"Xiphos", 3318 }, 
      48              : {"Alexandria", 3317 }, 
      49              : {"Devre", 3315 }, 
      50              : {"Mantissa", 3314 }, 
      51              : {"Weiss", 3314 }, 
      52              : {"Zahak", 3311 }, 
      53              : {"Expositor", 3310 }, 
      54              : {"Combusken", 3305 }, 
      55              : {"Counter", 3305 }, 
      56              : {"Stash", 3296 }, 
      57              : {"HIARCS", 3289 }, 
      58              : {"Frozenight", 3275 }, 
      59              : {"Laser", 3274 }, 
      60              : {"Winter", 3272 }, 
      61              : {"Defenchess", 3268 }, 
      62              : {"Shredder", 3267 }, 
      63              : {"Pawn", 3264 }, 
      64              : {"Fizbo", 3259 }, 
      65              : {"Drofa", 3257 }, 
      66              : {"Andscacs", 3251 }, 
      67              : {"StockDory", 3250 }
      68              : };
      69              : 
      70            0 : std::string strToLower(std::string s) {
      71            0 :    std::transform(s.begin(), s.end(), s.begin(), [](uint8_t c) { return std::tolower(c); });
      72            0 :    return s;
      73              : }
      74              : 
      75            3 : void init() {
      76              :    // restore default parameter
      77            3 :    DynamicConfig::ratingFactor = 1.; // this means "full" contempt given by user
      78              : 
      79            3 :    const int myRating = ratings["Minic"];
      80              : 
      81              :    // this TCEC specific option is prefered when present
      82            3 :    if (DynamicConfig::ratingAdvReceived) {
      83            2 :       Logging::LogIt(Logging::logInfo) << "Using ratingAdv...";
      84              :       // ratingFactor will go from -2 to 2
      85            2 :       const double x = 1. / (1 - DynamicConfig::ratingAdv / static_cast<double>(myRating));
      86            2 :       DynamicConfig::ratingFactor = 2 * std::tanh((x - 1) * 20.);
      87              :    }
      88              :    // else rely on the UCI_Opponent string
      89              :    else {
      90            1 :       Logging::LogIt(Logging::logInfo) << "No ratingAdv given (TCEC specific option)...";
      91              : 
      92            2 :       if (DynamicConfig::opponent.empty()) { Logging::LogIt(Logging::logInfo) << "No opponent string given ..."; }
      93              :       else {
      94            0 :          Logging::LogIt(Logging::logInfo) << "Opponent string received: \"" << DynamicConfig::opponent << "\"";
      95            0 :          std::vector<std::string> tokens;
      96            0 :          tokenize(DynamicConfig::opponent, tokens);
      97              :          //Logging::LogIt(Logging::logInfo) << "nb token " << tokens.size();
      98            0 :          if (tokens.size() >= 4) {
      99              :             std::string       oppName      = tokens[3];
     100              :             const std::string oppRatingStr = tokens[1];
     101              :             int               oppRating    = 0;
     102              :             bool              ratingFound  = false;
     103              : 
     104              : /*
     105              :             if ( oppRatingStr != "none" ){
     106              :                const int oppRatingRead = std::atoi(oppRatingStr.c_str());
     107              :                if ( oppRatingRead ){
     108              :                      oppRating = oppRatingRead;
     109              :                      Logging::LogIt(Logging::logInfo) << "Opponent rating is " << oppRating;
     110              :                      ratingFound = true;
     111              :                }
     112              :             }
     113              : */
     114              : 
     115              :             if (!ratingFound) {
     116              :                //Logging::LogIt(Logging::logWarn) << "No rating given";
     117              :                unsigned int i = 4;
     118            0 :                while (i < tokens.size()) oppName += " " + tokens[i++];
     119            0 :                oppName = strToLower(oppName);
     120            0 :                Logging::LogIt(Logging::logInfo) << "Looking for Elo rating of " << oppName;
     121            0 :                for (const auto & it : ratings) {
     122            0 :                   if ((oppName.find(strToLower(it.first)) != std::string::npos) || (strToLower(it.first).find(oppName) != std::string::npos)) {
     123            0 :                      oppRating = it.second;
     124            0 :                      Logging::LogIt(Logging::logInfo) << "Opponent is " << it.first << ", Elo " << oppRating;
     125              :                      ratingFound = true;
     126            0 :                      break;
     127              :                   }
     128              :                }
     129            0 :                if (!ratingFound) Logging::LogIt(Logging::logWarn) << "Unknown opponent";
     130              :             }
     131              : 
     132              :             if (ratingFound) {
     133              :                // ratingFactor will go from -2 to 2
     134            0 :                DynamicConfig::ratingFactor = 2 * std::tanh((myRating / static_cast<double>(oppRating) - 1) * 20.);
     135              :             }
     136              :          }
     137              :          else {
     138            0 :             Logging::LogIt(Logging::logWarn) << "Invalid opponent string";
     139              :          }
     140              : 
     141              :          ///@todo play with style also ?
     142            0 :       }
     143              :    }
     144              : 
     145            3 :    Logging::LogIt(Logging::logInfo) << "Rating factor set to: " << DynamicConfig::ratingFactor;
     146            3 : }
     147              : 
     148            1 : void ratingReceived() { DynamicConfig::ratingAdvReceived = true; }
     149              : 
     150              : } // namespace Opponent
        

Generated by: LCOV version 2.0-1