From a7a26f7810e9bc5fec6020324a83a2a89b69ff79 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Thu, 23 Feb 2023 15:35:15 +0300 Subject: refactor: implement Player trait for Grossmeister --- src/main.rs | 50 ++++++++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 24 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index e7633bc..049dc68 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,27 +5,28 @@ use chessnost::board::color::Color; use chessnost::board::io::IO; use chessnost::board::Board; use chessnost::grossmeister::Grossmeister; +use chessnost::player::Player; -fn opponent_move(gm: &mut Grossmeister) { - let mov = match gm.board.read_move() { +fn opponent_move(board: &mut Board) { + let mov = match board.read_move() { Ok(m) => m, Err(e) => { println!("Error: {}", e); - return opponent_move(gm); + return opponent_move(board); } }; print!("{:?}", mov); - gm.board.make_move(mov); - gm.board.print(); + board.make_move(mov); + board.print(); } -fn computer_move(gm: &mut Grossmeister, time_left: &mut Duration) { +fn computer_move(board: &mut Board, gm: &mut Grossmeister, time_left: &mut Duration) { let allowed_move_duration = *time_left / 20; - let max_depth = 7 + (gm.board.ply as i32 / 15) as u8; + // let max_depth = 7 + (board.ply as i32 / 15) as u8; println!("~{:?} left, allocating {:?} for a move", time_left, allowed_move_duration); let move_start = Instant::now(); - let (score, pv) = gm.iterative_deepening(max_depth, allowed_move_duration, true); + let (score, pv) = gm.analyze(*board, allowed_move_duration); let elapsed = move_start.elapsed(); if *time_left >= elapsed { @@ -38,17 +39,17 @@ fn computer_move(gm: &mut Grossmeister, time_left: &mut Duration) { let mov = pv[0]; println!("{:?}", mov); - gm.board.make_move(mov); - gm.board.print(); + board.make_move(mov); + board.print(); // Ponder for some time - println!("Pondering, assume opponent move from PV: {:?}", pv[1]); - let ep_target_before = gm.board.ep_target; - let castling_rights_before = gm.board.castling_rights; - let hash_before = gm.board.hash; - let captured_piece = gm.board.make_move(pv[1]); - gm.iterative_deepening(max_depth, Duration::from_secs(3), false); - gm.board.unmake_move(pv[1], captured_piece, ep_target_before, castling_rights_before, hash_before); + // println!("Pondering, assume opponent move from PV: {:?}", pv[1]); + // let ep_target_before = gm.board.ep_target; + // let castling_rights_before = gm.board.castling_rights; + // let hash_before = gm.board.hash; + // let captured_piece = gm.board.make_move(pv[1]); + // gm.iterative_deepening(max_depth, Duration::from_secs(3), false); + // gm.board.unmake_move(pv[1], captured_piece, ep_target_before, castling_rights_before, hash_before); } fn main() { @@ -72,26 +73,27 @@ fn main() { }); let manual_decrement = Duration::from_secs(7); // Time to sync moves with the game - let board = if args.len() == 5 { + let mut board = if args.len() == 5 { Board::from_FEN(args[4].to_string()) } else { Board::new() }; + board.print(); + + let mut gm = Grossmeister::default(); - let mut gm = Grossmeister::new(board); - gm.board.print(); loop { time_left += increment; time_left -= manual_decrement; match color { Color::White => { - computer_move(&mut gm, &mut time_left); - opponent_move(&mut gm); + computer_move(&mut board, &mut gm, &mut time_left); + opponent_move(&mut board); } Color::Black => { - opponent_move(&mut gm); - computer_move(&mut gm, &mut time_left); + opponent_move(&mut board); + computer_move(&mut board, &mut gm, &mut time_left); } } } -- cgit v1.2.3