- this.play(moves2[j]);
- let evalPos = this.atLeastOneMove()
- ? this.evalPosition("yes")
- : (this.checkGameEnd()=="1/2" ? 0 : (this.turn=="w"?-maxeval:maxeval));
- if ((color == "w" && evalPos < eval2) || (color=="b" && evalPos > eval2))
- eval2 = evalPos;
- this.undo(moves2[j]);
+ eval2 = (color=="w" ? 1 : -1) * maxeval; //initialized with checkmate value
+ // Second half-move:
+ let moves2 = this.getAllValidMoves();
+ for (let j=0; j<moves2.length; j++)
+ {
+ this.play(moves2[j]);
+ let evalPos = undefined;
+ if (this.atLeastOneMove())
+ evalPos = this.evalPosition()
+ else
+ {
+ // Work with scores for Loser variant
+ const score = this.checkGameEnd();
+ evalPos = (score=="1/2" ? 0 : (score=="1-0" ? 1 : -1) * maxeval);
+ }
+ if ((color == "w" && evalPos < eval2) || (color=="b" && evalPos > eval2))
+ eval2 = evalPos;
+ this.undo(moves2[j]);
+ }
+ }
+ else
+ {
+ const score = this.checkGameEnd();
+ eval2 = (score=="1/2" ? 0 : (score=="1-0" ? 1 : -1) * maxeval);