interface.rs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. use std::io::{self, BufRead};
  2. use std::collections::BTreeMap;
  3. use std::sync::mpsc::{Receiver, Sender};
  4. use std::process::exit;
  5. use engine::{EngineMsg, InterfaceMsg, SearchInfo};
  6. use game::{Game};
  7. use log::info;
  8. pub fn run(r: Receiver<InterfaceMsg>, s: Sender<EngineMsg>) {
  9. //unsafe { debug_log = Box::new(File::open("debug.log").unwrap()); }
  10. //unsafe { debug_log = Arc::new(Some(Mutex::new(File::create("~/debug.log").unwrap()))) };
  11. let stdin = io::stdin();
  12. for line_m in stdin.lock().lines() {
  13. let line = line_m.unwrap();
  14. info!("received line: {}", line);
  15. let split = line.split_whitespace().collect::<Vec<&str>>();
  16. run_command(split, &r, &s);
  17. }
  18. }
  19. fn run_command(mut cmd: Vec<&str>, r: &Receiver<InterfaceMsg>, s: &Sender<EngineMsg>) {
  20. if cmd.len() > 0 {
  21. let command = cmd[0];
  22. cmd.drain(0..1);
  23. match command {
  24. "uci" => cmd_uci(cmd),
  25. "isready" => cmd_isready(cmd, r, s),
  26. "position" => cmd_position(cmd, r, s),
  27. "print" => cmd_print(cmd, r, s),
  28. "go" => cmd_go(cmd, r, s),
  29. "stop" => cmd_stop(cmd, r, s),
  30. "ucinewgame" => cmd_newgame(cmd, r, s),
  31. "quit" | "exit" => cmd_quit(cmd, r, s),
  32. cmd => { println!("unknown command: {}", cmd); }
  33. }
  34. }
  35. }
  36. fn cmd_uci(_args: Vec<&str>) {
  37. println!("id name bishop 1.0");
  38. println!("id author Geile Siech");
  39. println!("uciok");
  40. }
  41. fn cmd_isready(_args: Vec<&str>, _r: &Receiver<InterfaceMsg>, s: &Sender<EngineMsg>) {
  42. s.send(EngineMsg::IsReady).unwrap();
  43. }
  44. fn cmd_position(mut args: Vec<&str>, _r: &Receiver<InterfaceMsg>, s: &Sender<EngineMsg>) {
  45. let position = args[0];
  46. args.drain(0..1);
  47. let mut game = Game::default();
  48. if position == "startpos" {
  49. }
  50. else if position == "fen" {
  51. let fen_parts: Vec<&str> = Vec::from(&args[0..6]).into_iter().collect::<Vec<&str>>();
  52. game = Game::from_fen(fen_parts.as_slice()).unwrap();
  53. args.drain(0..6);
  54. }
  55. let moves =
  56. if args.len() > 0 {
  57. if args[0] != "moves" {
  58. info!("unexpected {}", args[6]);
  59. println!("unexpected {}", args[6]);
  60. }
  61. args.drain(0..1);
  62. args.into_iter().map(|m| String::from(m)).collect::<Vec<String>>()
  63. }
  64. else {
  65. Vec::new()
  66. };
  67. s.send(EngineMsg::SetBoard{ pos: game, moves }).unwrap();
  68. }
  69. fn cmd_print(mut _args: Vec<&str>, _r: &Receiver<InterfaceMsg>, s: &Sender<EngineMsg>) {
  70. s.send(EngineMsg::Print).unwrap();
  71. }
  72. fn cmd_go(args: Vec<&str>, _r: &Receiver<InterfaceMsg>, s: &Sender<EngineMsg>) {
  73. let mut options: BTreeMap<String, String> = BTreeMap::new();
  74. let mut opt_name: Option<&str> = None;
  75. let mut si = SearchInfo::new();
  76. for arg in args {
  77. if let Some(on) = opt_name {
  78. options.insert(on.to_owned(), arg.to_owned());
  79. opt_name = None;
  80. }
  81. else if arg == "infinite" {
  82. si.infinite = true;
  83. }
  84. else if arg == "perft" {
  85. si.perft = true;
  86. }
  87. else {
  88. opt_name = Some(arg);
  89. }
  90. }
  91. si.movetime = options.get("movetime").map(|x| x.parse::<_>().unwrap_or(0));
  92. si.depth = options.get("depth").map(|x| x.parse::<_>().unwrap_or(0));
  93. si.wtime = options.get("wtime").map(|x| x.parse::<_>().unwrap_or(0));
  94. si.btime = options.get("btime").map(|x| x.parse::<_>().unwrap_or(0));
  95. si.winc = options.get("winc").map(|x| x.parse::<_>().unwrap_or(0));
  96. si.binc = options.get("binc").map(|x| x.parse::<_>().unwrap_or(0));
  97. si.movestogo = options.get("movestogo").map(|x| x.parse::<_>().unwrap_or(0));
  98. s.send(EngineMsg::Search(si)).unwrap();
  99. }
  100. fn cmd_stop(_args: Vec<&str>, _r: &Receiver<InterfaceMsg>, s: &Sender<EngineMsg>) {
  101. s.send(EngineMsg::Stop).unwrap();
  102. }
  103. fn cmd_newgame(_args: Vec<&str>, _r: &Receiver<InterfaceMsg>, s: &Sender<EngineMsg>) {
  104. s.send(EngineMsg::NewGame).unwrap();
  105. }
  106. fn cmd_quit(_args: Vec<&str>, _r: &Receiver<InterfaceMsg>, _s: &Sender<EngineMsg>) {
  107. exit(0);
  108. }