|
@@ -212,8 +212,10 @@ impl SearchControl {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- if Some(depth) == max_depth {
|
|
|
- break;
|
|
|
+ if let Some(d) = max_depth {
|
|
|
+ if depth >= d {
|
|
|
+ break;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -453,8 +455,7 @@ pub fn negamax(game: &mut Board, sc: &mut SearchControl, hash: &mut Cache, mut a
|
|
|
return alpha;
|
|
|
}
|
|
|
|
|
|
-fn quiescence_search(game: &mut Board, sc: &mut SearchControl, hash: &mut Cache, mut alpha: PosValue, beta: PosValue, depth: i32) -> PosValue {
|
|
|
-
|
|
|
+fn quiescence_search(board: &mut Board, sc: &mut SearchControl, hash: &mut Cache, mut alpha: PosValue, beta: PosValue, depth: i32) -> PosValue {
|
|
|
sc.nodes += 1;
|
|
|
if sc.nodes % 1024 == 0 {
|
|
|
if sc.check_stop() {
|
|
@@ -463,7 +464,7 @@ fn quiescence_search(game: &mut Board, sc: &mut SearchControl, hash: &mut Cache,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- let val = evaluate(game);
|
|
|
+ let val = evaluate(board);
|
|
|
if val >= beta {
|
|
|
return beta;
|
|
|
}
|
|
@@ -475,18 +476,39 @@ fn quiescence_search(game: &mut Board, sc: &mut SearchControl, hash: &mut Cache,
|
|
|
return alpha;
|
|
|
}
|
|
|
|
|
|
- if game.get_piece(KING, game.turn) == 0 { return checkmated(); }
|
|
|
+ if board.get_piece(KING, board.turn) == 0 { return checkmated(); }
|
|
|
|
|
|
//let mut moves = generate_legal_sorted_moves(game, hash, &[], None, true, game.turn);
|
|
|
- let mut moves = generate_legal_moves(game, game.turn, true);
|
|
|
+ let mut moves = generate_legal_moves(board, board.turn, true);
|
|
|
|
|
|
//sort_moves_no_hash(game, &mut moves);
|
|
|
- sort_moves_least_valuable_attacker(game, &mut moves);
|
|
|
+ sort_moves_least_valuable_attacker(board, &mut moves);
|
|
|
+
|
|
|
+ let apply_delta_pruning = game_lateness(board) < 50;
|
|
|
|
|
|
for mov in moves {
|
|
|
- let undo = game.apply(mov);
|
|
|
- let val = -quiescence_search(game, sc, hash, decrease_mate_in(-beta), decrease_mate_in(-alpha), depth - 1);
|
|
|
- game.undo_move(undo);
|
|
|
+
|
|
|
+ if apply_delta_pruning {
|
|
|
+ const PIECE_VALUES: [i32; 6] = [
|
|
|
+ 100, // Pawn
|
|
|
+ 220, // Knight
|
|
|
+ 320, // Bishop
|
|
|
+ 400, // Rook
|
|
|
+ 800, // Queen
|
|
|
+ 100000 // King
|
|
|
+ ];
|
|
|
+ const SAFETY_MARGIN: i32 = 200;
|
|
|
+ let target_sq = mov.to_simple().to;
|
|
|
+ if let Some((piece_type, side)) = board.get_square(target_sq) {
|
|
|
+ if val + PIECE_VALUES[piece_type as usize] + SAFETY_MARGIN < alpha {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ let undo = board.apply(mov);
|
|
|
+ let val = -quiescence_search(board, sc, hash, decrease_mate_in(-beta), decrease_mate_in(-alpha), depth - 1);
|
|
|
+ board.undo_move(undo);
|
|
|
|
|
|
if sc.stopping {
|
|
|
return alpha
|