StefanSalewski / rust-chess

Port of salewski-chess from Nim to Rust
MIT License
0 stars 1 forks source link

Bug in abeta() for return with transposition table value #6

Open StefanSalewski opened 2 months ago

StefanSalewski commented 2 months ago

The following code is wrong, likely caused by bad cleanup attempts to save a single line. Function should only return in IF or ELSE branch, but not always (wrong zero default score.)

        for i in (depth_0..(MAX_DEPTH + 1) as usize).rev() {
            if hash_res.score[i].s != INVALID_SCORE {
                // we have the exact score, so return it
                if i == depth_0
                    || hash_res.score[i].s.abs() < KING_VALUE_DIV_2 as i16
                    || hash_res.score[i].s.abs() >= KING_VALUE as i16
                {
                    // use of deeper knowledge in endgame can give wrong moves to mate reports
                    // or generate repeated moves.
                    result.score = pmq(hash_res.score[i].s as i64, -cup);
                    result.src = hash_res.score[i].si as i64; // these details are currently only needed for cup == 0
                    result.dst = hash_res.score[i].di as i64;
                    result.promote_to = hash_res.score[i].promote_to as i64;
                    result.state = hash_res.state;
                    debug_inc(&mut g.score_hash_succ);
                } else if pmq(hash_res.score[i].s as i64, -cup) >= beta {
                    // at least we can use the score for a beta cutoff
                    result.score = beta;
                }
                return result;
            }
StefanSalewski commented 2 months ago

Fixed.