Skip to main content

timecat/utils/
pv_utils.rs

1use super::*;
2
3// TODO: Is is possible to change Vec<Move> to impl Iterator<Item = Move>?
4pub fn extract_pv_from_t_table(
5    position: &ChessPosition,
6    transposition_table: &TranspositionTable,
7) -> Vec<Move> {
8    let mut pv = Vec::new();
9    let best_move = transposition_table.read_best_move(position.get_hash());
10    if let Some(best_move) = best_move {
11        pv.push(best_move);
12        pv.append(&mut extract_pv_from_t_table(
13            &position.make_move_new(best_move),
14            transposition_table,
15        ));
16    }
17    pv
18}
19
20pub fn get_pv_as_uci(pv: &[Move]) -> String {
21    let mut pv_string = String::new();
22    for move_ in pv {
23        write_unchecked!(pv_string, "{} ", move_.uci());
24    }
25    pv_string.pop(); // Remove trailing space
26    pv_string
27}
28
29pub fn get_pv_as_algebraic(position: &ChessPosition, pv: &[Move], long: bool) -> String {
30    let mut position = position.clone();
31    let mut pv_string = String::new();
32    for move_ in pv {
33        pv_string += &if position.is_legal(move_) {
34            let (san, new_position) = move_.algebraic_and_new_position(&position, long).unwrap();
35            position = new_position;
36            san
37        } else {
38            move_.uci().colorize(ERROR_MESSAGE_STYLE).into()
39        };
40        pv_string.push(' ');
41    }
42    pv_string.pop(); // Remove trailing space
43    pv_string
44}
45
46#[inline]
47pub fn get_pv_as_san(position: &ChessPosition, pv: &[Move]) -> String {
48    get_pv_as_algebraic(position, pv, false)
49}
50
51#[inline]
52pub fn get_pv_as_lan(position: &ChessPosition, pv: &[Move]) -> String {
53    get_pv_as_algebraic(position, pv, true)
54}
55
56#[inline]
57pub fn get_pv_string(position: &ChessPosition, pv: &[Move]) -> String {
58    if GLOBAL_TIMECAT_STATE.is_in_console_mode() {
59        get_pv_as_algebraic(
60            position,
61            pv,
62            GLOBAL_TIMECAT_STATE.use_long_algebraic_notation(),
63        )
64    } else {
65        get_pv_as_uci(pv)
66    }
67}