diff --git a/crates/game-solver/src/game.rs b/crates/game-solver/src/game.rs index 3733a8c..6f42779 100644 --- a/crates/game-solver/src/game.rs +++ b/crates/game-solver/src/game.rs @@ -186,7 +186,9 @@ pub fn upper_bound(game: &T) -> isize { /// Represents an outcome of a game derived by a score and a valid instance of a game. #[derive(Clone, Copy, PartialEq, Eq)] pub enum GameScoreOutcome { + /// The inner field represents the amount of moves till a win. Win(usize), + /// The inner field represents the amount of moves till a loss. Loss(usize), Tie } @@ -195,8 +197,8 @@ pub enum GameScoreOutcome { /// amount of moves to a win or loss, or a tie. pub fn score_to_outcome(game: &T, score: isize) -> GameScoreOutcome { match score.cmp(&0) { - Ordering::Greater => GameScoreOutcome::Win((-score + upper_bound(game)) as usize), + Ordering::Greater => GameScoreOutcome::Win((-score + upper_bound(game) - game.move_count() as isize) as usize), Ordering::Equal => GameScoreOutcome::Tie, - Ordering::Less => GameScoreOutcome::Loss((score + upper_bound(game)) as usize) + Ordering::Less => GameScoreOutcome::Loss((score + upper_bound(game) - game.move_count() as isize) as usize) } } diff --git a/crates/games-cli/src/main.rs b/crates/games-cli/src/main.rs index fe2c4fb..2cac2a4 100644 --- a/crates/games-cli/src/main.rs +++ b/crates/games-cli/src/main.rs @@ -19,7 +19,7 @@ fn main() -> Result<()> { match cli.command { Games::Reversi(args) => play::(args.try_into().unwrap()), Games::TicTacToe(args) => play::(args.try_into().unwrap()), - Games::OrderAndChaos(args) => play::(args.try_into().unwrap()), + Games::OrderAndChaos(args) => play::>(args.try_into().unwrap()), Games::Nim(args) => play::(args.try_into().unwrap()), Games::Domineering(args) => play::>(args.try_into().unwrap()), Games::Chomp(args) => play::(args.try_into().unwrap()), diff --git a/crates/games/src/order_and_chaos/mod.rs b/crates/games/src/order_and_chaos/mod.rs index 0d28fba..33afaa8 100644 --- a/crates/games/src/order_and_chaos/mod.rs +++ b/crates/games/src/order_and_chaos/mod.rs @@ -190,9 +190,9 @@ impl().expect("Not a number!")) .collect::>(); - let player = match args[2] { + let player = match args[2].to_ascii_lowercase().as_str() { "x" => Ok(CellType::X), "o" => Ok(CellType::O), _ => Err(anyhow!("Invalid player!")), diff --git a/crates/games/src/util/cli/mod.rs b/crates/games/src/util/cli/mod.rs index 2bddc8b..d42ec4d 100644 --- a/crates/games/src/util/cli/mod.rs +++ b/crates/games/src/util/cli/mod.rs @@ -67,14 +67,14 @@ where T::MoveError: Display, T::Player: Debug, { - game.make_move(m) - .map_err(|err| anyhow!("Failed to move: {}", err))?; - match game.state() { - GameState::Playable => Ok(()), - GameState::Tie => Err(anyhow!("Can't continue - game is tied.")), - GameState::Win(player) => Err(anyhow!( + GameState::Playable => (), + GameState::Tie => return Err(anyhow!("Can't continue - game is tied.")), + GameState::Win(player) => return Err(anyhow!( "Can't continue game if player {player:?} already won." )), - } + }; + + game.make_move(m) + .map_err(|err| anyhow!("Failed to move: {}", err)) }