use std::io::{self, BufRead}; use std::sync::mpsc::{Receiver, Sender}; use std::process::exit; use engine::{EngineMsg, InterfaceMsg}; use game::{Game}; use std::fs::File; use std::io::Write; use std::cell::RefCell; thread_local!(static debug_log: RefCell = RefCell::new(File::create("debug.log").unwrap())); pub fn run(r: Receiver, s: Sender) { //unsafe { debug_log = Box::new(File::open("debug.log").unwrap()); } let stdin = io::stdin(); for line_m in stdin.lock().lines() { let line = line_m.unwrap(); debug_log.with(|fc| writeln!(fc.borrow_mut(), "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), "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 = Vec::from(&args[0..6]).into_iter().map(|x| x.to_owned() + " ").collect::>(); debug_log.with(|fc| writeln!(fc.borrow_mut(), "fen string is: {:?}", fen_parts) ); // "3N4/5P2/2K1P1pP/p2PNn2/1p3r2/2P4P/4k2B/8 w - - 0 1" } s.send(EngineMsg::SetBoard(game)).unwrap(); } fn cmd_go(_args: Vec<&str>, _r: &Receiver, s: &Sender) { s.send(EngineMsg::Search(3)).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); }