1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
use crate::bitboard::*;
use crate::constants::*;
use crate::lineargame::*;
use crate::piece::*;
use crate::square::*;
//use crate::state::*;

use std::io::{self, BufRead};

pub struct Uci {
    pub engine_name: String,
    pub engine_author: String,
    pub linear_game: LinearGame,
}

pub fn create_default_uci() -> Uci {
    let mut uci = Uci {
        engine_name: "rustengine".to_string(),
        engine_author: "easychessanimations".to_string(),
        linear_game: LinearGame::new(),
    };
    uci.linear_game.init(DEFAULT_VARIANT);
    uci
}

pub fn demo() {
    let x: Bitboard = 0xffff00000000ffff;

    println!("{}", x.pretty_print_string());

    let sq: Square = rank_file(RANK_3, FILE_D);

    println!("square {} file {} rank {}", sq.uci(), sq.file(), sq.rank());

    let fig: Figure = LANCERNE;

    println!("\nfigure {} symbol {}", fig, fig.symbol());

    let p: Piece = color_figure(WHITE, LANCERNE);

    println!(
        "\npiece {} fen symbol {} san symbol {} uci symbol {} san letter {}",
        p,
        p.fen_symbol(),
        p.san_symbol(),
        p.uci_symbol(),
        p.san_letter()
    );

    let mut bb: Bitboard = sq.bitboard() | SQUARE_G6.bitboard();

    loop {
        println!("\n{}", bb.pretty_print_string());

        let (sq, ok) = bb.pop_square();

        if ok {
            println!("{}", sq.uci());
        } else {
            println!("no square could be popped\n");
            break;
        }
    }

    println!(
        "{}",
        jump_attack(SQUARE_E4, &KNIGHT_DELTAS, SQUARE_F6.bitboard()).pretty_print_string()
    );

    println!(
        "{}",
        sliding_attack(SQUARE_E4, &QUEEN_DELTAS, SQUARE_G6.bitboard()).pretty_print_string()
    );

    println!("{}", BISHOP_ATTACK[SQUARE_C7].pretty_print_string());

    println!("{}", KING_AREA[SQUARE_G8].pretty_print_string());
}

pub fn enum_occup_demo() {
    let occup = BISHOP_ATTACK[SQUARE_C7];

    let mut mask: usize = 0;

    loop {
        if mask < occup.variation_count() {
            println!(
                "{}\n{}",
                mask,
                translate_mask_to_occupancy(mask, occup).pretty_print_string()
            );
            mask += 1;
        } else {
            break;
        }
    }
}

pub fn mobility_demo() {
    println!(
        "{}",
        queen_mobility(
            SQUARE_E4,
            MoveGenMode::All,
            SQUARE_G6.bitboard() | SQUARE_C4.bitboard(),
            SQUARE_D5.bitboard() | SQUARE_E7.bitboard()
        )
        .pretty_print_string()
    )
}

pub fn magic_space() {
    let tb = total_magic_space(BISHOP_MAGICS);

    let sb = tb * std::mem::size_of::<Bitboard>();

    println!("total bishop magic space {} bytes", sb);

    let tr = total_magic_space(ROOK_MAGICS);

    let sr = tr * std::mem::size_of::<Bitboard>();

    println!("total rook magic space {} bytes", sr);

    println!(
        "\ngrand total magic space {} bytes = {:0.2} MiBs",
        sb + sr,
        (sb + sr) as f32 / 1e6
    );

    println!("magic units bishop {} rook {} total {}", tb, tr, tb + tr);
}

impl Uci {
    pub fn execute_uci_command(&self) {
        println!("id name {}", self.engine_name);
        println!("id author {}\n", self.engine_author);
        println!("uciok");
    }

    pub fn process_uci_command(&mut self, line: String) -> bool {
        let parts: Vec<&str> = line.split(" ").collect();

        let command = parts[0];

        if command == "quit" || command == "q" || command == "exit" || command == "x" {
            return false;
        }

        if command == "uci" {
            self.execute_uci_command();
            return true;
        }

        if command == "i" {
            self.linear_game.print();
            return true;
        }

        if command == "demo" {
            let mut arg = "";

            if parts.len() > 0 {
                arg = parts[1];
            }

            match arg {
                "occup" => enum_occup_demo(),
                "mob" => mobility_demo(),
                "space" => magic_space(),
                _ => demo(),
            }

            return true;
        }

        if command == "perft" || command == "p" {
            let depth = if command == "p" {
                4
            } else {
                parts[1].parse().expect("illegal perft depth")
            };
            let perft_result = self.linear_game.perft(depth);

            println!(
                "node(s) {:?} , time {:.2} sec(s) , nps {} kNode(s)/sec",
                perft_result.0, perft_result.1, perft_result.2
            );
        }

        if command == "bb" {
            self.linear_game.current().print_bitboards();

            return true;
        }

        if command == "d" {
            self.linear_game.pop();
            self.linear_game.print();
        }

        match command.parse::<usize>() {
            Ok(n) => {
                if n > 0 {
                    self.linear_game.push_by_index(n - 1);
                    self.linear_game.print();
                }
                return true;
            }
            Err(_) => (),
        }

        true
    }

    pub fn welcome(&self, build_info: &str) {
        println!(
            "{} bitboard multi variant uci chess analysis engine by {} [ {} ]",
            self.engine_name, self.engine_author, build_info
        );
    }

    pub fn uci_loop(&mut self) {
        let stdin = io::stdin();

        for line in stdin.lock().lines() {
            if !self.process_uci_command(line.unwrap()) {
                break;
            }
        }
    }
}