use anyhow::{bail, Result}; use termion::color::{self}; mod agents; mod board; mod util; use board::Board; use util::{Player, Symb}; fn play( maxi: &mut dyn agents::MaximizerAgent, mini: &mut dyn agents::MinimizerAgent, ) -> Result { let mut board = Board::new(); let mut is_first_turn = true; let mut is_maxi_turn = true; while !board.is_done() { // Print board println!( "\r{}{}{}{}", " ".repeat(6), if is_first_turn { '╓' } else { '║' }, board, if is_first_turn { '╖' } else { '║' }, ); is_first_turn = false; // Take action let action = if is_maxi_turn { maxi.step_max(&board)? } else { mini.step_min(&board)? }; is_maxi_turn = !is_maxi_turn; if !board.play( action, if is_maxi_turn { Player::Human } else { Player::Computer }, ) { bail!("agent made invalid move") } } println!("\r{}║{}║", " ".repeat(6), board,); println!("\r{}╙{}╜", " ".repeat(6), " ".repeat(board.size())); Ok(board) } fn main() -> Result<()> { let mut maxi = agents::PlayerAgent::new(Player::Human); let mut mini = agents::MinMaxTree {}; let a = play(&mut maxi, &mut mini)?; if a.is_done() { println!( "\r\n{}Your score:{} {:.2}\n\n", color::Fg(Player::Human.color()), color::Fg(color::Reset), a.evaluate().unwrap() ); } else { println!( "\r\n{}Quitting{}\r\n", color::Fg(color::Red), color::Fg(color::Reset), ); return Ok(()); } Ok(()) }