better cli
cargo devel CI / cargo CI (push) Successful in 47s Details

This commit is contained in:
Christoph J. Scherr 2024-03-22 10:11:26 +01:00
parent 3f60238c07
commit df15d117ef
Signed by: cscherrNT
GPG Key ID: 8E2B45BC51A27EA7
3 changed files with 21 additions and 16 deletions

View File

@ -41,6 +41,7 @@ fn main() -> anyhow::Result<()> {
let mut game = game::Game::<BuiltinWList>::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::<GameError>() {
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<Word> {
fn get_word(_cli: &Cli, step: usize) -> std::io::Result<Word> {
let mut word = Word::new();
let stdin = std::io::stdin();
let mut stdout = std::io::stdout();

View File

@ -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<WL: WordList> Game<WL> {
@ -44,6 +46,7 @@ impl<WL: WordList> Game<WL> {
max_steps: usize,
wlist: WL,
) -> GameResult<Self> {
// 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<WL: WordList> Game<WL> {
}
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<WL: WordList> {
impl<WL: WordList> GameBuilder<WL> {
/// build a [`Game`] with the stored configuration
pub fn build(self) -> GameResult<Game<WL>> {
debug!("{:#?}", self);
let game: Game<WL> =
Game::build(self.length, self.precompute, self.max_steps, WL::default())?;
Ok(game)
@ -177,6 +182,7 @@ impl<WL: WordList> GameBuilder<WL> {
/// 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
}
}

View File

@ -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;