moar generics and data

This commit is contained in:
Christoph J. Scherr 2024-03-22 14:55:08 +01:00
parent 1336fdba10
commit c5ba15a144
Signed by: cscherrNT
GPG Key ID: 8E2B45BC51A27EA7
3 changed files with 40 additions and 7 deletions

View File

@ -71,7 +71,7 @@ fn main() -> anyhow::Result<()> {
} }
} }
if response.won() { if response.won() {
println!("You win! You took {} guesses.", game.step() - 1); println!("You win! You took {} guesses. {:?}", game.step() - 1, game.solution());
} else { } else {
println!("You lose! The solution was {:?}.", game.solution()); println!("You lose! The solution was {:?}.", game.solution());
} }

View File

@ -23,6 +23,7 @@ where
solution: WordData, solution: WordData,
wordlist: &'wl WL, wordlist: &'wl WL,
finished: bool, finished: bool,
responses: Vec<GuessResponse>,
// TODO: keep track of the letters the user has tried // TODO: keep track of the letters the user has tried
} }
@ -57,8 +58,9 @@ impl<'wl, WL: WordList> Game<'wl, WL> {
max_steps, max_steps,
step: 1, step: 1,
solution, solution,
wordlist: &wlist, wordlist: wlist,
finished: false, finished: false,
responses: Vec::new()
}; };
Ok(game) Ok(game)
@ -123,6 +125,9 @@ impl<'wl, WL: WordList> Game<'wl, WL> {
pub fn max_steps(&self) -> usize { pub fn max_steps(&self) -> usize {
self.max_steps self.max_steps
} }
pub fn responses(&self) -> &Vec<GuessResponse> {
&self.responses
}
} }
/// Build and Configure a [`Game`] /// Build and Configure a [`Game`]

View File

@ -3,17 +3,45 @@ use crate::wlist::WordList;
use super::Game; use super::Game;
pub struct Summary<'wl, WL: WordList> { pub struct Summary<'wl, WL: WordList> {
data: &'wl Vec<Game<'wl, WL>>, data: Vec<&'wl Game<'wl, WL>>,
} }
impl<'wl, WL: WordList> From<&'wl Vec<Game<'wl, WL>>> for Summary<'wl, WL> { impl<'wl, WL: WordList> Summary<'wl, WL> {
fn from(value: &'wl Vec<Game<'wl, WL>>) -> Self { pub fn new() -> Self {
Self { data: Vec::new() }
}
pub fn push(&mut self, game: &'wl Game<WL>) {
self.data.push(game)
}
pub fn pop(&mut self) -> Option<&Game<WL>> {
self.data.pop()
}
}
impl<'wl, WL: WordList> From<Vec<&'wl Game<'wl, WL>>> for Summary<'wl, WL> {
fn from(value: Vec<&'wl Game<'wl, WL>>) -> Self {
Summary { data: value } Summary { data: value }
} }
} }
impl<'wl, WL: WordList> From<&'wl mut Vec<Game<'wl, WL>>> for Summary<'wl, WL> { impl<'wl, WL: WordList> From<Vec<&'wl mut Game<'wl, WL>>> for Summary<'wl, WL> {
fn from(value: &'wl mut Vec<Game<'wl, WL>>) -> Self { 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<Game<'wl, WL>>> for Summary<'wl, WL> {
fn from(value: &'wl mut std::vec::Vec<Game<'wl, WL>>) -> Self {
let value: Vec<&'wl Game<'wl, WL>> = value.iter().collect();
Summary { data: value }
}
}
impl<'wl, WL: WordList> From<&'wl std::vec::Vec<Game<'wl, WL>>> for Summary<'wl, WL> {
fn from(value: &'wl std::vec::Vec<Game<'wl, WL>>) -> Self {
let value: Vec<&'wl Game<'wl, WL>> = value.iter().collect();
Summary { data: value } Summary { data: value }
} }
} }