engine.rs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. use search::apply_move;
  2. use bitboard::Bitboard;
  3. use game::Game;
  4. use search::*;
  5. use std::io::Write;
  6. use movegen::*;
  7. use log::{info};
  8. use std::time::{Duration, Instant};
  9. use std::sync::mpsc::{Receiver, Sender};
  10. pub enum EngineMsg {
  11. SetBoard(Game),
  12. SetPiece(Bitboard),
  13. Search(i32),
  14. Ping,
  15. Stop,
  16. NewGame,
  17. GetState,
  18. GetInfo,
  19. }
  20. pub enum EngineState {
  21. Idle,
  22. Searching
  23. }
  24. pub enum InterfaceMsg {
  25. BestMove(Move),
  26. State(EngineState)
  27. }
  28. pub fn run_engine(r: Receiver<EngineMsg>, s: Sender<InterfaceMsg>) {
  29. let mut game = Game::default();
  30. loop {
  31. //game.pieces[0] = 0x00ff_0000_0000_0100;
  32. //game.pieces[QUEEN as usize] = 0x0000_0000_0000_0080;
  33. //game.pieces[10] = 0x4000_0000_0000_0000;
  34. let msg = r.recv().unwrap();
  35. match msg {
  36. EngineMsg::SetBoard(g) => {
  37. //println!("SetBoard");
  38. game = g;
  39. },
  40. EngineMsg::SetPiece(_) => { println!("SetPiece") },
  41. EngineMsg::Search(depth) => {
  42. //info!("searching in pos\n {}", game.beautiful_print());
  43. //println!("Search {}", depth);
  44. let check_fn = || -> bool {
  45. if let Ok(msg) = r.try_recv() {
  46. return true;
  47. }
  48. return false;
  49. };
  50. let mut sc = SearchControl{ nodes: 0, check: &check_fn };
  51. let before = Instant::now();
  52. let best_move = search(&game, &mut sc, depth);
  53. let time = before.elapsed();
  54. println!("bestmove {}", best_move.to_string());
  55. info!("bestmove {}", best_move.to_string());
  56. info!("searched {} nodes in {} ms ({} nodes/s)", sc.nodes, time.as_millis(), (sc.nodes as f64 / time.as_millis() as f64 * 1000.0) as i64);
  57. },
  58. EngineMsg::Ping => { println!("Ping") },
  59. EngineMsg::Stop => { println!("Stop") },
  60. EngineMsg::NewGame => {
  61. //println!("NewGame")
  62. },
  63. EngineMsg::GetState => { println!("GetState") },
  64. EngineMsg::GetInfo => { println!("GetInfo") },
  65. }
  66. //println!("{}", game.beautiful_print());
  67. //let moves = generate_moves(&game, WHITE);
  68. }
  69. }