From df15d117ef085444646313bd9db9183b31b45012 Mon Sep 17 00:00:00 2001 From: "Christoph J. Scherr" Date: Fri, 22 Mar 2024 10:11:26 +0100 Subject: [PATCH] better cli --- src/bin/game/cli.rs | 28 +++++++++++++--------------- src/game/mod.rs | 6 ++++++ src/game/response.rs | 3 ++- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/bin/game/cli.rs b/src/bin/game/cli.rs index 61e1fad..17d70b2 100644 --- a/src/bin/game/cli.rs +++ b/src/bin/game/cli.rs @@ -41,6 +41,7 @@ fn main() -> anyhow::Result<()> { let mut game = game::Game::::builder() .length(cli.length) + .max_steps(cli.max_steps) .precompute(cli.precompute) .build()?; @@ -49,22 +50,19 @@ fn main() -> anyhow::Result<()> { let mut response: GuessResponse; let mut guess: Word; loop { - guess = match get_word(&cli, game.step()) { - Ok(g) => g, - Err(err) => match err.downcast::() { - Ok(game_err) => match game_err { - GameError::GuessHasWrongLength => { - println!("wring length: must be {} long", game.length()); - continue; - } - _ => { - return Err(game_err.into()); - } - }, - Err(err) => return Err(anyhow!(err.to_string())), + guess = get_word(&cli, game.step())?; + response = match game.guess(guess) { + Ok(r) => r, + Err(err) => match err { + GameError::GuessHasWrongLength => { + println!("word length: must be {} long", game.length()); + continue; + } + _ => { + return Err(err.into()); + } }, }; - response = game.guess(guess)?; println!("{response}"); @@ -81,7 +79,7 @@ fn main() -> anyhow::Result<()> { Ok(()) } -fn get_word(_cli: &Cli, step: usize) -> anyhow::Result { +fn get_word(_cli: &Cli, step: usize) -> std::io::Result { let mut word = Word::new(); let stdin = std::io::stdin(); let mut stdout = std::io::stdout(); diff --git a/src/game/mod.rs b/src/game/mod.rs index 9060a03..8faf465 100644 --- a/src/game/mod.rs +++ b/src/game/mod.rs @@ -3,6 +3,7 @@ use crate::wlist::word::{Solution, Word}; use crate::wlist::WordList; pub mod response; +use libpt::log::debug; use response::GuessResponse; use self::response::Status; @@ -19,6 +20,7 @@ where solution: Solution, wordlist: WL, finished: bool, + // TODO: keep track of the letters the user has tried } impl Game { @@ -44,6 +46,7 @@ impl Game { max_steps: usize, wlist: WL, ) -> GameResult { + // TODO: check if the length is in the range bounds of the wordlist let solution = wlist.rand_solution(); let game = Game { length, @@ -91,6 +94,7 @@ impl Game { } let mut response = GuessResponse::new(guess, evaluation, self.step, self.max_steps); + self.finished = response.finished(); Ok(response) } @@ -150,6 +154,7 @@ pub struct GameBuilder { impl GameBuilder { /// build a [`Game`] with the stored configuration pub fn build(self) -> GameResult> { + debug!("{:#?}", self); let game: Game = Game::build(self.length, self.precompute, self.max_steps, WL::default())?; Ok(game) @@ -177,6 +182,7 @@ impl GameBuilder { /// Default is [`super::DEFAULT_MAX_STEPS`] pub fn max_steps(mut self, max_steps: usize) -> Self { self.max_steps = max_steps; + debug!("max steps: {:#?}", self.max_steps); self } } diff --git a/src/game/response.rs b/src/game/response.rs index 5cd8919..df685cb 100644 --- a/src/game/response.rs +++ b/src/game/response.rs @@ -1,6 +1,7 @@ use crate::wlist::word::Word; use anyhow::Ok; use colored::{ColoredString, Colorize}; +use libpt::log::debug; use std::fmt::Display; #[derive(Debug, Clone, PartialEq, Eq)] @@ -27,7 +28,7 @@ impl GuessResponse { max_step: usize, ) -> Self { let mut win = false; - let mut finish: bool = if step >= max_step { + let mut finish: bool = if step > max_step { true } else { let mut matched = true;