Spaces:
Runtime error
Runtime error
File size: 4,724 Bytes
3014f14 | 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 | // uci.rs
use crate::position::Position;
use crate::types::{Move, Square, PieceType};
use crate::search::Searcher;
use crate::timeman::TimeManager;
use crate::perft::divide;
pub fn handle_command(line: &str, pos: &mut Position, searcher: &mut Searcher) {
let tokens: Vec<&str> = line.split_whitespace().collect();
if tokens.is_empty() { return; }
match tokens[0] {
"uci" => {
println!("id name VeloCT Ultimate");
println!("id author Vasireddy Tejdeep");
println!("uciok");
}
"isready" => {
println!("readyok");
}
"ucinewgame" => {
searcher.tt.clear();
pos.set_fen("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
}
"position" => {
if tokens.len() > 1 && tokens[1] == "startpos" {
pos.set_fen("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
if let Some(idx) = tokens.iter().position(|&x| x == "moves") {
parse_and_do_moves(pos, &tokens[idx+1..]);
}
} else if tokens.len() > 1 && tokens[1] == "fen" {
let fen_end = tokens.iter().position(|&x| x == "moves").unwrap_or(tokens.len());
if fen_end > 2 {
let fen_parts = tokens[2..fen_end].join(" ");
pos.set_fen(&fen_parts);
}
if fen_end < tokens.len() {
parse_and_do_moves(pos, &tokens[fen_end+1..]);
}
}
}
"go" => {
let mut wtime = 300000;
let mut btime = 300000;
let mut winc = 0;
let mut binc = 0;
let mut depth = 6;
let mut i = 1;
while i < tokens.len() {
match tokens[i] {
"wtime" => if let Ok(v) = tokens[i+1].parse() { wtime = v; i += 1; },
"btime" => if let Ok(v) = tokens[i+1].parse() { btime = v; i += 1; },
"winc" => if let Ok(v) = tokens[i+1].parse() { winc = v; i += 1; },
"binc" => if let Ok(v) = tokens[i+1].parse() { binc = v; i += 1; },
"depth" => if let Ok(v) = tokens[i+1].parse() { depth = v; i += 1; },
"perft" => {
if let Ok(v) = tokens[i+1].parse() {
divide(pos, v);
return;
}
}
_ => {}
}
i += 1;
}
let mut tm = TimeManager::new(wtime, btime, winc, binc, pos.side_to_move);
let best = searcher.search_best(pos, &mut tm, depth);
if best != Move::NONE {
println!("bestmove {}{}{}", sq_to_str(best.from()), sq_to_str(best.to()), promo_to_str(best.promotion()));
} else {
println!("bestmove 0000");
}
}
_ => {}
}
}
fn parse_and_do_moves(pos: &mut Position, moves: &[&str]) {
for m_str in moves {
if m_str.len() < 4 { continue; }
let f_file = (m_str.as_bytes()[0] - b'a') as u8;
let f_rank = (m_str.as_bytes()[1] - b'1') as u8;
let t_file = (m_str.as_bytes()[2] - b'a') as u8;
let t_rank = (m_str.as_bytes()[3] - b'1') as u8;
let from = Square::from_u8(f_rank * 8 + f_file);
let to = Square::from_u8(t_rank * 8 + t_file);
let mut promo = PieceType::None;
if m_str.len() == 5 {
promo = match m_str.as_bytes()[4] {
b'q' => PieceType::Queen,
b'r' => PieceType::Rook,
b'b' => PieceType::Bishop,
b'n' => PieceType::Knight,
_ => PieceType::None,
};
}
let target_move = Move::new(from, to, promo);
let mut pseudo_legal = Vec::new();
crate::movegen::generate_moves(pos, &mut pseudo_legal);
for m in pseudo_legal {
if m.from() == target_move.from() && m.to() == target_move.to() && m.promotion() == target_move.promotion() {
pos.do_move(m);
break;
}
}
}
}
pub fn sq_to_str(sq: Square) -> String {
if sq == Square::None { return "-".to_string(); }
let f = (sq as u8) % 8;
let r = (sq as u8) / 8;
format!("{}{}", (b'a' + f) as char, (b'1' + r) as char)
}
fn promo_to_str(p: PieceType) -> &'static str {
match p {
PieceType::Queen => "q",
PieceType::Rook => "r",
PieceType::Bishop => "b",
PieceType::Knight => "n",
_ => "",
}
}
|