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() {
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());
}

View File

@ -23,6 +23,7 @@ where
solution: WordData,
wordlist: &'wl WL,
finished: bool,
responses: Vec<GuessResponse>,
// 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<GuessResponse> {
&self.responses
}
}
/// Build and Configure a [`Game`]

View File

@ -3,17 +3,45 @@ use crate::wlist::WordList;
use super::Game;
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> {
fn from(value: &'wl Vec<Game<'wl, WL>>) -> Self {
impl<'wl, WL: WordList> Summary<'wl, WL> {
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 }
}
}
impl<'wl, WL: WordList> From<&'wl mut Vec<Game<'wl, WL>>> for Summary<'wl, WL> {
fn from(value: &'wl mut Vec<Game<'wl, WL>>) -> Self {
impl<'wl, WL: WordList> From<Vec<&'wl mut Game<'wl, WL>>> 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<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 }
}
}