use search::apply_move; use std::io::{self, BufRead}; use std::sync::mpsc::{Receiver, Sender}; use std::process::exit; use engine::{EngineMsg, InterfaceMsg, SearchInfo}; use game::{Game}; use std::thread::sleep_ms; use log::info; pub fn run(r: Receiver, s: Sender) { //unsafe { debug_log = Box::new(File::open("debug.log").unwrap()); } //unsafe { debug_log = Arc::new(Some(Mutex::new(File::create("~/debug.log").unwrap()))) }; let stdin = io::stdin(); for line_m in stdin.lock().lines() { let line = line_m.unwrap(); info!("received line: {}", line); let split = line.split_whitespace().collect::>(); run_command(split, &r, &s); } } fn run_command(mut cmd: Vec<&str>, r: &Receiver, s: &Sender) { if cmd.len() > 0 { let command = cmd[0]; cmd.drain(0..1); match command { "uci" => cmd_uci(cmd), "isready" => cmd_isready(cmd), "position" => cmd_position(cmd, r, s), "go" => cmd_go(cmd, r, s), "stop" => cmd_stop(cmd, r, s), "ucinewgame" => cmd_newgame(cmd, r, s), "quit" | "exit" => cmd_quit(cmd, r, s), cmd => { println!("unknown command: {}", cmd); } } } } fn cmd_uci(_args: Vec<&str>) { println!("id name bishop 1.0"); println!("id author Geile Siech"); println!("uciok"); } fn cmd_isready(_args: Vec<&str>) { println!("readyok"); } fn cmd_position(mut args: Vec<&str>, r: &Receiver, s: &Sender) { let position = args[0]; args.drain(0..1); let mut game = Game::default(); if position == "startpos" { } else if position == "fen" { let fen_parts: Vec<&str> = Vec::from(&args[0..6]).into_iter().collect::>(); let moves = &args[6..]; game = Game::from_fen(fen_parts.as_slice()).unwrap(); for mov in moves { let m = game.parse_move(mov); if let Ok(mm) = m { game = apply_move(&game, mm); } else { println!("error"); } } } s.send(EngineMsg::SetBoard(game)).unwrap(); } fn cmd_go(args: Vec<&str>, _r: &Receiver, s: &Sender) { s.send(EngineMsg::Search(SearchInfo::Movetime(1000))).unwrap(); } fn cmd_stop(_args: Vec<&str>, _r: &Receiver, s: &Sender) { s.send(EngineMsg::Stop).unwrap(); } fn cmd_newgame(_args: Vec<&str>, _r: &Receiver, s: &Sender) { s.send(EngineMsg::NewGame).unwrap(); } fn cmd_quit(_args: Vec<&str>, _r: &Receiver, _s: &Sender) { exit(0); }