diff --git a/src/bin/game/cli.rs b/src/bin/game/cli.rs index 2ef14b6..c3c30eb 100644 --- a/src/bin/game/cli.rs +++ b/src/bin/game/cli.rs @@ -71,7 +71,7 @@ fn main() -> anyhow::Result<()> { } } if response.won() { - println!("You win! You took {} guesses.", game.step() - 1); + println!("You win! You took {} guesses. {:?}", game.step() - 1, game.solution()); } else { println!("You lose! The solution was {:?}.", game.solution()); } diff --git a/src/game/mod.rs b/src/game/mod.rs index be12292..19891e6 100644 --- a/src/game/mod.rs +++ b/src/game/mod.rs @@ -23,6 +23,7 @@ where solution: WordData, wordlist: &'wl WL, finished: bool, + responses: Vec, // TODO: keep track of the letters the user has tried } @@ -57,8 +58,9 @@ impl<'wl, WL: WordList> Game<'wl, WL> { max_steps, step: 1, solution, - wordlist: &wlist, + wordlist: wlist, finished: false, + responses: Vec::new() }; Ok(game) @@ -123,6 +125,9 @@ impl<'wl, WL: WordList> Game<'wl, WL> { pub fn max_steps(&self) -> usize { self.max_steps } + pub fn responses(&self) -> &Vec { + &self.responses + } } /// Build and Configure a [`Game`] diff --git a/src/game/summary.rs b/src/game/summary.rs index 05d7d12..21bec99 100644 --- a/src/game/summary.rs +++ b/src/game/summary.rs @@ -3,17 +3,45 @@ use crate::wlist::WordList; use super::Game; pub struct Summary<'wl, WL: WordList> { - data: &'wl Vec>, + data: Vec<&'wl Game<'wl, WL>>, } -impl<'wl, WL: WordList> From<&'wl Vec>> for Summary<'wl, WL> { - fn from(value: &'wl Vec>) -> Self { +impl<'wl, WL: WordList> Summary<'wl, WL> { + pub fn new() -> Self { + Self { data: Vec::new() } + } + pub fn push(&mut self, game: &'wl Game) { + self.data.push(game) + } + pub fn pop(&mut self) -> Option<&Game> { + self.data.pop() + } +} + +impl<'wl, WL: WordList> From>> for Summary<'wl, WL> { + fn from(value: Vec<&'wl Game<'wl, WL>>) -> Self { Summary { data: value } } } -impl<'wl, WL: WordList> From<&'wl mut Vec>> for Summary<'wl, WL> { - fn from(value: &'wl mut Vec>) -> Self { +impl<'wl, WL: WordList> From>> for Summary<'wl, WL> { + fn from(value: Vec<&'wl mut Game<'wl, WL>>) -> Self { + // looks weird, but is ok + let value: Vec<&'wl Game<'wl, WL>> = value.into_iter().map(|v| &*v).collect(); + Summary { data: value } + } +} + +impl<'wl, WL: WordList> From<&'wl mut std::vec::Vec>> for Summary<'wl, WL> { + fn from(value: &'wl mut std::vec::Vec>) -> Self { + let value: Vec<&'wl Game<'wl, WL>> = value.iter().collect(); + Summary { data: value } + } +} + +impl<'wl, WL: WordList> From<&'wl std::vec::Vec>> for Summary<'wl, WL> { + fn from(value: &'wl std::vec::Vec>) -> Self { + let value: Vec<&'wl Game<'wl, WL>> = value.iter().collect(); Summary { data: value } } }