aboutsummaryrefslogtreecommitdiff
path: root/src/grossmeister/UCI.rs
blob: 2c09a7b0911304f876ee0ded021207ec4ca14130 (plain)
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
use std::{io::stdin, thread::Builder};

use crate::{board::{Board, io::IO}, moves::Move, player::Player};

use super::Grossmeister;

const NAME: &str = "Chessnost";
const AUTHOR: &str = "Eugene Sokolov";

impl Grossmeister {
    pub fn start(&mut self) {
        let mut board = Board::new();
        let mut handle = None;

        loop {
            let mut cmd = String::new();
            stdin().read_line(&mut cmd).unwrap();
            let tokens: Vec<&str> = cmd.trim().split(' ').collect();
            println!("Command: {:?}, tokens: {:?}", cmd, tokens);

            match tokens[0] {
                "uci" => {
                    println!("id name {}", NAME);
                    println!("id author {}", AUTHOR);
                    println!("uciok");
                }
                "isready" => {
                    println!("readyok");
                }
                "position" => {
                    match tokens[1] {
                        "startpos" => {
                            board = Board::new();
                        }
                        _ => {
                            todo!("Parse FEN")
                        }
                    }
                    assert!(tokens[2] == "moves");
                    for move_token in tokens.iter().skip(3) {
                        let mov = Move::from_notation(move_token.chars());
                        board.make_move(mov);
                        println!("{:?}", mov);
                        board.print();
                    }
                }
                "go" => {
                    match tokens[1] {
                        "infinite" => {
                            let mut gm = self.clone();
                            let builder = Builder::new();

                            handle = Some(builder.spawn(move || {
                                gm.analyze(board);
                            }).unwrap());
                        },
                        _ => todo!()
                    }
                },
                "stop" => {
                    self.should_halt.store(true, std::sync::atomic::Ordering::Relaxed);
                    if let Some(hand) = handle.take() {
                        hand.join().unwrap();
                    }
                    self.should_halt.store(false, std::sync::atomic::Ordering::Relaxed);
                }
                _ => {},
            }
        }
    }
}