From 171d8b8d6c8866bc0ab018d62134952ad02c9d43 Mon Sep 17 00:00:00 2001 From: Mark Date: Tue, 5 Mar 2024 10:18:34 -0800 Subject: [PATCH] Main tweaks --- src/main.rs | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 53 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 6705233..d2063c0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,5 @@ +use std::cmp::Ordering; + use anyhow::{bail, Result}; use termion::color::{self}; @@ -23,7 +25,7 @@ fn play( "\r{}{}{}{}", " ".repeat(6), if is_first_turn { '╓' } else { '║' }, - board, + board.prettyprint()?, if is_first_turn { '╖' } else { '║' }, ); is_first_turn = false; @@ -49,7 +51,7 @@ fn play( is_maxi_turn = !is_maxi_turn; } - println!("\r{}║{}║", " ".repeat(6), board,); + println!("\r{}║{}║", " ".repeat(6), board.prettyprint()?); println!("\r{}╙{}╜", " ".repeat(6), " ".repeat(board.size())); Ok(board) } @@ -61,7 +63,7 @@ fn main() -> Result<()> { .unwrap(); let mut maxi = agents::PlayerAgent::new(Player::Human); - let mut mini = agents::MinMaxTree {}; + let mut mini = agents::Diffuse {}; let a = play(&mut maxi, Player::Human, &mut mini, Player::Computer)?; if a.is_done() { @@ -79,5 +81,53 @@ fn main() -> Result<()> { ); return Ok(()); } + + let mut mini = agents::PlayerAgent::new(Player::Human); + let mut maxi = agents::Diffuse {}; + + let b = play(&mut maxi, Player::Computer, &mut mini, Player::Human)?; + if b.is_done() { + println!( + "\r\n{}Computer score:{} {:.2}\n\n", + color::Fg(Player::Human.color()), + color::Fg(color::Reset), + b.evaluate().unwrap() + ); + } else { + println!( + "\r\n{}Quitting{}\r\n", + color::Fg(color::Red), + color::Fg(color::Reset), + ); + return Ok(()); + } + + match a.evaluate().partial_cmp(&b.evaluate()) { + Some(Ordering::Equal) => { + println!("\r\nTie"); + } + Some(Ordering::Greater) => { + println!( + "\r\n{}Human wins{}", + color::Fg(Player::Human.color()), + color::Fg(color::Reset), + ); + } + Some(Ordering::Less) => { + println!( + "\r\n{}Computer wins{}", + color::Fg(Player::Computer.color()), + color::Fg(color::Reset), + ); + } + None => { + println!( + "\r\n{}Error{}", + color::Fg(color::Red), + color::Fg(color::Reset), + ); + } + } + Ok(()) }