- // Complete the process of switching sides. The first line swaps
- // all negative numbers with non-negative numbers. The compiler
- // probably knows that it is just the bitwise negation ~balance.
- balance = -balance-1;
- stm = ~stm;
+ // If balance is still non-negative after giving away nextVictim then we
+ // win. The only thing to be careful about it is that we should revert
+ // stm if we captured with the king when the opponent still has attackers.
+ if (balance >= VALUE_ZERO)
+ {
+ if (nextVictim == KING && (attackers & pieces(stm)))
+ stm = ~stm;
+ break;
+ }
+ assert(nextVictim != KING);