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() let mut game = game::Game::<BuiltinWList>::builder()
.length(cli.length) .length(cli.length)
.max_steps(cli.max_steps)
.precompute(cli.precompute) .precompute(cli.precompute)
.build()?; .build()?;
@ -49,22 +50,19 @@ fn main() -> anyhow::Result<()> {
let mut response: GuessResponse; let mut response: GuessResponse;
let mut guess: Word; let mut guess: Word;
loop { loop {
guess = match get_word(&cli, game.step()) { guess = get_word(&cli, game.step())?;
Ok(g) => g, response = match game.guess(guess) {
Err(err) => match err.downcast::<GameError>() { Ok(r) => r,
Ok(game_err) => match game_err { Err(err) => match err {
GameError::GuessHasWrongLength => { GameError::GuessHasWrongLength => {
println!("wring length: must be {} long", game.length()); println!("word length: must be {} long", game.length());
continue; continue;
} }
_ => { _ => {
return Err(game_err.into()); return Err(err.into());
} }
}, },
Err(err) => return Err(anyhow!(err.to_string())),
},
}; };
response = game.guess(guess)?;
println!("{response}"); println!("{response}");
@ -81,7 +79,7 @@ fn main() -> anyhow::Result<()> {
Ok(()) 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 mut word = Word::new();
let stdin = std::io::stdin(); let stdin = std::io::stdin();
let mut stdout = std::io::stdout(); let mut stdout = std::io::stdout();

View File

@ -3,6 +3,7 @@ use crate::wlist::word::{Solution, Word};
use crate::wlist::WordList; use crate::wlist::WordList;
pub mod response; pub mod response;
use libpt::log::debug;
use response::GuessResponse; use response::GuessResponse;
use self::response::Status; use self::response::Status;
@ -19,6 +20,7 @@ where
solution: Solution, solution: Solution,
wordlist: WL, wordlist: WL,
finished: bool, finished: bool,
// TODO: keep track of the letters the user has tried
} }
impl<WL: WordList> Game<WL> { impl<WL: WordList> Game<WL> {
@ -44,6 +46,7 @@ impl<WL: WordList> Game<WL> {
max_steps: usize, max_steps: usize,
wlist: WL, wlist: WL,
) -> GameResult<Self> { ) -> GameResult<Self> {
// TODO: check if the length is in the range bounds of the wordlist
let solution = wlist.rand_solution(); let solution = wlist.rand_solution();
let game = Game { let game = Game {
length, length,
@ -91,6 +94,7 @@ impl<WL: WordList> Game<WL> {
} }
let mut response = GuessResponse::new(guess, evaluation, self.step, self.max_steps); let mut response = GuessResponse::new(guess, evaluation, self.step, self.max_steps);
self.finished = response.finished();
Ok(response) Ok(response)
} }
@ -150,6 +154,7 @@ pub struct GameBuilder<WL: WordList> {
impl<WL: WordList> GameBuilder<WL> { impl<WL: WordList> GameBuilder<WL> {
/// build a [`Game`] with the stored configuration /// build a [`Game`] with the stored configuration
pub fn build(self) -> GameResult<Game<WL>> { pub fn build(self) -> GameResult<Game<WL>> {
debug!("{:#?}", self);
let game: Game<WL> = let game: Game<WL> =
Game::build(self.length, self.precompute, self.max_steps, WL::default())?; Game::build(self.length, self.precompute, self.max_steps, WL::default())?;
Ok(game) Ok(game)
@ -177,6 +182,7 @@ impl<WL: WordList> GameBuilder<WL> {
/// Default is [`super::DEFAULT_MAX_STEPS`] /// Default is [`super::DEFAULT_MAX_STEPS`]
pub fn max_steps(mut self, max_steps: usize) -> Self { pub fn max_steps(mut self, max_steps: usize) -> Self {
self.max_steps = max_steps; self.max_steps = max_steps;
debug!("max steps: {:#?}", self.max_steps);
self self
} }
} }

View File

@ -1,6 +1,7 @@
use crate::wlist::word::Word; use crate::wlist::word::Word;
use anyhow::Ok; use anyhow::Ok;
use colored::{ColoredString, Colorize}; use colored::{ColoredString, Colorize};
use libpt::log::debug;
use std::fmt::Display; use std::fmt::Display;
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
@ -27,7 +28,7 @@ impl GuessResponse {
max_step: usize, max_step: usize,
) -> Self { ) -> Self {
let mut win = false; let mut win = false;
let mut finish: bool = if step >= max_step { let mut finish: bool = if step > max_step {
true true
} else { } else {
let mut matched = true; let mut matched = true;