Skip to content

Commit

Permalink
refactor: remove useless Option for engine
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-mauran committed Nov 26, 2024
1 parent 793c274 commit 9be9511
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions src/game_logic/bot.rs
Original file line number Diff line number Diff line change
@@ -2,22 +2,30 @@ use uci::Engine;

use crate::utils::convert_notation_into_position;

#[derive(Default, Clone)]
#[derive(Clone)]
pub struct Bot {
// the chess engine
pub engine: Option<Engine>,
pub engine: Engine,
/// Used to indicate if a bot move is following
pub bot_will_move: bool,
// if the bot is starting, meaning the player is black
pub is_bot_starting: bool,
}

// Custom Default implementation
impl Default for Bot {
fn default() -> Self {
Bot {
engine: Engine::new("path_to_engine").expect("Failed to load engine"), // Specify the default engine path
bot_will_move: false,
is_bot_starting: false,
}
}
}

impl Bot {
pub fn new(engine_path: &str, is_bot_starting: bool) -> Bot {
let engine = match Bot::create_engine(engine_path) {
Some(engine) => Some(engine),
_ => panic!("An error occcured with the selected chess engine path: {engine_path} Make sure you specified the right path using chess-tui -e")
};
let engine = Bot::create_engine(engine_path);

Self {
engine,
@@ -31,9 +39,9 @@ impl Bot {
self.engine = Bot::create_engine(engine_path)
}

pub fn create_engine(engine_path: &str) -> Option<Engine> {
pub fn create_engine(engine_path: &str) -> Engine {
match Engine::new(engine_path) {
Ok(engine) => Some(engine),
Ok(engine) => engine,
Err(e) => {
panic!(
"Failed to initialize the engine at path: {}. Error: {:?}",
@@ -46,10 +54,8 @@ impl Bot {
We use the UCI protocol to communicate with the chess engine
*/
pub fn get_bot_move(&mut self, fen_position: String) -> String {
let engine = self.engine.clone().expect("Missing the chess engine");

engine.set_position(&(fen_position as String)).unwrap();
let best_move = engine.bestmove();
self.engine.set_position(&(fen_position as String)).unwrap();
let best_move = self.engine.bestmove();
let Ok(movement) = best_move else {
panic!("An error has occured")
};

0 comments on commit 9be9511

Please sign in to comment.