@@ -173,18 +173,16 @@ class Engine {
173173      return 
174174    } 
175175
176-     // Handle  mate values properly instead  of converting to ±10000  
176+     // here,  mate is in the perspective  of white  
177177    let  mateIn : number  |  undefined  =  undefined 
178178    if  ( ! isNaN ( mate )  &&  isNaN ( cp ) )  { 
179-       // For mate scores, use a very high cp value for sorting but keep mate info 
180179      mateIn  =  mate 
181180      cp  =  mate  >  0  ? 10000  : - 10000 
182181    } 
183182
184183    /* 
185-       The Stockfish engine, by default, reports centipawn (CP) scores from White's perspective. 
186-       This means a positive CP indicates an advantage for White, while a negative CP indicates 
187-       an advantage for Black. 
184+       The Stockfish engine, by default, reports mate and centipawn (CP) scores from White's perspective. 
185+       This means a positive CP indicates an advantage for White, while a negative CP indicates an advantage for Black. 
188186
189187      However, when it's Black's turn to move, we want to interpret the CP score from Black's 
190188      perspective. To achieve this, we invert the sign of the CP score when it's Black's turn. 
@@ -225,9 +223,13 @@ class Engine {
225223        : cp  -  this . store [ depth ] . model_optimal_cp 
226224
227225      const  winrate  =  mateIn 
228-         ? mateIn  >  0 
229-           ? 1.0 
230-           : 0.0 
226+         ? mateIn  >  0  // if white has a mate in sight 
227+           ? isBlackTurn 
228+             ? 0.0  // black has no chance of winning if this move is played 
229+             : 1.0  // white has a 100% chance of winning if this move is played 
230+           : isBlackTurn 
231+             ? 1.0  // black has a 100% chance of winning if this move is played 
232+             : 0.0  // white has no chance of winning if this move is played 
231233        : cpToWinrate ( cp  *  ( isBlackTurn  ? - 1  : 1 ) ,  false ) 
232234
233235      if  ( ! this . store [ depth ] . winrate_vec )  { 
0 commit comments