+ }
+
+
+ // When playing with strength handicap choose best move among the MultiPV set
+ // using a statistical rule dependent on SkillLevel. Idea by Heinz van Saanen.
+ void do_skill_level(Move* best, Move* ponder) {
+
+ assert(MultiPV > 1);
+
+ // Rml list is already sorted by pv_score in descending order
+ int s;
+ int max_s = -VALUE_INFINITE;
+ int size = Min(MultiPV, (int)Rml.size());
+ int max = Rml[0].pv_score;
+ int var = Min(max - Rml[size - 1].pv_score, PawnValueMidgame);
+ int wk = 120 - 2 * SkillLevel;
+
+ // PRNG sequence should be non deterministic
+ for (int i = abs(get_system_time() % 50); i > 0; i--)
+ RK.rand<unsigned>();
+
+ // Choose best move. For each move's score we add two terms both dependent
+ // on wk, one deterministic and bigger for weaker moves, and one random,
+ // then we choose the move with the resulting highest score.
+ for (int i = 0; i < size; i++)
+ {
+ s = Rml[i].pv_score;
+
+ // Don't allow crazy blunders even at very low skills
+ if (i > 0 && Rml[i-1].pv_score > s + EasyMoveMargin)
+ break;
+
+ // This is our magical formula
+ s += ((max - s) * wk + var * (RK.rand<unsigned>() % wk)) / 128;
+
+ if (s > max_s)
+ {
+ max_s = s;
+ *best = Rml[i].pv[0];
+ *ponder = Rml[i].pv[1];
+ }
+ }